I'm a beginner and have tried searching but nothing is coming up, apologies. I have 2 classes, I don't know how to link the rewardCoins (oneCoin/twoCoin/threeCoin) so that if called, it adds coins to the Bag inventory. The return is probably incorrect as well.
public class Bag {
private int oneCoin;
private int twoCoin;
private int threeCoin;
//etc
}
public class Thief {
public Thief(String name, int level) {
this.name = name;
this.health = level * 100;
this.level = level;
}
//problem code
public Bag rewardCoins(){
oneCoin = (int)(level-Math.random()*level/6);
twoCoin = (int)(level-Math.random()*level/2);
threeCoin = (int)((level-level/2)-Math.random()*(level-level/2));
return rewardCoins();
}
}
Not looking for immediate help, I just don't know what I should be reading up on to figure it out.
Edit: I may have explained it poorly - I can't use setters, as there are already things inside the Bag inventory. I want to add the coins to the inventory when a Thief is "defeated". Like dropping loot. I don't know how to access the private fields one/two/threeCoin in Thief to add them to the other class. They are 2 different classes, Thief.java and Bag.java
CodePudding user response:
You may want something like this:
public class Thief {
private final String name;
private final int health;
private final int level;
public Thief(String name, int level) {
this.name = name;
this.health = level * 100;
this.level = level;
}
public Bag rewardCoins(Bag bag){
int oneCoin = bag.getOneCoin();
int twoCoin = bag.getTwoCoin();
int threeCoin = bag.getThreeCoin();
oneCoin = (int)(level-Math.random()*level/6);
twoCoin = (int)(level-Math.random()*level/2);
threeCoin = (int)((level-level/2)-Math.random()*(level-level/2));
Bag result = new Bag();
result.setOneCoin(oneCoin);
result.setTwoCoin(twoCoin);
result.setThreeCoin(threeCoin);
return result;
}
}
or this:
public class Thief {
private final String name;
private final int health;
private final int level;
private Bag bag;
public Thief(String name, int level) {
this.name = name;
this.health = level * 100;
this.level = level;
}
public Bag rewardCoins(){
int oneCoin = bag.getOneCoin();
int twoCoin = bag.getTwoCoin();
int threeCoin = bag.getThreeCoin();
oneCoin = (int)(level-Math.random()*level/6);
twoCoin = (int)(level-Math.random()*level/2);
threeCoin = (int)((level-level/2)-Math.random()*(level-level/2));
Bag result = new Bag();
result.setOneCoin(oneCoin);
result.setTwoCoin(twoCoin);
result.setThreeCoin(threeCoin);
return result;
}
}
CodePudding user response:
Well it depends. Looking at your code I can suppose that you want the thief to reward coins to the inventory which is shared among thieves. In that case you could have it as a variable of the thief and pass it as a parameter on the constructor. This way all thieves will be able to access the same Bag object.
public class Bag {
private int oneCoin;
private int twoCoin;
private int threeCoin;
//getters and setters
}
public class Thief {
private Bag bag;
public Thief(String name, int level,final Bag bag) {
this.name = name;
this.health = level * 100;
this.level = level;
this.bag=bag;
}
public void rewardOneCoin(){
bag.setOneCoin(bag.getOneCoin() (int)(level-Math.random()*level/6))
}
// Appropriate functions for two and three coins
}
If you can give me a bit more context I may cal provide you with a bit more help