Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What happens if typeOfFrame is not R, r, F, or f? A possible solution is to add a "default" case to the switch.</p> <p>I think this code is closer to what you want (I reduced a lot of duplication). Note that I did not really deal with errors (the calls to isValidChoice and isFrameType methods are a start with how to deal with them). The issue above was that you are basically assuming that the user will only ever enter in valid input and forgetting that there are times that, even though, Y and N are the only valid choices they could enter in Q. I also fixed a bug where you were not always printing out the grand total, and were not setting the grand total (the last part above the printf).</p> <p>The last suggestion I have is do NOT use doubles for this (unless you were told to by your instructor) because they will give you inaccurate numbers in some cases. Take a look at java.math.BigDecimal instead.</p> <pre><code>class Main { static Scanner console = new Scanner(System.in); static final double REGULAR_FRAME = .15; static final double FANCY_FRAME = .25; static final double COLOR = .10; static final double CARDBOARD = .02; static final double GLASS = .07; static final double CROWNS = .35; public static void main (String[] args) { final double length; final double width; final char typeOfFrame; final char choiceOfColor; System.out.println ("Please enter the length of your picure in inches:"); length = console.nextDouble(); System.out.println ("Please enter the width of your picure in inches: "); width = console.nextDouble(); System.out.println ("Please enter the type of frame: R or r (Regular), F or f (Fancy). "); typeOfFrame = console.next().charAt(0); System.out.println ("Would you like to add color?: Y for (Yes), N for (No): "); choiceOfColor = console.next().charAt(0); if(!(isFrameType(typeOfFrame))) { } else { final double area; final double perimeter; final double priceOfFrame; final double priceOfCardboard; final double priceOfGlass; area = (length * width); perimeter = (2 * length) + (2 * width); priceOfFrame = (perimeter * REGULAR_FRAME); priceOfCardboard = (area * CARDBOARD); priceOfGlass = (area * GLASS); if(isValidChoice(choiceOfColor)) { final double priceOfColor; final double finalPrice; final char choiceOfCrowns; final double grandTotalPrice; if(choiceOfColor == 'N') { finalPrice = (priceOfFrame + priceOfCardboard + priceOfGlass); } else { priceOfColor = (area * COLOR); finalPrice = (priceOfFrame + priceOfColor + priceOfCardboard + priceOfGlass); } System.out.println ("Would you like to add crowns? Enter Y (Yes), or N (No): "); choiceOfCrowns = console.next().charAt(0); if(isValidChoice(choiceOfCrowns)) { if(choiceOfCrowns == 'Y') { final double crownFinalPrice; final int numberOfCrowns; System.out.println ("How many crowns would you like? "); numberOfCrowns = console.nextInt(); crownFinalPrice =(numberOfCrowns * CROWNS); grandTotalPrice = (crownFinalPrice + finalPrice); } else { grandTotalPrice = finalPrice; } System.out.printf ("Your total comes to: $%.2f%n", grandTotalPrice); } } } } private static boolean isFrameType(final char c) { final char lower; lower = Character.toLowerCase(c); return (lower == 'r' || lower == 'f'); } private static boolean isValidChoice(final char c) { return (c == 'Y' || c == 'N'); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload