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);

}

}

}

new GroceryList(); looks for a constructor with no parameters, but the only constructor you have defined is
public GroceryList(String name, int quantity, double unitPrice)

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);

}

}

}

to add to what jamescherrill said: you also have another constructor: GroceryList(String name)

Edited 4 Years Ago by DavidKroukamp: n/a

also, why exactly is your List class extending your Item class?
by any logic, your List class would have a List which can contain only elements of the Item type.

He does? Which line number is that defined in?

sorry you are correct looking at the OP's original code and mine, i confused the class name with GroceryItemOrder :O in my netbeans so it was showing me that constructor as titled GroceryList intsead of GroceryItemOrder and vice versa!!

Edited 4 Years Ago by DavidKroukamp: n/a

You need to create a GroceryList constructor like this one below.

public GroceryList() {
 name = "Milk";
 quantity = 20;
 unitPrice = 3.00;
}

No, not really, that's a way of stopping the compiler error, but it's just making the design error even worse. stultuske already made the point - the GroceryList class should just have a list of GroceryItemOrders. It should not have instance variables for name/quantity/unitPrice at all, and therefore no constructor that sets those variables.

Best guess (without studying it properly):
In TestGroceryList you create an empty GroceryList, then create a load of GroceryItemOrders and add those to the list. Then in GroceryList there will be other methods (eg total cost) that access the contents of the list.

please this thread as solve if it solve your problem.

there's not really any need to revive the thread, unless you're actually adding something to it.

It looks like we have a parasite here - making a post to a thread in the hope of getting a "solved thread" to his credit without actually helping solve it.

well ... at least he wants the OP to 'please the thread' ...
considering the feelings of your threads is so important these days :)

This article has been dead for over six months. Start a new discussion instead.