Can anyone pls edit this Java code? I need to be able to add the 3 amounts liste
ID: 3584066 • Letter: C
Question
Can anyone pls edit this Java code? I need to be able to add the 3 amounts listed on the main method public class Money { private Currency currency; private int wholenum; private int decnum; public Money(Currency currency, int wholenum, int decnum) { this.currency = currency; this.wholenum = wholenum; if (decnum > 99) this.wholenum = this.wholenum + decnum/100; this.decnum = decnum0; } public String toString() { if (decnum<10) return currency +" "+wholenum+".0"+decnum; return currency +" "+wholenum+"."+decnum; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Money M = (Money) obj; // casting obj to Money if (this.currency != M.currency) return false; if (this.wholenum != M.wholenum) return false; return (this.decnum == M.decnum); } public int hashCode() { Integer I = this.decnum; return I.toString().hashCode(); } // TESTING public static void main(String[] args) { Money M1 = new Money(Currency.PHP, 500, 1); Money M2 = new Money(Currency.PHP, 500, 1234); Money M3 = new Money(Currency.PHP, 500, 1234); System.out.println(M1 +" "+ M1.equals(M2) +" "+ M2); System.out.println(M2 +" "+ M2.equals(M2) +" "+ M2); System.out.println(M2 +" "+ M2.equals(M3) +" "+ M3); System.out.println(M1 +" "+ M1.hashCode()); System.out.println(M2 +" "+ M2.hashCode()); System.out.println(M3 +" "+ M3.hashCode()); } } --------------------------------------------------------------------------------------------------------------------------------------------------------------------- public enum Currency { USD, PHP, JPY, KRW, EUR, GBP }Explanation / Answer
please rate - thanks
public class Money
{ private Currency currency;
private int wholenum;
private int decnum;
public Money(Currency currency, int wholenum, int decnum)
{ this.currency = currency;
this.wholenum = wholenum;
if (decnum > 99)
this.wholenum = this.wholenum + decnum/100;
this.decnum = decnum%100;
}
public static Money add(Object one,Object two, Object three)
{Money M4=new Money(Currency.PHP,0,0);
Money M1 = (Money) one;
Money M2 = (Money) two;
Money M3 = (Money) three;
M4.wholenum=M1.wholenum+M2.wholenum+M3.wholenum;
M4.decnum=M1.decnum+M2.decnum+M3.decnum;
if(M4.decnum>99)
{M4.wholenum=M4.wholenum+M4.decnum/100;
M4.decnum%=100;
}
return M4;
}
public String toString()
{ if (decnum<10)
return currency +" "+wholenum+".0"+decnum;
return currency +" "+wholenum+"."+decnum;
}
public boolean equals(Object obj)
{ if (this == obj)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Money M = (Money) obj;
// casting obj to Money
if (this.currency != M.currency)
return false;
if (this.wholenum != M.wholenum)
return false;
return (this.decnum == M.decnum);
}
public int hashCode()
{ Integer I = this.decnum;
return I.toString().hashCode();
}
// TESTING
public static void main(String[] args)
{ Money M1 = new Money(Currency.PHP, 500, 1);
Money M2 = new Money(Currency.PHP, 500, 1234);
Money M3 = new Money(Currency.PHP, 500, 1234);
System.out.println(M1 +" "+ M1.equals(M2) +" "+ M2);
System.out.println(M2 +" "+ M2.equals(M2) +" "+ M2);
System.out.println(M2 +" "+ M2.equals(M3) +" "+ M3);
System.out.println(M1 +" "+ M1.hashCode());
System.out.println(M2 +" "+ M2.hashCode());
System.out.println(M3 +" "+ M3.hashCode());
Money M4=new Money(Currency.PHP,0,0);
M4=add(M1,M2,M3);
System.out.println("Sum of the 3 values is "+M4 +" "+ M4.hashCode());
}
}