I saw a lot of similar questions in the archives, but I can not find such a scenario as the problem I am facing.
Below is my code. I ran into errors with "finalPrice" and "grandTotalPrice" might not have been initialized. The lines of code are at the end of the program.
Variables must be assigned totals through console input at the top. I am not sure what the error is or why. Can anyone help me and explain?
the code:
import java.util.*;
public class PictureFrames
{
static Scanner console = new Scanner(System.in);
static final double REGULAR_FRAME = .15, FANCY_FRAME = .25;
static final double COLOR = .10, CARDBOARD = .02, GLASS = .07, CROWNS = .35;
public static void main (String[] args)
{
double length, width, area, perimeter;
double priceOfFrame, priceOfColor, priceOfCardboard, priceOfGlass, priceOfCrowns, finalPrice, crownFinalPrice, grandTotalPrice;
int numberOfCrowns;
char typeOfFrame, choiceOfColor, choiceOfCrowns;
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);
switch (typeOfFrame)
{
case 'R':
case 'r':
if (choiceOfColor == 'N')
{
area = (length * width);
perimeter = (2 * length) + (2 * width);
priceOfFrame = (perimeter * REGULAR_FRAME);
priceOfCardboard = (area * CARDBOARD);
priceOfGlass = (area * GLASS);
finalPrice = (priceOfFrame + priceOfCardboard + priceOfGlass);
break;
}
else if (choiceOfColor == 'Y')
{
area = (length * width);
perimeter = (2 * length) + (2 * width);
priceOfColor = (area * COLOR);
priceOfFrame = (perimeter * REGULAR_FRAME);
priceOfCardboard = (area * CARDBOARD);
priceOfGlass = (area * GLASS);
finalPrice = (priceOfFrame + priceOfColor + priceOfCardboard + priceOfGlass);
break;
}
case 'F':
case 'f':
if (choiceOfColor == 'N')
{
area = (length * width);
perimeter = (2 * length) + (2 * width);
priceOfFrame = (perimeter * FANCY_FRAME);
priceOfCardboard = (area * CARDBOARD);
priceOfGlass = (area * GLASS);
finalPrice = (priceOfFrame + priceOfCardboard + priceOfGlass);
break;
}
else if (choiceOfColor == 'Y')
{
area = (length * width);
perimeter = (2 * length) + (2 * width);
priceOfColor = (area * COLOR);
priceOfFrame = (perimeter * FANCY_FRAME);
priceOfCardboard = (area * CARDBOARD);
priceOfGlass = (area * GLASS);
finalPrice = (priceOfFrame + priceOfColor + priceOfCardboard + priceOfGlass);
break;
}
}
System.out.println ("Would you like to add crowns? Enter Y (Yes), or N (No): ");
choiceOfCrowns = console.next().charAt(0);
if (choiceOfCrowns == 'Y')
{
System.out.println ("How many crowns would you like? ");
numberOfCrowns = console.nextInt();
crownFinalPrice =(numberOfCrowns * CROWNS);
grandTotalPrice = (crownFinalPrice + finalPrice);
}
else if (choiceOfCrowns == 'N')
System.out.printf ("Your total comes to: $%.2f%n", grandTotalPrice);
}
}
source
share