Question

I should begin by saying that I am relatively new to java and programming in general. I have searched for a few answers relating to this but none seem to answer my problem. The closest answer I could find was this: java calling a method from another class. The reason this answer didn't help was because I already understand that an instance needs to be initialised to call a method from a seperate class. The problem I'm having is, how can i call a method to specify which object I want to read data from?

The code I have at the moment is. The method from the Interface Class:

private void writeOutput()
//The user enters the name of a product, if the product doesn't exist, 
//the user is prompted to enter a product that does exist
//Then displays the data stored for said product
{             
    String productName;
    System.out.println("Please enter a product: ");
    productName = console.next();
    matesStore.isAProduct(productName);  //Checks whether the product exists
    while (!matesStore.isAProduct(productName))
    //If the product doesn't exist, the user must enter a valid product
    {
        System.out.println("That is not a valid product.\n" + "Please choose a product: ");
        productName = console.next();
    }
    while (matesStore.isAProduct(productName))
    {
        System.out.println("The records of " +productName+ " are:");
        System.out.println("Demand rate: "+matesStore.readDemand(productName));
        System.out.println("Setup Cost: "+matesStore.readSetup(productName));
        System.out.println("Unit Cost: "+matesStore.readUnit(productName));
        System.out.println("Inventory Cost: "+matesStore.readInventory(productName));
        System.out.println("Selling Price: "+matesStore.readPrice(productName));
    }
    continueOption();
}

One of the read methods from the Store class:

public String readDemand(String nameOfProduct)
{
    String productDemandRate;
    productDemandRate = product1.getDemand() + "."; 
    //!!!!This line is the problem. product1 is here just so I could 
    //test that the method actually works



    return productDemandRate;       
}

And the accessor method from the Product class:

public String getName()
{
    return name;        
}

So the question I have is what would I replace product1 with in the line productDemandRate = product1.getDemand() + "."; so that it displays the correct data?

Sorry if my questions is a bit vague or perhaps even blindingly obvious. I've been trying to solve this on my own for the past couple of hours with no luck. So any help or advice would be greatly appreciated.

Cheers :)

P.S This is for a University project where arrays are strictly not allowed in case you're wondering why I'm not using one.

Was it helpful?

Solution

You have a Product class, which allows you to create Product objects to hold the data that you are trying to use here. Then, you could simply alter your method like this:

public String readDemand(Product product1){
  ...
}

Instead of using the name of the product from the input, create a Product with a name property that holds the name of the product. Pass in your product to the methods so that you can use them the way you want to here.

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