3 Replies - 2472 Views - Last Post: 05 July 2011 - 10:30 AM Rate Topic: -----

#1 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Queue/Linked List: Clinic Reservation System

Posted 05 July 2011 - 09:59 AM

Peace. I have a bit of a problem with the code design. Below is the problem description. For priority case, I need to put the reservation at front. But Queue doesn't have anything to do with that. How to put it at front for one queue only? I also have problem accessing all elements in a queue that is NormalQueue and Priority Queue. Displaying them is quite okay, but trying to delete them starting to make my head hurt. I only can remove the front queue. I have idea of using a LinkedList that connect NormalQueue and PriorityQueue. But that means the LinkedList is in type Queue, not Patient. So kinda useless and redundant. Is there a better way? Please help point me to the right direction. If there is anything else to comment about my incomplete codes, no need to keep to yourself. But please try to answer my question rather than focusing on just small detail.

Quote

Problem Description:
Using the Linked List and Queue concept, develop a
reservation system for Clinic Ruslan,
whereby the system should be able to:
-Delete the reservation
-Add the reservation
-View the current reservation
-Insert new reservation(priority case)

Do you want to continue?

Choose Option
Add
Delete
View
Exit
Due Date: 07 July 2011



Current Coding
//In the name of Allah
/*	Problem Description:
	Using the Linked List and Queue concept, develop a
	reservation system for Clinic Ruslan,
	whereby the system should be able to:
	-Delete the reservation
	-Add the reservation
	-View the current reservation
	-Insert new reservation(priority case)
	
	Do you want to continue?

	Choose Option
	Add
	Delete
	View
	Exit
Due Date: 07 July 2011
*/
/*==Class ClinicRuslan=============================================================*/
import javax.swing.*; //JOP and JFrame
import java.util.*;

 class ClinicRuslan{
	public static void main(String [] args){
	/*==Variable Declaration, Object Constructs=============================================================*/	
		String name="";
		String IC="";			//MYKad No
		String contactNo="";	//Use string to get zero at the front e.g. 0134567890
		String address="";
		String gender="";
		int age=0;
		//String date="";		//reservation date
		double time=0.0;		//reservation time
		String priority="";	    //normal or critical
		//
		int choice = 0;
		String option = "";
		String output = "";
		//Setting Queue
		Queue normalQueue = new Queue();
		Queue priorityQueue = new Queue();;
		Patient temp;
		LinkedList AllQueue = new LinkedList();/*
		AllQueue.add(normalQueue);
		AllQueue.add(priorityQueue);*/
		
		
	/*==MAIN CONTENT=============================================================*/
		while (choice==0){
			option=JOptionPane.showInputDialog(null,"Choose Option\n\tAdd\n\tDelete\n\tView\n\tExit");
			/*==Add=============================================================*/
			if (option.equalsIgnoreCase("Add"))
			{
				//JOptionPane.showMessageDialog(null,"ADDING");
				name=JOptionPane.showInputDialog(null,"Name");
				IC=JOptionPane.showInputDialog(null,"IC");
				contactNo=JOptionPane.showInputDialog(null,"Contact No");
				address=JOptionPane.showInputDialog(null,"Address");
				gender=JOptionPane.showInputDialog(null,"Gender[Male/Female]");
				age=Integer.parseInt(JOptionPane.showInputDialog(null,"Age"));
				time=Double.parseDouble(JOptionPane.showInputDialog(null,"Time[24hours system]"));
				priority=JOptionPane.showInputDialog(null,"Priority[normal/critical]");
				temp = new Patient(name,IC,contactNo,address,gender,age,time,priority);
				if (temp.priorityCheck())
				{
					priorityQueue.enqueue(temp);  //Queue for Critical Priority Condition
				}
				else
				{
					normalQueue.enqueue(temp);	  //Queue for Normal Patients
				}
			}
			/*==Delete=============================================================*/
			else if (option.equalsIgnoreCase("Delete"))
			{
				//View First
				if(!priorityQueue.isEmpty())
                {
					for(int i=0; i<priorityQueue.size(); i++)
					{
					temp = (Patient) priorityQueue.front();
					//System.out.println(temp.toString());
					output+=temp.toString();
					}
				}
				if(!normalQueue.isEmpty())
                {
					for(int i=0; i<normalQueue.size(); i++)
					{
					temp = (Patient) normalQueue.front();
					//System.out.println(temp.toString());
					output+=temp.toString();
					}
				}
				JOptionPane.showMessageDialog(null,"Select which patient?(Define the name.)\n"+output);
				output="";
				
			}
			/*==View=============================================================*/
			else if (option.equalsIgnoreCase("View"))
			{
				/*for(int i=0; i<AllQueue.size(); i++)
					{
					temp = (Patient) AllQueue.get(i);
					System.out.println(temp.toString());
					}*/
				if(!priorityQueue.isEmpty())
                {
					for(int i=0; i<priorityQueue.size(); i++)
					{
					temp = (Patient) priorityQueue.front();
					//System.out.println(temp.toString());
					output+=temp.toString();
					}
				}
				if(!normalQueue.isEmpty())
                {
					for(int i=0; i<normalQueue.size(); i++)
					{
					temp = (Patient) normalQueue.front();
					//System.out.println(temp.toString());
					output+=temp.toString();
					}
				}
				JOptionPane.showMessageDialog(null,"VIEWING\n"+output);
				output="";
				
			}
			/*==Exit=============================================================*/
			else if (option.equalsIgnoreCase("Exit"))
				break;
			/*==Error Input Notification=============================================================*/
			else
				JOptionPane.showMessageDialog(new JFrame(), "You have entered invalid input. Please enter \"Add\", \"Delete\", \"View\", or \"Exit\" ", "Wrong Input",JOptionPane.ERROR_MESSAGE);
			/*==Choose to continue or not=============================================================*/
			choice=JOptionPane.showConfirmDialog(null,"Do you want to continue?","Continue?", JOptionPane.YES_NO_OPTION);//return 0 for yes option.
		}
		/*==END MAIN CONTENT=============================================================*/
		System.exit(0);
	}
}

