2013-03-02 64 views
0

好基本上我創建一個酒店,其作用以下選項酒店使用數組

System.out.println("V: To View all rooms"); 
System.out.println("A: To Add customer to a room"); 
System.out.println("E: To Display empty rooms"); 
System.out.println("D: To Delete customer from a room"); 
System.out.println("F: Find room from customer name"); 
System.out.println("O: View rooms alphabetically by name"); 
  • 現在,我能夠做的查看,添加客戶,顯示空房間並刪除」。
  • 但是,我只是意識到我不能添加多個客戶,代碼只會爲每個房間分配一個同名的客戶。

例如:如果我將名稱TOM添加到房間2,然後再添加DENNIS到r oom 3. 房間2和房間3顯示它被丹尼斯佔用。 - 我知道爲什麼,因爲String客戶只能存儲一個信息。

我有一種感覺,我需要將客戶更改爲數組。

(請記住,我不想使用ArrayList)。

SO:我的問題是,我該如何解決這個問題?因爲我試圖與客戶合作作爲一個數組,我毀了整個編碼(thankgod本地歷史恢復!)

代碼:

package testroom; 

import java.util.*; 

public class TestRoom{ 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     String Customer = null; 
     int roomNum = 1; 
     String Option; 

     String[] hotel = new String[12]; 

     initialise(hotel); 

     while (roomNum < 11) 
     { 

      System.out.println("Hotel Booking Options"); 
      System.out.println("V: To View all rooms"); 
      System.out.println("A: To Add customer to a room"); 
      System.out.println("E: To Display empty rooms"); 
      System.out.println("D: To Delete customer from a room"); 
      System.out.println("F: Find room from customer name"); 
      System.out.println("O: View rooms alphabetically by name"); 

      Option = input.next(); 

      if (Option.equals("V")){ //viewing all rooms 
       view(hotel, Customer); 
      } 

      if (Option.equals("A")){ // adding a customer to a room 

       System.out.println("Enter room number(1-10)"); 
       roomNum =input.nextInt(); 
       System.out.println("Enter name for room " + roomNum + " : ") ; 
       Customer = input.next(); 
       hotel[roomNum] = Customer ; 
       add(hotel, Customer); 
       System.out.println(" "); 

      } 

      if (Option.equals("E")){ //view all empty rooms 
       vacant(hotel, Customer); //links to private static void empty 
      } 

      if (Option.equals("D")){ //Deletes a customer from a room 

       //Searches if room is occupied, if it is then it will delete customer from that room 
       view(hotel, Customer); 
       System.out.println("Enter the room which you want to delete a customer from: "); 
       roomNum = input.nextInt(); 
       hotel[roomNum] = "empty"; 

       delete(hotel, Customer); 
       System.out.println(""); 

      } 

      if (Option.equals("F")){ //view all empty rooms 
       find(hotel); //links to private static void empty 
      } 

     } 
    } 

    private static void initialise(String hotelRef[]) 
    { 
     for (int x = 1; x < 11; x++) 
      hotelRef[x] = "empty"; 
     System.out.println("Welcome to The Plaza"); 
    } 


    public static void view(String hotel[], String Customer){ 

     for (int x =1; x <11; x++) 
     { 
      int z=0; 
      String Customername = Customer; 
      hotel[z]= Customername; 

      if (hotel[x].equals("empty")) 
       System.out.println("room " + x + " is empty"); 
      else { 
       System.out.println("room " + x + " is occupied by "+ hotel[z]); 
      } 
     } 
    } 

    private static void add(String hotel[], String Customer){ 
     for (int x =1; x <11; x++) 
     { 
      int z=0; 
      String Customername = Customer; 
      hotel[z]= Customername; 
      if (hotel[x].equals("empty")) 
       System.out.println("room " + x + " is empty"); 
      else { 
       System.out.println("room " + x + " is occupied by "+ hotel[z]); 
      } 

     } 
    } 

    private static void vacant(String hotel[], String Customer){ 
     for (int x = 1; x < 11; x++) 
     { 
      int z=0; 
      String Customername = Customer; 
      hotel[z]= Customername; 
      if (hotel[x].equals("empty")) //if statement 
       System.out.println("room " + x + " is empty"); 
     } 

    } 


    private static void delete(String hotel[], String Customer){ //link to this when pressed the D key 
     //view (hotel); 
     for (int x = 1; x < 11; x++) 
     { 
      int z=0; 
      String Customername = Customer; 
      hotel[z]= Customername; 
      if (hotel[x].equals("empty")) 
      { 
       System.out.println("room " + x + " is empty"); 
      } 
      else { 
       System.out.println("room " + x + " occupied by " + hotel[x]); 
      } 
     } 
    } 

    private static void find(String hotel[]){ 

     Scanner input = new Scanner(System.in); 
     System.out.println("Enter customer name for room:"); 
     String customersname; 
     customersname = input.next(); //stores name they enter as customers name 
     for (int x = 0; x < 10; x++) 
     { 
      if (hotel[x].equals(customersname)) 
       System.out.println("room " + x + " is occupied      by "+hotel[x]); 

     } 
    } 
} 
+0

你在這裏的問題是什麼? – 2013-03-02 19:48:27

+0

