I have some code here with a logical error and i can not for the life of me figu
ID: 3637192 • Letter: I
Question
I have some code here with a logical error and i can not for the life of me figure out why it isn't working. I have designed it completely so i'm sorry in advance if it is hard to follow i'm new to the world of programming. The error occurs when the code is run and calculates the total, it will not take into effect if one of the items is on sale. The code is below. The first set of code is the Class containing some static prices and return methods for them as well as a static counter. The second bit of code is the actual program with multiple methods to calculate the prices. I would recommend just copying and pasting the code to an IDE you may have and running it to see the error. Thanks in advance guys. One other problem i just realized is that a couple of my JOptionPane.showMessageDialog are acting like InputDailog box's any help with fixing this would be great too. I don't see any problem with how they are written, but the computer does.
//Create a Public Class named Hats
public class Hats {
private static int hatClassCount = 0;
private static double beanie = 2.50;
private static double cap = 4.00;
private static double beret = 10.00;
private static double fedora = 5.00;
public static double getCostBeanie() {
return beanie;
}
public static double getCostCap(){
return cap;
}
public static double getCostBeret(){
return beret;
}
public static double getCostFedora(){
return fedora;
}
public Hats(){
hatClassCount++;
}
public int gethatClassCount(){
return hatClassCount;
}
}
import javax.swing.JOptionPane;
public class HatDemo {
public static void main(String[] args) {
double totalCost = 0; //get total cost of products
String input; //hold user input
double h1hat; //Print Beanie cost
double nBeanie; //hold number of Beanie's sold
double beanieCost = 0; //hold Beanie final sale
double h2hat; //Print Ball cap cost
double nCap; //hold number of Ball cap's sold
double capCost = 0; //hold Ball cap final sale
double h3hat; //Hold Beret Object
double nBeret; //hold number of Beret hats sold
double beretCost = 0; //hold Beret hat final sale
double h4hat; //Hold Fedora Object
double nFedora; //Hold number of Fedora hats sold
double fedoraCost = 0; //hold Fedora hat final sale
//Creating object of hats class to display costs and count usage
Hats h1 = new Hats();
//Call class counter
int h1value ; //count class instances
h1value = h1.gethatClassCount();
System.out.println(h1value + "counts");
//Create four hat objects
h1hat = Hats.getCostBeanie();
h2hat = Hats.getCostCap();
h3hat = Hats.getCostBeret();
h4hat = Hats.getCostFedora();
//Display Cost of all products
JOptionPane.showMessageDialog(null, "Beanie Cost: $" + h1hat +
" Cap Cost: $" + h2hat +
" Beret Cost: $" + h3hat +
" Fedora Cost: $" + h4hat);
//Ask which hat is on sale
input = JOptionPane.showInputDialog("Which hat is on sale? ");
String saleHat = input;
//Declare salePrice to 0 & set variables to store hat names
double salePrice = 0;
String Beanie= "Beanie";
String Cap = "Cap";
String Beret = "Beret";
String Fedora = "Fedora";
if (saleHat.equals(Beanie)){
salePrice = salePriceBeanie();
JOptionPane.showInputDialog(null,"The " + saleHat + " is on sale");
}
else if (saleHat.equals(Cap)){
salePrice = salePriceCap();
JOptionPane.showInputDialog(null,"The " + saleHat + " is on sale");
}
else if (saleHat.equals(Beret)){
salePrice = salePriceBeret();
JOptionPane.showInputDialog(null,"The " + saleHat + " is on sale");
}
else if (saleHat.equals(Fedora)){
salePrice = salePriceFedora();
JOptionPane.showInputDialog(null,"The " + saleHat + " is on sale");
}
else{
JOptionPane.showMessageDialog(null, "You Entered and invalid hat type.");
}
//Display which hat is on sale
//Display amount of items sold
input = JOptionPane.showInputDialog("Enter number of Beanie's sold: ");
nBeanie = Double.parseDouble(input);
if (nBeanie >= 0)
beanieCost = beanieTotal(nBeanie);
else
JOptionPane.showMessageDialog(null, "You entered an invalid number.");
//Display cost and get amount to buy
input = JOptionPane.showInputDialog("Enter number of Ball cap's sold: ");
nCap = Double.parseDouble(input);
if (nCap >= 0)
capCost = ballCapTotal(nCap);
else
JOptionPane.showMessageDialog(null, "You entered an invalid amount.");
//Display cost and get amount to buy
input = JOptionPane.showInputDialog("Enter number of Beret's sold: ");
nBeret = Double.parseDouble(input);
if (nBeret >= 0)
beretCost = beretTotal(nBeret);
else
JOptionPane.showMessageDialog(null, "You entered an invalid amount.");
input = JOptionPane.showInputDialog("Enter number of Fedora's sold: ");
nFedora = Double.parseDouble(input);
if (nFedora >= 0)
fedoraCost = fedoraTotal(nFedora);
else
JOptionPane.showMessageDialog(null, "You entered an invalid amount.");
//Calculate total of all items sold and display total
if (saleHat.equals(Beanie))
totalCost = salePrice + capCost + beretCost + fedoraCost;
else if (saleHat.equals(Cap))
totalCost = salePrice + beanieCost + beretCost + fedoraCost;
else if (saleHat.equals(Beret))
totalCost = salePrice + beanieCost + capCost + fedoraCost;
else if (saleHat.equals(Fedora))
totalCost = salePrice + beanieCost + capCost + beretCost;
JOptionPane.showMessageDialog(null, "Total Sales: $" + totalCost + " Have a nice day!!");
System.exit(0);
}
private static double salePriceFedora() {
double salePrice;
salePrice = (Hats.getCostFedora()/2);
return salePrice;
}
private static double salePriceBeret() {
double salePrice;
salePrice = (Hats.getCostBeret()/2);
return salePrice;
}
private static double salePriceCap() {
double salePrice;
salePrice = (Hats.getCostCap()/2);
return salePrice;
}
private static double salePriceBeanie() {
double salePrice;
salePrice = (Hats.getCostBeanie()/2);
return salePrice;
}
//method for calculating beanie sale costs
private static double beanieTotal(double nBeanie) {
double beanieCost;
double beanieTotal;
beanieCost = Hats.getCostBeanie();
beanieTotal = beanieCost * nBeanie;
return beanieTotal;
}
//method for calculating ball cap sale costs
private static double ballCapTotal(double nBallCap) {
double ballCapCost;
double ballCapTotal;
ballCapCost = Hats.getCostCap();
ballCapTotal = ballCapCost * nBallCap;
return ballCapTotal;
}
//method for calculating Beret hat sale costs
private static double beretTotal(double nBeret) {
double beretCost;
double beretTotal;
beretCost = Hats.getCostBeret();
beretTotal = beretCost * nBeret;
return beretTotal;
}
//method for calculating Fedora hat sale cost
private static double fedoraTotal(double nFedora) {
double fedoraCost;
double fedoraTotal;
fedoraCost = Hats.getCostBeret();
fedoraTotal = fedoraCost * nFedora;
return fedoraTotal;
}
}
Explanation / Answer
please rate - thanks
you weren't using the saleprice
I made as little changes as possible
only change this 1 class
check if the hat is on sale when get the price, and then get regular price or sale price
import javax.swing.JOptionPane;
public class HatDemo {
public static void main(String[] args) {
double totalCost = 0; //get total cost of products
String input; //hold user input
double h1hat; //Print Beanie cost
double nBeanie; //hold number of Beanie's sold
double beanieCost = 0; //hold Beanie final sale
double h2hat; //Print Ball cap cost
double nCap; //hold number of Ball cap's sold
double capCost = 0; //hold Ball cap final sale
double h3hat; //Hold Beret Object
double nBeret; //hold number of Beret hats sold
double beretCost = 0; //hold Beret hat final sale
double h4hat; //Hold Fedora Object
double nFedora; //Hold number of Fedora hats sold
double fedoraCost = 0; //hold Fedora hat final sale
//Creating object of hats class to display costs and count usage
Hats h1 = new Hats();
//Call class counter
int h1value ; //count class instances
h1value = h1.gethatClassCount();
System.out.println(h1value + "counts");
//Create four hat objects
h1hat = Hats.getCostBeanie();
h2hat = Hats.getCostCap();
h3hat = Hats.getCostBeret();
h4hat = Hats.getCostFedora();
//Display Cost of all products
JOptionPane.showMessageDialog(null, "Beanie Cost: $" + h1hat +
" Cap Cost: $" + h2hat +
" Beret Cost: $" + h3hat +
" Fedora Cost: $" + h4hat);
//Ask which hat is on sale
input = JOptionPane.showInputDialog("Which hat is on sale? ");
String saleHat = input;
//Declare salePrice to 0 & set variables to store hat names
double salePrice = 0;
String Beanie= "Beanie";
String Cap = "Cap";
String Beret = "Beret";
String Fedora = "Fedora";
if (saleHat.equals(Beanie)){
JOptionPane.showMessageDialog(null,"The " + saleHat + " is on sale");
}
else if (saleHat.equals(Cap)){
salePrice = salePriceCap();
JOptionPane.showMessageDialog(null,"The " + saleHat + " is on sale");
}
else if (saleHat.equals(Beret)){
JOptionPane.showMessageDialog(null,"The " + saleHat + " is on sale");
}
else if (saleHat.equals(Fedora)){
salePrice = salePriceFedora();
JOptionPane.showMessageDialog(null,"The " + saleHat + " is on sale");
}
else{
JOptionPane.showMessageDialog(null, "You Entered and invalid hat type.");
}
//Display which hat is on sale
//Display amount of items sold
input = JOptionPane.showInputDialog("Enter number of Beanie's sold: ");
nBeanie = Double.parseDouble(input);
if (nBeanie >= 0)
beanieCost = beanieTotal(nBeanie,saleHat);
else
JOptionPane.showMessageDialog(null, "You entered an invalid number.");
//Display cost and get amount to buy
input = JOptionPane.showInputDialog("Enter number of Ball cap's sold: ");
nCap = Double.parseDouble(input);
if (nCap >= 0)
capCost = ballCapTotal(nCap,saleHat);
else
JOptionPane.showMessageDialog(null, "You entered an invalid amount.");
//Display cost and get amount to buy
input = JOptionPane.showInputDialog("Enter number of Beret's sold: ");
nBeret = Double.parseDouble(input);
if (nBeret >= 0)
beretCost = beretTotal(nBeret,saleHat);
else
JOptionPane.showMessageDialog(null, "You entered an invalid amount.");
input = JOptionPane.showInputDialog("Enter number of Fedora's sold: ");
nFedora = Double.parseDouble(input);
if (nFedora >= 0)
fedoraCost = fedoraTotal(nFedora,saleHat);
else
JOptionPane.showMessageDialog(null, "You entered an invalid amount.");
totalCost = capCost + beanieCost + beretCost + fedoraCost;
JOptionPane.showMessageDialog(null, "Total Sales: $" + totalCost + " Have a nice day!!");
System.exit(0);
}
private static double salePriceFedora() {
double salePrice;
salePrice = (Hats.getCostFedora()/2);
return salePrice;
}
private static double salePriceBeret() {
double salePrice;
salePrice = (Hats.getCostBeret()/2);
return salePrice;
}
private static double salePriceCap() {
double salePrice;
salePrice = (Hats.getCostCap()/2);
return salePrice;
}
private static double salePriceBeanie() {
double salePrice;
salePrice = (Hats.getCostBeanie()/2);
return salePrice;
}
//method for calculating beanie sale costs
private static double beanieTotal(double nBeanie,String s) {
double beanieCost;
double beanieTotal;
String Beanie= "Beanie";
if(s.equals(Beanie))
beanieCost= salePriceBeanie();
else
beanieCost = Hats.getCostBeanie();
beanieTotal = beanieCost * nBeanie;
return beanieTotal;
}
//method for calculating ball cap sale costs
private static double ballCapTotal(double nBallCap,String s) {
double ballCapCost;
double ballCapTotal;
String Cap = "Cap";
if(s.equals(Cap))
ballCapCost = salePriceCap();
else
ballCapCost = Hats.getCostCap();
ballCapTotal = ballCapCost * nBallCap;
return ballCapTotal;
}
//method for calculating Beret hat sale costs
private static double beretTotal(double nBeret,String s) {
double beretCost;
double beretTotal;
String Beret = "Beret";
if(s.equals(Beret))
beretCost = salePriceBeret();
else
beretCost = Hats.getCostBeret();
beretTotal = beretCost * nBeret;
return beretTotal;
}
//method for calculating Fedora hat sale cost
private static double fedoraTotal(double nFedora,String s) {
double fedoraCost;
double fedoraTotal;
String Fedora = "Fedora";
if(s.equals(Fedora))
fedoraCost = salePriceFedora();
else
fedoraCost = Hats.getCostFedora();
fedoraTotal = fedoraCost * nFedora;
return fedoraTotal;
}
}