2016-03-05 75 views
0

下面我創建了一個酒店計劃,允許客戶留在房間裏。我最近已經能夠創建一個查看我的程序當前所有房間的功能,但是我無法將正確的代碼寫入添加一個客戶裏面的addCustomer()功能。這是我目前的代碼:酒店計劃 - 添加客戶

package hotelprogram; 

import java.util.Scanner; 

public class HotelProgram { 

// global variables 

static String[] hotel = new String[7]; 

private static void initialise(String hotelRef[]) { 

    for (int x = 0; x < 6; x++) { 
     hotelRef[x] = "e"; 
     System.out.println("initilise"); 
    } 
} 

private static void viewALL() { 

    // Views all current rooms 

    for (int x = 0; x < 6; x++) { 
     if (!hotel[x].equals("e")) { 
      System.out.println("Room " + x + " occupied by " + hotel[x]); 
     }else{ 
      System.out.println("Room" + x + " is Empty"); 
     } 
    } 
} 

private static void addCustomer() 
{ 
    // Add a customer 

    Scanner sc = new Scanner (System.in); 

    int roomNumfc; 
    int roomNamefc; 

    for (int x = 0; x < 6; x++) 
    { 
     System.out.println("Select room number to add a customer: "); 
     roomNumfc = sc.nextInt(); 
     System.out.println("New customer to add: "); 
     roomNamefc = sc.nextInt(); 

    } 


} 


public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    String roomName; 
    int roomNum = 0; 
    char selectChoice; 

    for (int x = 0; x < 6; x++) { 
     hotel[x] = ""; 
     initialise(hotel); 
    } 

    while (roomNum < 6) { 
     // Displays current rooms 

     for (int x = 0; x < 6; x++) { 

      // displays an empty room 

      if (hotel[x].equals("e")) { 
       System.out.println("Room" + x + " is Empty"); 
      } 
     } 
     // Enter a room prompt 

     System.out.println("Enter a room number from (0-5) or higher to stop (10 to display menu): "); 
     roomNum = input.nextInt(); 

     if (roomNum == 10) { 

      // menu if roomNum = 10 

      System.out.println("Make a selection: "); 
      System.out.println("A. Add a customer to room"); 
      System.out.println("V. View all rooms"); 
      selectChoice = input.next().charAt(0); 

      switch (selectChoice) { 
      case 'A': 
       addCustomer(); 
       break; 
      case 'V': 
       viewALL(); 
       break; 
      default: 
       System.out.println("Bad input, exiting program..."); 
       break; 
      } 
      break; 
     } 
     // Name current room promt 

     System.out.println("Please name this room: " + roomNum + " :"); 
     roomName = input.next(); 
     hotel[roomNum] = roomName; 

     // Room displayed 
     for (int x = 0; x < 6; x++) { 
      if (!hotel[x].equals("e")) { 
       System.out.println("Room " + x + " occupied by " + hotel[x]); 
      } 
     } 
    } 
    } 
} 

我不太確定如何訪問所使用的全局數組,真的可以在這方面使用一些幫助。提前致謝!

回答

0

不要把你的程序邏輯收集輸入(最多)在你的主要。通過這樣做,你遵循了面向對象的設計方法,避免討厭的使用全局數組/變量

public static void main(String... args){ 
    HotelProgram hotel = new HotelProgram(); 
    //...gather input 
    //while input != 10 
     //get room num 
     //get room name 
     hotel.addCustomer(num, name); 
     hotel.printRooms(); 

} 

:該實例創建的類和調用方法的實例。

這裏是你的代碼修改,以消除全局和更加友好的OO:

public class HotelProgram { 
    private String[] rooms = new String[7]; 

    public HotelProgram(){ 
     initialise(); 
    } 

    private void initialise() { 
     for (int x = 0; x < rooms.length; x++) { 
      rooms[x] = "e"; 
     } 
     System.out.println("initilise is complete."); 
    } 

    public void printRooms() { 
     for (int i = 0; i < rooms.length; i++){ 
      if(rooms[i].equals("e")){ 
       System.out.println("Room(" + (i+1) + ") is Empty."); 
      } 
      else{ 
       System.out.println("Room(" + (i+1) + ") is occupied by " + rooms[i]); 
      } 
     } 
    } 

    public void addCustomer(Scanner sc){ 
     int roomNum = 0; 
     while(roomNum-1 < 0 || roomNum-1 > rooms.length-1){ 
      System.out.println("Enter a room number from (1-" + (rooms.length) + "):"); 
      roomNum = sc.nextInt(); 
     } 

     System.out.println("Please provide the customer name for room " + (roomNum) + " :"); 
     sc.nextLine(); 
     String customer = sc.nextLine(); 
     rooms[roomNum-1] = customer; 
    } 

    public void printMenu(){ 
     System.out.println("\nMake a selection: "); 
     System.out.println("A. Add a customer to room"); 
     System.out.println("V. View all rooms"); 
     System.out.println("M. Print this menu."); 
     System.out.println("E. Exit\n"); 
    } 


    public static void main(String[] args) { 
     HotelProgram hotel = new HotelProgram(); 
     Scanner sc = new Scanner (System.in); 
     hotel.printMenu(); 

     String selection = ""; 
     while(true){ 
      selection = sc.nextLine(); 
      switch(selection){ 
       case "E": 
        System.out.println("Thanks for visiting. Have a good day!"); 
        System.exit(0); 
       case "A": 
        hotel.addCustomer(sc); 
        break; 
       case "V": 
        hotel.printRooms(); 
        break; 
       case "M": 
        hotel.printMenu(); 
        break; 
       default: 
        System.out.println("Bad input (" + selection + "), Try Again..."); 
      } 
     } 
    } 
} 
0

這是不好的面向對象設計。我強烈建議你改變你的程序邏輯。此外,通過getter和setter方法更好地訪問變量,並避免使用靜態變量。但是,如果要訪問main()中的靜態數組,則應該創建類的實例

HotelProgram myhotel = new HotelProgram(); 
myhotel.hotel[i]=.... // i can be any of the allowed array indices.