Hi all,

I'm working on a school project and I am running into a problem, possibly more...

This is my first post, so I am sorry if it ends up ugly.

Here is a description of the project:

This project focuses on demonstrating your understanding of classes and objects. Before attempting this project, be sure you have completed all of the reading assignments listed in the syllabus to date, participated in the weekly conferences, and thoroughly understand the examples throughout the chapters. The project requirements include:

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.

Your programs should compile and run without errors.

Be sure to test your program carefully. Provide a list of comprehensive test cases used to validate your application and include these test cases in your word document containing your UML diagrams and descriptions. Similar to Project 1, your test data can be shown in a table that includes input data, expected output, actual output and pass/fail results from the test.

Submission requirements:
Your deliverables include 3 Java files (LineItem.java, Shoppingcart.java, and TestShoppingCart.java), and a Word document. Do not forget to have a comment indicating your name in each java file. Your word document should include your test table, and UML diagrams and descriptions and be named TestShoppingCart.doc. Your completed assignment should be submitted to your WebTycho Project 3 assignment area no later than the due date listed in the syllabus.

Yay.. that was a fun read!!!

Here is my first file :

import.java.util.*;
public class TestShoppingCart {
	public static void main(String [] args){
		

		//Create two shopping carts
		ShoppingCart newCartOne = new ShoppingCart(totalcost);
		ShoppingCart newCartTwo = new ShoppingCart(totalcost);
		//I think I need this to pull the product names when asking for input
		LineItem shoppingCartTest = new LineItem(productNames[], itemCost[], shoppingCartQuantity[]);

		//ask the user to select a cart (Not sure that I will need this 
		Scanner userInput = new Scanner(System.in);
		System.out.println("Please select your shopping cart: \nFor cart 1 enter integer 1.\nFor cart 2 enter integer 2.");
		int userCart = userInput.nextInt();

		//Ask the user to input the contents of each cart
		for (int x = 0; x < shoppingCartQuantity.length; x++){
			System.out.println("How many " + productNames[x] + "Would you like? Enter an Integer: ");
			shoppingCartQuantity[x] = userInput.nextInt();
		}

		//Once they input the content, Display the contents of the cart, Quantity of each item and the total cost.
		newCartOne.getCartDisplay();	
		newCartOne.getTotalCost();
		newCartTwo.getCartDisplay();	
		newCartTwo.getTotalCost();
	}
}

Here is the ShoppingCart file

import java.util.*;

/*this class needs to :
*contain a list of lineItem objects
*Provide a constructor for this class
*method to add a line item to this class
*/

class ShoppingCart{
	private int totalCost = 0;

	public ShoppingCart(int totalCost){
		this.totalCost = totalCost;
	}
	//Method to display the contents and quantity of the cart
	public void getCartDisplay(int[] shoppingCartQuantity, String[] productNames){
		for (int x = 0; x < shoppingCartQuanitity.length; x++){
			System.out.println("Your cart has " + shoppingCartQuantity[x] + productNames[x]);
		}
	}
	//Method to display the total Cost
	public int getTotalCost(int[] shoppingCartQuantity, int[] itemcost, int totalCost){
		for(int i = 0; i < shoppingCartQuantity.length; i++){
			int lineItemCost = itemCost[i] * shoppingCartQuantity[i];
			totalCost = totalCost + lineItemCost;
		}
		System.out.println("The total Shopping Cart cost is: $" + totalCost);
		return totalCost;
	}
}

And lastly the LineItem.java file

/*
*Characteristics are: name of the product, cost per unit, number of items
*Provide a constructor that takes 3 arguments and intializes the corresponding instance variables
*provide the accessors to the 3 instance variables
*/

import java.util.*;
class LineItem{
	private int[] itemCost = {50, 25, 75, 100, 10};
	private String[] productNames = {"Bananas","Apples","Oranges","Grapefruits","Cherries"};
	private int[] shoppingCartQuantity = new int[5];

	public lineItem(string[] productNames, int[] itemCost, int[] shoppingCartQuantity){
		this.itemCost = itemCost;
		this.productNames = productNames;
		this.shoppingCartQuantity = shoppingCartQuantity;
	} 
	public int[] getShoppingCartQuantity(){
		return shoppingCartQuantity;
	}
	public int[] getItemCost(){
		return itemCost;
	}
	public String[] getProductNames(){
		return productNames;
	}
}

in the first file I get a '.class' expected error on the following line

LineItem shoppingCartTest = new LineItem(productNames[], itemCost[], shoppingCartQuantity[]);

Thoughts? Am I on the right track as far as the program goes, or am I completely off my rocker?

i see a lot of problems in your main :

public class TestShoppingCart {
public static void main(String [] args){
 
//first i'd create an array of carts not 2 seperatly named carts (ill come back to this a bit lower)

ShoppingCart newCartOne = new ShoppingCart(totalcost); 
//totalcost doesnt exist in this scope, it was never declared nor initiated.
ShoppingCart newCartTwo = new ShoppingCart(totalcost); //same thing here

//the array variables productNames , itemCost and shoppingCartQuantity were all declared in the LineItem class and can't be used outside of their scope , thus are unavailable here
LineItem shoppingCartTest = new LineItem(productNames[], itemCost[], shoppingCartQuantity[]);

the whole idea between your LineItem class is wrong, it shouldn't hold arrays, just single Strings, then your carts class should have LineItem arrays in them (not use the same out of scope array variables from LineItem like you're doing now)


here if you built an array of carts those numbers could be 0 or 1 to determine the cart index(or keep 1 and 2 but substract 1 when using to get the index)
also youl need a kind of do-while structure here to keep coming back here untill your done filling carts
exemple choices : 1)put something in a cart 2)see a cart's content 3)quit
choices 1 and 2 ask which cart (cart1 or cart2)
after choosing a cart for option 1 then display the list of items, ask which the user wants to add, how many, and add it to the corresponding cart, then move on to the next while loop to start over from main menu...

Scanner userInput = new Scanner(System.in);
System.out.println("Please select your shopping cart: \nFor cart 1 enter integer 1.\nFor cart 2 enter integer 2.");
		int userCart = userInput.nextInt(); 
 

