class Money{
private int cents;
private int dollars;
public Money(){
this.cents=0;
}
public Money(Scanner sc){
String token=sc.next();
int dot=token.indexOf(".");
this.cents=Integer.parseInt(token.substring(dot 1));
this.dollars=Integer.parseInt(token.substring(1,dot));
}
public String toString(){
return "$" dollars "." cents;
}
public boolean equals(Money other){
if(!(other instanceof Money)){
return false;
}
return this.dollars==other.dollars && this.cents==other.cents;
}
public Money add(Money other){
return
}
}
Here is my class, I can't seem to figure out how to create the add method that adds an object's value to the receiver's. Any tips or help is greatly appreciated!
CodePudding user response:
Possible solution assuming that the cents
value should always be kept within [0, 100] range
in any instance of Money
:
public Money add(Money other) {
if (null != other) {
this.cents = other.cents;
if (this.cents >= 100) {
this.cents %= 100;
this.dollars ;
}
this.dollars = other.dollars;
}
return this;
}