costPerItem, itemQuantity, and productName keep getting the error "cannot find symbol"

package shoppingcart;

import java.text.DecimalFormat;

public class ShoppingCart {

    private double totalCost;
    private LineItem[] myItems;
    private int max = 100;
    int numberOfItems;
 
//Provide a constructor for this class
    public ShoppingCart(int size){
    max = size;
    this.totalCost = 0;
    myItems = new LineItem[max];
    numberOfItems = 0;
}

//add a method to add a line item object to this class
    public void addItem(LineItem newItemObject){
        if(numberOfItems < max - 1){ //max - 1 due to the actual index
        myItems[numberOfItems++] = newItemObject;
 
//update the totalcost
    totalCost += newItemObject.costPerItem * newItemObject.itemQuantity;
}
 
}
 
//a method printing out the carts contents with the total cost of all the items/quantities
public String getDisplayData(){
DecimalFormat myFormat = new DecimalFormat("#.##");
String output = "";
output += "Your shopping cart contains: \n";
output += "\n\t----------------------------------------------";
//Create a loop to display the contents and quantity of the selected cart
for(int x = 0; x < numberOfItems; x++){
//Append the string with a new lineItem
output += "\n\tItem : " + myItems[x].itemQuantity + " x " +
myItems[x].productName + " @ " +
myFormat.format(myItems[x].costPerItem) + " = $" +
myItems[x].costPerItem * myItems[x].itemQuantity;
}
output += "\n\tTotal : $" + myFormat.format(totalCost);
output += "\n\t----------------------------------------------";
 
return output;
}
public double getTotalCost(){
return totalCost;
}
}

Recommended Answers

All 3 Replies

Where are the variables that are referenced in the error messages defined?
The compiler can not find their definitions in the LineItem class.

Please help me with the errors. Thank you in advance.

Write a class representing a line item in a shopping cart: the characteristics of this class are the name of the product, the cost per unit, and the number of items. This class is named LineItem.Provide a constructor that takes 3 arguments, and initializes the corresponding instance variables. Provide the accessors to the 3 instance variables.

Write a class representing a shopping cart: a shopping cart contains a list of line item objects. This class is named ShoppingCart: provide a constructor for this class, a method to add a line item object to it, and a method printing out its content, with the total cost adding up the cost of all line items.

Write a test program that creates 2 shopping carts, and asks the user to input the content of each shopping cart. Once the user is done inputing the content of the shopping carts, the program displays their contents with their cost.

1.

6. Your programs should compile and run without errors.

TestShoppingCart

The issue with this program is that it cannot find the symbol for ShoppingCart and LineItem.

package testshoppingcart;

import java.util.Scanner;

