Results 1 to 14 of 14
  1. #1
    Combo is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Many Switches with Many Instances

    Hello I am new to Java and I am trying to use values that are set for cases in the switch operator.
    The first menu ask for you to pick a product and each product has a price on it.
    The second menu ask you to pick a state with each state having a decimal value on it.
    The third is asking you to put the number of cases and each case is 12 items.
    A key note to remember is that each part that a person is choosing is on a different instance!

    Here is an example of what i am trying to do.
    Menu 1: I picked case 1 that is Computer and it is worth 1000
    Menu 2: I picked case 1 that is CT and it's tax is 7.5
    Third choice: I picked case 1 and that has 12 items

    I want the subtotal witch is: (1000 * 12)
    Subtotal in this situation is: 120000

    Next i need the total value which is based on what state they picked for the tax percent value picked from the state menu case: (12000 * 0.075 + 120000)
    Total value is: 129000

    I will post the code I have but based on the choices a person makes will determine the values and I need those values set in the cases to put in a math equation. The problem I am having is retrieving these numbers form the cases inside the menu options and they are on a different instance. So How can I do this in Java code?

    Here is the code:
    This is menu 1

    Java Code:
    import java.util.*; //scanners and more
    class menu{ 
    	public void display_menu() { 
    	 System.out.println ("Please select your product"); //Gives user direction
    	 System.out.println ( "1) Computer \n2) Car \n3) TV \n4) Phone \n5) IPAD" ); //menu options
         System.out.print ( "Selection: " );//Display for selection
    	}
    	public menu() {
    	   Scanner in = new Scanner ( System.in );//scanner#1
    	  
       	  display_menu();//menu #1
    	   switch ( in.nextInt() ) { 
    	    case 1:
    		int Computer = (1000);
    		System.out.println ( "You picked Computer, Price: 1000 per item." );//if 1 is picked
    		break;
    		case 2:
    		int Car = (13000);
    		System.out.println ( "You picked Car, Price: 13000 per item." );//if 2 is picked
    		break;
    		case 3:
    		int TV = (500);
    		System.out.println ( "You picked TV, Price: 500 per item." );//if 3 is picked
    		break;
    		case 4:
    		int Phone = (298);
    		System.out.println ( "You picked Phone, Price: 298 per item." );//if 4 is picked
    		break;
    		case 5:
    		int IPAD = (699);
    		System.out.println ( "You picked IPAD, Price: 699 per item." );//if 5 is picked
    		break;
    		default:
    		System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
    		new menu();//gives the user another choice to make
    		break;
    		}
    	}  
    	public static void main ( String[] args) {
    	   new menu();
    	   new statemenu();
    	   new cases();
    	   
    	}
    }
    Here is menu 2:

    Java Code:
    import java.util.*; //scanners and more
    class statemenu{	
    	public void state_menu() {
    	 System.out.println ("Please select your State");//Gives user direction
    	 System.out.println ( "1) CT 7.5% Tax \n2) VT 7.8% Tax \n3) WI 6.8% Tax \n4) CA 7.2% Tax \n5) WA 6.4% Tax" );//State menu options with taxes
    	 System.out.print ( "Selection: " );//Display for selection
        }
    	public statemenu() {
    		 Scanner out = new Scanner ( System.in );//scanner #2
    		 
    		 state_menu();//menu #2
    		 switch ( out.nextInt() ) {
    		  case 1:
    		  double CT = (7.5);
    		  System.out.print ( "You picked CT 7.5% Tax" );//if 1 is picked
    		  break;
    		  case 2:
    		  double VT = (7.8);
    		  System.out.println ( "You picked VT 7.8% Tax" );//if 2 is picked
    		  break;
    		  case 3:
    		  double WI = (6.8);
    		  System.out.println ( "You picked WI 6.8% Tax" );//if 3 is picked
    		  break;
    		  case 4:
    		  double CA = (7.2);
    		  System.out.println ( "You picked CA 7.2% Tax" );//if 4 is picked
    		  break;
    		  case 5:
    		  double WA = (7.4);
    		  System.out.println ( "You picked WA 7.4% Tax" );//if 5 is picked
    		  break;
    		  default:
    		  System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
    		  new statemenu();//gives the user another choice to make
    		  break;
    		  }
    		}
    		public static void main ( String[] args) {
    	   new statemenu();
    	}
    }
    Here is choice 3:

    Java Code:
    import java.util.*;
    class cases{
    	public void cases_amount () { 
    	 System.out.println ( "Please put the number of cases, 5 Max, 12 items per case");
    	 System.out.print ( "Selection: ");
    	 }
    	 public cases() {
    		Scanner left = new Scanner ( System.in );
    		
    		cases_amount();//menu #3
    		 switch ( left.nextInt() ) {
    		  case 1:
    		  int choice1 = (12);
    		  System.out.println ( "You picked 1 case, 12 items" );//if 1 is picked
    		  break;
    		  case 2:
    		  int choice2 = (24);
    		  System.out.println ( "You picked 2 cases, 24 items" );//if 2 is picked
    		  break;
    		  case 3:
    		  int choice3 = (36);
    		  System.out.println ( "You picked 3 cases, 36 items" );//if 3 is picked
    		  break;
    		  case 4:
    		  int choice4 = (48);
    		  System.out.println ( "You picked 4 cases, 48 items" );//if 4 is picked
    		  break;
    		  case 5:
    		  int choice5 = (60);
    		  System.out.println ( "You picked 5 cases, 60 items" );//if 5 is picked
    		  break;
    		  default:
    		  System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
    		  new cases();//gives the user another choice to make
    		  break;
    		  }
    	 }
    	public static void main ( String[] args) {
    	   new cases();
      }	
    }
    Last edited by Combo; 03-24-2014 at 04:18 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Many Switches with Many Instances

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    problem I am having is retrieving these numbers
    Are you talking about the contents of the variables that are locally defined in the switch statement?
    Move their definitions outside the switch statement so that they are at the same scope as where you want to use them.
    Last edited by Norm; 03-23-2014 at 11:23 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Combo is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Many Switches with Many Instances

    Yes the variables that are locally defined in the switch statement and I did edit to wrap my code with code tags and it looks much better.
    When you say move their definition outside the switch statement do you mean change this code below:
    From this?

    Java Code:
    import java.util.*;
    class cases{
    	public void cases_amount () { 
    	 System.out.println ( "Please put the number of cases, 5 Max, 12 items per case");
    	 System.out.print ( "Selection: ");
    	 }
    	 public cases() {
    		Scanner left = new Scanner ( System.in );
    		
    		cases_amount();//menu #3
    		 switch ( left.nextInt() ) {
    		  case 1:
    		  int choice1 = (12);
    		  System.out.println ( "You picked 1 case, 12 items" );//if 1 is picked
    		  break;
    		  case 2:
    		  int choice2 = (24);
    		  System.out.println ( "You picked 2 cases, 24 items" );//if 2 is picked
    		  break;
    		  case 3:
    		  int choice3 = (36);
    		  System.out.println ( "You picked 3 cases, 36 items" );//if 3 is picked
    		  break;
    		  case 4:
    		  int choice4 = (48);
    		  System.out.println ( "You picked 4 cases, 48 items" );//if 4 is picked
    		  break;
    		  case 5:
    		  int choice5 = (60);
    		  System.out.println ( "You picked 5 cases, 60 items" );//if 5 is picked
    		  break;
    		  default:
    		  System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
    		  new cases();//gives the user another choice to make
    		  break;
    		  }
    	 }
    	public static void main ( String[] args) {
    	   new cases();
      }	
    }
    To this?

    Java Code:
    import java.util.*;
    class cases{
    	public void cases_amount () { 
    	 System.out.println ( "Please put the number of cases, 5 Max, 12 items per case");
    	 System.out.print ( "Selection: ");
    	 }
    	 public cases() {
    		Scanner left = new Scanner ( System.in );
    		  int choice1 = left.nextInt(1);
    		  int choice2 = left.nextInt(2);
    		  int choice3 = left.nextInt(3);
    		  int choice4 = left.nextInt(4);
    		  int choice5 = left.nextInt(5);
    		  choice1 = 12;
    		  choice2 = 24;
    		  choice3 = 36;
    		  choice4 = 48;
    		  choice5 = 60;
    		
    		cases_amount();//menu #3
    		 switch ( left.nextInt() ) {
    		  case 1:
    		  System.out.println ( "You picked 1 case, 12 items" );//if 1 is picked
    		  break;
    		  case 2:
    		  System.out.println ( "You picked 2 cases, 24 items" );//if 2 is picked
    		  break;
    		  case 3:
    		  System.out.println ( "You picked 3 cases, 36 items" );//if 3 is picked
    		  break;
    		  case 4:
    		  System.out.println ( "You picked 4 cases, 48 items" );//if 4 is picked
    		  break;
    		  case 5: 
    		  System.out.println ( "You picked 5 cases, 60 items" );//if 5 is picked
    		  break;
    		  default:
    		  System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
    		  new cases();//gives the user another choice to make
    		  break;
    		  }
    	 }
    	public static void main ( String[] args) {
    	   new cases();
      }	
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Many Switches with Many Instances

    Does the code work now?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Combo is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Many Switches with Many Instances

    The code does compile and works, now I am trying to use those variables on a different instance.
    I even added public int to my code but still having trouble using it on different instances.
    I am thinking of using a IF and Else IF but having trouble making it work.
    If you have a better suggestion let me know.
    How would I use the variable if the person picks 1 in cases as their choice and picks 1 as their choice in the menu.
    Here is cases code,
    Java Code:
    import java.util.*;
    class cases{
    	   public int choice1;
    	   public int choice2;
    	   public int choice3;
    	   public int choice4;
    	   public int choice5;
    	public void cases_amount () { 
    	 System.out.println ( "Please put the number of cases, 5 Max, 12 items per case");
    	 System.out.print ( "Selection: ");
    	 }
    	 public cases() {
    		Scanner left = new Scanner ( System.in );
    		  int choice1 = left.nextInt(1);
    		  int choice2 = left.nextInt(2);
    		  int choice3 = left.nextInt(3);
    		  int choice4 = left.nextInt(4);
    		  int choice5 = left.nextInt(5);
    		  choice1 = 12;
    		  choice2 = 24;
    		  choice3 = 36;
    		  choice4 = 48;
    		  choice5 = 60;
    		
    		cases_amount();//menu #3
    		 switch ( left.nextInt() ) {
    		  case 1:
    		  System.out.println ( "You picked 1 case, 12 items" );//if 1 is picked
    		  break;
    		  case 2:
    		  System.out.println ( "You picked 2 cases, 24 items" );//if 2 is picked
    		  break;
    		  case 3:
    		  System.out.println ( "You picked 3 cases, 36 items" );//if 3 is picked
    		  break;
    		  case 4:
    		  System.out.println ( "You picked 4 cases, 48 items" );//if 4 is picked
    		  break;
    		  case 5: 
    		  System.out.println ( "You picked 5 cases, 60 items" );//if 5 is picked
    		  break;
    		  default:
    		  System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
    		  new cases();//gives the user another choice to make
    		  break;
    		  }
    	 }
    	public static void main ( String[] args) {
    	   new cases();
      }	
    }
    Here is menu code.
    Java Code:
    import java.util.*; //scanners and more
    class menu{ 
    	 public int Computer;
    	 public int Car;
    	 public int TV;
    	 public int Phone;
    	 public int IPAD;
    	public void display_menu() { 
    	 System.out.println ("Please select your product"); //Gives user direction
    	 System.out.println ( "1) Computer \n2) Car \n3) TV \n4) Phone \n5) IPAD" ); //menu options
         System.out.print ( "Selection: " );//Display for selection
    	}
    	public menu() {
    	   Scanner in = new Scanner ( System.in );//scanner#1
    	   int Computer = in.nextInt(1);
    	   int Car = in.nextInt(2);
    	   int TV = in.nextInt(3);
    	   int Phone = in.nextInt(4);
    	   int IPAD = in.nextInt(5);
    	   Computer = 1000;
    	   Car = 13000;
    	   TV = 500;
    	   Phone = 298;
    	   IPAD = 699;
    	  
       	  display_menu();//menu #1
    	   switch ( in.nextInt() ) { 
    	    case 1:
    		System.out.println ( "You picked Computer, Price: 1000 per item." );//if 1 is picked
    		break;
    		case 2:
    		System.out.println ( "You picked Car, Price: 13000 per item." );//if 2 is picked
    		break;
    		case 3:
    		System.out.println ( "You picked TV, Price: 500 per item." );//if 3 is picked
    		break;
    		case 4:
    		System.out.println ( "You picked Phone, Price: 298 per item." );//if 4 is picked
    		break;
    		case 5:
    		System.out.println ( "You picked IPAD, Price: 699 per item." );//if 5 is picked
    		break;
    		default:
    		System.err.println ( "Not a valid choice, Please choose a different choice" );//if number picked is not in group
    		new menu();//gives the user another choice to make
    		break;
    		}
    	}  
    	public static void main ( String[] args) {
    	   new menu();
    	   new statemenu();
    	   new cases();
    	  
    	     
    	}
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Many Switches with Many Instances

    The code has a problem defining variables with the same names at the class level and inside of a method. The variables in the method "shadow" the ones at the class level. It is NOT good to have local variables use the same names as variables defined at the class level.

    Also The code gets values for the variables from the user and then immediately assigns them new values in lines 15 to 24 which replace and loses what the user entered.

    When getting input from the user, it is better to ask ONE question and get ONE response from the user instead of like the code does (lines 15-19) where it gets 6 values from the user without any prompts to the user.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Combo is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Many Switches with Many Instances

    Ok, is there a way to save what the user entered in the code then use that saved input to use it somewhere else?
    Last edited by Combo; 03-24-2014 at 03:23 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Many Switches with Many Instances

    Yes. The posted code has most of the statements to do that. but they are in the wrong places and there are several extra statements that are causing problems.

    Sample flow:
    0) define variable to hold user's response
    1)ask user a question
    2)read user's response and save it in variable
    ...
    later in the code the value that was saved in 2) will be available
    Last edited by Norm; 03-24-2014 at 03:25 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Many Switches with Many Instances

    Very confusing; I get the idea that the code is supposed to ask the user what the numbers are for computer, tv, etc. , but that is at this point only half implemented.

    Java Code:
    public menu() {
           Scanner in = new Scanner ( System.in );
           int Computer = in.nextInt(1);                      // ???
           int Car = in.nextInt(2);
           int TV = in.nextInt(3);
           int Phone = in.nextInt(4);
           int IPAD = in.nextInt(5);
    Why are those five reads from the keyboard there?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Combo is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Many Switches with Many Instances

    Is there anyway you can point me in the right direction and where I should put them. I know you already said to
    not have local variables use the same names as variables defined at the class level.
    and I understand that part but I don't know where to put the variables.

  11. #11
    Combo is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Many Switches with Many Instances

    Why are those five reads from the keyboard there?
    Because I have no idea what I am doing with Java code. I was told to put the variables outside of the switch operator so I though it would be put there. Right now I understand that is the wrong place. If you can please tell me where I would put this at it would be great.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Many Switches with Many Instances

    Defining a variable
    and assigning a value to a variable
    can be done at different places in the code.

    If several methods need to access a variable, then that variable should be defined at the class level, like in lines 3 to 7.
    Values can be assigned to those variables or read from those variables in any method in the class.

    Before reading a user's input, the user needs to be asked a question so he knows what data to enter.

    One problem you are having is trying to do 6 things at once.
    It is better to do one thing at a time.

    Suggestion: Simplify the code. Work on getting choice1 ONLY, remove the other 5 choice variables. Write code to get the value for choice1 and use it. When that works, then move to working with choice2. etc
    Last edited by Norm; 03-24-2014 at 04:13 PM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Combo is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: Many Switches with Many Instances

    Ok, thanks I will work on this

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Many Switches with Many Instances

    Quote Originally Posted by Combo View Post
    Because I have no idea what I am doing with Java code. I was told to put the variables outside of the switch operator so I though it would be put there. Right now I understand that is the wrong place. If you can please tell me where I would put this at it would be great.
    ... No, I really cannot. Sorry that I asked a question assuming you understood your own code, pretend I didn't say anything.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Switches question.
    By Pacoheadley in forum New To Java
    Replies: 11
    Last Post: 10-11-2012, 03:50 AM
  2. Compiling with -d -cp switches
    By codeAJ in forum New To Java
    Replies: 8
    Last Post: 06-05-2011, 03:25 AM
  3. Using switches...
    By besweeet in forum New To Java
    Replies: 11
    Last Post: 03-04-2010, 04:48 PM
  4. help with switches
    By spots of fire in forum New To Java
    Replies: 1
    Last Post: 01-23-2010, 09:02 PM
  5. Help with switches
    By Daniel in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •