Working on this program that uses 2 classes and a client program to test them,
I keep getting this error pointing at my constructor, not sure what i am doing wrong but could use some assistance if anyone is able to offer,
the error i am getting is:
cannot find symbol, constructor GroceryList() line 25
GroceryList tempItemOrder = new GroceryList();
here are the files...
import java.util.*;
public class GroceryList extends GroceryItemOrder {
private int size = 10;
int[] items = new int[size];
//default constructor to handle normal data
public GroceryList(String name, int quantity, double unitPrice) {
super(name,quantity,unitPrice);
}
public void add(GroceryItemOrder g){
GroceryList tempItemOrder = new GroceryList();
this.add(tempItemOrder);
}
public double getTotalCost()
{
double dblTotalCost = 0;
for(int i=0; i< items.length; i++)
{
dblTotalCost += this.getCost();
}
return dblTotalCost;
}
public String toString(){
return String.format("%s: %d @ $%.2f",myName,myQuantity,getCost());
}
}
public class GroceryItemOrder {
protected String myName; //name of grocery item
protected int myQuantity; //number of items
protected double myPrice; //price of each item
//default constructor to handle normal data
public GroceryItemOrder(String name, int quantity, double unitPrice){
myName = name;
myQuantity = quantity;
myPrice = unitPrice;
}
//extra constructor to work with the setQuantity
// and setUnitPrice methods when only name is available
public GroceryItemOrder(String name){
myName = name;
}
// Precondition: myPrice and myQuantity > 0
// Returns: total cost
public double getCost(){
//calculates final price of item(s)
double total = myPrice * myQuantity;
//returns the total price of item(s)
return total;
}
// Parameter: quantity
// Precondition: quantity > 0
public void setQuantity(int quantity){
myQuantity = quantity;
}
// Parameter: unitPrice
// Precondition: unitPrice > 0
public void setUnitPrice(double unitPrice){
myPrice = unitPrice;
}
public void setName(String name){
myName = name;
}
// Returns: string output
public String toString(){
//returns the final output string, formatted
return String.format("%s: %d @ $%.2f",myName,myQuantity,getCost());
}
}
import java.util.*;
public class TestGroceryList{
public static void main (String [] args) {
GroceryList list = new GroceryList();
// The empty grocery list should print out as a blank
// and its total cost should be $0
System.out.println("Grocery List:");
System.out.println(list);
System.out.printf("Total Cost: $%.2f\n",list.getTotalCost());
System.out.println();
// fill the list with items 10 items
fillList(list,10);
// This next item should not be added
list.add(new GroceryItemOrder("celery", 2, 1.67));
System.out.println("Grocery List:");
System.out.print(list);
System.out.printf("Total Cost: $%.2f\n",list.getTotalCost());
System.out.println();
// make a new list that is not "full"
list = new GroceryList();
fillList(list, new Random().nextInt(7)+1);
System.out.println("Grocery List:");
System.out.print(list);
// just the items should print, no NULLs!
System.out.printf("Total Cost: $%.2f\n",list.getTotalCost());
System.out.println();
}
public static void fillList(GroceryList list, int numItems) {
Random rand = new Random();
for (int i = 0; i < numItems; i++) {
GroceryItemOrder g = new GroceryItemOrder("g" + (i+1));
g.setQuantity(rand.nextInt(5) + 1);
double unitPrice = rand.nextDouble() * 1000;
unitPrice = (int) unitPrice / 100.0;
g.setUnitPrice(unitPrice);
list.add(g);
}
}
}