/*==Class Queue=============================================================*/
class Queue {

	protected LinkedList list;

	public Queue() {
		list = new LinkedList();
	} //default constructor

	public boolean isEmpty() {
		return list.isEmpty();
	}//method isEmpty

	public int size() {
		return list.size();
	}// method size

	public void enqueue(Object element) {
		list.addLast(element);
	} //method enqueue

	public Object dequeue() {
		return list.removeFirst();
	} //method dequeue

	public Object front() {
		return list.getFirst();
	} //method front

	public Object rear(){
		return list.getLast();
	}
} // Queue class

//class Patient
class Patient{
	protected String name;
	protected String IC;			//MYKad No
	protected String contactNo;
	protected String address;
	protected String gender;
	protected int age;
	//protected String date;		//reservation date
	protected double time;		//reservation time
	protected String priority;	//normal or critical

	//Patient(String a,String b,int c,String d,String e,int f,String g,double h,String i){
	Patient(String a,String b,String c,String d,String e,int f,double h,String i){
		name=a;
		IC=b;
		contactNo=c;
		address=d;
		gender=e;
		age=f;
		//date=g;
		time=h;
		priority=i;
	}

	public String getName(){return name;}
	public String getIC(){return IC;}
	public String getContact(){return contactNo;}
	public String getAddress(){return address;}
	public String getGender(){return gender;}
	public int getAge(){return age;}
	//public String getDate(){return date;}
	public double getTime(){return time;}
	public String getPriority(){return priority;}

	public boolean priorityCheck(){
		if (getPriority().equalsIgnoreCase("critical"))
			return true;
		else
			return false;
	}

	public String toString(){
		return "Name: " + getName() + ", IC No: " + getIC() + ", Contact no : " +  getContact() + ", Address : " +  getAddress() + ", Gender : " 
				+  getGender() +  ", Age : " + getAge() + ", Time : " + getTime() + ", Condition : " +  getPriority() + "\n";
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Queue/Linked List: Clinic Reservation System

#2 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 193
  • Joined: 28-October 06

Re: Queue/Linked List: Clinic Reservation System

Posted 05 July 2011 - 10:14 AM

Hello Shadowk1.

Are you required to use queues and linked lists? It seems to me the simplest solution would be to use a simple ArrayList.

An implementation might be as follows:
ArrayList<Object> reservations; // holds all reservations

private void addReservation(Object reservation) {
  // .. validation
  reservations.add(reservation);
  // ...
}

private void addPriorityReservation(Object reservation) {
  // .. validation
  reservations.add(0, reservation);
  // ...
}

private Object getNextReservation() {
  // ...
  return reservations.remove(0);
  // ...
}



Of course, this means that there are only two levels of priority: "regular" and "priority". All priority reservations get stuck in the front of the list. Only when there are no more priority reservations in the list will a regular reservation be processed. It's not perfect -- the priority reservations will be processed in a Last-In-First-Out manner and the regular reservations in a First-In-First-Out manner.

If you want to use your queues, another idea would be this. When a priority request comes in, add it to priorityQueue. When a normal request comes in, add it to normalQueue. When it comes time to grab the next request from the queues, check if priorityQueue is empty. If it is not empty, remove the next request from priorityQueue. If priorityQueue is empty, remove the next request from normalQueue.

Mila
Was This Post Helpful? 2
  • +
  • -

#3 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Queue/Linked List: Clinic Reservation System

Posted 05 July 2011 - 10:21 AM

I'm not very experienced with the Queue interface as I've only had to use it once but aren't there methods where you can copy a Queue? If so can't you create an new queue add a priority reservation to the front of a new queue and add the copy queue to the new queue? I know its very inefficient but it's one way it could be done.

View PostMila, on 05 July 2011 - 11:14 AM, said:

If you want to use your queues, another idea would be this. When a priority request comes in, add it to priorityQueue. When a normal request comes in, add it to normalQueue. When it comes time to grab the next request from the queues, check if priorityQueue is empty. If it is not empty, remove the next request from priorityQueue. If priorityQueue is empty, remove the next request from normalQueue.

Mila


This is a great idea and is much more efficient then my way.

This post has been edited by giggly kisses: 05 July 2011 - 10:22 AM

Was This Post Helpful? 0
  • +
  • -

#4 shad0wk1  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: Queue/Linked List: Clinic Reservation System

Posted 05 July 2011 - 10:30 AM

View PostMila, on 05 July 2011 - 10:14 AM, said:

Hello Shadowk1.

Are you required to use queues and linked lists? It seems to me the simplest solution would be to use a simple ArrayList.

If you want to use your queues, another idea would be this. When a priority request comes in, add it to priorityQueue. When a normal request comes in, add it to normalQueue. When it comes time to grab the next request from the queues, check if priorityQueue is empty. If it is not empty, remove the next request from priorityQueue. If priorityQueue is empty, remove the next request from normalQueue.

Mila

Yes, I do need to use queue. That's seems to be a good idea. If I have no need to worry about deleting a patient from the list of the queue in between. I guess I can just delete only at the end of the row for starter. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1