Please Read the Instructions, I have made the COIN CLASS but im confused about t
ID: 3549880 • Letter: P
Question
Please Read the Instructions,
I have made the COIN CLASS but im confused about the WALLET CLASS and got many errors.
Create a class called Coin as follows:
Attributes: name and value of a coin (valid names and values are Toonie(200), Loonie(100), Quarter(25), Dime(10) and Nickel(5)).
Methods : Constructor that sets the name and value;
Get methods
toString method
Create another class called Wallet as follows:
Attributes: total value of all coins (in cents) stored in the wallet
Methods: Constructor (that sets the total value to zero)
addCoin (that adds a given Coin object's value to the total value)
removeCoin (that removes a given Coin object's value from the total value)
toString method
Test the classes by creating a Demo, and adding and removing coins from it.
Explanation / Answer
import java.util.HashMap;
public class Coin {
private String name;
private int value;
static HashMap<Integer,String> validCoins = new HashMap<Integer,String>();
public Coin(String name, int value) {
super();
validCoins.put(200, "Toonie");
validCoins.put(100, "Loonie");
validCoins.put(25, "Quarter");
validCoins.put(10, "Dime");
validCoins.put(5, "Nickel");
if(validCoins.containsKey(value)){
if(validCoins.get(value).equalsIgnoreCase(name)){
this.name = name;
this.value = value;
}
else{
System.out.println("Incorrect Coin.......exiting");
System.exit(0);
}
}
else{
System.out.println("Incorrect Coin.......exiting");
System.exit(0);
}
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Coin [name=");
builder.append(name);
builder.append(", value=");
builder.append(value);
builder.append("]");
return builder.toString();
}
}