我剛剛編輯我的帖子。 – 2013-03-02 19:49:01

+2

您可以在發佈時更好地設置您的代碼的格式嗎? – vikingsteve 2013-03-02 19:51:24

回答

1

原因你的問題是,你是不是實例化每個新條目的類。我很抱歉,但是這個問題的根本錯誤太多了。你需要做的只是定義客戶類及其數據成員和數據訪問功能,然後根據需要創建新對象,並從另一個類完全填充/使用它們。

你可以嘗試這樣的事:

import java.util.*; 

class Customer 
{ 
    private String name; 
    private int room; 

    public void setName(String name) 
    { 
     this.name=name; 
    } 

    public String getName() 
    { 
     return this.name; 
    } 

    public void setRoom(int room) 
    { 
     this.room=room; 
    } 

    public int getRoom() 
    { 
     return this.room; 
    } 
} 

class Hotel 
{ 
    public static void initialize(Customer RoomList[]) 
    { 
     for(int i=0; i<RoomList.length; i++) 
     { 
      RoomList[i]=new Customer(); 
      RoomList[i].setName("EMPTY"); 
      RoomList[i].setRoom(i+1); 
     } 
    } 

    public static void viewList(Customer RoomList[]) 
    { 
     for(int i=0; i<RoomList.length; i++) 
     { 
      if(RoomList[i].getName()=="EMPTY") 
       System.out.println("Room number "+RoomList[i].getRoom()+" is vacant."); 
      else 
       System.out.println("Room number "+RoomList[i].getRoom()+" is ocupied by "+RoomList[i].getName()+"."); 
     } 
     System.out.println(); 
    } 

    public static boolean addCustomer(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals("EMPTY")) 
      { 
       RoomList[i].setName(name); 
       return true; 
      } 
     return false; 
    } 

    public static void showEmptyRooms(Customer RoomList[]) 
    { 
     System.out.println("Available rooms are:"); 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName()=="EMPTY") 
       System.out.println(RoomList[i].getRoom()); 
     System.out.println(); 
    } 

    public static boolean deleteCustomer(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals(name)) 
      { 
       RoomList[i].setName("EMPTY"); 
       System.out.println("Deletion successful.\n"); 
       return true; 
      } 
     return false; 
    } 

    public static int getIndex(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals(name)) 
       return i; 
     return -1; 
    } 

    public static void main(String[] args) 
    { 
     Customer[] RoomList = new Customer[12]; 
     String name; 
     initialize(RoomList); 
     Scanner input = new Scanner(System.in); 
     int option=0; 

     do 
     { 
      System.out.println("  Hotel Booking Options"); 
      System.out.println("====================================="); 
      System.out.println("1: To View all rooms"); 
      System.out.println("2: To Add customer to a room"); 
      System.out.println("3: To Display empty rooms"); 
      System.out.println("4: To Delete customer from a room"); 
      System.out.println("5: Find room from customer name"); 
      System.out.println("0: Exit"); 

      System.out.print("\nEnter your choice: "); 
      option = input.nextInt(); 
      System.out.println(); 

      switch(option) 
      { 
       case 1: 
       { 
        viewList(RoomList); 
        break; 
       } 
       case 2: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        if(!addCustomer(RoomList, name)) 
         System.out.println("No rooms available!"); 
        break; 
       } 
       case 3: 
       { 
        showEmptyRooms(RoomList); 
        break; 
       } 
       case 4: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        deleteCustomer(RoomList, name); 
        break; 
       } 
       case 5: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        System.out.println("Customer's room: "+RoomList[getIndex(RoomList, name)].getRoom()+"\n"); 
        break; 
       } 
       case 0: 
       { 
        System.out.println("\nThank you!\n"); 
        break; 
       } 
       default: 
       { 
        System.out.println("Invalid option!\n"); 
        break; 
       } 
      } 


     }while(option!=0); 
    } 
} 
+0

我明白了..我是Java新手,所以還有很多東西需要學習。但是謝謝 – 2013-03-02 20:10:12

+0

@ShaziaE:試着理解我剛剛添加的新代碼。嘗試按照相同的方式編造一些東西。我希望這有幫助。 – 2013-03-02 22:00:25

+0

@ShaziaE面向對象編程的評論在這個過程中很有用。 – Varaquilex 2013-03-02 22:06:52

0

你的代碼似乎稍微不正確。看看添加的客戶代碼。無論您添加客戶的位置如何,它也會添加到索引零。 :)

if (Option.equals("A")){ // adding a customer to a room 
    System.out.println("Enter room number(1-10)"); 
    roomNum =input.nextInt(); 
    System.out.println("Enter name for room " + roomNum + " : ") ; 
    Customer = input.next(); 
    hotel[roomNum] = Customer ; // adds to the room number. 
    add(hotel, Customer); // adds to the zero-th index. 
    System.out.println(" "); 
} 


private static void add(String hotel[], String Customer){ 
    for (int x =1; x <11; x++) 
    { 
     int z=0; 
     String Customername = Customer; 
     hotel[z]= Customername; // this line is the culprit. 
     if (hotel[x].equals("empty")) 
      System.out.println("room " + x + " is empty"); 
     else { 
      System.out.println("room " + x + " is occupied by "+ hotel[z]); 
     } 

    } 
}