CHAPTER 7 EXERCISES 205 24 Write a program that supports the following dialog wi
ID: 3742962 • Letter: C
Question
CHAPTER 7 EXERCISES 205 24 Write a program that supports the following dialog with the user: Enter quantity: 75 You have ordered 75 ripples$19.50 Next customer (y/n): y Enter quantity: 97 Ripples can be ordered only in packs of 25. Next customer (y/n): t Next customer (y/n): n Thank you for using Ripple Systems. If, in response to the "Next customer" prompt, the user presses or enters anything other than a 'y' or an 'n', the program should repeat the prompt. Define the unit price of a ripple as a constant equal to 26 cents. Hints: Use the following statement to display the quantity ordered and the total dollar amount of the order: System.out.print f("You have ordered quantity, price quantity) %d Ripples $%.2f ". _ Use the following statements to read the quantity ordered and the user response to the "Next customer?" prompt: Scanner keyboard new Scanner (System.in) char answer int quantity keyboard.nextInt O: keyboard.nextLine): skip the rest of the line String str- keyboard.nextLine ).trimO; if (str.length O1) answer -str.charAt (0) else anawer-Explanation / Answer
import java.util.Scanner;
public class Chegg {
static int qunatity;
static Scanner keyboard;
static char answer;
static double price = 0.26; // fixed Unit price of ripple is 26 cent so
// convert in dollar 0.26
public static void main(String[] args) {
keyboard = new Scanner(System.in);
while (true) { // To continuous run program for input
System.out.println("Enter quantity");
qunatity = keyboard.nextInt(); // Assign quantity value
keyboard.nextLine(); // skip the rest of the lines
calculate();
System.out.println("Next customer (y/n):");
String str = keyboard.nextLine().trim();
if (str.length() == 1) {
answer = str.charAt(0);
if (str.equals("y") || str.equals("Y")) { // || is Logical OR condition to check for y or Uppercase letter also //Y
calculate(); // to call calculate function
} else if (str.equals("n") || str.equals("N")) { // || is or
// condition to check for n or Uppercase letter also N
System.out.println("Thank you for using Ripple system");
break; // to exit this loop
}
}
else // condition for other than single letter y/n
answer = ' ';
}
}
private static void calculate() {
// this function for Check condition multiple of 25
if (qunatity % 25 == 0) { // to check is in multiple of 25 this case is
// for true
System.out.printf("You have ordered %d Ripple -- $%.2f ", qunatity, price * qunatity);
} else { // else is for if quantity is not in multiple of 25
System.out.println("Ripple can be order only in pack of 25");
}
}
}