5 Replies - 5418 Views - Last Post: 19 April 2010 - 05:51 PM Rate Topic: -----

#1 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Hotel Program Guest Search

Posted 18 April 2010 - 02:51 PM

I am in the progress of making a hotel program that was assigned to me. I will give all of the code to the program, and label and explain some errors that I am having.

import java.util.Scanner;
import java.util.Arrays;//attempt at something different to help sort the arrays
public class Parrish_Hotel
{
	public static Scanner sc = new Scanner(System.in);
	public static void main(String args[])
	{
		
		String names[]=new String[0];
		int rooms[]=new int[0]; 
			while(true)
			{
				int choice=0;
				System.out.println("\n\nMENU ");//menu
				System.out.println("\t\t1) Add Guest");
				System.out.println("\t\t2) List All Guests and Rooms");
				System.out.println("\t\t3) Search for a Guest");
				System.out.println("\t\t4) Remove a Guest");
				System.out.println("\t\t5) QUIT");
				System.out.print("Choice: ");
				choice=sc.nextInt();
				if(choice==5||choice ==0)//either or
					break;
				switch(choice)
				{
					case 1:
					{
						//Add Guest
						sc.nextLine();
						System.out.print("Please enter your guest: ");
						String guest=sc.nextLine();
						names=addName(guest,names);
						System.out.print("Room: ");
						int room=sc.nextInt();
						rooms=addRoom(room,rooms);
						//sort rooms by ascending... don't use arrays.sort!
						for(int x=0;x<rooms.length;x++)
						{
							for(int y=0;y<rooms.length;y++)
							{
								if(rooms[x]<rooms[y])
								{
									int dummy=rooms[x];
									String dummy2=names[x];
									rooms[x]=rooms[y];
									names[x]=names[y];
									rooms[y]=dummy;
									names[y]=dummy2;
								}
							}
						}
						break;
					}// e c
					case 2:
					{
						//List All Guests and Room Numbers
						System.out.println("ROOM\t\t\tGUEST");
						for(int x=0;x<rooms.length;x++)
						{
							System.out.println(rooms[x]+"\t\t\t"+ names[x]);
						}
						break;	
					}
					case 3:
					{
						//Search for a guest
						sc.nextLine();
						int search=-1;
						System.out.print("Enter the name of the guest that is being searched: ");
						String searchName=sc.nextLine();
						for(int x=0;x<names.length;x++)//my compiler says "x++" is dead code
						{
							if(names[x].equalsIgnoreCase(searchName));
							{//Every time I search for a guest it says the guest of the first cell is here will explain with output
								search=x;
								break;
							}
						}
						if(search==-1)
							System.out.println(names[search]+" has not checked into this hotel");
						else
							System.out.println(names[search]+" is checked into room "+rooms[search]);
						break;
					}//end case
					//make a case to where a guest checks out
					case 4:
					{
						System.out.print("Enter the name of the guest checking out: ");
						String removeName=sc.nextLine();
						//work in progress
					}
					default:
						System.out.println("Invalid Entry, Please Try Again");
				}//end switch
			}//end while
	}
	public static String[] addName(String guest, String names[])
	{
		String x[]=new String[(names.length)+1];
		System.arraycopy(names,0,x,0,names.length);//System.arraycopy(first array, starting point, second array, start point, how much
		//add something for maybe if the person is already here, but cased differently??
		for(int a=0;a<x.length;a++)
		{
			if(guest.equalsIgnoreCase(x[a]))
			{
				System.out.println("This guest has already checked in.");
				System.out.print("New Guest: ");
				guest=sc.nextLine();
			}
		}
		x[x.length-1]=guest;
		return x;	
	}
	public static int[] addRoom(int room, int rooms[])
	{
		int r[]=new int[(rooms.length)+1];
		System.arraycopy(rooms,0,r,0,rooms.length);//System.arraycopy(first array, starting point, second array, start point, how much
		for(int x=0;x<r.length;x++)
		{
			if(r[x]==room)
			{
				System.out.println("ROOM ALREADY OCCUPIED!");
				System.out.print("Reenter room: ");
				room=sc.nextInt();
			}
		}
		r[r.length-1]=room;
		return r;
	}
}