//that for is flawed, shopping quantity is out of scope, and irrelevant to the ammount of items you want to store in carts
for (int x = 0; x < shoppingCartQuantity.length; x++){
			System.out.println("How many " + productNames[x] + "Would you like? Enter an Integer: ");
			shoppingCartQuantity[x] = userInput.nextInt();
		}
 

//do the displaying in a method and execute that method only when user chooses to do so from a menu
		newCartOne.getCartDisplay();	
		newCartOne.getTotalCost();
		newCartTwo.getCartDisplay();	
		newCartTwo.getTotalCost();
	}
}

the shopping cart fucntion needs rethinking, it doesnt have a LineItem list, and uses arrays that are private variables from LineItem class.

LineItem class needs rethinking, don't use arrays, see the class as 1 single item, be it a bannana or an apple, but not the whole contents of the cart. also the "number of items" would be more appropriate somewhere in the structure of your cart class, but the project's description seems to want you to keep it there so keep it there.

Good luck, let me know how your doing after the few improvements i suggested.

Okay.. So I am taking a new approach... Thoughts? I have questions but want to make sure we are on the same page...

TestShoppingCart.java :

import.java.util.*;
public class TestShoppingCart {
	public static void main(String [] args){
		//Create an array for 2 shopping carts (might need some guidance on how this will work)
		int[] shoppingCart = new int[2];
		int choice;
		String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
		String enterQuantity = "Please enter a quantity"

		//Create or Select or something with the carts :-)

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

		//The next section asks the user to select items and quantities and displays the content
		do {
			//read the next input from the user
			Scanner input = new Scanner(System.in);
			System.out.println("Please select and option:\n1)To add an item to your cart.\n2)To view your cart and total.\n3)Quit");
			choice = input.nextInt();
			if(choice == 1){
				//Have the user select an item from the list
				System.out.println(itemsList);
				Scanner selectItem = new Scanner(System.in);
				
				//Now do something with that input (not sure what yet)

				//Prompt user to enter a quantity
				System.out.println(enterQuantity);
				Scanner selectQuantity = new Scanner(System.in);
				
				//Now do something with that input (not sure what yet)
			}
			else if (choice == 2){
				//Display Cart contents
				//Assuming I will pull this from ShoppingCart.java

				//Display Cart total
				//Assuming I will pull this from shoppingCart.java
			}
			else{
				contintue;
			}
		}while(choice != 3);
	}
}

ShoppingCart.java

import java.util.*;

/*this class needs to :
*contain a list of lineItem objects
*Provide a constructor for this class
*method to add a line item to this class
*/

public class ShoppingCart{
	private int totalCost = 0;
	

	//Provide a constructor for this class
	public ShoppingCart(){
		this.totalCost = totalCost;
	}
	//add a method to add a line item object to this class
	//Not sure what to do here

	//a method printing out the carts contents with the total cost of all the items/quantities
	//Might break this down to two methods?

	public String getDisplayData(){
		//Create a loop to display the contents and quantity of the selected cart

		return something; //Not sure how to construct all of this right now
	}	
	public int getTotalCost(){
		//Total cost is going to equal productName * Quantity for all product names in a cart

		return totalCost;
	}



}

LineItem.java

/*
*Characteristics are: name of the product, cost per unit, number of items
*Provide a constructor that takes 3 arguments and intializes the corresponding instance variables
*provide the accessors to the 3 instance variables
*/

import java.util.*;
class LineItem{
	String productName;
	int costPerItem;
	int itemQuantity;

