Question: Using a Boolean Sentinel to Control a While Loop Suppose you work at a music store for minimum wage plus a 12.5% commission on sales.
Using a Boolean Sentinel to Control a While Loop
Suppose you work at a music store for minimum wage plus a 12.5% commission on sales. Write a program that allows you to enter your daily sales. The program should print the total and number of sales for the day plus your commission. You should be able to enter any number of sales for the day. When you are finished for the day, type in 0 (zero) to stop entering sales and print your commission report. Here is a sample run:

This program uses a Boolean variable to control the loop and shows you how to use counters and accumulators.
Heres some pseudocode to help you get started:
import a scanner
import a currency formatter
In the main method
Setup scanner and currency number formatter
Declare a constant for the commission percent
Declare variables for the current sale, the total sales (accumulator) and the number of sales (counter)
Declare a Boolean loop control variable done and initialize it to false
While (not done)
{
Prompt for current sale
If (current sale = 0)
{
done = true
}
Else
{
Accumulate total sales
Count sales
}
} // end of while loop
Print the total sales and number of sales
Print the commission
Youll find my solution on the next page. Yours may differ. Try to write the program first the review my solution.
package whileloopbooleansentinel;
import java.text.NumberFormat;
import java.util.Scanner;
/**
* @author Larry Waldrop
*/
public class WhileLoopBooleanSentinel
{
public static void main(String[] args)
{
// TODO code application logic here
Scanner input = new Scanner(System.in);
NumberFormat currency = NumberFormat.getCurrencyInstance();
final double COMMISSION_PERCENT = 0.125;
// be sure to initialize boolean variable
boolean done = false;
double currentSale, commission;
// initialize counters and accumulators
double totalSales = 0.0; // accumulator
int numSales = 0; // counter
while (!done) // while not done
{
System.out.print("Enter sales: ");
currentSale = input.nextDouble();
if (currentSale == 0.0)
{
done = true; // we are done
}
else // we are not done
{
// accumulate and count sales
totalSales = totalSales + currentSale;
numSales = numSales + 1;
}
}
// compute commission
commission = totalSales * COMMISSION_PERCENT;
// output results
System.out.println();
System.out.printf("Today you made %d sales totaling %s%n", numSales,
currency.format(totalSales));
System.out.printf("You commission on today's sales is %s%n",
currency.format(commission));
}
}
run: Enter sales: 212.45 Enter sales: 36.20 Enter sales: 9.73 Enter sales: 12.46 Enter sales: 38.20 Enter sales: 57.41 Enter sales: 125.86 Enter sales: 330. 67 Enter sales: 0 Today you made 8 sales totaling $822.98 You commission on today's sales is $102.87 BUILD SUCCESSFUL (total time: 42 seconds)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
