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); } }