	public LineItem(){
		this.costPerItem = costPerItem;
		this.productName = productName;
		this.itemQuantity = itemQuantity;
	}	
	public int getCostPerItem(){
		return costPerItem;
	{
	public String getProductName(){
		return productName;
	}
	public int getItemQuantity(){
		return itemQuantity;
	}
}

When you create the constructor for LineItem you can't just make variables appear, they need to be received as arguments, like this :

public LineItem(String sName , int iCost, int iQty){
		this.costPerItem = iCost;
		this.productName = sName;
		this.itemQuantity = iQty;
	}

in your ShoppingCart class, you need to have a global variable that is an array of LineItems, that is where you will store the items. (Ideally you would chose a List or ArrayList or some other dynamic kind of array instead of just a plain array , cause they need to be instanciated with a fixed size which is not practical, for now just set the max size to like 10 or 100 its not like your computer can't handle it for this test program)

public class ShoppingCart{
        private LineItem[] myItems;
        private int max;
	private int totalCost;

same thing here dont set totalCost with a variable out of nowhere , here its not giving you an error cause its the same name, so your just saying this.x = x; since totalCost is null cause it has not been initialized your just setting it to null while it already is.

//Provide a constructor for this class
	public ShoppingCart(int size){
                //set your max size to the argument "size" you received :
                max = size;

		this.totalCost = 0; //set it to 0 instead, making sure every new cart is empty and free
                //initialize your LineItem array here :
                myItems = new LineItems[max];
	}

and one more thing , from your main :

//Create an array for 2 shopping carts (might need some guidance on how this will work)
		int[] shoppingCart = new int[2];

dont make an array of ints as carts, you have a cart class , make an array of that :

ShoppingCart[] myCarts = new ShoppingCarts[2];

overall, great improvement from the first version, you were very close on many points, do the changes i suggested and let me know how that goes.

First, I want to thank you for helping me out. I understand the basics here. It seems as though if this was just one file I could get it all working. The documentation for the course does not give a good description of using multiple classes.

So thank you very much!

Here is TestShoppingCart.java (the Main)

When I create the Array should it be called "myCarts" or "myItems"(Used in ShoppingCart.java)

When I ask them to select a cart, What am I going to do with that input?

of course I have questions about adding items, but I don't think I am there yet.

import java.util.*;

public class TestShoppingCart {

    public static void main(String [] args){
		//Create an array for 2 shopping carts (might need some guidance on how this will work)
		ShoppingCart[]myCarts = new ShoppingCart[2];
		int choice;
		String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
		String enterQuantity = "Please enter a quantity";

		//Create or Select or something with the carts :-)

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

		//The next section asks the user to select items and quantities and displays the content
		do {
			//read the next input from the user
			Scanner input = new Scanner(System.in);
			System.out.println("Please select and option:\n1)To add an item to your cart.\n2)To view your cart and total.\n3)Quit");
			choice = input.nextInt();
			if(choice == 1){
				//Have the user select an item from the list
				System.out.println(itemList);
				Scanner selectItem = new Scanner(System.in);
				
				//Now do something with that input (not sure what yet)

				//Prompt user to enter a quantity
				System.out.println(enterQuantity);
				Scanner selectQuantity = new Scanner(System.in);
				
				//Now do something with that input (not sure what yet)
			}
			else if (choice == 2){
				//Display Cart contents
				//Assuming I will pull this from ShoppingCart.java

				//Display Cart total
				//Assuming I will pull this from shoppingCart.java
			}
			else{
				continue;
			}


		}while(choice != 3);
		
	}
}

Here is ShoppingCart.java

I'm pretty good with most of this. Now that we have an Array, how would we add the line item? Is this coming from the main?

once we get the line items, it should not be hard for me to figure out the totalcost

import java.util.*;
public class ShoppingCart {
	private int totalCost;
        private LineItem[] myItems;
        private int max = 100;
        

	//Provide a constructor for this class
	public ShoppingCart(int size){
            max = size;
            //Set every new car to "empty"
            this.totalCost = 0;
            //Intialize the array
            myItems = new LineItem[max];
	}
	//add a method to add a line item object to this class
	//Not sure what to do here

	//a method printing out the carts contents with the total cost of all the items/quantities
	//Might break this down to two methods?

	public String getDisplayData(){
		//Create a loop to display the contents and quantity of the selected cart

		return something; //Not sure how to construct all of this right now
	}	
	public int getTotalCost(){
		//Total cost is going to equal productName * Quantity for all product names in a cart

		return totalCost;
	}
}

Here is the code for line item. Can the variables be the same name in the following code? or do they need to be named something different? Where do I use this? Shopping cart or main?

public LineItem(String productName, int costPerItem, int itemQuantity){
		this.costPerItem = costPerItem;
		this.productName = productName;
		this.itemQuantity = itemQuantity;
	}
import java.util.*;
class LineItem {
    String productName;
	int costPerItem;
	int itemQuantity;

	public LineItem(String productName, int costPerItem, int itemQuantity){
		this.costPerItem = costPerItem;
		this.productName = productName;
		this.itemQuantity = itemQuantity;
	}	
	public int getCostPerItem(){
		return costPerItem;
        }
	public String getProductName(){
		return productName;
	}
	public int getItemQuantity(){
		return itemQuantity;
	}
}

very good!

for the main :

1 suggestion about the carts , since your choosing blue or red before the menu loop, id add a menu option to switch cart.

at line 12 : //Create or Select or something with the carts :-)

creating an array of objects , does not create the objects.
so you will need to loop through your array (i prefer "for" loops to do this job)
and initialize all the carts in your array

for(...you'll figure this one out! :D ...){
    myCarts[i] = new ShoppingCart( ...arguments here... );
}

as for the choice of cart you save in userCart, that's the index of the cart the user chose in the array. right now you save 1 or 2 for carts 0 or 1 (java arrays are always 0 based) you can do 3 things to make sure your code is safe for this part :

-validate the user entered ONLY 1 OR 2 , then substract 1 from that choice and make him start over if the choice was wrong.

-take responsability of whatever choice the user takes and make it work with your array.
( int choice = (choice+1)%myCarts.length; )
that would turn 5 into (5+1)%2 = 6%2 = 0 and 6 into (6+1)%2 = 7%2 = 1

-set a default cart value if user input is not valid (if user enters 7 then userCart=0 )

up to you.

ShoppingCart.java :

totalCost should be double

getTotalCost is fine the way it is as long as you update the global variable everytime you add something to the cart (except its gonna return a double not an int after previous recommendation)

to add an item to the items array you will need a method that receives either a LineItem object, in which case you just directly add it to the array (you'll need an additional global variable in that class which i'll talk about in a minute) , Or the method can receive the arguments needed to create a new LineItem Object and create it localy before adding the object to the array.

I personally think your main should create an array of LineItems corresponding to the choices you give the user. When they enter a quantity you just set the quatity of the corresponding LineItem in your LineItem array and send it to your cart :

myCarts[userCart].addItem(myMainArrayOfDefaultLineItems[userItemChoice]);

to do either way, you will need a int variable that keeps track of how many items are in your cart. the reason for this is that you're using a fixed array of 100 (by default, you can change that from the constructor) and not a dynamic storing structure with a .Count attribute. so while your items array in each cart can contain 100 items, they might only have 90 , or 50 or none! lets say its empty , then the variable (lets call it "iNbItems") is at 0 , and you add the item to myItemsArray[iNbItems]... (0) , the same way lets say you have 50 items in the cart then the array contains LineItems objects from index 0 to 49, so the next logical spot to store a new item IS 50 , then you can manually add 1 to iNbItems.

while doing this be sure to compare iNbItems' value to max's value before adding, you'll get an IndexOutOfBounds Exception if you try to add an item when iNbItems is >= to max.

When you add an item dont forget to calculate the cost per item * qty and add that sum to totalCost.

as for the displayCartContent method simply create a temporary string = "" , use a for loop to build that string the way you want ( "\n" switches line ) and then return that temporary string.

LineItem.java :

yes you can have global variables and local arguments with the same name, but its not very practical , java will (i believe) use the local variable unless you specify "this." like you do , in which case it refers to the global variable.

so it doesn't hurt but it would be better not to use this in a work environement where you might not be the only person working on the code and it can get confusing.

like i specified higher in the addItem method help, you could use this class either strictly in your shoppingCart class, or use it in your main also. I suggested you use it in your main to have a hardcoded list of items, but that's all up to you.

To do things correctly really, you would ideally keep the ShoppingCart class, LineItem class AND a management Class in a library, let ONLY the management class handle the cart and items' methods , and provide his own methods to the library's user.

This would allow to reuse the whole cart & items code with diferent programs with diferent interfaces (GUI or console) without ever changing the cart or items' code.

But it's beyond te scope of your assignement i believe ;)

I didn't make all of the changes that you suggested yet. I'm going to try to take baby steps so I'm sure I understand it all.

Here are my updates.


Here is the TestShoppingCart.java

I've made the changes to the "select Cart" option. It does make sense to have it in the while statement and to verify the input.

should the "addItem" method be in ShoppingCart.java? It makes sense for it to be in the shoppingcart not the main.

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(/* What arguments will be going in here? LineItems? */);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	Scanner input = new Scanner(System.in);
    	System.out.println("Please select and option:\n1)Select a 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
    		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();
		//ensure the user input is either 0 or 1
		int userCart = (cartInput + 1)%mycarts.length);
	}
	else if(choice == 2){
    		//Have the user select an item from the list
    		System.out.println(itemList);
    		Scanner selectItem = new Scanner(System.in);
		int addedItem = selectItem.nextInt();
		//This seems like a logical step
		myCarts[userCart].addItem(myMainArrayOfDefaultLineItems[addedItem];
     
     
    		//Prompt user to enter a quantity
    		System.out.println(enterQuantity);
    		Scanner selectQuantity = new Scanner(System.in);
		int addedQuantity = selectQuantity.nextInt();
		//This seems like a logical step
		myCarts[userCart].addQuantity(myMainArrayOfDefaultLineItems[addedQuantity];
     		//what is "myMainArrayOfDefaultLineItems" ?
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

Here is ShoppingCart.java

import java.util.*;
    public class ShoppingCart {
    	private double totalCost;
    	private LineItem[] myItems;
    	private int max = 100;
     
     
    	//Provide a constructor for this class
    public ShoppingCart(int size){
    	max = size;
    	//Set every new car to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	myItems = new LineItem[max];
    }
    	//add a method to add a line item object to this class
    	//Not sure if the next two methods should be combined.
   	
    public addItem(){
	//Do something
    }
    public addQuantity(){
	//Do something
    }  

	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     
    	return something; //Not sure how to construct all of this right now
    }
    public double getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     
    	return totalCost;
    }
}

Here is LineItem.java

Unchanged. Even though you said keeping them the same was not normal, if it works, I'd like to keep it for my sanity (at least to this point it makes sense to me). Based on what I've read and you've stated this file should be complete? for now at least.

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
}

this is fine, you're learning and thats the important point!

as far as arguments while your initializing your carts in the cart array, take a look at the ONLY constructor you programmed for cart :

public ShoppingCart(int size)...

thats the argument you need. You could also make a default constructor that takes no arguments and leaves the size at 100.

with the way you changed the menu, it would be possible for a user to select "add items to my cart" before selecting a cart. either test if a cart was selected before trying to add items, or move the cart choosing before the menu loop and simply add to the menu an option to CHANGE selected cart, this way 1 will always be selected.


you will have a big problem here with your cart selection, since you declare the int userCart at line 28 it is withing the IF statement from line 22 to 29, which means it's scope is only within there and the variable cant be used elsewhere. Keep the line there but dont DECLARE it there, declare it as a global variable (line 4ish between class and main) or arround line 11 with choice, so it's scope stays alive for the whole duration of the main.


as far as adding an item to the list you'll need to create an ARRAY of LineItems in your main , with the generic items your offering in the string , (the fruits). You will have to do this manualy and keep them in the same order you propose in the menu. THAT ARRAY will be what i refered to as "myMainArrayOfDefaultLineItems" the name was a tip :P

You will also need to prompt a quantity before adding the item, this way you can modify saaid item in the array before passing it to the ShoppingCart's addItem() method.


Lets leave it at this for now , for these changes to work you will need to at least add a LineItem argument to Cart's addItem(...) method. even if it doesnt do anything, it'll fix a compiler error for when you fix the main. In ShoppingCart drop the addQuantity() method, it doesn't belong there. and for now leave the variables public in LineItem not private. Its better to have them private but it would require to program get/set methods which we will come back to later ;)

keep working, we're getting there!

Good catch on the Possibility to add items without selecting a cart. I bet if you ask my wife if I take hints she will say NO! :-)

Main -

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	String[] storeItems = {"Bananas","Apples","Oranges","Grapes","Cherries"};

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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() - 1;  /*Added the ( - 1) to set it to index not the input*/
		
		//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
		myCarts[userCart].addItem(storeItems[addedItem];
		myCarts[userCart].addQuantity(storeItems[addedQuantity];
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

Shopping Cart

import java.util.*;
    public class ShoppingCart {
    	private double totalCost;
    	private LineItem[] myItems;
    	private int max = 100;
     
     
    	//Provide a constructor for this class
    public ShoppingCart(int size){
    	max = size;
    	//Set every new car to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	myItems = new LineItem[max];
    }
    	//add a method to add a line item object to this class
    	//Not sure if the next two methods should be combined.
   	
    public addItem(productName, itemQuantity, costPerItem){
	//Do something almost magic like.
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     
    	return something; //Not sure how to construct all of this right now
    }
    public double getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     
    	return totalCost;
    }
}

Lastly - LineItem

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
}

Where do I create the array "storeItems" I see that I call it below....

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	String[] storeItems = {"Bananas","Apples","Oranges","Grapes","Cherries"};

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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
		myCarts[userCart].addItem(storeItems[addedItem, addedQuantity]); //If I did this would I need to change the array?  Not even sure that I created this array yet.
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

Yeah first the storeItems array should be a LineItem array not a String array, the reason for this is that the shoppingCart's method to add an item will require a LineItem argument , and you will pass one to it directly from storeItems.

LineItems[] storeItems = new LineItems[5];

//fill the array with your fruits :
//here set the quantity to 0 or create a new constructor in LineItem that doesnt require quantity, because you will set a diferent value manualy everytime the user adds this
storeItems[0] = new LineItem("Banana" , 2.50 , 0);  item.
storeItems[1] = ...

so :

//Add the item and quantity to the cart
myCarts[userCart].addItem(storeItems[addedItem, addedQuantity]); //If I did this would I need to change the array?  Not even sure that I created this array yet.

will look like :

addedItem -= 1; //because your array is zero based and your menu is one based.
storeItems[addedItem].setQuantity(addedQuantity);//set the quantity of the selected item
myCarts[userCart].addItem(storeItems[addedItem]);//add the item to the cart

in this code setQuantity(int qty) is a method from LineItem that allows to set the quantity variable.

and addItem(LineItem newItemObject) is a method from ShoppingCart that expects a LineItem Object as argument, which were taking from the main's storeItems array.

While I understand the changes you want me to make... It makes no sense to me from a higher level. As we get further along I understand most of whats going on.

Taking a step back from an over all perspective, I really feel like this project could be done with one java file. so what is the benefit of having multiple files in this case? The reason I ask is that we seem to be putting a lot of code in the "testshoppingcart.java" file... seems like a waste of 2 other files.

Either way... here is what I have:

TestShoppingCart.java

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	storeItems[0] = new LineItem("Bananas" ,2.50, 0);
	storeItems[1] = new LineItem("Apples" ,1.50, 0);
	storeItems[2] = new LineItem("Oranges" ,3.50, 0);
	storeItems[3] = new LineItem("Grapes" ,0.50, 0);
	storeItems[4] = new LineItem("Cherries" ,0.10, 0);

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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[addItem]);
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

ShoppingCart.java

import java.util.*;
    public class ShoppingCart {
    	private double totalCost;
    	private LineItem[] myItems;
    	private int max = 100;
     
     
    	//Provide a constructor for this class
    public ShoppingCart(int size){
    	max = size;
    	//Set every new cart to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	myItems = new LineItem[max];
    }
    	//add a method to add a line item object to this class
    	//Not sure if the next two methods should be combined.
   	
