I have to create two classes one class that accepts an object, stores the object
ID: 3731377 • Letter: I
Question
I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object.
Here is my first class named "ShoppingList":
import java.util.*;
public class ShoppingList
{
private ShoppingItem [] list;
private int amtItems = 0;
public ShoppingList()
{
list=new ShoppingItem[8];
}
public void add(ShoppingItem item)
{
if(amtItems<8)
{
list[amtItems] = ShoppingItem(item);
amtItems++;
}
else
System.out.println("Your item's have exceeded the amount of items that can fit in the bag.");
}
public void add(String item, int amt, double cost)
{
if(amtItems<8)
{
list[amtItems] = new ShoppingItem(item, amt, cost);
amtItems++;
}
else
System.out.println("Your item's have exceeded the amount of items that can fit in the bag.");
}
public double getTotalCost()
{
double totalCost = 0;
for(int i = 0; i totalCost+=list[i].getCost();
return totalCost;
}
public String toString()
{
for(int i = 0;i {
if(i==0)
System.out.println("This shopping list has " + amtItems + " shopping items: " + list[i].toString());
else
System.out.println(", " + list[i].toString);
}
return"";
}
}
Here is my second class called "ShoppingItem":
public class ShoppingItem
{
private String itemName;
private int amount;
private double pricePU;
public ShoppingItem(String x, int y, double z)
{
this.itemName = x;
this.amount=y;
this.pricePU=z;
}
public double getCost()
{
return this.amount*this.pricePU;
}
public void setQuantity(int x)
{
this.amount=x;
}
public String toString()
{
return (this.amount + " " + this.itemName);
}
}
The teacher created a test program to test our algorithms, the constructors in ShoppingItem seem to work. I bolded the area's in ShoppingList that seem to be disagreeing. The second add function was me playing around with different ideas. Her exact line is "g.add( new ShoppingItem("Tissues", 4, 2.30));". So since this part works "ShoppingItem go = new ShoppingItem("Milk", 2, 3.75);" I think my costructor is fine in ShoppingItem. I think my problem is I don't know how to accept an object in my shopping list constructor and save it to my array of objects.
Oh, my .toString function in ShoppingList also has a compiler error if you get a chance to look at it, but I figure I can probably figure that one out.
Here is the teacher's client:
/* Use this client program to test your class implementations
*
*/
import java.util.*;
public class ShoppingListClient {
public static final int N = 1;
public static final double DISCOUNT = 0.1;
public static void main (String [] args) {
System.out.println("Creating ShoppingList");
ShoppingList g = new ShoppingList();
System.out.println("Creating new ShoppingItem");
ShoppingItem go = new ShoppingItem("Milk", 2, 3.75);
System.out.println("Changing the quantity to 3");
go.setQuantity( 3 );
System.out.println("The total cost should be 11.25. It is actually: " + go.getCost() );
System.out.println("Adding ShoppingItems to ShoppingList");
g.add( go );
g.add( new ShoppingItem("Tissues", 4, 2.30));
g.add (new ShoppingItem ("Toothpaste", 1, 4));
g.add (new ShoppingItem ("Eggs", 12, 0.50));
g.add (new ShoppingItem ("Apples", 5, 2.99));
g.add (new ShoppingItem ("Bananas", 4, 0.99));
System.out.println (g.toString());
System.out.println("The total cost of shopping list should be 49.36. It is actually: " + g.getTotalCost() );
Scanner input = new Scanner (System.in);
System.out.print ("Enter the customer name: ");
String name = input.next();
for (int i=0; i<N; i++) {
System.out.print ("Enter the shopping item: ");
String shoppingItemName = input.next();
System.out.print ("Enter the quantity: ");
int quan = input.nextInt();
System.out.print ("Enter the price per unit: ");
double ppu = input.nextDouble();
g.add (new ShoppingItem (shoppingItemName, quan, ppu));
System.out.println (g.toString());
}
}
}
Explanation / Answer
import java.util.*;
public class ShoppingList
{
private ShoppingItem [] list;
private int amtItems = 0;
public ShoppingList()
{
list=new ShoppingItem[8];
}
public void add(ShoppingItem item)
{
if(amtItems<8)
{
// here you are already passing object of class ShoppingItem and you have an array of
// ShoppingItem type, so you just need to assign ShoppingItem class object item into
// i'th index of array
list[amtItems] = item;
amtItems++;
}
else
System.out.println("Your item's have exceeded the amount of items that can fit in the bag.");
}
public void add(String item, int amt, double cost)
{
if(amtItems<8)
{
list[amtItems] = new ShoppingItem(item, amt, cost);
amtItems++;
}
else
System.out.println("Your item's have exceeded the amount of items that can fit in the bag.");
}
public double getTotalCost()
{
double totalCost = 0;
for(int i = 0; i<amtItems; i++)
totalCost+=list[i].getCost();
return totalCost;
}
public String toString()
{
for(int i = 0;i<amtItems;i++) {
if(i==0)
System.out.println("This shopping list has " + amtItems + " shopping items: " + list[i].toString());
else
System.out.println(", " + list[i].toString()); // in .toString method parenthesis were missing
}
return"";
}
}