When i enter a value (202 cents) it should give me this amount either in 5 cents or 2 cents coins. Everything works fine in my code, but the output is given as System.out statement in the TwoCent and FiveCent class it self. WHat i want to do is to return the number of 5 cents and 2 cents coins so that i could capture and display it in my Test class.
Note: In the code below, the number of 2 and 5 cents coins are returned(displayed/printed to the console) from that class it self. but i want to modify the code so those methods (in the TwoCents and FiveCents classes) will return and int may be with the amount of coins. I should be able to get these values and display from the Test class only.
public abstract class Coin {
protected Coin co;
public abstract void finCoin(Money m);
public void setnext(Coin c) {
co = c;
}
}
public class TwoCent extends Coin{
@Override
public void finCoin(Money m) {
if(m.getChange()%2==0){
System.out.PrintLn("Return "+ m.getChange/2);
else {
int remainngCoins=m.getChange()*((m.getChange/2)*2));
m.setChange(remainngCoins);
co.setnext(m);
}
}
public class FiveCent extends Coin{
@Override
public void finCoin(Money m) {
if(m.getChange()%5==0){
System.out.PrintLn("Return "+ m.getChange/5);
else {
int remainngCoins=m.getChange()*((m.getChange/5)*5));
m.setChange(remainngCoins);
co.setnext(m);
}
}
public class Test {
public void showcoin(){
Coin f = new FiveCent();
Cash t = new TwoCent();
f.setSuccessor(t);
Money cr = new Money(200);
f.finCoin(cr);
}
}
Interesting, the definition of the abstract class Coin already describes that the method finCoin(Money m) must return an int. But in neither of the implementations (FiveCent and TwoCent) this value gets returned.
What you do is replace the System.out.println stuff with a return like this:
instead of
** you do **