I want to create a program where you're able to add and remove coins to a wallet, as well as show the total amount of money in the wallet. All through OOP. I've created a while loop where there are multiple options the user can choose from. Which is add coins, display num of coins, and remove coins from wallet.
What I am struggling with is how to keep track of these coins, for example: if I add a dime 2 times and a quarter 3 times, I want the program to count those times + the values and add it into the wallet, calculating the total.
Main class
import java.text.DecimalFormat;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Coin myCoin = new Coin();
Wallet myWallet = new Wallet();
while(true) {
Scanner input = new Scanner(System.in);
// Options
System.out.println("Select an option: ");
System.out.println("1. Enter a coin to add");
System.out.println("2. Display number of coins");
System.out.println("3. Remove Coins from Wallet");
System.out.println("4. Exit Program");
int choice = input.nextInt();
// If statements for 1
if (choice == 1) {
System.out.println("Enter the coin name: ");
String coin = input.next();
if (coin.equals("Dime")) {
myCoin.setName("Dime");
System.out.println(myCoin.getName()); }
else if (coin.equals("Quarter")) {
myCoin.setName("Quarter");
System.out.println(myCoin.getName());
}
else if (coin.equals("Nickel")) {
myCoin.setName("Nickel");
System.out.println(myCoin.getName());
}
else if (coin.equals("Loonie")) {
myCoin.setName("Loonie");
System.out.println(myCoin.getName());
}
else if (coin.equals("Toonie")) {
myCoin.setName("Toonie");
System.out.println(myCoin.getName());
}
}
// If statements for 2
if (choice == 2) {
System.out.println("Coins in your wallet: ");
System.out.println("Toonies: " + myWallet.getToonies());
System.out.println("Loonies: " + myWallet.getLoonie());
System.out.println("Quarters: " + myWallet.getQuarter());
System.out.println("Dimes: " + myWallet.getDimes());
System.out.println("Nickels: " + myWallet.getNickel());
System.out.println("Total Money: $" + new DecimalFormat("#0.00").format(myWallet.totalCoins()));
}
// If statements for 3
if (choice == 3) {
System.out.println("Enter coin to remove: ");
String removeCoin = input.next();
System.out.println("Enter the amount of coins: ");
String amountRemoved = input.next();
}
// If statements for 4
if (choice == 4) {
System.exit(0);
}
}
}
}
Coin Class
import java.text.DecimalFormat;
class Coin {
private String name;
private double value;
public void setName(String name) {
this.name = name;
//dime
if (name.equals("Dime")) {
this.value = 0.1;
}
//quarter
if (name.equals("Quarter")) {
this.value = 0.25;
}
//nickel
if (name.equals("Nickel")) {
this.value = 0.05;
}
//loonie
if (name.equals("Loonie")) {
this.value = 1.0;
}
//toonie
if (name.equals("Toonie")) {
this.value = 2.0;
}
}
public String getName() {
return "You added a " + this.name + ". Value is $" + new DecimalFormat("#0.00").format(this.value);
}
}
Wallet Class
import java.util.ArrayList;
class Wallet {
private int toonies;
private int loonie;
private int quarter;
private int dimes;
private int nickel;
// array list
private ArrayList<String> coins = new ArrayList<>();
//setters
public void setToonies(int toonies) {
this.toonies = toonies;
}
public void setLoonie(int loonie) {
this.loonie = loonie;
}
public void setQuarter(int quarter) {
this.quarter = quarter;
}
public void setDimes(int dimes) {
this.dimes = dimes;
}
public void setNickel(int nickel) {
this.nickel = nickel;
}
//getters
public int getToonies() {
return toonies;
}
public int getLoonie() {
return loonie;
}
public int getQuarter() {
return quarter;
}
public int getDimes() {
return dimes;
}
public int getNickel() {
return nickel;
}
// total number of coins
public int totalCoins() {
return toonies + loonie + quarter + dimes + nickel;
}
}
I've tried using increments to count the different coins in the Coin class but they would not work out for me and I'm trying to use an ArrayList because I'm pretty sure I need that to store the coins, but I am not well-versed in this kind of code so I am confused on how to go about this.
Also, I am not sure if the explanation of my problem is coherent or not, so feel free to ask for clarification on anything I have said
enum
for coin types (enum Type { DIME, QUARTER, NICKEL, ...}
instead ofString
s; if not possible/allowed/wanted, at least use constants for that (e.g. public static final String DIME = "Dime"; public static final String QUARTER = "Quarter"; ...`), so all parts are using the same constant