    public addItem(LineItem newItemObject){
	//I understand this is a method, but what does this actually do?   
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     
    	return something; //Not sure how to construct all of this right now
    }
    public double getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     
    	return totalCost;
    }
}

LineItem.java

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
    public setQuantity(int qty){
	//do I need to return addedQuantity here? or just qty?
	return addedQuantity;
    }
}

it probly could all be done within one file, it could even be done without the ShoppingCart and LineItem classes , but then you wouldn't be following a OOP course, you'd simply be following a modular programming course.

oop on wikipedia
modular on wikipedia

the idea is to split the functionalities that belong to certain objects in the respective classes...

setting the quantity of an item belongs to the LineItem object while adding an item to the cart belongs to the ShoppingCart class, so those specific methods will be written in their respective files. The main program uses both these files for their functionalities, the more complex your program gets, the bigger that file is gonna be, and that will not really change the functionalities of a shopping cart or a cart item.

your main looks pretty good for now, missing some validation but we will get back to that much later.

your LineItem class is fine for now, just make your setQuantity method void so it doesnt need to return any value, it's just a set not a get.

let's focus on your ShoppingCart adding an item, i think i mentioned in an earlier post you would need an extra variable in there to count where you are at, alternatively you could import java.util.ArrayList; and use that dynamic structure instead, but if you want to keep using an array it's still possible :

