0

My getter wants the method it's getting to have a static method but if I do that it will break my code is there any way around this?. Language Java.

This is the line in my test class that wants to get the method in my vending class.

System.out.println("Total Sales for both machines today $"+ VendingMachine.getTotalsSales());

This is my getter method that takes the array I created from the string provided in the test class that put the items into an array. I want to be able to keep this array that I have created while also getting the total sales.

public double getTotalsSales() {
    totalSales += availableItems[this.selectItem].price;
    return totalSales;
}

Is there any way I can keep my array and with also being able to grab the total sales from the tester class?

  • Welcome to Stack Overflow. Please provide a [mcve] so we can help with your exact problem. Also be sure to include the error message and indicate what line is causing it. – Code-Apprentice Sep 08 '18 at 14:58
  • 1
    your sentences are so ambiguous and hard to understand. please make them a little more understandable. Moreover, as @Code-Apprentice mentioned, please include your code here. – Amin Heydari Alashti Sep 08 '18 at 15:01

2 Answers2

1

It sounds like VendingMachine is the name of your class, not the name of an instance of your class. Calling the method getTotalsSales on the class instead of an instance is trying to call it in a static context, which would be why it asks for it to be static.

Create an instance of the class and call it on that instead!

tychon
  • 540
  • 4
  • 9
  • That makes sense now, I do have two instances being drinksVending and snacksVending from the VendingMachine class so all I have to do is specify that I want to add these two vending machines together and that will work. Anyways Thank you! – Sally Trivone Sep 08 '18 at 15:15
0

I assume that VendingMachine is the class name. You need to first create an instance of this class:

VendingMachine machine = new VendingMachine();

Then call the method on that instance:

machine.getTotalsSales()

For more details, I suggest you read a tutorial on the difference between classes and objects and how they are related.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • That makes sense now, I do have two instances being drinksVending and snacksVending from the VendingMachine class so all I have to do is specify that I want to add these two vending machines together and that will work. Anyways Thank you! – Sally Trivone Sep 08 '18 at 15:15
  • @SallyTrivone Without more of your code, I can't say exactly. However, `getTotalsSales()` will only return the total for a single vending machine. Note that your current implementation isn't correct. At least, I assume that `getTotalsSales()` is supposed to add up all of the sales for all items in the machine. Your current code doesn't do this. – Code-Apprentice Sep 08 '18 at 16:53