Hotel mit array

Okay, im Grunde erstelle ich ein hotel, das die Handlungen der folgenden Optionen

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");
  • JETZT bin ich in der Lage zu tun, die ANZEIGEN, HINZUFÜGEN KUNDEN, ANZEIGE der LEEREN RÄUME UND LÖSCHEN".
  • ABER ich habe gerade realisiert, dass ich nicht fügen Sie mehrere Kunden. Der code wird einfach zuweisen ein Kunde mit dem gleichen Namen für jedes Zimmer gewählt.

Beispiel: Wenn ich den Namen TOM auf Platz 2, und dann später hinzufügen DENNIS auf Zimmer 3.
Zimmer 2 und Zimmer 3, die zeigen, dass es besetzt ist von Dennis.
- Ich weiß, warum, da der String Kunde kann nur speichern eine information.

Ich habe das Gefühl, dass ich ändern müssen Kunden auf ein Array.

(bitte im Hinterkopf kahl, ich will nicht von einer ArrayList).

SO: meine Frage ist, wie gehe ich vor, dies zu lösen? Denn ich habe versucht, die Arbeit mit den Kunden als ein array und ich ruiniert die gesamte Codierung (thankgod für die lokale Geschichte zurückkehren!)

Code:

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]);

        }
    }
}
  • Was ist Ihre Frage?
  • Ich habe gerade bearbeitet meine post.
  • Können Sie bitte formatieren Sie Ihren code besser ist, wenn Sie post?
  • Dein code ist sehr schlecht eingerückt, so dauerte es eine Weile, um es zu Bearbeiten. In der Zeit, dass ich das Bearbeiten, Ihr bearbeitet die Frage, so war es überschrieben. Habe ich wieder Hinzugefügt, es zurück.
  • Danke..ich bin neu bei Stackoverflow so war das nicht bewusst. aber danke
  • Nächstes mal bitte Einrücken Ihres Codes vor der Veröffentlichung. Es gibt viele Editoren, kann dies für Sie tun. Ich weiß nicht, wie Sie Lesen können Ihren eigenen code, wenn Sie die Programmierung ohne die richtige Einrückung.
  • Ich würde auch empfehlen, ein Lesen durch den Java Code-Konventionen für die Namensgebung. (Hinweis: zum Starten einer Variablen mit einem Großbuchstaben ist eine sehr schlecht Idee). Vielleicht möchten Sie auch zu suchen in einer Objekt-orientierten Ansatz für diese, wie Java ist schließlich eine Objekt-orientierte Sprache. Denken Sie es so: anstatt nur zu sparen der name des letzten Kunden eingegeben, speichern Sie einen anderen Customer Objekt (die Sie nicht gemacht haben, noch sollten Sie) in eine Liste von Ihnen. Dann speichern Sie alle von Ihnen. Schauen Roney-Antwort für Beispiel auf eine einfache Customer Objekt.

InformationsquelleAutor Shazia E | 2013-03-02
Schreibe einen Kommentar