0

I am trying to create a Java class with certain number of pizzas that decreases in number

if someone steals it.

I have two classes.

class House where pizza is,

public class House { private static int totalPizzas; public House() { totalPizzas = totalPizzas; } public int getTotalPizzas() { return totalPizzas; } public static void setTotalPizzas(int totalPizzas) { totalPizzas = totalPizzas - Thief.stealPizza(House stolenPizza); } } 

and class Thief that steals the pizza.

public class Thief { private String name; private int age; public Thief() { name = "abc"; age = 11; } public static void stealPizza(House stolenPizza) { ??????? } } 

My main concern is the ??????? part where I feel like I should set stolenPizza to certain

integers but

stolenPizza = 1; 

certainly does not work.

Could someone give me a bit of advice on how I should approach this?

Thank you very much for reading.

3
  • Make a main class that have object of both class. You probably don't need any static methods besides main Commented Sep 23, 2019 at 22:15
  • Thief.stealPizza(House stolenPizza) is not valid syntax for a method call. And you can't call a method that returns void as part of an arithmetic expression Commented Sep 23, 2019 at 22:15
  • 1
    Is there a reason that some of these methods are static? Commented Sep 23, 2019 at 22:20

2 Answers 2

1

One way to do it would be to do something like:

 public class Thief { private String name; private int age; public Thief() { name = "abc"; age = 11; } public static void stealPizza() { House.setTotalPizzas(House.totalPizzas - 1); } } public class House { private static int totalPizzas; public House() { totalPizzas = totalPizzas; } public int getTotalPizzas() { return totalPizzas; } public static void setTotalPizzas(int totalPizzas) { House.totalPizzas = totalPizzas; } } 
Sign up to request clarification or add additional context in comments.

6 Comments

Though static methods make no sense.
I can see the value. There is no relation between the house and the thief. What if a Thief wants to steal jewels? What they donate pizza? A better option might be to create a singleton to manage the pizza count, but again, this is just one way to do it. Also, without more context who is to say it isn't the right option; let alone suggest a new way.
For thief maybe (Assuming there is only one thief) but not for House. A thief can steal pizzas from multiple homes
Do you know there are multiple homes? Maybe it's a "Pizza house" restaurant.
Fair enough but shouldn't getter be static as well?
|
0

Your constructor is missing something if I understand your code right:

Your code

 public House() { totalPizzas = totalPizzas; } 

will set the amount of totalPizzas on itself without assigning any "real" integer value to it. Try

 public House(int totalPizzas) { totalPizzas = totalPizzas; } 

so that you actually can assign a number of Pizzas to the house when calling the constructor, for example:

House house = new House (12); 

if you want to have 12 Pizzas in the house.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.