selections help in java

Discussion in 'OT Technology' started by airbball23, Nov 1, 2009.

  1. airbball23

    airbball23 Rent this Space only $5/mnth

    Joined:
    Jan 13, 2007
    Messages:
    1,489
    Likes Received:
    0
    I'm trying to create a reservation system but i'm stuck on the first step. I've been trying to do this for the past 2 days but i'm clueless. I tried to google it but i was unsuccessful. I binged it and was semi-successful but only to find an assignment sheet simliar to the one assigned to me :ugh:

    So when the java program loads it prints this message automatically:

    Welcome to OT Express Train Station!

    What would you like to do:
    1. Buy Train Ticket(s)
    2. Return Train Ticket(s)
    3. Print Info
    4. Exit

    Now printing that message was the easy part - the part which i'm stuck at and need your help is - how do i get it so when the user types in the number 1 it goes to the buy train ticket screen - pressing 2 goes to return train ticket screen etc.? Do i use a do while? the only hint i got was using a scanner.readInt();

    this is what i tried:

    Code:
    import java.util.Scanner;
    
    public class ts
    {
        public static final int COST_1 = 200;
        public static final int COST_2 = 100;
        public static final int COST_3 = 75;
        public static final int COST_4 = 125;
        private static int i;
    
        public static void main(String[] args)
        {
            Scanner input = new Scanner (System.in);
                int buy = 1;
                int returntix = 2;
                int printtix = 3;
                int exit = 4;
    
                System.out.println("Welcome to OT Express Train Station!\n");
    
    	    //....your code
    
    	    System.out.println("What would you like to do:");
    	    System.out.println("  1. Buy Train Ticket(s)");
    	    System.out.println("  2. Return Train Ticket(s)");
    	    System.out.println("  3. Print Info");
    	    System.out.println("  4. Exit\n");
    
                //...your code
                 int x = 1;
                    do {
                    System.out.println("Buy ticket to:");
                    System.out.println("  1. Chicago");
                    System.out.println("  2. Indianapolis");
                    System.out.println("  3. South Bend");
                    System.out.println("  4. Bloomington");
                        } while (i == 1)
        }
    }
    But i know the int x = 1 is wrong and the while statement might be wrong. so i'm not sure if it's a do while. I tried to think if it's an if statement like

    Code:
     if (buy == 1)
                {
                    System.out.println("Buy ticket to:");
                    System.out.println("  1. Chicago");
                    System.out.println("  2. Indianapolis");
                    System.out.println("  3. South Bend");
                    System.out.println("  4. Bloomington");
    and this semi worked but when i tried to input the number 2 it would give me the same menu as the above one instead of giving me the return ticket screen.
     
  2. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    create a ticket class and then extend that ticket class to each destination class..

    TicketClass
    private int numberOfTickets
    public double calculateCost(double ticketCost)

    extends ChicagoTixClass
    private double ticketCost

    then setter and getter both members of each class

    then add the created class to the user... Haha I know I overcomplicated things
     
  3. airbball23

    airbball23 Rent this Space only $5/mnth

    Joined:
    Jan 13, 2007
    Messages:
    1,489
    Likes Received:
    0
    :rofl: i dont think i can handle that.

    besides - there is more to the program but i need a way to first make the program select from the menu. :dunno: this is driving me insane! lol
     
  4. airbball23

    airbball23 Rent this Space only $5/mnth

    Joined:
    Jan 13, 2007
    Messages:
    1,489
    Likes Received:
    0
    this is the hint the professor gave us:

    I kind of sortof got it but not really:
    Code:
    import java.util.Scanner;
    
    public class ts
    {
        public static final int COST_1 = 200;
        public static final int COST_2 = 100;
        public static final int COST_3 = 75;
        public static final int COST_4 = 125;
        private static int i;
    
        public static void main(String[] args)
        {
            Scanner input = new Scanner (System.in);
                int buy = 1;
                int returntix = 2;
                int printtix = 3;
                int exit = 4;
    
                System.out.println("Welcome to OT Express Train Station!\n");
    
    	    //....your code
    
    	    System.out.println("What would you like to do:");
    	    System.out.println("  1. Buy Train Ticket(s)");
                System.out.println("  2. Return Train Ticket(s)");
    	    System.out.println("  3. Print Info");
    	    System.out.println("  4. Exit\n");
    
                buy = input.nextInt();
                System.out.println("Buy ticket to:");
                System.out.println("  1. Chicago");
                System.out.println("  2. Indianapolis");
                System.out.println("  3. South Bend");
                System.out.println("  4. Bloomington");
                
                returntix = input.nextInt();
                System.out.println("works");
                //...your code
                 if (buy == 1);
                    do {
                    System.out.println("Buy ticket to:");
                    System.out.println("  1. Chicago");
                    System.out.println("  2. Indianapolis");
                    System.out.println("  3. South Bend");
                    System.out.println("  4. Bloomington");
                    } while (buy == 0);
        }
    
            //...your code
    
    }
    
     
  5. sharp65

    sharp65 OT Supporter

    Joined:
    Feb 26, 2007
    Messages:
    378
    Likes Received:
    0
    Location:
    Boston
    Your going to want to use a switch statement to handle the input from the menu.

    Code:
    import java.util.Scanner;
    
    public class ts
    {
    	....
    	....
    	
    	public static void main(String[] args)
    	{
    		Scanner in = new Scanner(System.in);
    		
    		System.out.println("Welcome to OT Express Train Station!\n");
    
    	    System.out.println("What would you like to do:");
    	    System.out.println("  1. Buy Train Ticket(s)");
    	    System.out.println("  2. Return Train Ticket(s)");
    	    System.out.println("  3. Print Info");
    	    System.out.println("  4. Exit\n");
    		
    		int choice = in.nextInt();
    		
    		while (choice != 4){
    		
    			switch(choice) {
    				case 1: {
    					//buy train tickets
    				}
    				case 2: {
    					// return train tickets
    				}
    				case 3: {
    					//print info
    				}
    				case 4: {
    					//exit
    				}
    				default: {
    					//invalid input
    			}
    			}
    		}
    	}
    }	
    
     
  6. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    the point of Java is to think and act OOP .. meaning reuse code. for buying tickets, there should be a method like ticket.buy(int quantity)
     
  7. airbball23

    airbball23 Rent this Space only $5/mnth

    Joined:
    Jan 13, 2007
    Messages:
    1,489
    Likes Received:
    0
    thanks a lot for your help guys! Man it was taking me 3 days to figure out how to do something so simple. I have another roadblock now. I am confused on how I can use the count variable to figure out the line "Cost is $X, do you want to proceed?"

    Code:
    import java.util.Scanner;
    
    public class ts1
    {
        public static final int COST_1 = 200;
        public static final int COST_2 = 100;
        public static final int COST_3 = 75;
        public static final int COST_4 = 125;
        private static int i;
    
    	public static void main(String[] args)
    	{
                int Chicagotix = 0;
                int IndianapolisTix = 0;
                int SouthBendTix = 0;
                int BloomingtonTix = 0;
    		Scanner in = new Scanner(System.in);
    
    	    System.out.println("Welcome to OT Express Train Station!\n");
    
    	    System.out.println("What would you like to do:");
    	    System.out.println("  1. Buy Train Ticket(s)");
    	    System.out.println("  2. Return Train Ticket(s)");
    	    System.out.println("  3. Print Info");
    	    System.out.println("  4. Exit\n");
    
    		int choice = in.nextInt();
    
    		while (choice != 4){
    
    			switch(choice) {
    				case 1: {
    	    System.out.println("Buy ticket to:");
                System.out.println("  1. Chicago");
                System.out.println("  2. Indianapolis");
                System.out.println("  3. South Bend");
                System.out.println("  4. Bloomington");
                choice = in.nextInt();
                Chicagotix = Chicagotix +1;
                IndianapolisTix = IndianapolisTix +1;
                SouthBendTix = SouthBendTix +1;
                BloomingtonTix = BloomingtonTix +1;
    
                        switch (choice){
                            case 1: {
                    System.out.println("Make a selection:");
                    System.out.println("  1. One Way");
                    System.out.println("  2. Round Trip");
                    choice = in.nextInt();
    
                    System.out.printf("Cost is %d", COST_1);
                            }
                        }
                break;//buy train tickets
    				}
    				case 2: {
                System.out.println("Return (sell back) ticket to:");
                System.out.println("  1. Chicago");
                System.out.println("  2. Indianapolis");
                System.out.println("  3. South Bend");
                System.out.println("  4. Bloomington");
                choice = in.nextInt();
                
                break;// return train tickets
    				}
    				case 3: {
    					break;//print info
    				}
    				case 4: {
    					break;//exit
    				}
    				default: {
    					//invalid input
    			}
    			}
    		}
    	}
    }
    so i tried to figure it out with this:

    Code:
                        switch (choice){
                            case 1: {
                    System.out.println("Make a selection:");
                    System.out.println("  1. One Way");
                    System.out.println("  2. Round Trip");
                    choice = in.nextInt();
    
                    System.out.printf("Cost is %d", COST_1);
    where I was hoping i could use the COST_1 to bring in cost of the trip but i'm getting an error - nothing is showing up :confused:

    My professor told me for the make a selection to use:
    continue;

    but the System.out.printf("xyz", cost); wont allow me to run the program until after i remove continue.

    what do you think i'm doing wrong?
     
  8. SIGirl

    SIGirl Super Duper Moderator Super Moderator

    Joined:
    Nov 1, 2001
    Messages:
    22,640
    Likes Received:
    1,292
    Location:
    Austin, TX
    OK - Why are you hard coding COST_1 every time? I wrote it out for you in a way you should be able to understand. I hate the whole "Do it all in main" thing. This is way too much crap to throw in one place. Anyway, it could be refactored A LOT but I wanted you to be able to understand every line. I hope it helps.


    Code:
    while (choice != 4) {
    
    			switch (choice) {
    			case 1: {
    				System.out.println("Buy ticket to:");
    				System.out.println("  1. Chicago");
    				System.out.println("  2. Indianapolis");
    				System.out.println("  3. South Bend");
    				System.out.println("  4. Bloomington");
    				choice = in.nextInt();
    
    				System.out.println("How many?");
    				int amount = in.nextInt();
    
    				System.out.println("Make a selection:");
    				System.out.println("  1. One Way");
    				System.out.println("  2. Round Trip");
    				int sChoice = in.nextInt();
    
    				switch (sChoice) {
    				case 1: {
    					Chicagotix += amount;
    					System.out.printf("Cost is %d \n", COST_1 * amount
    							* sChoice);
    					break;
    				}
    				case 2: {
    					IndianapolisTix += amount;
    					System.out.printf("Cost is %d \n", COST_2 * amount
    							* sChoice);
    					break;
    				}
    				case 3: {
    					SouthBendTix += amount;
    					System.out.printf("Cost is %d \n", COST_3 * amount
    							* sChoice);
    					break;
    				}
    				case 4: {
    					BloomingtonTix += amount;
    					System.out.printf("Cost is %d \n", COST_4 * amount
    							* sChoice);
    					break;
    				}
    				default: {
    				}
    				}
    				break;
    			}
    
     
    Last edited: Nov 3, 2009
    Narc likes this.
  9. airbball23

    airbball23 Rent this Space only $5/mnth

    Joined:
    Jan 13, 2007
    Messages:
    1,489
    Likes Received:
    0
    I got the above to work but i'm having issues with the confirming if it's okay to proceed with x price:

    Code:
    import java.util.Scanner;
    
    public class ts709 {
        public static final int COST_1 = 200;
        public static final int COST_2 = 100;
        public static final int COST_3 = 75;
        public static final int COST_4 = 125;
    
    public static void main(String[] args) {
                int Chicagotix = 0;
                int IndianapolisTix = 0;
                int SouthBendTix = 0;
                int BloomingtonTix = 0;
                int Totalcost = 0;
                int choicemm, choicedest, choicett = 0, choiceconf;
    Scanner in = new Scanner(System.in);
    
       System.out.println("Welcome to OT Express Train Station!\n");
    
       System.out.println("What would you like to do:");
       System.out.println("  1. Buy Train Ticket(s)");
       System.out.println("  2. Return Train Ticket(s)");
       System.out.println("  3. Print Info");
       System.out.println("  4. Exit\n");
    
    choicemm = in.nextInt();
    
    while (choicemm != 4){
    
    switch(choicemm) {
    case 1: {
       System.out.println("Buy ticket to:");
                System.out.println("  1. Chicago");
                System.out.println("  2. Indianapolis");
                System.out.println("  3. South Bend");
                System.out.println("  4. Bloomington");
    
                choicedest = in.nextInt();
                switch (choicedest){
                    case 1: {
                    System.out.println("Make a selection:");
                    System.out.println("  1. One Way");
                    System.out.println("  2. Round Trip");
    
                    choicett = in.nextInt();
                            switch (choicett) {
                                case 1:
                                    System.out.printf("Cost is $%d. Would you like to purchase?\n", COST_1);
                                    System.out.println("   1. Yes");
                                    System.out.println("   2. No");
                                    choiceconf = in.nextInt();
    
                                    if (choiceconf == 1) {
                                        Chicagotix = Chicagotix +1;
                                        Totalcost = Totalcost +COST_1;
                                    }
    
                    choicett = in.nextInt();
                                    break;
    
                                case 2:
                                {
                                    System.out.printf("Cost is $%d. Would you like to purchase?\n", COST_1*2);
                                    System.out.println("   1. Yes");
                                    System.out.println("   2. No");
                                    choiceconf = in.nextInt();
                                    if (choiceconf == 2) {
                                        Chicagotix = Chicagotix +1;
                                        Totalcost = Totalcost +COST_1*2;
                                    }
                                    break;
                                }
                            }
    
                    }
    break;
                    case 2:
                        System.out.println("Make a selection:");
                        System.out.println("  1. One Way");
                        System.out.println("  2. Round Trip");
                        choicett = in.nextInt();
    
                            switch (choicett) {
                                 case 1:
    
                                    System.out.printf("Cost is $%d. Would you like to purchase?\n", COST_2);
    
                                    System.out.println("   1. Yes");
    
                                    System.out.println("   2. No");
    
                                    choiceconf = in.nextInt();
    
                                    if (choiceconf == 1) {
    
    
                                        IndianapolisTix = IndianapolisTix +1;
    
    
                                        Totalcost = Totalcost +COST_2;
    
                                    }
    
                    break;
    
                    case 2:
    
                    {
    
                        System.out.printf("Cost is $%d. Would you like to purchase?\n", COST_2*2);
    
                        System.out.println("   1. Yes");
    
                        System.out.println("   2. No");
    
                        choiceconf = in.nextInt();
    
                        if (choiceconf == 2) {
                            IndianapolisTix = IndianapolisTix +1;
                            Totalcost = Totalcost +COST_2*2;
                        }
                        break;
                    }
                            }
                            }
    
                        switch (choicett) { //new switch
                                  case 1:
    
                                    System.out.printf("Cost is $%d. Would you like to purchase?\n", COST_3);
    
                                    System.out.println("   1. Yes");
    
                                    System.out.println("   2. No");
    
                                    choiceconf = in.nextInt();
    
                                    if (choiceconf == 1) {
    
    
                                        SouthBendTix = SouthBendTix +1;
    
    
                                        Totalcost = Totalcost +COST_3;
    
                                    }
    
                    break;
    
                    case 2:
    
                    {
    
                        System.out.printf("Cost is $%d. Would you like to purchase?\n", COST_3*2);
    
                        System.out.println("   1. Yes");
    
                        System.out.println("   2. No");
    
                        choiceconf = in.nextInt();
    
                        if (choiceconf == 2) {
    
                            SouthBendTix = SouthBendTix +1;
    
                            Totalcost = Totalcost +COST_3*2;
    
                        }
    
                        break;
                    }
                            }
                            }
    
                        switch (choicett) {
                                    case 1:
                                    System.out.printf("Cost is $%d. Would you like to purchase?\n", COST_4);
                                    System.out.println("   1. Yes");
                                    System.out.println("   2. No");
                                    choiceconf = in.nextInt();
                                    if (choiceconf == 1) {
                                        BloomingtonTix = BloomingtonTix +1;
                                        Totalcost = Totalcost +COST_4;
    
                                    }
    
                    break;
    
                    case 2:
    
                    {
    
                        System.out.printf("Cost is $%d. Would you like to purchase?\n", COST_1*2);
    
                        System.out.println("   1. Yes");
    
                        System.out.println("   2. No");
    
                        choiceconf = in.nextInt();
    
                        if (choiceconf == 2) {
    
                            BloomingtonTix = BloomingtonTix +1;
    
                            Totalcost = Totalcost +COST_1*2;
    
                        }
    
                        break;
                    }
                            }
    
    
                case 2: {
                System.out.println("Return (sell back) ticket to:");
                System.out.println("  1. Chicago");
                System.out.println("  2. Indianapolis");
                System.out.println("  3. South Bend");
                System.out.println("  4. Bloomington");
                choicemm = in.nextInt();
                System.out.printf("Ticket Returned.",COST_1);
    
                break;// return train tickets
    }
    
                case 3: {
                 System.out.printf("Chicago: %d tickets.\n", Chicagotix);
                 System.out.printf("Indianpolis: %d tickets.\n",IndianapolisTix);
                 System.out.printf("South Bend: %d tickets.\n",SouthBendTix);
                 System.out.printf("Bloomington: %d tickets.\n",BloomingtonTix);
                 System.out.printf("Total Cost: %d tickets.\n", Totalcost);
    }
    
       System.out.println("\nWhat would you like to do:");{
    
       System.out.println("  1. Buy Train Ticket(s)");
    
       System.out.println("  2. Return Train Ticket(s)");
    
       System.out.println("  3. Print Info");
    
       System.out.println("  4. Exit\n");
    
                choicemm = in.nextInt();
       }
    
    break;//
    
    }
    }
    }
    }
    
    
    this is what happens:

    I just want it to be like this though:

     
  10. airbball23

    airbball23 Rent this Space only $5/mnth

    Joined:
    Jan 13, 2007
    Messages:
    1,489
    Likes Received:
    0
    n/m i figured it out. thanks anyways
     

Share This Page