To further develop an understanding of my question:
I have a problem that I am working on in my Java textbook where it asks me to write a class named GroceryList that represents a list of items to buy from the market, and also another class named GroceryItemOrder that represents a request to purchase a particular item in a given quantity (for instance: four units of oranges). Upon further instruction, I am to develop a client that tests and prints the results of all implemented methods.
Disclaimer: I am not asking ANYONE to do this problem for me. I am only trying to ask for a mentor to clarify how I may implement an instance method from my GroceryItemOrder class into my GroceryList class upon instruction from my client GroceryListClient.
Code from my GroceryList class:
public class GroceryList_test {
public GroceryList_test() {
GroceryItemOrder_test[] GroceryList = new GroceryItemOrder_test[10];
}
public void add(GroceryItemOrder_test item) {
GroceryItemOrder_test[] GroceryList = new GroceryItemOrder_test[10];
GroceryList[1] = item;
System.out.println(GroceryList[1]);
}
/*public double getTotalCost(GroceryItemOrder_test item) {
}*/
}
Under the instance method add(GroceryItemOrder_test item):
- Adds the given item order (as instructed by GroceryListClient when accessing the GroceryItemOrder class) to this list if the list has fewer than 10 items (do not worry about this element; I can do this with an if statement and null exception).
I want this method to load the item order from the GroceryItemOrder_test (String name, int quantity, double priceperunit) into an array dynamically each time I instruct it to do so in the GroceryListClient client, which will be accessed later to compute the total cost in the TotalCost() instance method below.
Code from my GroceryItemOrder class:
public class GroceryItemOrder_test {
String itemID;
int NumberofItems;
double priceperunit;
public GroceryItemOrder_test(String name, int quantity, double pricePerUnit) {
this.itemID = name;
System.out.println("Item: " + itemID);
this.NumberofItems = quantity;
System.out.println("Quantity: " + NumberofItems);
this.priceperunit = pricePerUnit;
System.out.println("Price per unit: $" + priceperunit);
}
public double getCost() {
return ((NumberofItems * priceperunit) * 100.0) / 100.0;
}
public void setQuantity(int quantity) {
}
}
Under the public class public GroceryItemOrder_test(String name, int quantity, double pricePerUnit):
- Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit.
I want this method to be loaded into an array in the instance method add(GroceryItemOrder_test item) from the GroceryList class.
Code from my GroceryListClient client:
public class GroceryListClient {
public static void main(String[] args) {
GroceryList_test addorder = new GroceryList_test();
GroceryItemOrder(addorder);
}
public static void GroceryItemOrder(GroceryList_test addorder) {
GroceryItemOrder_test Oranges = new GroceryItemOrder_test("oranges", 3, 2.46);
System.out.println("Total cost of " + Oranges.itemID + " = $" + Oranges.getCost());
addorder.add(Oranges);
System.out.println();
GroceryItemOrder_test Grapes = new GroceryItemOrder_test("grapes", 15, 0.55);
System.out.println("Total cost of " + Grapes.itemID + " = $" + Grapes.getCost());
addorder.add(Grapes);
}
}
Under the method GroceryItemOrder(GroceryList_test addorder):
GroceryItemOrder_test Oranges = new GroceryItemOrder_test("oranges", 3, 2.46);
System.out.println("Total cost of " + Oranges.itemID + " = $" + Oranges.getCost());
addorder.add(Oranges);
Specifically the statement addorder.add(Oranges);:
This should add the item as an 'object' specified by "item" into an array under the add(GroceryItemOrder_test item) method.
These are the conditions in which I cannot figure out how to perform. Please give any help if possible.
DO NOT SUGGEST AN ArrayList because I haven't gotten to this yet, and upon further research I think it will not work easily for this purpose.