Alright! So I'm still new to Java and we just started learning about Instances and classes this week.
My problem is that my professor assigned a program (that I've already spent around 10 hours on, I've made headway but I'm still having problems) that has slightly vague instructions, and I'm not sure how to incorporate them into my program.
It's a shopping cart program that takes the name, description, price, and quantity of a product. All of these qualities go into the Item2
class, which I'm not having problems with, and then print using the toString()
method in the Item2
class.
Next, I take the name and add this to an array in the ShoppingCart2
class (addItems
method), and then make the getTotal
method that will go through arrayItems
, and add up prices of each item (should call calculateUnitTotal()
method of each Item object and add up).
My problem is that by trying to call calculateUnitTotal()
, I am either getting the error:
non static method cannot be referenced from a static context
or the dereference error.
My professor does not want me to call the units and price objects from the Item2
class, I need to call this method specifically.
I know that I need to create an instance to do this, and that an object can't be converted to a double, but everything I try doesn't seem to work.
I'm not sure what I'm doing wrong. I also know that the code is kinda messy, I tried my best to clean it up. Any advice would be appreciated!
Item2.java:
class Item2{
private String productName;
public class Item2{
private String productName;
private String productDesc;
private double unitPrice;
private int units;
public String getProductName(){
return productName;
}
public String getproductDesc(){
return productDesc;
}
public double getUnitPrice(){
return unitPrice;
}
public int getUnits(){
return units;
}
public void setProductName(String newProductName){
productName = newProductName;
}
public void setProductDesc(String newProductDesc){
productDesc = newProductDesc;
}
public void setUnitPrice(double newUnitPrice){
unitPrice = newUnitPrice;
}
public void setUnits(int newUnits){
units = newUnits;
}
void Item2(){
productName = "";
productDesc = "";
unitPrice = -1;
units = -1;}
public void Item2(String newProductName, String newProductDesc,
double newUnitPrice, int newUnits) {
productName = newProductName;
productDesc = newProductDesc;
unitPrice = newUnitPrice;
units = newUnits;
}
public double calculateUnitTotal(){
double total = unitPrice * units;
return total;
}
public String toStrings() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (productName + "\t" + fmt.format(unitPrice) + "\t" +
units + "\t" + fmt.format(unitPrice * units));
}
}
ShoppingCart2.java:
class ShoppingCart2{
private String[] arrayItems;
private int numItems;
public ShoppingCart2(){
arrayItems = new String[20];
numItems = 0;
}
public void addItems(String itemName){
for(int i = 0; i < numItems; i++){
if(numItems==arrayItems.length)
System.out.println("Cart is full.");
else{
arrayItems[numItems]= itemName;
numItems++;
}
}
}
public ShoppingCart2 getTotal(){
ShoppingCart2 total = new ShoppingCart2();
for(int i = 0; i < numItems; i++){
/////I've tried several different methods here, they always
////lead to either
/// a need to dereference or the non static method error
}
return total;}
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String cart = "\nShopping Cart\n";
for (int i = 0; i < numItems; i++)
cart += arrayItems[i] + "\n";
cart += "\nTotal Price: " + fmt.format(total);
cart += "\n";
return cart;
}
}
I expect the output to be the names of the items in the array and the total of all the items.