pretend you have an int variable called iNbItems.

in the cart constructor you set it to 0;

everytime you add an item in the cart, you add it at the index of your items array [iNbItems] and then add 1 to iNbItems.

//reminder :
//array first index -> 0   ;   array's size -> 100  ;  array last index -> 99

//starting values :
//iNbItems -> 0
//items in array -> 0
//next available array index -> 0

cart.add(someItem);

//iNbItems -> 1
//items in array -> 1 (@ 0 )
//next available array index -> 1

cart.add(someItem);

//iNbItems -> 2
//items in array -> 2 (@ 0 & 1)
//next available array index -> 2

...and so on...

Thanks for the explanation. I guess you need to start small huh? This is why I love college!

Let me say that I am sorry, I am doing some of this from work and I do not have an IDE to catch silly mistakes. I've noticed some typos when switching back and forth.

Testshoppingcart.java

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	storeItems[0] = new LineItem("Bananas" ,2.50, 0);
	storeItems[1] = new LineItem("Apples" ,1.50, 0);
	storeItems[2] = new LineItem("Oranges" ,3.50, 0);
	storeItems[3] = new LineItem("Grapes" ,0.50, 0);
	storeItems[4] = new LineItem("Cherries" ,0.10, 0);

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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[addItem]);
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     		System.out.println(output);
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
		System.out.println(total);
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

Shoppingcart.java

import java.util.*;
    public class ShoppingCart {
    	private double totalCost;
    	private LineItem[] myItems;
    	private int max = 100;
     
     
    	//Provide a constructor for this class
    public ShoppingCart(int size){
    	max = size;
    	//Set every new cart to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	myItems = new LineItem[max];
    }
    	//add a method to add a line item object to this class
    	//Not sure if the next two methods should be combined.
   	
    public addItem(LineItem newItemObject){
	while(int numberOFLineItems < max){//This will count the number of line items in the cart, not to exceed the max (100, or index 99)	
	mycarts.add(newItemObject);//?
	++numberOFLineItems; 
	}
	   
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     	String output = "I love fruit";
    	return something; //Not sure how to construct all of this right now
    }
    public double getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     	String total = "The total is $5";
    	return totalCost;
    }
}

LineItem.java

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
    public void setQuantity(int qty){
	
    }
}
public addItem(LineItem newItemObject){
	while(int numberOFLineItems < max){ 
	mycarts.add(newItemObject);//?
	++numberOFLineItems; 
	}
 
    }

aight this is a bit wrong , the int numberOfLines is declared but never initialized.
while is not the structure you will want for this.
and myCart is a variable from your main, not shopping cart, your array here is myItems

just set a global variable in ShoppingCart (numberOfItems would be more appropriate than lines)

public class ShoppingCart{
    ...
    int numberOfItems;
    ...

    //constructor
    public ShoppingCart(int size){
        ...
        numberOfItems = 0;
        ...
    }

    //addItem
    public void addItem(LineItem newItemObject){
        if(numberOfItems < max - 1){ //max -1 because if max=100 then lastIndex = 99
            myItems[numberOfItems++] = newItemObject; 
            //++ after the variable increases it AFTER the expression is evaluated
        }
    }
}

So, I think we only have a few more things left to figure out.

TestShoppingCart.java

