How can OOP be implemented to call an instance method from one class to be stored in an array in another instance method in a different class?

StackOverflow https://stackoverflow.com/questions/15233527

  •  18-03-2022
  •  | 
  •  

Question

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.

Was it helpful?

Solution

If I understand your meaning, you want the GroceryList to store different GroceryItemOrder objects.

You're close to having the solution in your code. Look here, however:

public void add(GroceryItemOrder_test item) {
    GroceryItemOrder_test[] GroceryList = new GroceryItemOrder_test[10];
    GroceryList[1] = item;
    System.out.println(GroceryList[1]);
}

If you trace how this works, then each time you enter the add() method, a new array will be created with ten empty elements. You will immediately lose all references to this when the method exits because it will be out of scope and eventually garbage collected.

To get around this, you need to use a variable which stays in scope. I recommend an instance variable for your GroceryList class, like this:

public class GroceryList {

    private GroceryItemOrder[] list = null;

    public GroceryList() {
        list = new GroceryItemOrder[10];
    }

    public void add(GroceryItemOrder item) {
        for(int i=0; i<list.length; i++) {
            if(list[i] == null) {
                list[i] = item;
                System.out.println(item);
                break;
            }
        }
    }
}

Now in this add() method we only look at the already existing and persisting list variable, rather than creating a new one. We loop through it to find an available slot (if there is one), and then perform the rest of your processing. The break statement is necessary so that every item in the list does not get set to the one item you are adding.

Then your GroceryClient class can create GroceryItemOrder objects as it pleases and use the add() method from the GroceryList (because it's public).

Note that this would indeed be much easier with an ArrayList, contrary to what you say in the closing comments of your post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top