Reputation:
I am a beginner trying to program a train booking system which has 8 seats and requires the user to input various letters in order to execute a method. I am struggling to create a method through a private static void which when the user enters ‘E’ It shows all the empty seats through an array.
Here is the code that I have currently done so far:
package trainbookingsystem;
import java.util.Scanner;
public class Trainbookingsystem {
static final int NUMBER_OF_ROOMS = 8;
public static void main(String[] args) {
int [] Train = new int [NUMBER_OF_ROOMS];
//Display an welcome and introduction to program
//repeat
Scanner in = new Scanner(System.in);
char choice;
do
{
//display a menu
displayMenu();
//read a choice
System.out.println("--------------------------------------------------");
System.out.println("Enter a letter to select an option or (Q) to exit");
System.out.println("--------------------------------------------------");
choice = in.next().toUpperCase().charAt(0);
//process that choice
switch(choice)
{
case 'Q' :System.out.println("");
break;
case 'E' : System.out.println("You chose empty room");
showEmptySeats(Train);
break;
default: System.out.println("You enetered an invalid choice");
}
//until the user presses 'Q', while choice is not 'Q'
} while (choice != 'Q');
//Exit anf Farewell
System.out.println("Thank you for using our train booking system!");
}
private static void displayMenu() {
System.out.println("|Welcome to the Train booking system|");
System.out.println("*Use the following guide to naviagte through the program*");
System.out.println("---------------------------------------------------------");
System.out.println("| A |Add customer to seat");
System.out.println("| V |View all seats");
System.out.println("| E |Display Empty seats");
System.out.println("| D |Delete customer from seat");
System.out.println("| F |Find the seat for a given customers name");
System.out.println("| S |Store program data in to file");
System.out.println("| L |Load program data in to file");
System.out.println("| O | View seats Ordered alphabetically by name");
}
private static void showEmptySeats(int[] someArray ) {
//Go through train seats array
// if a seat is empty
int a = someArray[4];
//dsiplay it
}
}
Upvotes: 1
Views: 550
Reputation: 78965
Note that uninitialized indices in an int
array will have a value of 0
. You can put 1
into the indices (seats) which get occupied. Just for the demo, I have reserved seat no. 0, 3 and 7 by putting 1
into them. Inside showEmptySeats
, I have checked the train
array for the indices which have 0
(means empty).
import java.util.Scanner;
public class Trainbookingsystem {
static final int NUMBER_OF_ROOMS = 8;
public static void main(String[] args) {
int [] train = new int [NUMBER_OF_ROOMS];
train[0]=1;
train[3]=1;
train[7]=1;
//Display an welcome and introduction to program
//repeat
Scanner in = new Scanner(System.in);
char choice;
do
{
//display a menu
displayMenu();
//read a choice
System.out.println("--------------------------------------------------");
System.out.println("Enter a letter to select an option or (Q) to exit");
System.out.println("--------------------------------------------------");
choice = in.next().toUpperCase().charAt(0);
//process that choice
switch(choice)
{
case 'Q':
System.out.println("");
break;
case 'E':
System.out.println("You chose empty room");
showEmptySeats(train);
break;
default:
System.out.println("You enetered an invalid choice");
}
//until the user presses 'Q', while choice is not 'Q'
} while (choice != 'Q');
//Exit anf Farewell
System.out.println("Thank you for using our train booking system!");
}
private static void displayMenu() {
System.out.println("|Welcome to the Train booking system|");
System.out.println("*Use the following guide to naviagte through the program*");
System.out.println("---------------------------------------------------------");
System.out.println("| A |Add customer to seat");
System.out.println("| V |View all seats");
System.out.println("| E |Display Empty seats");
System.out.println("| D |Delete customer from seat");
System.out.println("| F |Find the seat for a given customers name");
System.out.println("| S |Store program data in to file");
System.out.println("| L |Load program data in to file");
System.out.println("| O | View seats Ordered alphabetically by name");
}
private static void showEmptySeats(int[] train) {
for(int i = 0;i<train.length;i++) {
if(train[i]==0) {
System.out.println("Seat no. "+i+" is empty");
}
}
}
}
A sample run:
|Welcome to the Train booking system|
*Use the following guide to naviagte through the program*
---------------------------------------------------------
| A |Add customer to seat
| V |View all seats
| E |Display Empty seats
| D |Delete customer from seat
| F |Find the seat for a given customers name
| S |Store program data in to file
| L |Load program data in to file
| O | View seats Ordered alphabetically by name
--------------------------------------------------
Enter a letter to select an option or (Q) to exit
--------------------------------------------------
E
You chose empty room
Seat no. 1 is empty
Seat no. 2 is empty
Seat no. 4 is empty
Seat no. 5 is empty
Seat no. 6 is empty
|Welcome to the Train booking system|
*Use the following guide to naviagte through the program*
---------------------------------------------------------
| A |Add customer to seat
| V |View all seats
| E |Display Empty seats
| D |Delete customer from seat
| F |Find the seat for a given customers name
| S |Store program data in to file
| L |Load program data in to file
| O | View seats Ordered alphabetically by name
--------------------------------------------------
Enter a letter to select an option or (Q) to exit
--------------------------------------------------
I hope, you should be able to proceed from here. Feel free to comment in case you need any further help.
Upvotes: 2