public class TestShoppingCart {
    public static void main(String[] args) {
      
//Create an array for the shopping carts 
    ShoppingCart[] myCarts = new ShoppingCart[2];
    LineItem[] storeItems = new LineItem[5];

    for(int i = 0; i < myCarts.length; i++){
    myCarts[i] = new ShoppingCart(25);
}

    int choice;

    String itemList = "Please select one of our items:\n 1)Steak \n 2)Potatoes \n 3)Green Beans \n 4)Gravy \n 5)Sauce";
    
    String enterQuantity = "Please enter a quantity";

//Create the shopping cart line items

    storeItems[0] = new LineItem("Steak" ,5.50, 0);
    storeItems[1] = new LineItem("Potatoes" ,3.50, 0);
    storeItems[2] = new LineItem("Green Beans" ,1.50, 0);
    storeItems[3] = new LineItem("Gravy" ,1.00, 0);
    storeItems[4] = new LineItem("Sauce" ,0.75, 0);
 

    Scanner selectCart = new Scanner(System.in);
    System.out.println("Please select your shopping cart: \n1)Shopping Cart 1.\n2)Shopping Cart 2");

    int cartInput = selectCart.nextInt();

//Makes sure the user selects a 1 or 0
    int userCart = ((cartInput + 1)%myCarts.length);
 
//Ask user to select a cart
do {

    Scanner input = new Scanner(System.in);
    System.out.println("Please select and option:\n1)Change your shopping cart.\n2)To add an item to your shopping cart.\n3)To view your cart and total.\n4)Quit");
    choice = input.nextInt();
    if(choice == 1){

//Prompt the user to select a shopping cart
    selectCart = new Scanner(System.in);
        System.out.println("Please select your shopping cart: \n1)Shopping Cart 1.\n2)Shopping Cart 2");
    cartInput = selectCart.nextInt();

//ensure the user input is either 0 or 1
    userCart = ((cartInput + 1)%myCarts.length);
}

    else if(choice == 2){

//Prompt the user to select an item

    System.out.println(itemList);
    Scanner selectItem = new Scanner(System.in);
    int addedItem = selectItem.nextInt();
 
//Prompt user to enter a quantity
    System.out.println(enterQuantity);
    Scanner selectQuantity = new Scanner(System.in);
    int addedQuantity = selectQuantity.nextInt();
 
//Add the item and quantity to the cart
    addedItem -=1;

//Set the quantity of the selected item to the cart
    storeItems[addedItem].setQuantity(addedQuantity);

//Add the item to the cart
    myCarts[userCart].addItem(storeItems[addedItem]);
 
}

    else if (choice == 3){

//Display Cart contents & Total

    System.out.println(myCarts[userCart].getDisplayData());
}

    else{
    continue;
}
}
    while(choice != 4);
}
}

ShoppingCart

Issue is that the program is stating it cannot find the symbol for LineItem.

package shoppingcart;

import java.text.DecimalFormat;

public class ShoppingCart {

    private double totalCost;
    private LineItem[] myItems;
    private int max = 100;
    int numberOfItems;
 
//Provide a constructor for this class
    public ShoppingCart(int size){
    max = size;
    this.totalCost = 0;
    myItems = new LineItem[max];
    numberOfItems = 0;
}

//add a method to add a line item object to this class
    public void addItem(LineItem newItemObject){
        if(numberOfItems < max - 1){ //max - 1 due to the actual index
        myItems[numberOfItems++] = newItemObject;
 
//update the totalcost
    totalCost += newItemObject.costPerItem * newItemObject.itemQuantity;
}
 
}
 
//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){    
    
    DecimalFormat myFormat = new DecimalFormat("#.##");
    String output = "";
    output += "Your shopping cart contains: \n";

//Create a loop to display the contents and quantity of the selected cart
    for(int x = 0; x < numberOfItems; x++){

//Append the string with a new lineItem
    output += "\n\tItem : " + myItems[x].itemQuantity + " x " +
    myItems[x] .productName + " @ " +
    myFormat.format(myItems[x].costPerItem) + " = $" +
    myItems[x].costPerItem * myItems[x].itemQuantity;
}

    output += "\n\tTotal : $" + myFormat.format(totalCost);
 
return output;
}

    public double getTotalCost(){
return totalCost;
}
}

LineItem

This program has no errors.

package lineitem;

    class LineItem {
    String productName;
    double costPerItem;
    int itemQuantity;
     
    public LineItem(String productName, double costPerItem, int itemQuantity){
    this.costPerItem = costPerItem;
    this.productName = productName;
    this.itemQuantity = itemQuantity;
    }

    public double getCostPerItem(){
    return costPerItem;
    }
    public String getProductName(){
    return productName;
    }
    public int getItemQuantity(){
    return itemQuantity;
    }
    public void setQuantity(int qty){
    itemQuantity = qty;
    }
    }

Your classes are in different packages. That means each file must be in the folder with the package name.
You would need import statements so the compiler can find the correct path to the class files.
Why are you making the project so complicated? Can you put everything in the same package?
Where are the source files located?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.