DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 1 of 1
  1. #1
    Join Date
    Nov 2005
    Posts
    16

    constructor help!

    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);
    	}
    }
    Last edited by Darrel104; 04-19-2006 at 09:02 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links