Looks like the missing piece here is to pull the Cart contents/Quantity/and total from ShoppingCart.java, I put some temp stuff there just to see if I can pull data from that class/method.

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	storeItems[0] = new LineItem("Bananas" ,2.50, 0);
	storeItems[1] = new LineItem("Apples" ,1.50, 0);
	storeItems[2] = new LineItem("Oranges" ,3.50, 0);
	storeItems[3] = new LineItem("Grapes" ,0.50, 0);
	storeItems[4] = new LineItem("Cherries" ,0.10, 0);

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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[addItem]);
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     		System.out.println(output);
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
		System.out.println(total);
    	}
    	else{
    		continue;
    	}      
    	}while(choice != 4);
     
    }
}

ShoppingCart.java

The tasks left here are to create a display for the cart contents and quantities. Also to get the totalcost. How do I get the total cost from the array in the main? we created (storeItems[0] = new LineItem("Bananas" ,2.50, 0);) but how do I multiply the second and third elements of the LineItem array? I think once I get that I can loop through the line items to add the total of each line item.

import java.util.*;
    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;
    	//Set every new cart to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	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; 
	}
	   
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     	String output = "I love fruit";
    	return output;
	//return something; 
    }
    public double getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     	String total = "The total is $5";
    	return total;
	//return totalCost;
    }
}

LineItem.java

no changes here

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
    public void setQuantity(int qty){
	
    }
}

aight well, this is a pretty basic mistake, since its still early today i'm going to let you figure this one out :P

System.out.println(output);
System.out.println(total);

Tell me what is wrong with this code, i'm 100% sure you can figure it out.
What does trying to compile tell you?
What are output and total?
Where are they?

Going to skip over ShoppingCart since it's related to the above mentioned problem.
But for now DON'T change ShoppingCart, thats the only advice i have!

(actually i just notcied getTotalCost returns a String while it's signature announces it's going to return a double, quick fix that and we will get back to it.)

LineItem :

public void setQuantity(int qty){
   //set itemQuantity to qty here , keep returning nothing, just set it's value...
}

They are both Strings and are located in ShoppingCart.java in their own methods. I'm going to assume the problem is that they are not set to be global variables therefor I cannot pass them to the TestShoppingCart class. I'm also not calling the method.. just trying to get the variable...

I saw the error about the type of variable it is ( I was looking into the future ). Today I'm at work and the best tool I have is regular notepad to review.. no compiling until tonight :-(

TestShoppingCart.java

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	storeItems[0] = new LineItem("Bananas" ,2.50, 0);
	storeItems[1] = new LineItem("Apples" ,1.50, 0);
	storeItems[2] = new LineItem("Oranges" ,3.50, 0);
	storeItems[3] = new LineItem("Grapes" ,0.50, 0);
	storeItems[4] = new LineItem("Cherries" ,0.10, 0);

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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[addItem]);
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     		System.out.println(output);
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
		System.out.println(total);
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

ShoppingCart.java

import java.util.*;
    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;
    	//Set every new cart to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	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; 
	}
	   
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     	String output = "I love fruit";
    	return output;
	//return something; 
    }
    public String getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     	String total = "The total is $5";
    	return total;
	//return totalCost;
    }
}

LineItem.java

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
    public void setQuantity(int qty){
	itemQuantity = qty;  //should it be this.itemQuantity?
    }
}

heh, you gave 2 solutions , the right one and a bad one :P

you dont need to make total and output globals within ShoppingCart, they are within their own method because that's the only place they are needed , you ARE returning them... which means in your main instead of calling those variables call the respective methods, and those will "return" you the variables you want.

Edit : and no itemQuantity is just fine without "this." both ways would work , but it isn't required in this case.

LOL.. at least you are honest with me.. I can appreciate that!

So in the main do I call the method or display the method?

System.out.println(getDisplayData());

or just

getDisplayData();

Sorry to keep posting all the code, but it makes more sense when reviewing the changes.

TestShoppingCart.java

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	storeItems[0] = new LineItem("Bananas" ,2.50, 0);
	storeItems[1] = new LineItem("Apples" ,1.50, 0);
	storeItems[2] = new LineItem("Oranges" ,3.50, 0);
	storeItems[3] = new LineItem("Grapes" ,0.50, 0);
	storeItems[4] = new LineItem("Cherries" ,0.10, 0);

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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[addItem]);
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     		System.out.println(getDisplayData());
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
		System.out.println(getTotalCost());
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

ShoppingCart.java

import java.util.*;
    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;
    	//Set every new cart to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	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; 
	}
	   
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     	String output = "I love fruit";
    	return output;
	//return something; 
    }
    public String getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     	String total = "The total is $5";
    	return total;
	//return totalCost;
    }
}

LineItem.java

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
    public void setQuantity(int qty){
	itemQuantity = qty;
    }
}

you print them out, or save them ,anything you want as long as you grab them...

picture everything as expressions , expressions always have a value , sometimes it's null, but anytime you call a method which has a return value, the expression in which the method call is can be seen as that return type...

i might not be very clear here but imagine the expression : cart.getTotal()

getTotal is a method that returns a double so :

//the expression "block" 
{cart.getTotal()} can be seen as {some double value}

//it then makes sense to do some calculations with it like adding taxes : 
{some double value} * 1.15

//this whole calculation can be seen as one resulting expression block worth 1 double value

{ {some double value} * 1.15 } can be seen as {some other double value}

//and if you want to save the resulting value of this whole expression block, just do the same thing you would do with a single double value, since thats what it evaluates to :

double myDouble = 278.59;

//or

double myDouble = cart.getTotal() * 1.15;

TestShoppingCart.java

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	storeItems[0] = new LineItem("Bananas" ,2.50, 0);
	storeItems[1] = new LineItem("Apples" ,1.50, 0);
	storeItems[2] = new LineItem("Oranges" ,3.50, 0);
	storeItems[3] = new LineItem("Grapes" ,0.50, 0);
	storeItems[4] = new LineItem("Cherries" ,0.10, 0);

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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[addItem]);
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
    		//Assuming I will pull this from ShoppingCart.java
     		ShoppingCart.getDisplayData();
    		//Display Cart total
    		//Assuming I will pull this from shoppingCart.java
		ShoppingCart.getTotalCost();
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

ShoppingCart.java

import java.util.*;
    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;
    	//Set every new cart to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	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; 
	}
	   
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     	String output = "I love fruit";
	System.out.println(output);
    	return output; // not sure that I am returning the right thing here.
	//return something; 
    }
    public String getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     	String total = "The total is $5";
	System.out.println(total);
    	return total; // not sure that I am returning the right thing here.
	//return totalCost;
    }
}

