I have an assignment to make a program that will use classes and objects to set up a piggy bank system.
It should have a separate class that contains all of the actual calculations. I've been working on it and will post my progress below.
I know (I think anyway) that I have to use objects in the main class to access the constructors of the second.
This is as far as my understanding goes. I need help figuring out the syntax for doing so.
My main class is just a switch that will tell my other class which calculations need to be made.
There has to be a way to add a penny, nickel, dime, quarter, and to view the total or exit (just the way it was assigned).
The code for my main class so far:
// ******************************************************************************** // // PROGRAM: My Savings // // PROGRAMMER: IkeIII // CLASS: CSC 2623 // SOURCES: N/A // // DESCRIPTION: Uses classes to make simulate a piggy bank. // // ******************************************************************************** package mysavings; import java.util.Scanner;//allows inputs public class MySavings { public static void main(String[] args) { Scanner input = new Scanner(System.in); int choice = input.nextInt(); System.out.print("1. Show total in bank."); System.out.print("2. Add a penny."); System.out.print("3. Add a nickel."); System.out.print("4. Add a dime."); System.out.print("5. Add a quarter."); System.out.print("6. Take money out of bank."); System.out.print("Enter 0 to quit."); System.out.print("Enter your choice: "); while(choice == 0){ switch(choice){ case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break; default: break; } } //Declare a class object, savings PiggyBank obj = new PiggyBank(); //Initialize a class object, savings obj.setSavings(0); } //End of main } // End of test circle
And the code for my sub-class:
// ******************************************************************************** // // PROGRAM: Piggy Bank (Class) // // PROGRAMMER: IkeIII // CLASS: CSC 2623 // SOURCES: N/A // // DESCRIPTION: Uses classes to make simulate a piggy bank. // // ******************************************************************************** package mysavings; public class PiggyBank { public double savings; //Constructor public PiggyBank(){ savings = 0; }//End of constructor //CONSTRUCTOR With a parameter public PiggyBank( double s ) { savings = s; }//End of constructor with a parameter //METHOD: Set the raius of the circle. public void setSavings( double newSavings ) { savings = newSavings; //Display output. }//End of setRadius //METHOD: Get the current of the radius public double getSavings() { return (savings);} }//End of PiggyBank
--- Update ---
I know how to do most of the main stuff. Even if I haven't corrected some of it above. It's only the stuff dealing with moving between the two classes that has me confused if anyone can explain.