shihmeng91 0 Newbie Poster

This fe. My system able to run, but at seat number for the ferry is not changing. i am not sure what happening, but i guess it related to the for loop.

It you start the system. Press P -> Press B-> Name -> Select ferry -> Then it print out the ferry ticket. ( repeat few time ). Then you will realize the seat number is not changing.

import java.util.Scanner;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class FerryTicketingSystem
{
	public static int[] A = new int[50]; public static int Ab=0;
	public static int[] B = new int[50]; public static int Bb=0;
	public static int[] C = new int[50]; public static int Cb=0;
	public static int[] D = new int[50]; public static int Db=0;
	public static int[] E = new int[50]; public static int Eb=0;
	public static int[] F = new int[50]; public static int Fb=0;
	public static int[] G = new int[50]; public static int Gb=0;
	public static int[] H = new int[50]; public static int Hb=0;
	public static int[] I = new int[50]; public static int Ib=0;
	public static int[] J = new int[50]; public static int Jb=0;
	public static int[] K = new int[50]; public static int Kb=0;
	public static int[] L = new int[50]; public static int Lb=0;
	public static int[] M = new int[50]; public static int Mb=0;
	public static int[] N = new int[50]; public static int Nb=0;
	public static int[] O = new int[50]; public static int Ob=0;
	public static int[] P = new int[50]; public static int Pb=0;


	public static int Ae=10;
	public static int Be=10;
	public static int Ce=10;
	public static int De=10;
	public static int Ee=10;
	public static int Fe=10;
	public static int Ge=10;
	public static int He=10;
	public static int Ie=10;
	public static int Je=10;
	public static int Ke=10;
	public static int Le=10;
	public static int Me=10;
	public static int Ne=10;
	public static int Oe=10;
	public static int Pe=10;


   	public static void main (String [] args){
        mainMenu(); //go to Ferry ticket Main Menu
    }

   		public static void mainMenu(){
        Scanner keyboard = new Scanner(System.in);//A scanner object to read input

        char selection; //customer's selection

        //Main Menu print out
        System.out.println("\nWelcome to Ferry Ticketing System.");
        System.out.println("************Main Menu*************");
        System.out.println("Purchase Ticket: \t\t\t P       ");
        System.out.println("View Seating Arragement: \t V  ");
        System.out.println("Quit the system: \t\t\t Q       ");

		System.out.printf("Please enter your selection: ");	//A scanner object to read input
        selection = keyboard.next().charAt(0);//store selection as character

		switch(selection){
	            case'P'://Purchase Ferry Ticket
	            case'p':
	            	subMenu();

	            case'v'://View Ferry Seat
	            case'V':
	            	ferryView();

	            break;

	            case'q':
	            case'Q':
	                System.out.println("Thank you, have a nice day.");
	                System.exit(0);//exit the ferry ticketing system
	                break;

	            default:
	                System.out.println("Invalid selection.");
	                mainMenu();//go back to main menu
	            break;
	        }
        }

        public static void subMenu(){
        Scanner keyboard = new Scanner(System.in);

        System.out.println("\n************Welcome to Class Selection.************");
        System.out.println("Purchase ticket for Business class\tB");
        System.out.println("Purchase ticket for Economy class\tE");
        System.out.println("Return to Main Menu\t\t\t\t\tM");

        char suboption;
        System.out.printf("Please enter your class selection: ");
      	suboption = keyboard.next().toUpperCase().charAt(0);

	        if(suboption == 'B'){
	        	businessMenu();//business class
	        }

		    else if(suboption == 'E'){
		        economyMenu();//economy class
		    }

		    else if(suboption == 'M'){
		        mainMenu();//back to Main Menu

		    }
		    else{
		    	System.out.println("\n!!Invail selection!!");
		    	subMenu();//Purchase Menu
		    }

        }

		public static void businessMenu()
		{
			Scanner keyboard = new Scanner(System.in);

			char Businessferry; //Business class Time Schedule choices
			char Ans; // choice to continue
			String Name; // User Input Name

			System.out.printf("\nYour Name: ");
			Name = keyboard.nextLine();

			System.out.println ("\n\n*************Business Class Menu****************");
	    	ferrySchedule(); // To Print Out the Ferry Schedule
		   	System.out.printf("Please select a ferry: ");



		   		Businessferry = keyboard.next().toUpperCase().charAt(0);
		   		if (Businessferry == 'A'){
		   			RunArray(A,Ab,10,Name,"01","10:00am","Business","Penang","Langkawi",50,"Economy",Ae );
		   		}//End for A
		   		else if (Businessferry == 'B'){
		   			RunArray(B,Bb,10,Name,"02","11:00am","Business","Penang","Langkawi",50,"Economy",Be );
		   		}//End for B
		   		else if (Businessferry == 'C'){
		   			RunArray(C,Cb,10,Name,"03","12:00pm","Business","Penang","Langkawi",50,"Economy",Ce );
		   		}//End for C
		   		else if (Businessferry == 'D'){
		   			RunArray(D,Db,10,Name,"04","1:00pm","Business","Penang","Langkawi",50,"Economy",De );
		   		}//End for D
		   		else if (Businessferry == 'E'){
		   			RunArray(E,Eb,10,Name,"05","2:00pm","Business","Penang","Langkawi",50,"Economy",Ee );
		   		}//End for E
		   		else if (Businessferry == 'F'){
		   			RunArray(F,Fb,10,Name,"06","3:00pm","Business","Penang","Langkawi",50,"Economy",Fe );
		   		}//End for F
			   	else if (Businessferry == 'G'){
		   			RunArray(G,Gb,10,Name,"07","4:00pm","Business","Penang","Langkawi",50,"Economy",Ge );
		   		}//End for G
		   		else if (Businessferry == 'H'){
		   			RunArray(H,Hb,10,Name,"08","5:00pm","Business","Penang","Langkawi",50,"Economy",He );
		   		}//End for H
		   		else if (Businessferry == 'I'){
		   			RunArray(I,Ib,10,Name,"05","10:00am","Business","Penang","Langkawi",50,"Economy",Ie );
		   		}//End for I
		   		else if (Businessferry == 'J'){
		   			RunArray(J,Jb,10,Name,"06","11:00am","Business","Penang","Langkawi",50,"Economy",Je );
		   		}//End for J
		   		else if (Businessferry == 'K'){
		   			RunArray(K,Kb,10,Name,"07","12:00pm","Business","Penang","Langkawi",50,"Economy",Ke );
		   		}//End for K
		   		else if (Businessferry == 'L'){
		   			RunArray(L,Lb,10,Name,"08","1:00pm","Business","Penang","Langkawi",50,"Economy",Le );
		   		}//End for L
		   		else if (Businessferry == 'M'){
		   			RunArray(M,Mb,10,Name,"01","2:00pm","Business","Penang","Langkawi",50,"Economy",Me );
		   		}//End for M
		   		else if (Businessferry == 'N'){
		   			RunArray(N,Nb,10,Name,"02","3:00pm","Business","Penang","Langkawi",50,"Economy",Ne );
		   		}//End for N
		   		else if (Businessferry == 'O'){
		   			RunArray(O,Ob,10,Name,"03","4:00pm","Business","Penang","Langkawi",50,"Economy",Oe );
		   		}//End for O
		   		else if (Businessferry == 'P'){
		   			RunArray(P,Pb,10,Name,"04","5:00pm","Business","Penang","Langkawi",50,"Economy",Pe );
		   		}//End for P
		   		else if (Businessferry == 'R'){
		   			System.out.println("Return to Main Menu...");
		   			mainMenu();//back to Main Menu
		   		}
		   		else{
			   		System.out.println("\n!!invalid Ferry input, please try again!!");
			   		businessMenu();//back to Business Menu
		   		}


		}

		public static void economyMenu()
		{
			Scanner keyboard = new Scanner(System.in);

			char Economyferry; //Economy class Time Schedule choices
			char Ans; // choice to continue
			String Name; // User Input Name

			System.out.printf("\nYour Name: ");
			Name = keyboard.nextLine();

			System.out.println ("\n\n*************Economy Class Menu****************");
	    	ferrySchedule(); //Print out Ferry Schedule
		   	System.out.printf("Please select a ferry: ");


		   	for (int i=0 ; i<10 ; i++){
		   		Economyferry = keyboard.next().toUpperCase().charAt(0);
		   		if (Economyferry == 'A'){
		   			RunArray(A,Ae,50,Name,"01","10:00am","Economy","Langkawi","Penang",50,"Business",Ab );
		   		}
		   		else if (Economyferry == 'B'){
		   			RunArray(B,Be,50,Name,"02","11:00am","Economy","Langkawi","Penang",50,"Business",Bb );
		   		}
		   		else if (Economyferry == 'C'){
		   			RunArray(C,Ce,50,Name,"03","12:00pm","Economy","Langkawi","Penang",50,"Business",Cb );
		   		}
		   		else if (Economyferry == 'D'){
		   			RunArray(D,De,50,Name,"04","1:00pm","Economy","Langkawi","Penang",50,"Business",Db );
		   		}
		   		else if (Economyferry == 'E'){
		   			RunArray(E,Ee,50,Name,"05","2:00pm","Economy","Langkawi","Penang",50,"Business",Eb );
		   		}
		   		else if (Economyferry == 'F'){
		   			RunArray(F,Fe,50,Name,"06","3:00pm","Economy","Langkawi","Penang",50,"Business",Fb );
		   		}
			   	else if (Economyferry == 'G'){
		   			RunArray(G,Ge,50,Name,"07","4:00pm","Economy","Langkawi","Penang",50,"Business",Gb );
		   		}
		   		else if (Economyferry == 'H'){
		   			RunArray(H,He,50,Name,"08","5:00pm","Economy","Langkawi","Penang",50,"Business",Hb );
		   		}
		   		else if (Economyferry == 'I'){
		   			RunArray(I,Ie,50,Name,"05","10:00am","Economy","Langkawi","Penang",50,"Business",Ib );
		   		}
		   		else if (Economyferry == 'J'){
		   			RunArray(J,Je,50,Name,"06","11:00am","Economy","Langkawi","Penang",50,"Business",Jb );
		   		}
		   		else if (Economyferry == 'K'){
		   			RunArray(K,Ke,50,Name,"07","12:00pm","Economy","Langkawi","Penang",50,"Business",Kb );
		   		}
		   		else if (Economyferry == 'L'){
		   			RunArray(L,Le,50,Name,"08","1:00pm","Economy","Langkawi","Penang",50,"Business",Lb );
		   		}
		   		else if (Economyferry == 'M'){
		   			RunArray(M,Me,50,Name,"01","2:00pm","Economy","Langkawi","Penang",50,"Business",Mb );
		   		}
		   		else if (Economyferry == 'N'){
		   			RunArray(N,Ne,50,Name,"02","3:00pm","Economy","Langkawi","Penang",50,"Business",Nb );
		   		}
		   		else if (Economyferry == 'O'){
		   			RunArray(O,Oe,50,Name,"03","4:00pm","Economy","Langkawi","Penang",50,"Business",Ob );
		   		}
		   		else if (Economyferry == 'P'){
		   			RunArray(P,Pe,50,Name,"04","5:00pm","Economy","Langkawi","Penang",50,"Business",Pb );
		   		}
		   		else if (Economyferry == 'R'){
		   			System.out.println("Return to Main Menu...");
		   			mainMenu();
		   		}
		   		else{
			   		System.out.println("\n!!invalid Ferry input, please try again!!");
			   		economyMenu();
		   		}
		   	}


		}

		public static void ferryView(){
			Scanner keyboard = new Scanner (System.in);
			char viewSeletion;

			System.out.println("\n\n*******************View Menu********************");
	    	ferrySchedule();
		   	System.out.printf("Please select a ferry to view: ");
		   	viewSeletion = keyboard.next().toUpperCase().charAt(0);

		   			if (viewSeletion == 'A') {
		        		view(A,"01","10a.m.","Penang");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'B') {
		        		view(B,"02","11a.m.","Penang");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'C') {
		        		view(C,"03","12p.m.","Penang");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'D') {
		        		view(D,"04","01p.m.","Penang");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'E') {
		        		view(E,"05","02p.m.","Penang");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'F') {
		        		view(F,"06","03p.m.","Penang");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'G') {
		        		view(G,"07","04p.m.","Penang");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'H') {
		        		view(H,"08","05p.m.","Penang");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'I') {
		        		view(I,"05","10a.m.","Langkawi");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'J') {
		        		view(J,"06","11a.m.","Langkawi");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'K') {
		        		view(K,"07","12p.m.","Langkawi");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'L') {
		        		view(L,"08","01p.m.","Langkawi");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'M') {
		        		view(M,"01","02p.m.","Langkawi");
		        		mainMenu();
		        	}else 	if (viewSeletion == 'N') {
		        		view(N,"02","03p.m.","Langkawi");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'O') {
		        		view(O,"03","04p.m.","Langkawi");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'P') {
		        		view(P,"04","05p.m.","Langkawi");
		        		mainMenu();
		        	} else 	if (viewSeletion == 'R') {
		        		System.out.println("Back to Main Menu");
		        		mainMenu();
		        	} else {
		        		System.out.println("Wrong command! Returning to Menu.");
	   					System.out.println();
	   					mainMenu();
		        	}
		}

		public static void view(int[] i,String s, String time, String Destination){

			Date Now = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

			System.out.println();
			System.out.println("---------------------Seating Chart----------------------");
			displayStar();
			System.out.println("*  Ferry ID: "+s+"   Time: "+time+"     Date: "+formatter.format(Now)+"   *");
			displayStar();
			System.out.println("*  Ferry leaving to "+Destination+"                          *");
			displayStar();
			System.out.println("*    BUSINESS CLASS                                   *");
			displayStar();
			System.out.println("*          "+i[0]+"   *   "+i[1]+"   *   "+i[2]+"   *   "+i[3]+"   *   "+i[4]+"          *");
			displayStar();
			System.out.println("*          "+i[5]+"   *   "+i[6]+"   *   "+i[7]+"   *   "+i[8]+"   *   "+i[9]+"          *");
	     	displayStar();
			System.out.println("*    ECONOMY  CLASS                                   *");
			displayStar();
			System.out.println("*          "+i[10]+"   *   "+i[11]+"   *   "+i[12]+"   *   "+i[13]+"   *   "+i[14]+"          *");
			displayStar();
			System.out.println("*          "+i[15]+"   *   "+i[16]+"   *   "+i[17]+"   *   "+i[18]+"   *   "+i[19]+"          *");
			displayStar();
			System.out.println("*          "+i[20]+"   *   "+i[21]+"   *   "+i[22]+"   *   "+i[23]+"   *   "+i[24]+"          *");
			displayStar();
			System.out.println("*          "+i[25]+"   *   "+i[26]+"   *   "+i[27]+"   *   "+i[28]+"   *   "+i[29]+"          *");
			displayStar();
			System.out.println("*          "+i[30]+"   *   "+i[31]+"   *   "+i[32]+"   *   "+i[33]+"   *   "+i[34]+"          *");
			displayStar();
			System.out.println("*          "+i[35]+"   *   "+i[36]+"   *   "+i[37]+"   *   "+i[38]+"   *   "+i[39]+"          *");
			displayStar();
			System.out.println("*          "+i[40]+"   *   "+i[41]+"   *   "+i[42]+"   *   "+i[43]+"   *   "+i[44]+"          *");
			displayStar();
			System.out.println("*          "+i[45]+"   *   "+i[46]+"   *   "+i[47]+"   *   "+i[48]+"   *   "+i[49]+"          *");
			displayStar();
			System.out.println("\n");
	    }

	    public static void displayStar() {
	    	System.out.println("*******************************************************");
	    }

	    public static void ferrySchedule(){
	    	System.out.println("**************Ferry Schedule *******************");
		   	System.out.println("******To Penang  *******|******To Langkawi  ****");
		   	System.out.println("**  A - (10a.m.)       *|*      I - (10a.m.)  **");
		   	System.out.println("**  B - (11a.m.)       *|*      J - (11a.m.)  **");
		   	System.out.println("**  C - (12p.m.)       *|*      K - (12p.m.)  **");
		   	System.out.println("**  D - (01p.m.)       *|*      L - (01p.m.)  **");
		   	System.out.println("**  E - (02p.m.)       *|*      M - (02p.m.)  **");
		   	System.out.println("**  F - (03p.m.)       *|*      N - (03p.m.)  **");
		   	System.out.println("**  G - (04p.m.)       *|*      O - (04p.m.)  **");
		   	System.out.println("**  H - (05p.m.)       *|*      P - (05p.m.)  **");
		    System.out.println("************************************************");
		   	System.out.println("******  R - Return to Main Menu	      	  ******");
		   	System.out.println("************************************************");
	    }

	    public static void Ticket(String xName, String xID, String xTime, int xA, String xClass, String xDesination, String xFrom){

	    	Date Now = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

	    	System.out.println("\n****************Ticket******************");
			System.out.println("Your Name\t\t: " + xName);
			System.out.println("Ferry ID\t\t: " + xID);
			System.out.println("Date\t\t\t: " +formatter.format(Now));
			System.out.println("Time\t\t\t: " + xTime);
			System.out.println("Seat number is\t: "+ xA +xClass);
			System.out.println("Desination\t\t: " + xDesination);
			System.out.println("From\t\t\t: " + xFrom);
			System.out.println("****************************************");

	    }

	    public static void RunArray(int [] i, int Ax, int seat, String xName, String xID, String xTime, String xClass, String xDesination, String xFrom, int seatx, String Classx, int xAx ){
			Scanner keyboard = new Scanner(System.in);

			char Ans;
		for (int z=0 ; z<10 ; z++){

	    		if(Ax<seat){
		   				i[Ax] = 1; Ax++;
			   			Ticket(xName,xID,xTime,Ax,"(" + xClass + ")",xDesination,xFrom);
			   			mainMenu();
			   			}//End of if(Ax)
			   			else{
			   				if(xAx<seatx){
					   			do{
					   				System.out.println("Sorry, the "+ xClass +" ticket currently sell out.\nDo you want to purchase "+ Classx +" ticket? (Y/N)");
					   				Ans = keyboard.next().toUpperCase().charAt(0);

						   				if( Ans == 'Y' ){
						   					Ticket(xName,xID,xTime,xAx,"(" + Classx + ")",xFrom,xDesination);
						   					mainMenu();
						   				}
						   				else if ( Ans == 'N'){
						   					System.out.println("Back to Main Menu....");
						   					mainMenu();
						   				}
										else{
											System.out.println("\n!!Invalid input. Please try again!!");
										}
						   			}while (Ans!='Y'||Ans!='N');//do first then while loop
			   					}//End of if(xAx)
			   					else{
			   						System.out.println("Sorry the trip is full, cannot purchase anymore.\nPlease try others.");
			   					}
			   			}
				}
	    }

}

Here is the Java file of my coding like the above.
FerryTicketingSystem.java

if you wondering, why i make it this way, because i want to save spaces not like the below file, which work perfectly, but too long the coding.
FerryTicketingSystemTTT.java

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.