I'm having a few issues with my program. First off, for some reason when I do setBasePrice, it doesn't return as a -1 nor 1, depending on what I do. As well, for calculateTotalPrice, it doesn't return as -1 when under 0 and over 1 and my program overall gives the wrong value (for instance for a Refrigerated item with the price of $5.60 and taxRate of .06, it's supposed to be $7.562 and it's not), but I think if I figure out the return issue, it will help with that problem.
Could someone please help me figure out my errors?
Thank you.
public class GroceryItem { private String groceryName, groceryCategory; public final double minPrice = 0, maxPrice = 500, taxHigh = 1; private static final String GENERAL = "General", PRODUCE = "Produce", REFRIGERATED = "Refrigerated", FROZEN = "Frozen"; private double basePrice = 0, localRate = 0, totalPrice = 0; public GroceryItem(String name, String category) { groceryName = name; groceryCategory = category; category = GENERAL; basePrice = 0; } public String setName(String itemName) { if (itemName == null || itemName.trim().equals("")) { itemName = "No Name"; groceryName = itemName; return itemName; } else { groceryName = itemName; return itemName; } } public String getName() { return groceryName; } public int setBasePrice(double base) { basePrice = base; if (basePrice > minPrice || base <= maxPrice) { return 1; } else if (basePrice > maxPrice) { return 0; } else { return -1; } } public double getBasePrice() { return basePrice; } public double calculateTotalPrice(double taxRate) { localRate = taxRate; if (taxRate < minPrice || taxRate > taxHigh) { return -1; } else if (groceryCategory == GENERAL) { totalPrice = (basePrice + taxRate); } else if (groceryCategory == PRODUCE) { totalPrice = (basePrice + (basePrice * 0.05)); } else if (groceryCategory == REFRIGERATED) { totalPrice = ((basePrice + 1.50) + taxRate); } else if (groceryCategory == FROZEN) { totalPrice = ((basePrice + 3.00) + taxRate); } return totalPrice; } public boolean setCategory(String cat) { groceryCategory = cat; if (cat == GENERAL) { return true; } else if (cat == PRODUCE) { return true; } else if (cat == REFRIGERATED) { return true; } else if (cat == FROZEN) { return true; } else { return false; } } public String getCategory() { return groceryCategory; } public String toString() { String output = "Grocery Item: " + groceryName + "\n" + "Category: " + groceryCategory + "\n" + "Base Price of Grocery Item: " + basePrice + "\n"; return output; } }