LineItem.java

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
    public void setQuantity(int qty){
	itemQuantity = qty;
    }
}

2 posts ago :

System.out.println(getDisplayData());

+

last post :

ShoppingCart.getDisplayData();

you're almost there.

TestShoppingCart.java

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	storeItems[0] = new LineItem("Bananas" ,2.50, 0);
	storeItems[1] = new LineItem("Apples" ,1.50, 0);
	storeItems[2] = new LineItem("Oranges" ,3.50, 0);
	storeItems[3] = new LineItem("Grapes" ,0.50, 0);
	storeItems[4] = new LineItem("Cherries" ,0.10, 0);

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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[addItem]);
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
     		System.out.println(ShoppingCart.getDisplayData());
    		//Display Cart total
    		System.out.println(ShoppingCart.getTotalCost());
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

ShoppingCart.java

import java.util.*;
    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;
    	//Set every new cart to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	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; 
	}
	   
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
     	String output = "I love fruit";
	return output; // not sure that I am returning the right thing here.
	//return something; 
    }
    public String getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     	String total = "The total is $5";
	return total; // not sure that I am returning the right thing here.
	//return totalCost;
    }
}

LineItem.java

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
    public void setQuantity(int qty){
	itemQuantity = qty;
    }
}

looks good, now the next logical step to program would be your "getDisplayData()" method!

Here is my first attempt. Again, kinda confused as how to pass the "storeItems" information to the display, and then further how to format it to make sense.

TestShoppingCart.java

import java.util.*;
     
public class TestShoppingCart {
     
    public static void main(String [] args){
    	//Create an array for 2 shopping carts (might need some guidance on how this will work)
    	ShoppingCart[]myCarts = new ShoppingCart[2];
	for(int i = 0; i < mycarts.length; i++){
		myCarts[i] = new ShoppingCart(size);
	}
    	int choice;
    	String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
    	String enterQuantity = "Please enter a quantity";
  	storeItems[0] = new LineItem("Bananas" ,2.50, 0);
	storeItems[1] = new LineItem("Apples" ,1.50, 0);
	storeItems[2] = new LineItem("Oranges" ,3.50, 0);
	storeItems[3] = new LineItem("Grapes" ,0.50, 0);
	storeItems[4] = new LineItem("Cherries" ,0.10, 0);

	//Prompt the user to select/Initialize a shopping cart
    	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();
	//ensure the user input is either 0 or 1
	int userCart = (cartInput + 1)%mycarts.length);
     
    	//The next section asks the user to select a cart, items and quantities and displays the content
    	do {
    	//read the next input from the user
    	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
    		Scanner 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[addItem]);
		
 
    		
    	}
    	else if (choice == 3){
    		//Display Cart contents
     		System.out.println(ShoppingCart.getDisplayData());
    		//Display Cart total
    		System.out.println(ShoppingCart.getTotalCost());
    	}
    	else{
    		continue;
    	}
     
     
    	}while(choice != 4);
     
    }
}

ShoppingCart.java

import java.util.*;
    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;
    	//Set every new cart to "empty"
    	this.totalCost = 0;
    	//Intialize the array
    	myItems = new LineItem[max];  /* Is this supposed to be myItems[] ? */
	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; 
	}
	   
    }
    
	//a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
	for(int x = 0; x< numberOfLineItems; x++){
		//Append the string with a new lineItem
     		String output = "\n" + newItemObject; /*Sadly I think this is wrong I need so somehow translate the "storeItems" into the lineItems here. and hopefully have the quantity...*/
		output = output + output //Can you do this with strings? 
	}
	String finalOutput = "Your Shopping Cart contains:\nItem\t\tPrice\t\tQuantity\n" + output;
	return finalOutput; // not sure that I am returning the right thing here.
	//return something; 
    }
    public String getTotalCost(){
    	//Total cost is going to equal productName * Quantity for all product names in a cart
     	String total = "The total is $5";
	return total; // not sure that I am returning the right thing here.
	//return totalCost;
    }
}

LineItem.java

import java.util.*;
    class LineItem {
    	String productName;
    	int costPerItem;
    	int itemQuantity;
     
    public LineItem(String productName, int costPerItem, int itemQuantity){
    	this.costPerItem = costPerItem;
    	this.productName = productName;
    	this.itemQuantity = itemQuantity;
    }
    public int getCostPerItem(){
    	return costPerItem;
    }
    public String getProductName(){
    	return productName;
    }
    public int getItemQuantity(){
    	return itemQuantity;
    }
    public void setQuantity(int qty){
	itemQuantity = qty;
    }
}
public String getDisplayData(){
    	//Create a loop to display the contents and quantity of the selected cart
	for(int x = 0; x< numberOfLineItems; x++){
		//Append the string with a new lineItem
     		String output = "\n" + newItemObject; /*Sadly I think this is wrong I need so somehow translate the "storeItems" into the lineItems here. and hopefully have the quantity...*/
		output = output + output //Can you do this with strings? 
	}
	String finalOutput = "Your Shopping Cart contains:\nItem\t\tPrice\t\tQuantity\n" + output;
	return finalOutput; // not sure that I am returning the right thing here.
	//return something; 
    }

close , the idea is good, few mistakes again with variables scope but at least you spotted it as you made it

you dont want to re-declare your output String for every loop your "for" does...

and yes you can do : output = output + output; but if output is "hello" you would jsut end up with "hellohello"

you want to :

