Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Code Simplification

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Code Simplification

    Ladies and Gentlemen,

    I am writing a program for my Java 1 class. My code compiles and the end result is exactly what the professor wants. However, I don't think I did it exactly how it was wanted.

    The instructions say "You will modify this problem to run for three salesmen. You will have the user enter each salesman’s name, the item number and the quantity for each item number. All input/output is through dialog boxes. You will use a for loop and a while loop."

    I used a while loop with a switch statement (forgive me if that's the wrong way to say that), but I don't see how to incorporate a for loop in there.

    Any help would be appreciated.

    import javax.swing.JOptionPane;
    import java.util.Scanner;
     
    public class CalculatingSales
    {
    	public static void main(String args[])
    	{
    		Scanner input = new Scanner(System.in);
     
    		int quantity;
    		int itemNumber;
    		String e1;
    		String e2;
    		String e3;
    		double sales1 = 0.0;
    		double sales2 = 0.0;
    		double sales3 = 0.0;
    		double product1 = 2.98;
    		double product2 = 4.50;
    		double product3 = 9.98;
    		double product4 = 4.49;
    		double product5 = 6.87;
     
    		e1 =
    			JOptionPane.showInputDialog("Enter salesman name");
     
    		itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));
     
    		while (itemNumber != 0)
    		{
    			switch(itemNumber)
    			{
    				case 1: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales1 = sales1 + (quantity * product1);
    					break;	
     
    				case 2: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales1 = sales1 + (quantity * product2);
    					break;	
     
    				case 3: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales1 = sales1 + (quantity * product3);
    					break;	
     
    				case 4: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales1 = sales1 + (quantity * product4);
    					break;	
     
    				case 5: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales1 = sales1 + (quantity * product5);
    					break;
     
    				default: JOptionPane.showMessageDialog(null, "Invalid item number", "Error",
    							JOptionPane.ERROR_MESSAGE);
    			}
     
    			itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));						
    		}
     
    		e2 =
    			JOptionPane.showInputDialog("Enter salesman name");
     
    		itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));
     
    		while (itemNumber != 0)
    		{
    			switch(itemNumber)
    			{
    				case 1: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales2 = sales2 + (quantity * product1);
    					break;	
     
    				case 2: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales2 = sales2 + (quantity * product2);
    					break;	
     
    				case 3: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales2 = sales2 + (quantity * product3);
    					break;	
     
    				case 4: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales2 = sales2 + (quantity * product4);
    					break;	
     
    				case 5: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales2 = sales2 + (quantity * product5);
    					break;
     
    				default: JOptionPane.showMessageDialog(null, "Invalid item number", "Error",
    							JOptionPane.ERROR_MESSAGE);
    			}
     
    			itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));						
    		}
     
    		e3 =
    			JOptionPane.showInputDialog("Enter salesman name");
     
    		itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));
     
    		while (itemNumber != 0)
    		{
    			switch(itemNumber)
    			{
    				case 1: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales3 = sales3 + (quantity * product1);
    					break;	
     
    				case 2: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales3 = sales3 + (quantity * product2);
    					break;	
     
    				case 3: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales3 = sales3 + (quantity * product3);
    					break;	
     
    				case 4: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales3 = sales3 + (quantity * product4);
    					break;	
     
    				case 5: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    							sales3 = sales3 + (quantity * product5);
    					break;
     
    				default: JOptionPane.showMessageDialog(null, "Invalid item number", "Error",
    							JOptionPane.ERROR_MESSAGE);
    			}
     
    			itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));						
    		}
     
    		String msg = 
    					String.format("%s %s $%,.2f\n%s %s $%,.2f\n%s %s $%,.2f\n", 
    					e1, "sales are", sales1, e2, "sales are", sales2,
    					e3, "sales are", sales3);
     
    		JOptionPane.showMessageDialog(null, msg);	
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Code Simplification

    Have you learned arrays yet? Maybe it doesn't matter.

    Anytime you're repeating the exact same code, you should wonder, "How can I put this in a loop?" or "How can I write a method to do this?" In this case, you could do something like:
    for ( int i = 0 ; i < 3 ; i++ )
    {
    	e1 =
    		JOptionPane.showInputDialog("Enter salesman name");
     
    	itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));
     
    	while (itemNumber != 0)
    	{
    		switch(itemNumber)
    		{
    But the salesman's info, name and sales, would have to either be stored or output each time through the for loop. This would be best accomplished using a class to hold each salesman's info, but I'm not sure what you're able to do.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    dirk.digler (September 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Code Simplification

    We haven't learned arrays yet, and we are not allowed to build a class. I just can't figure out how the loop would switch to the different employee variables to hold the new information. Is this even possible?

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Code Simplification

    You could use an if statement to record the correct data based on the value of the for loop's control variable, but that's almost (not quite) as much trouble as repeating the same code 3 times.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    dirk.digler (September 26th, 2013)

  7. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Code Simplification

    I ended up figuring it out. The part that I couldn't wrap my head around was how the final display message was going to remember the names and such. Once I understood how that would work it all just fell into place.

    Thanks for your assistance.

    import javax.swing.JOptionPane;
    import java.util.Scanner;
     
    public class CalculatingSales
    {
    	public static void main(String args[])
    	{
    		Scanner input = new Scanner(System.in);
     
    		int quantity;
    		int itemNumber;
    		double sales = 0;
    		double product1 = 2.98;
    		double product2 = 4.50;
    		double product3 = 9.98;
    		double product4 = 4.49;
    		double product5 = 6.87;
    		String msg = "";
    		String salesman;
     
    		for (int i = 1; i <= 3; i++)
    		{
    			salesman = JOptionPane.showInputDialog("Enter salesman name");
     
    			itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));
     
    			while (itemNumber != 0)
    			{
    				switch(itemNumber)
    				{
    					case 1: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    								sales = sales + (quantity * product1);
    						break;	
     
    					case 2: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    								sales = sales + (quantity * product2);
    						break;	
     
    					case 3: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    								sales = sales + (quantity * product3);
    						break;	
     
    					case 4: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    								sales = sales + (quantity * product4);
    						break;	
     
    					case 5: quantity = Integer.parseInt(JOptionPane.showInputDialog("Enter quantity"));
    								sales = sales + (quantity * product5);
    						break;
     
    					default: JOptionPane.showMessageDialog(null, "Invalid item number", "Error",
    								JOptionPane.ERROR_MESSAGE);
    				}
     
    				itemNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter item number"));
     
    			}
     
    			msg = msg +
    				String.format("%s %s $%,.2f\n", salesman, "sales are", sales);
     
    		}
     
    		JOptionPane.showMessageDialog(null, msg);	
     
    	}
    }

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Code Simplification

    Good job! In summary, add the results of each loop to a String object and output the completed String object after exiting the loop.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM