1

I find loop statements really hard so I need help. This program lets the user input each salesperson's gross sale, calculates their commission which is $200 plus 9% of their sale, and displays the salesperson's earning. Also, to sum up all of the total sales inputted, the user should input -1, hence ending the program. This program uses a while loop and the output should be like this:

 ENTER SALES IN DOLLAR (-1 TO END): 5000.00
 SALARY:  $ 650.00
 
 ENTER SALES IN DOLLAR (-1 TO END): 6000.00     
 SALARY:  $ 740.00
 
 ENTER SALES IN DOLLAR (-1 TO END): 7000.00     
 SALARY:  $ 830.00
 
 ENTER SALES IN DOLLAR (-1 TO END): -1
 
 THE TOTAL SALARY FOR THE WEEK:  $2200.00   
 GOOD DAY!  THANK YOU FOR USING THE SYSTEM!

and so far here's the code I have:

public static void main(String[] args) 
    {

        
        Scanner scan = new Scanner (System.in);
        
        double sales = 0, salary = 0, totalSalary;
            
        while (sales >= 1) 
        {
            System.out.print("ENTER SALES IN DOLLAR (-1 TO END):  ");
            sales = scan.nextDouble();
            
            salary = (sales + 200)* 0.09;
            System.out.println("SALARY: $ " +salary);
        }
            
    }

} 

I'm really stuck on where should I put the "while" and how would I stop the loop with "-1"

Liyanna
  • 119
  • 10
  • 3
    Do not use floating point types for monetary amounts! – Fildor Jan 21 '21 at 07:45
  • @Fildor may i ask why – Liyanna Jan 21 '21 at 07:46
  • you condition would be better with `sales > 0` – Aalexander Jan 21 '21 at 07:47
  • 2
    That's why: https://stackoverflow.com/a/3730040/982149 – Fildor Jan 21 '21 at 07:48
  • 1
    @Fildor noted. thanks for this – Liyanna Jan 21 '21 at 07:49
  • You're welcome. It's a common one. There are even some assignment questions using floating point for money and those have been assigned by uni professors ... *smh – Fildor Jan 21 '21 at 07:51
  • If you initialize `sales` with 0 and then check it to be > 1 in the loop condition, the loop will never start. Just change the condition `while (sales >= 0)` and don't forget to add up the totalSalary in the loop – Nowhere Man Jan 21 '21 at 07:52
  • @AlexRudenko That would allow for `sales = -0.5`, though. – Fildor Jan 21 '21 at 07:53
  • In fact, you actually could do with an unconditional loop. Since you need to fetch the input inside the loop, you have to validate input there, too. So you can simply `break;` if input == -1. – Fildor Jan 21 '21 at 07:55
  • Also, watch, what you actually want to loop. You need to sum up sales (loop and sum until -1, **exclude -1 from sum**), then calculate `f(totalSales) = 200 + ( totalSales * 0.9 )` – Fildor Jan 21 '21 at 08:00
  • And just a pro tip for clean code: avoid "magic numbers" like "200" and "0.9". Make them class fields or constants that have useful names. – Fildor Jan 21 '21 at 08:03
  • while (true) + if condition – silentsudo Jan 21 '21 at 08:04

4 Answers4

1

Your code will never enter into the loop because of sales=0. To go inside the loop use do while loop or initialise with some other value which is >= 1.

Go and read some more about loops :

  1. For loop
  2. while loop
  3. do-while loop

https://www.geeksforgeeks.org/loops-in-java/

Can use the following code reference

 public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        double sales = 0, salary = 0, totalSalary;
        System.out.print("ENTER SALES IN DOLLAR (-1 TO END):  ");
        sales = scan.nextDouble();
        while (sales >= 1) {
            salary = (sales + 200) * 0.09;
            System.out.println("SALARY: $ " + salary);

            System.out.print("ENTER SALES IN DOLLAR (-1 TO END):  ");
            sales = scan.nextDouble();
        }
    }
backdoor
  • 891
  • 1
  • 6
  • 18
  • 1
    1. salary is calculated _totally_ wrong 2. No need to use `do-while` loop and then use `if` inside it with the same condition – Nowhere Man Jan 21 '21 at 07:54
1

As I see you must check if input=-1 then end calculation and put result. Also user may input incorrect non numerical value and you must catch it and maybe it needs to add additional checking for negative input.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    double sales , salary, totalSalary = 0;
    while (true) {
        try {
            System.out.print("ENTER SALES IN DOLLAR (-1 TO END):  ");
            sales = Double.parseDouble(scan.nextLine());
            if (sales == -1) break;
            salary = 200 + sales * 0.09;               
            System.out.println("SALARY: $ " + salary);
            totalSalary += salary;
        } catch (NumberFormatException e) {
            System.out.println("NumberFormatException");
        }
    }
    System.out.println("THE TOTAL SALARY FOR THE WEEK:  $" + totalSalary + "\n" +
            " GOOD DAY!  THANK YOU FOR USING THE SYSTEM!");
}
 
Sergey Afinogenov
  • 2,137
  • 4
  • 13
1

You can create an infinity loop until the input set to -1.

Also, Your calc is wrong, you should use (x * 0.09) +200.

Take a look here

public static void main(String[] args) 
{
    double salary = 0, totalSalary = 0;
    try(Scanner scan = new Scanner (System.in))
    {
        while (true)
        {
            System.out.print("ENTER SALES IN DOLLAR (-1 TO END):  ");
            salary = scan.nextDouble();
            
            if (salary == -1)
                break;
            
            salary = (salary* 0.09) + 200;  
            totalSalary += salary;
            System.out.println("SALARY: $ " +salary);
        }
    }
    catch (Exception e)
    {
    }
    
    System.out.println("THE TOTAL SALARY FOR THE WEEK:  $" + totalSalary);
    System.out.println("GOOD DAY!  THANK YOU FOR USING THE SYSTEM!");
}
Melron
  • 569
  • 4
  • 10
  • Not bad, but you are adding up error. You should sum up sales only, then calculate commission on totalSales. As is, your are adding the base salary in each iteration + rounding / floating point error. – Fildor Jan 21 '21 at 08:05
  • And another thing: Do not trust user input. By accidentally typing -1000 you could mess up your calculation entirely. – Fildor Jan 21 '21 at 08:08
  • @Fildor about your 1st comment, I did it based the given output that it is the correct one. About your 2nd comment, i do not trust them. But i think the main question was about the exit case. – Melron Jan 21 '21 at 08:12
  • Hmm, the requirement is a little unclear, there. The text imho differs from the example. So nevermind 1st comment, please. – Fildor Jan 21 '21 at 08:13
  • @Fildor Yeah, i totally agree with you – Melron Jan 21 '21 at 08:14
0
int sales;
double salary = 0, totalSalary = 0;
boolean keep = true;


while (keep) 
{
    System.out.print("\nENTER SALES IN DOLLAR (-1 TO END):  ");
    sales = scan.nextInt();
    if (sales == -1)
    {
        keep = false; 
    }
    else 
    {
        salary = (sales * 0.09)+ 200;
        System.out.println("SALARY: $ " +salary);
        totalSalary = totalSalary + salary;
    }
        
}
scan.close();
System.out.println("\nTHE TOTAL SALARY FOR THE WEEK:  $ " +totalSalary);
System.out.print("GOOD DAY!  THANK YOU FOR USING THE SYSTEM!");
Liyanna
  • 119
  • 10