/* Grocery Store Program SAMPLE RUNS ----------------- enter item (type, ounces, $/pound): Candy 16 $5.00 1 pound(s) of Candy is $5.00 gross -0.00 discount --------------- $5.00 total enter item (type, ounces, $/pound): Fruit 32 $1.50 2 pound(s) of Fruit is $3.00 gross -0.30 discount --------------- $2.70 total enter item (type, ounces, $/pound): Candy 24 $6.00 1.5 pound(s) of Candy is $9.00 gross -0.50 discount --------------- $8.50 total # of tests needed == 8 as follows 1 invalid candy price 3 valid candy: no discount, .50 and 3.00 discounts 4 fruit: no discount, 10%, 15%, 20% */ import java.util.Scanner; public class grocery { public static void main(String[] args) { Scanner keys = new Scanner(System.in); String item, priceString; int ounces; // weight in ounces double price; // dollars/pound double pounds, gross; final int OZPERLB = 16; // ounces per pound double discount = 0.0; System.out.println("enter item (type, ounces, $/pound):"); item = keys.next(); ounces = keys.nextInt(); priceString = keys.next().substring(1); // get rid of $ price = Double.parseDouble(priceString); if (item.equalsIgnoreCase("candy") && price < 5.00) { System.out.println("invalid candy price, must be >= $5.00"); // System.exit(1); // force program to quit } else { // valid - process pounds = (double) ounces / OZPERLB; gross = price * pounds; System.out.println(pounds + " pound(s) of " + item + " is "); System.out.printf("$%.2f gross\n", gross); if (item.equalsIgnoreCase("candy")) { if (pounds > 1 && pounds < 2.5) discount = .5; else if (pounds >= 2.5) discount = 3.0; } else if (item.equalsIgnoreCase("fruit")) { if (pounds > 5) discount = 0.2; else if (pounds >= 3) discount = .15; else if (pounds > 1) discount = 0.1; discount = discount * gross; } System.out.printf("$%.2f discount\n", discount); System.out.println("-----------------"); System.out.printf("$%.2f total\n", gross - discount); } // process valid } // end of main } // end of class