|
| 1 | +//Exercise 5-G. |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class CrazyEdWholesaleCheese |
| 6 | +{ |
| 7 | + public static void main(String[] args) |
| 8 | + { |
| 9 | + Scanner in = new Scanner(System.in); |
| 10 | + |
| 11 | + System.out.print("What size cheese would you like to order? "); |
| 12 | + int cheeseSize = in.nextInt(); |
| 13 | + switch(cheeseSize) |
| 14 | + { |
| 15 | + case 1 : case 2 : case 3 : |
| 16 | + System.out.println("You chose: " + cheeseSize + " inch diameter string cheese."); |
| 17 | + break; |
| 18 | + |
| 19 | + default : |
| 20 | + System.out.println("Please choose 1, 2, or 3 inch diameter string cheese."); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + System.out.println("What length of cheese would you like to order? "); |
| 25 | + int cheeseLength = in.nextInt(); |
| 26 | + |
| 27 | + determineCostOfOrder(cheeseSize, cheeseLength); |
| 28 | + } |
| 29 | + |
| 30 | + public static void determineCostOfOrder(int cheeseSize, int cheeseLength) |
| 31 | + { |
| 32 | + final int COST_OF_1_INCH_CHEESE = 2; |
| 33 | + final int COST_OF_2_INCH_CHEESE = 4; |
| 34 | + final int COST_OF_3_INCH_CHEESE = 6; |
| 35 | + |
| 36 | + final int SHIPPING_COST_OF_1_INCH_CHEESE = 2; |
| 37 | + final int SHIPPING_COST_OF_2_INCH_CHEESE = 2; |
| 38 | + final int SHIPPING_COST_OF_3_INCH_CHEESE = 4; |
| 39 | + |
| 40 | + final int FREE_SHIPPING_BREAKPOINT_FOR_1_INCH_CHEESE = 50; |
| 41 | + final int FREE_SHIPPING_BREAKPOINT_FOR_2_INCH_CHEESE = 75; |
| 42 | + final int FREE_SHIPPING_BREAKPOINT_FOR_3_INCH_CHEESE = 25; |
| 43 | + |
| 44 | + final int HANDLING_CHARGE = 5; |
| 45 | + |
| 46 | + int cheeseCost = 0; |
| 47 | + int shippingCost = 0; |
| 48 | + |
| 49 | + boolean isValidOrder = true; |
| 50 | + |
| 51 | + if ((cheeseSize == 1) && (cheeseLength > FREE_SHIPPING_BREAKPOINT_FOR_1_INCH_CHEESE)) |
| 52 | + { |
| 53 | + cheeseCost = (cheeseLength * COST_OF_1_INCH_CHEESE); |
| 54 | + shippingCost = 0; |
| 55 | + } |
| 56 | + else if (cheeseSize == 1) |
| 57 | + { |
| 58 | + cheeseCost = (cheeseLength * COST_OF_1_INCH_CHEESE); |
| 59 | + shippingCost = (cheeseLength * SHIPPING_COST_OF_1_INCH_CHEESE); |
| 60 | + } |
| 61 | + else if ((cheeseSize == 2) && (cheeseLength > FREE_SHIPPING_BREAKPOINT_FOR_2_INCH_CHEESE)) |
| 62 | + { |
| 63 | + cheeseCost = (cheeseLength * COST_OF_2_INCH_CHEESE); |
| 64 | + shippingCost = 0; |
| 65 | + } |
| 66 | + else if (cheeseSize == 2) |
| 67 | + { |
| 68 | + cheeseCost = (cheeseLength * COST_OF_2_INCH_CHEESE); |
| 69 | + shippingCost = (cheeseLength * SHIPPING_COST_OF_2_INCH_CHEESE); |
| 70 | + } |
| 71 | + else if ((cheeseSize == 3) && (cheeseLength > FREE_SHIPPING_BREAKPOINT_FOR_3_INCH_CHEESE)) |
| 72 | + { |
| 73 | + cheeseCost = (cheeseLength * COST_OF_3_INCH_CHEESE); |
| 74 | + shippingCost = 0; |
| 75 | + } |
| 76 | + else if (cheeseSize == 3) |
| 77 | + { |
| 78 | + cheeseCost = (cheeseLength * COST_OF_3_INCH_CHEESE); |
| 79 | + shippingCost = (cheeseLength * SHIPPING_COST_OF_3_INCH_CHEESE); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + isValidOrder = false; |
| 84 | + } |
| 85 | + |
| 86 | + int totalCost = cheeseCost + shippingCost + HANDLING_CHARGE; |
| 87 | + |
| 88 | + if (isValidOrder) |
| 89 | + { |
| 90 | + System.out.println("The total cost of this cheese is: $" + cheeseCost); |
| 91 | + System.out.println("The total shipping cost is: $" + shippingCost); |
| 92 | + System.out.println("The Handling Charge for this order is: $" + HANDLING_CHARGE); |
| 93 | + System.out.println("The total cost of this order is: $" + totalCost); |
| 94 | + System.out.println(); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + System.out.println("This order is too crazy!"); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
0 commit comments