2: Remove Money from the Wallet: CISS 111 - Project 02 Wallet using a Vector and
ID: 3870366 • Letter: 2
Question
2: Remove Money from the Wallet: CISS 111 - Project 02 Wallet using a Vector and ENUM For project 02 you're going to simulate a wallet. In a wallet, you can store money duh. Money can be any of the following denominations: Denominations: 100, 50, 20, 10, 5, 2, 1, .50, .25, .10, .05,.01 You'll use an Enumeration called Money for the money denominations. Using a Vector, you're going to start out by filling the wallet with the following money: 5.00, 1.00, .25, .05, 10.00, .25,.10. Total: 16.65 Just like putting money in the wallet you'll allow the user to enter a denomination or-1 to return back to the main menu. If the user enters a denomination you'll check it against the ENUM Money and if it is not a valid denomination you'll tell them, prompting them to enter a new denomination or-1 to return to the menu. If it is a proper denomination then you'll search the Vector for the first instance of that denomination and if it is there remove it from the Vector telling the user that it has been removed from the wallet. After removing the denomination from the wallet Vector keep prompting them for money to remove until they enter -1 3: Display Money in Wallet: When display money is selected display the contents of the Vector to the screen. The output doesn't need to be sorted. Just display what's in the Vector 4: Empty Wallet: Create a menu with the following options: When empty wallet is selected you'll create variables for each of the denominations and loop through to determine how many of each you have. Display the breakdown of each type (displaying only what they have. If they have no pennies then don't show pennies) and quantity for the user along with the total of everything in the wallet. Empty the Vector at this point. 1) Put Money in Wallet 2) Remove Money from the Wallet 3) Display Money in Wallet 4) Empty Wallet 5) Exit 5: Exit will close the program. Let me know if you have any questions. This project is worth 15 points and is due 9/28/2017 by 11:59 PM. 1: Put Money in Wallet: When this choice is selected prompt the user for the denomination to put in the wallet (-1 to exit back to menu). It must be one of the set denominations. So the user would enter maybe 20. Then 20 would be inserted into the wallet Vector. Check for proper input against the ENUM Money the input is incorrect (an amount not in the ENUM) then tell the user and let them enter another until either a proper amount is entered or -1 is entered to skip putting money in the wallet and return back to the main menu. After a proper denomination has been inserted into the Vector prompt them again to 2 enter another denomination or -1 to return to the main menu. This way they can keep putting money into the wallet until they type in-1.Explanation / Answer
Given below is the Java code for the question. Please do rate the answer if it helped. Thank you.
Money.java
public enum Money {
HUNDRED(100),
FIFTY(50),
TWENTY(20),
TEN(10),
FIVE(5),
TWO(2),
ONE(1),
HALF(0.50),
QUARTER(0.25),
DIME(0.10),
NICKEL(0.05),
PENNY(0.01);
private final double value;
private Money(double v){
value = v;
}
public double value()
{
return value;
}
public String toString()
{
return "" + value;
}
}
MoneyMain.java
import java.util.Scanner;
import java.util.Vector;
public class MoneyMain {
static Scanner keybd = new Scanner(System.in);
public static void main(String[] args) {
Vector<Money> wallet = new Vector<Money>();
int choice = 0;
while(choice != 5)
{
System.out.println("1. Put Money in Wallet");
System.out.println("2. Remove Money from Wallet");
System.out.println("3. Display Money in Wallet");
System.out.println("4. Empty Money");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = keybd.nextInt();
switch(choice)
{
case 1:
putMoney(wallet);
break;
case 2:
removeMoney(wallet);
break;
case 3:
displayMoney(wallet);
break;
case 4:
emptyMoney(wallet);
break;
case 5:
break;
default:
System.out.println("Invalid menu choice!");
}
}
}
private static void emptyMoney(Vector<Money> wallet) {
int hundreds = 0;
int fifties = 0;
int twenties = 0;
int tens = 0;
int fives = 0;
int twos = 0;
int>
int halves = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
double total = 0;
for(Money m : wallet)
{
if(m == Money.HUNDRED)
hundreds ++;
else if(m == Money.FIFTY)
fifties++;
else if(m == Money.TWENTY)
twenties++;
else if(m == Money.TEN)
tens++;
else if (m== Money.FIVE)
fives++;
else if(m == Money.TWO)
twos++;
else if(m == Money.ONE)
ones++;
else if (m == Money.HALF)
halves ++;
else if(m == Money.QUARTER)
quarters++;
else if(m == Money.DIME)
dimes++;
else if(m == Money.NICKEL)
nickels++;
else if(m == Money.PENNY)
pennies ++;
total += m.value();
}
if(hundreds > 0)
System.out.println("Hundreds: " + hundreds);
if(fifties > 0)
System.out.println("Fifties: " + fifties);
if(twenties > 0)
System.out.println("Twenties: " + twenties);
if(tens > 0)
System.out.println("Tens: " + tens);
if(fives > 0)
System.out.println("Fives: " + fives);
if(twos > 0)
System.out.println("Twos: " + twos);
if(ones > 0)
System.out.println("Ones: " + ones);
if(halves > 0)
System.out.println("Half: " + halves);
if(quarters > 0)
System.out.println("Quarters: " + quarters);
if(dimes > 0)
System.out.println("Dimes: " + dimes);
if(nickels > 0)
System.out.println("Nickels: " + nickels);
if(pennies > 0)
System.out.println("Pennies: " + pennies);
System.out.println("Total: " + total);
wallet.clear();
System.out.println("Wallet cleared");
}
private static void displayMoney(Vector<Money> wallet) {
double total = 0;
String comma = "";
System.out.print(" Money: ");
for(Money m : wallet)
{
System.out.print( comma + m );
comma = ", ";
total += m.value();
}
System.out.println(" Total: " + total);
}
private static void removeMoney(Vector<Money> wallet) {
while(true)
{
Money m = getInput(" Enter the value to be removed (-1 to return to main menu): ");
if(m == null) //-1 , return back
return;
boolean removed = false;
for(int i = 0; i < wallet.size(); i++)
{
if(wallet.get(i) == m)
{
wallet.remove(i);
removed = true;
break;
}
}
if(removed)
System.out.println(m + " was removed from wallet");
else
System.out.println(m + " not in wallet");
}
}
private static void putMoney(Vector<Money> wallet) {
while(true)
{
Money m = getInput(" Enter the value to be added (-1 to return to main menu): ");
if(m == null) //-1 , return back
return;
wallet.add(m);
System.out.println(m + " added to wallet");
}
}
private static Money getInput(String message)
{
double num;
do
{
System.out.print(message);
num = keybd.nextDouble();
if(num == -1)
return null;
//check if input is one of the predefined money values
for(Money v : Money.values())
{
if(num == v.value())
return v;
}
System.out.println("Invalid value entered.");
}while(true);
}
}
output
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 1
Enter the value to be added (-1 to return to main menu): 100
100.0 added to wallet
Enter the value to be added (-1 to return to main menu): 12
Invalid value entered.
Enter the value to be added (-1 to return to main menu): 50
50.0 added to wallet
Enter the value to be added (-1 to return to main menu): 10
10.0 added to wallet
Enter the value to be added (-1 to return to main menu): 3
Invalid value entered.
Enter the value to be added (-1 to return to main menu): 5
5.0 added to wallet
Enter the value to be added (-1 to return to main menu): 0.25
0.25 added to wallet
Enter the value to be added (-1 to return to main menu): -1
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 3
Money: 100.0, 50.0, 10.0, 5.0, 0.25Total: 165.25
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 1
Enter the value to be added (-1 to return to main menu): 100
100.0 added to wallet
Enter the value to be added (-1 to return to main menu): 50
50.0 added to wallet
Enter the value to be added (-1 to return to main menu): 0.5
0.5 added to wallet
Enter the value to be added (-1 to return to main menu): -1
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 3
Money: 100.0, 50.0, 10.0, 5.0, 0.25, 100.0, 50.0, 0.5Total: 315.75
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 2
Enter the value to be removed (-1 to return to main menu): 9
Invalid value entered.
Enter the value to be removed (-1 to return to main menu): 0.05
0.05 not in wallet
Enter the value to be removed (-1 to return to main menu): 10
10.0 was removed from wallet
Enter the value to be removed (-1 to return to main menu): -1
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 3
Money: 100.0, 50.0, 5.0, 0.25, 100.0, 50.0, 0.5Total: 305.75
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 4
Hundreds: 2
Fifties: 2
Fives: 1
Half: 1
Quarters: 1
Total: 305.75
Wallet cleared
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 1
Enter the value to be added (-1 to return to main menu): 1
1.0 added to wallet
Enter the value to be added (-1 to return to main menu): -1
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 3
Money: 1.0Total: 1.0
1. Put Money in Wallet
2. Remove Money from Wallet
3. Display Money in Wallet
4. Empty Money
5. Exit
Enter your choice: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.