user3482500
user3482500

Reputation: 5

java link two classes together

This is a very simple java code for shop, allowing user to purchase few items.

In another class (tamagotchi pet class), I have created a feed() method, which one of the function will be to --food to show usage of the items purchased in the shop.

How do I link both classes to add this function to my feed() method?

Shop Class: 

public class Shop {


int balance = 80;
int reward = 100;
int dumplingsMEAL = 1;
int beddingPACK = 1;
int toy = 1;
int dumplingsPrice = 20;
int beddingPrice = 40;
int toyPrice = 10;
int food = 1;
int bedding = 1;
int toys = 1;

public void earnReward () {
    balance = balance + reward;
}

public double getBalance() {
    return balance;
}

public void purchaseDumplings() {
    balance = balance - dumplingsPrice;
    ++food;
}

public void purchaseBedding() {
    balance = balance - beddingPrice;
    ++bedding;
}

public void purchaseToy() {
    balance = balance - toyPrice;
    ++ toys;
}
}

Upvotes: 0

Views: 3270

Answers (1)

Suprabhat Biswal
Suprabhat Biswal

Reputation: 3216

In tamagotchi pet class, Create a new Method Feed() and here create the Object Of Shop class. Now with the help of reference variable you can access all Shop class member function.

public void feed() {
    Shop shop = new Shop();
    shop.earnReward();
    shop.getBalance();
    shop.purchaseDumplings();
    shop.purchaseToy();
}

Upvotes: 1

Related Questions