Reputation: 5
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
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