-declare a string and set it to an empty string
-set the initial stuff like : "Your Shopping Cart contains:\n"
-start looping, (your arguments for the loop were good , 0 to numberOfItems)
(numberOfLineItems doesn't exist tho, if you check your global variables for ShoppingCart its numberOfItems)
-then apped to the string the info of every Item in "myItems" at index "x" ( output += "item(" + x + ") is : " + myItems[x].productName )
(you can do "myItems[x].productName" for now because the variable productName in LineItem is public (by default) but you will eventually want to set it to private and program a "getName()" method, same for all other LineItem fields)
-Stop looping
-append totalCost to the output string
-return output


heres an example of my ShopManager class i've been building as i help you with yours. dont copy paste it because mine uses ArrayLists but it'll give you a good idea of the structures behind the string building process :

public String toString(){
    String s = "";
    s += "Shop contains " + carts.size() + " Carts :";
    s += "\n\t-------------------------------------------------------";
    int i = 0;
    for (Cart c : carts) {
        s += "\n\tCart " + i + " :";
        for(CartItem ci : c.myCart){
            s += "\n\t\tItem : " +  String.format("%3d", ci.iQuantity) + " x " + 
                 String.format("%10s",ci.myItem.sProductName) + " @ " + 
                 String.format("%6s",df.format(ci.myItem.dCost)) + " = " + 
                 String.format("%7s",df.format(ci.myItem.dCost * ci.iQuantity))
            ;
        }
        s += "\n\tTotal : " + df.format(c.dTotalCost);
        s += "\n\t-------------------------------------------------------";
        i++;
    }
    return s;
}

df is a decimal format; DecimalFormat df = new DecimalFormat("###0.00$");

and like you i haven't set all my fields to privates yet so it's actually not perfect yet but it produces this output :

Before I get too far gone or too frustrated trying to fix the errors. I've never used array lists so I don't to get too confused. Here is what I have some questions in the code. and the new loops look kinds sloppy. My appologies!

TestShoppingCart.java

import java.util.*;
     
public class TestShoppingCart { 
    public static void main(String [] args){
        //Create an array for 2 shopping carts (might need some guidance on how this will work)
        ShoppingCart[]myCarts = new ShoppingCart[2];
        for(int i = 0; i < myCarts.length; i++){
            myCarts[i] = new ShoppingCart(size);
        }
        int choice;
        String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
        String enterQuantity = "Please enter a quantity";
        storeItems[0] = new LineItem("Bananas" ,2.50, 0);
        storeItems[1] = new LineItem("Apples" ,1.50, 0);
        storeItems[2] = new LineItem("Oranges" ,3.50, 0);
        storeItems[3] = new LineItem("Grapes" ,0.50, 0);
        storeItems[4] = new LineItem("Cherries" ,0.10, 0);
     
        //Prompt the user to select/Initialize a shopping cart
        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();
        //ensure the user input is either 0 or 1
        int userCart = ((cartInput + 1)%myCarts.length);
     
        //The next section asks the user to select a cart, items and quantities and displays the content
        do {
            //read the next input from the user
            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[addItem]);

            }
            else if (choice == 3){
                //Display Cart contents
                System.out.println(ShoppingCart.getDisplayData());
                //Display Cart total
                System.out.println(ShoppingCart.getTotalCost());
            }
            else{
                continue;
            }
        }while(choice != 4);  
    }
}

ShoppingCart.java

import java.util.*;
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;
        //Set every new cart to "empty"
        this.totalCost = 0;
        //Intialize the array
        myItems = new LineItem[max]; /* Is this supposed to be myItems[] ? */
        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;
        }
     
    }
     
    //a method printing out the carts contents with the total cost of all the items/quantities
    public String getDisplayData(){
        DecimalFormat df = new DecimalFormat("###0.00$");
        String output = "";
        output += "Your shopping cart contains: \n";
        output += "\n\t----------------------------------------------";  
        int i = 0;
        //Create a loop to display the contents and quantity of the selected cart
        for(ShoppingCart c/* Not sure what this translates to for me yet Assuming its the Cart number? Is this where you were saying you were using array lists? */ : myCarts){
            output += "\n\tCart" + i + " :";
            for(int x = 0; x < numberOfItems; x++){
                //Append the string with a new lineItem
                output += "\n\tItem : " + String.format("%3d", myItems.itemQuantity) + " x " +
                    String.format("%10s", myItems.productName) + " @ " + 
                    String.format("%6s", df.format(myItems.costPerItem)) + " = " +
                    String.format("%7s", df.format(myItems.costPerItem * myItems.itemQuantity))
                    ;
            }
            output += "\n\tTotal : " + df.format(myCarts.totalCost);
            output += "\n\t------------------------------------------";
            i++;
        }
        
        return output; // not sure that I am returning the right thing here.
        //return something;
    }
    public double getTotalCost(){
        //Total cost is going to equal productName * Quantity for all product names in a cart
        double total = 5;
        totalCost = total; // not sure that I am returning the right thing here.
        return totalCost;
    }
}

LineItem.java

import java.util.*;
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;
    }
}

myItems = new LineItem[max]; /* Is this supposed to be myItems[] ? */

nope, that line is fine.

for(ShoppingCart c/* Not sure what this translates to for me yet Assuming its the Cart number? Is this where you were saying you were using array lists? */ : myCarts){

yup, thats a foreach Cart c in myCarts, and its why i said not to copy paste, you would have understood better starting your loop structure from scratch. i posted mine for the looping structure not the output formatting so drop the decimal format and String.Format(...)s for now and try to focus on just having a working loop that appends to your string the correct information :

myItems.productName

see the reason these are giving you errors is that "productName" is a variable in LineItem, you can access it from instances of LineItem Objects, but in this case you are trying to access it from a LineItem array Object. What you need first is to change the foreach into a for loop, that will allow you to drop my "int i = 0" since i was using that as a counter for display reasons. (I usualy have my for loops use "i" and if another for is embeded that one will use "j" , but that's just how i learned)

what you will need is something like this :

for(int i = 0 ; i < ... ; i++){
   output += "\n" + myItems[i].productName;
   ...
}

return output; // not sure that I am returning the right thing here.

yes, you are, output is the string we have been building, once we reach the end of the method, it should be all formated and ready to go, so you just need to return that :)

public double getTotalCost(){
//Total cost is going to equal productName * Quantity for all product names in a cart
double total = 5;
totalCost = total; // not sure that I am returning the right thing here.
return totalCost;
}

nope, you will not need to calculate the total cost in your get method, if everytime you add an item, you update the total :

public void addItem(LineItem newItemObject){
    if(numberOfItems < max - 1){ //max - 1 due to the actual index
        myItems[numberOfItems++] = newItemObject;

        //this line keeps te totalCost up to date;
        totalCost += newItemObject.costPerItem * newItemObject.itemQuantity;
    }
}

public double getTotalCost(){
   //just return the value since it's up to date
   return totalCost;
}
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.