My book says to :
Create a mySavings application that displeys a menu of choices for entering pennies,nickels,dimes, and quarters into a piggy bank and then prompts the user to make a selection. The my Savings application should include a PiggyBank object that can add coins to the piggy bank, remove coins, return the total amount in the bank. Application output should look simular to:
1. show total in bank
2. add a penny
3. add a nickel
4. add a dime
5. add a quarter
6. Take money out of bank
Enter 0 to quit
Enter your choice: 5
please note number 6 of where the menu has to change
I am having trouble with getting the constructor to execute every time the person enters a choice.
Please also note that in my code i use anther class called EasyReader to make input easyer and it works.
Variables; info held
choice; holds a int vale identifying witch choice the user has made
choice1; int value of either 0 or 1, indentifys if the user wants to put money in bank or take money out of bank.
total; int value of the total number of cents in the bank.
Program general code
Code:public class mySavings { public int choice,choice1,total; public mySavings(int choice) { if (choice != 0) { if (choice == 1) { System.out.println("The total cents in your account it: " + total); } if(choice == 2 && choice1 == 0) { total = total + 1; } if(choice == 3 && choice1 == 0) { total = total + 5; } if(choice == 4 && choice1 == 0) { total = total + 10; } if(choice == 5 && choice1 == 0) { total = total + 25; } if(choice == 2 && choice1 == 1) { total = total - 1; } if(choice == 3 && choice1 == 1) { total = total - 5; } if(choice == 4 && choice1 == 1) { total = total - 10; } if(choice == 5 && choice1 == 1) { total = total - 25; } if (choice == 6 && choice1 == 0) { choice1 = 1; } if (choice == 6 && choice1 == 1) { choice1 = 0; } } } }
Program Client
Code:public class savingsClient { public static void main(String[] args) { int choice = 0,choice1 = 0,total = 0; EasyReader console = new EasyReader(); mySavings mySav = new mySavings(choice); do { System.out.println("1. Show total in bank."); if(choice1 == 0) { System.out.println("2. Add a penny."); System.out.println("3. Add a nickel."); System.out.println("4. Add a dime."); System.out.println("5. Add a quarter."); System.out.println("6. Take money out of bank."); } if (choice1 == 1) { System.out.println("2. Take a penny."); System.out.println("3. Take a nickel."); System.out.println("4. Take a dime."); System.out.println("5. Take a quarter."); System.out.println("6. Put Money in Bank."); } System.out.println("Enter 0 to quit"); System.out.print("Enter your choice: "); choice = console.readInt(); if (choice == 1) { System.out.println("The total amount of cents in this account is: " + mySav.total + "\n"); } }while(choice != 0); System.out.println(mySav.total); } }


Reply With Quote


Bookmarks