user10344621
user10344621

Reputation: 21

Java calling method in another class

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.

Upvotes: 0

Views: 70

Answers (2)

GitPhilter
GitPhilter

Reputation: 171

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.

Upvotes: 0

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

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;
   }    
}

Upvotes: 1

Related Questions