Output:

Quote

MENU
1) Add Guest
2) List All Guests and Rooms
3) Search for a Guest
4) Remove a Guest
5) QUIT
Choice: 1
Please enter your guest: John Smith
Room: 102


MENU
1) Add Guest
2) List All Guests and Rooms
3) Search for a Guest
4) Remove a Guest
5) QUIT
Choice: 1
Please enter your guest: Bob
Room: 101


MENU
1) Add Guest
2) List All Guests and Rooms
3) Search for a Guest
4) Remove a Guest
5) QUIT
Choice: 1
Please enter your guest: William
Room: 105


MENU
1) Add Guest
2) List All Guests and Rooms
3) Search for a Guest
4) Remove a Guest
5) QUIT
Choice: 2
ROOM GUEST
101 Bob
102 John Smith
105 William


MENU
1) Add Guest
2) List All Guests and Rooms
3) Search for a Guest
4) Remove a Guest
5) QUIT
Choice: 3
Enter the name of the guest that is being searched: William
Bob is checked into room 101 <-------This should not be, what is wrong with the search code?


MENU
1) Add Guest
2) List All Guests and Rooms
3) Search for a Guest
4) Remove a Guest
5) QUIT
Choice: 3
Enter the name of the guest that is being searched: Scott
Bob is checked into room 101 <--------Second search Trial, I would believe something is wrong with the search code


MENU
1) Add Guest
2) List All Guests and Rooms
3) Search for a Guest
4) Remove a Guest
5) QUIT
Choice: 5


So I believe my search for a guest code has something wrong with it.
If anyone could please explain to me what I did wrong, that would be great

Is This A Good Question/Topic? 0
  • +

Replies To: Hotel Program Guest Search

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Hotel Program Guest Search

Posted 18 April 2010 - 09:05 PM

Your problem is that the if statement inside your case-3 for loop ends in a semi-colon, which sets search = 0 regardless of the condition in the if statement, here: if(names[x].equalsIgnoreCase(searchName));.

Also, parallel arrays are not good OO practic because you lack encapsulation. One of the big advantages of classes over parallel arrays is that classes allow for close association of variables for like Objects, and parallel arrays do not. In this case, I would design a Room class with attributes number (int) and occupant (String), then manage a Room[] for your Hotel. I explain more about class design vs. parallel arrays in my tutorial Moving Away From Parallel Arrays. You may want to check it out.
Was This Post Helpful? 0
  • +
  • -

#3 MKunstman  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 194
  • Joined: 26-October 09

Re: Hotel Program Guest Search

Posted 19 April 2010 - 06:41 AM

So for my own curiosity, being a student in Java. If you wanted each selection to open it's own window so say you chose 1, and then you wanted a new window to open with the data entry parts, would you create a base class then a class for each aspect of the case statement here? Or am I way off base?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Hotel Program Guest Search

Posted 19 April 2010 - 07:34 AM

If you wanted a new Window to manage each room, you could design a RoomManagement class that extends JFrame, and work in the management components from there. Or, if you want to stick with JOptionPane, you can use the showOptionDialog() method to accomplish this.
Was This Post Helpful? 0
  • +
  • -

#5 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 13-March 10

Re: Hotel Program Guest Search

Posted 19 April 2010 - 05:46 PM

How exactly could I implement using objects or classes the add rooms or names?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Hotel Program Guest Search

Posted 19 April 2010 - 05:51 PM

Quote

In this case, I would design a Room class with attributes number (int) and occupant (String), then manage a Room[] for your Hotel. I explain more about class design vs. parallel arrays in my tutorial Moving Away From Parallel Arrays. You may want to check it out.


So rather than managing two arrays for name and number, you can manage a single array of type Room. From there, you can define accessor and mutator (getter and setter) methods within the Room class or modifying the name and number attributes of the Room objects. If you read up on my tutorial Moving Away From Parallel Arrays, I go into more detail on using classes and Objects over parallel arrays, in an inventory case study (which your program essentially breaks down into).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1