I am working on this assignment in my java class, and am running into a few problems.
Basically there are 2 classes and a main/client program that tests the classes.
there is 1 superclass and a subclass that extends the superclass..
I have 2 constructors in my superclass but when i try to access them from my subclass I receive some errors, I believe I am supposed to create a constructor in my subclass and call the constructor using
super(arguments);
If someone could please explain what exactly I am doing wrong it would be greatly appreciated..
Here are my 3 files total..
public class GroceryItemOrder {
private String myName; //name of grocery item
private int myQuantity; //number of items
private double myPrice; //price of each item
private double total; //total price of items
//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)
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.io.*;
import java.util.*;
public class GroceryList extends ArrayList<GroceryItemOrder>{
//my fail attempt at using constructor from GroceryItemOrder class
public GroceryList(String name, int quantity, double unitPrice){
super(name,quantity,unitPrice);
}
public void add()
{
Scanner inp = new Scanner(System.in);
GroceryItemOrder tempGIO = new GroceryItemOrder();
System.out.print("Enter item name: ");
tempGIO.setName(inp.next());
System.out.print("Enter item price (for 1): ");
tempGIO.setUnitPrice(inp.nextDouble());
System.out.print("Enter item quantity: ");
tempGIO.setQuantity(inp.nextInt());
this.add(tempGIO);
}
public double getTotalCost()
{
double dblTotalCost = 0;
for(int i=0; i< this.size(); i++)
{
dblTotalCost += this.get(i).getCost();
}
return dblTotalCost;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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);
}
}
}
Any suggestions would be great!