I am supposed to create a class patient that will sort the patients according to their condition that is Serious(S), Routine(R), Critical(C), and Expectant (E). Use a heap based Priority Queue. The data type stored should be objects of class Patient. Class Patient will require a compare method that takes an argument of Patient class type that will return a Boolean based on comparing the Category member of the argument class vs this.Category. The Serve method of the Priority Queue should display the patient data of the next patient to be treated. The program is to be a Windows application

So far my code it builds successfully but upon running I get an error Format Exception was unhanded in the lines highlighted in bold.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client 
{

     class Patient
     {
          private int patientnumber;
          private string name;
          private string timein;
          private char condition;


          // Default constructor:
          public Patient()
          {
               name = "N/A";
          }

          // Constructor:
          public Patient(string name, int patientnumber, char condition, string timein)
          {
               this.name = name;
               this.patientnumber = patientnumber;
               this.condition = condition;
               this.timein = timein;
          }

          // Printing method:
          public void PrintPatient()
          {
             [B]  Console.WriteLine("{0}, {1} {3} {4} ", name, patientnumber, condition, timein)[/B];
          }
     }

     class StringTest
     {
          static void Main()
          {
               // Create objects by using the new operator:
               Patient patient1 = new Patient("Daniel Craig", 414092817, 'E', "12:45");
               Patient patient2 = new Patient("Sally May", 654125419, 'S', "03:00");

               // Create an object using the default constructor:
               Patient patient3 = new Patient("Jomo Stronghold", 124891017, 'C', "11:20");
               Patient patient4 = new Patient("Carlitos Way", 215210211, 'R', "12:20");
               Patient patient5 = new Patient("Keith Baird", 215961011, 'C', "19:50");
               Patient patient6=  new Patient("Sony Webuye", 29154631, 'R', "12:20");
               Patient patient7 = new Patient("Amy Whitehouse", 215961016, 'C', "21:00");
               Patient patient8 = new Patient("Santos Abreza", 813694011, 'R', "10:20");



               // Display results:
               Console.Write("Patient #1: ");
               patient1.PrintPatient();
               Console.Write("Patient #2: ");
               patient2.PrintPatient();
               Console.Write("Patient #3: ");
               patient3.PrintPatient();
               Console.Write("Patient #4: ");
               patient4.PrintPatient();
               Console.Write("Patient #5: ");
               patient3.PrintPatient();
               Console.Write("Patient #6: ");
               patient4.PrintPatient();
               Console.Write("Patient #7: ");
               patient3.PrintPatient();
               Console.Write("Patient #8: ");
               patient4.PrintPatient();
          }
     }
}

Edited 5 Years Ago by carlitosway17: n/a

Try this
Console.WriteLine("{0}, {1}, {2}, {3} ", name, patientnumber, condition, timein);

Edited 5 Years Ago by ddanbe: n/a

Try this
Console.WriteLine("{0}, {1}, {2}, {3} ", name, patientnumber, condition, timein);

Thanks how silly of me, so would I implement the priority que?

This is a generic heapsort that you could use to maintain your Heap based Priority Queue. You'd just have to implement IComparable on your Patient class.

using System;

namespace Whittle.Sorting.Selection {
    class Heap<T> : Sorts<T> where T : IComparable {
        public override string Name {
            get { return "Heap"; }
        }

        public override void Sort(T[] array) {
            Heapify(array, array.Length);
            int end = array.Length - 1;
            while (end > 0) {
                Swap(array, end, 0);
                SiftDown(array, 0, end - 1);
                end--;
            }
        }

        public void Heapify(T[] array, int count) {
            int start = count / 2 - 1;
            while (start >= 0) {
                SiftDown(array, start, count - 1);
                start--;
            }
        }

        public void SiftDown(T[] array, int start, int end) {
            int root = start;
            while (root * 2 + 1 <= end) {
                int child = root * 2 + 1;
                int swap = root;
                if (array[swap].CompareTo(array[child]) < 0) {
                    swap = child;
                }
                if (child < end && array[swap].CompareTo(array[child + 1]) < 0) {
                    swap = child + 1;
                }
                if (swap != root) {
                    Swap(array, root, swap);
                    root = swap;
                } else {
                    break;
                }
            }
        }
    }
}

The base class is

using System;

namespace Whittle.Sorting {
    abstract class Sorts<T> where T : IComparable {
        abstract public String Name { get; }
        abstract public void Sort(T[] array);
        public void Swap(T[] array, int a, int b) {
            T temp = array[a];
            array[a] = array[b];
            array[b] = temp;
        }
    }
}
Comments
Indeed! Good post.

Thanks guys so far so good. Now this application is meant for a hospital we are to sort the patients according to their level of injuries that is Serious(S),Critical(C) Routine(R), and Expectant (E). So how can I implement interface member 'System.IComparable.CompareTo(object)' based on only their level of injuries as mentioned above?

Edited 5 Years Ago by carlitosway17: n/a

Since you have condition as a char rather than an enum, we do this:

public int CompareTo(object obj) {
    return Order(this.condition).CompareTo(Order(((Patient)obj).condition));
}

private int Order(char condition) {
    int result = 4;
    switch (condition) {
        case 'S': result = 0; break;
        case 'C': result = 1; break;
        case 'R': result = 2; break;
        case 'E': result = 3; break;
    }
    return result;
}

And I hope you are ready to explain how the code works to your instructor.

I do understand whats happening so far thanks this has proven to be one of my toughest labs so far . It does not take int it gives an error: Expected class, enum, interface or structure

what doesn't take an int?

The error reads: Error 1 Expected class, delegate, enum, interface, or struct
occurs where it is red in the code

public int CompareTo(object obj) {
    return Order(this.condition).CompareTo(Order(((Patient)obj).condition));
}
 
private int Order(char condition) {
    int result = 4;
    switch (condition) {
        case 'S': result = 0; break;
        case 'C': result = 1; break;
        case 'R': result = 2; break;
        case 'E': result = 3; break;
    }
    return result;
}

Edited 5 Years Ago by __avd: Added [code] tags.

This question has already been answered. Start a new discussion instead.