dimanche 1 mars 2015

Creating a menu with arrays - sorting results alphabetically and storing, loading from a file

I'm trying to create a menu system for a hotel program using arrays; below are my hotel booking options:



A: To Add a customer to a room
V: To View all the rooms
E: To Display all the empty rooms
D: To Delete a customer from a room
F: Find a room from a customers name
S: Store program array data into a plain text file
L: Load program data back from the file into the array
O: View rooms alphabetically by name
e: To end


I've seem to be able to do from A-F but i'm having trouble ordering the rooms alphabetically and sorting and loading the program array data from a file. Completely lost on the S and L front. A friend has helped with S,L and O but i'm not entirely sure why its not working so i have had to comment it out. If you can help fix the already given solution or something new close to the style i have written the rest of my program in that would be great.



public class Hotel {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//allows you to input selection later

String roomName;
int roomNum = 0;

String[] hotel = new String[10];
//for (int x = 0; x < 10; x++ ) hotel[x] = ""

initialise(hotel); //better to initialise in a procedure

String MenuChoice; //calling menu choice a String
char MC; //Reffering menu choice (String) as char

do {
System.out.println("Hotel Booking Options");
System.out.println("=====================================");
System.out.println("A: To Add a customer to a room");
System.out.println("V: To View all the rooms");
System.out.println("E: To Display all the empty rooms");
System.out.println("D: To Delete a customer from a room");
System.out.println("F: Find a room from a customers name");
System.out.println("S: Store program array data into a plain text file");
System.out.println("L: Load program data back from the file into the array");
System.out.println("O: View rooms alphabetically by name");
System.out.println("e: To end ");
System.out.println(" ");
System.out.println("Please choose the required option and enter below: ");
//Print statement
MenuChoice = input.next();
MC = MenuChoice.charAt(0); //(MC)MenuChoice = MenuChoice.charAt(0);

switch (MC) {
case 'A':
addCustomerToRoom(hotel);//calls the addCustomerToRoom statement below
break; //case break A
case 'V':
viewAllRooms(hotel); //calls the viewAllRooms statement below
break; //case break V
case 'E':
displayEmptyRooms(hotel); //calls the displayEmptyRooms statement below
break; //case break E
case 'D':
deleteCustomer(hotel); //calls the deleteCustomer statement below
break; //case break D
case 'F':
findCustomer(hotel); //calls the findCustomer statement below
break; //case break F
case 'S':
// storeInTextFile(hotel); //calls the deleteCustomer statement below
break; //case break S
case 'L':
// loadFromTextFile(hotel); //calls the findCustomer statement below
break; //case break L
case 'O':
// orderAlphabetically(hotel);
break; //case break O
} //end menuChoice MC
System.out.print(" ");
} while (MC != 'e');
}

private static void initialise(String hRef[]) {
for (int x = 0; x < 10; x++) {
hRef[x] = "no customers";
} //end for loop
System.out.println("initilise ");
} //end initialise

private static void viewAllRooms(String hRef[]) {
for (int x = 0; x < 10; x++) {
if (hRef[x].equals("e")) {
System.out.println("Room " + x + " is empty ");
} //end if statement
else {
System.out.println("Room " + x + " is currently occupied by " + hRef[x]);
} //end else statement
} //end for loop
} //end viewAllRooms

private static void addCustomerToRoom(String hRef[]) {
Scanner input = new Scanner(System.in);
int rNumRef; //different variable to roomNum in Main method
String rNameRef;

System.out.println("Enter room number (0-9) or 10 to stop:");
rNumRef = input.nextInt(); //input the room number

System.out.println("Enter name for room " + rNumRef + " :");
rNameRef = input.next(); //input the customer name
hRef[rNumRef] = rNameRef; //both variables are the same

} //end addCustomerToRoom

private static void displayEmptyRooms(String hRef[]) {
for (int x = 0; x < 10; x++) {
if (hRef[x].equals("no customers")) {
System.out.println("Room " + x + " is empty ");
} //end if statement
} //end for loop
} //end displayEmptyRooms

private static void deleteCustomer(String hRef[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Customer Name");
String rNameRef;
rNameRef = input.next(); //input the customer name

for (int x = 0; x < 10; x++) {
if (hRef[x].equals(rNameRef)) {
hRef[x] = "no customers";
System.out.println("Customer " + rNameRef + " has been removed from Room " + x);
} //end if statement
} //end for loop
} //enddeleteCustomer

private static void findCustomer(String hRef[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Customer Name");
String rNameRef;
rNameRef = input.next(); //input the customer name

for (int x = 0; x < 10; x++) {
if (hRef[x].equalsIgnoreCase(rNameRef)) {
System.out.println("Room " + x + " is currently occupied by " + hRef[x]);
} //end if statement
} //end for loop
} //end findCustomer

// private static void storeInTextFile(String hRef[]) throws FileNotFoundException {
// String HotelFile = "sec/txtFile/HotelCoursework.txt";
// try (PrintStream HotelStream = new PrintStream(HoteFile))
// for (int x = 0; x < 10; x++) {
// HotelStream.println(/*x + " " +*/hRef[x]);
// }
// HotelStream.close();
// }
// /*String HotelFile = "H:HotelCoursework.txt";
// PrintStream HotelStream = new PrintStream (HotelFile);
// String rNameRef = new String();
// for (int x = 0; x < 10; x++) (HotelStream.println(/*x + " " + hRef[x]);
// HotelStream.close();
// */
// } //end storeInTextFile
// private static void loadFromTextFile(String hRef[]) throws FileNotFoundException {
// String HotelScan = new Scanner(new BufferedReader(new FileReader("sec/txtFile/HotelCoursework.txt")));
// for (int jj = 0; j < 10; j++) {
// hRef[jj] = HotelScan.nextLine();
// } //end for loop
// }//end loadFromTextFile
//
// private static void orderAlphabetically(String hRef[]) {
// boolean flag = true;
// String temp;
// int j;
// while (flag) {
// flag = false;
// for (j = 0; j < hotel.length - 1; j++) {
// if (hRef[j].compareToIgnoreCase(hRef[j + 1]) > 0) {
// temp = hRef[j];
// hRef[j] = hRef[j + 1];
// hRef[j + 1] = temp;
// flag = true;
// } //end if statement
// } //end for loop
// } //end while loop
// } //end orderAlphabetically
} //end public class

Aucun commentaire:

Enregistrer un commentaire