Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySavings
{
    class PiggyBank
    {

        private double numQuarters, numDimes, numNickels, numPennies;

        public PiggyBank()
        {
            numQuarters = 0;
            numDimes = 0;
            numNickels = 0;
            numPennies = 0;
            
        }

        /* Precondition: None.
         * showMenu just prints the menu to the screen.
         * Postcondition: The menu is printed to the screen.
         */
        public void showMenu()
        {
            Console.WriteLine("1. Show total in bank.");
            Console.WriteLine("2. Add a penny.");
            Console.WriteLine("3. Add a nickel.");
            Console.WriteLine("4. Add a dime.");
            Console.WriteLine("5. Add a quarter.");
            Console.WriteLine("6. Take all money out of bank.");
            Console.WriteLine("Enter 0 to quit.");            
        }

        /* Precondition: None.
         * addPenny increases the number of pennies in this bank by 1.
         * Postcondition: There is another penny in the bank.
         */
        public void addPenny()
        {
            numPennies++;
        }

        /* Precondition: None.
         * addNickel increases the number of nickels in this bank by 1.
         * Postcondition: There is another nickel in the bank.
         */
        public void addNickel()
        {
            numNickels++;
        }
        
        /* Precondition: None.
         * addDime increases the number of dimes in this bank by 1.
         * Postcondition: There is another dime in the bank.
         */
        public void addDime()
        {
            numDimes++;
        }
        
        /* Precondition: None.
         * addQuarter increases the number of quarters in this bank by 1.
         * Postcondition: There is another quarter in the bank.
         */
        public void addQuarter()
        {
            numQuarters++;
        }

        /* Precondition: None
         * getTotal returns the total amount of money in the bank currently.
         * The total is calculated exactly like it was in your AddCoins application.
         * Postcondition: The total is returned.
         */
        public double getTotal()
        {
            double Total = numQuarters + numPennies + numNickels + numDimes;
            return Total; //CHANGE THIS TO RETURN THE ACTUAL TOTAL
            
        }

        /* Precondition: None.
         * removeMoneyFromBank prints how much money is in the bank, and changes all
         * of the values of the coins in the bank to 0
         * Postcondition: The total is printed and the bank is now empty.
         */
        public void removeMoneyFromBank()
        {
            Console.Write("Your available money in the bank is:" + getTotal());
            numQuarters = 0;
            numPennies = 0;
            numNickels = 0;
            numDimes = 0;

        }
    }
}
Posted
Updated 6-Dec-11 17:30pm
v3

All of your if statements should be == not =

For example:

if (i = 6)

should be

if (i == 6)
 
Share this answer
 
Comments
Member 8349150 6-Dec-11 20:25pm    
thank you very much and how would I write the if statement for option 0
wizardzz 6-Dec-11 20:38pm    
you do realize that you have the if statements in a while loop of while greater than 0? The 0 case will never be hit in the while loop, just put an if i == 0 outside of the while i > 0.
wizardzz 6-Dec-11 20:57pm    
I know, I wasn't sure if the code provided was the exact code from the app. You are right, I addressed it in a reply to a different comment by the OP 4 minutes before your post ; )
BillWoodruff 6-Dec-11 22:37pm    
Sorry, WizardZZ, I deleted that comment immediately after I realized, further down the thread that you had said the same thing. You must have gotten a notification anyway.
[no name] 6-Dec-11 20:31pm    
5!
just add this to zero condition.

C#
class Program
    {
        static void Main(string[] args)
        {
            //You need a loop to keep showing the menu and asking for a response
            //until the user enters a 0.
            //Assume the person will always enter a valid option.
            //Main MUST be the place that an option is entered!

 
            PiggyBank pb = new PiggyBank();
            Console.WriteLine("1. Show total in bank.");
            Console.WriteLine("2. Add a penny.");
            Console.WriteLine("3. Add a nickel.");
            Console.WriteLine("4. Add a dime.");
            Console.WriteLine("5. Add a quarter.");
            Console.WriteLine("6. Take all money out of bank.");
            Console.WriteLine("Enter 0 to quit.");
 
            int i = Convert.ToInt32(Console.ReadLine());

 
                if (i == 1)
                {
                    Console.WriteLine("Total: " + pb.getTotal());
                }
                else if (i == 2)
                {
                    pb.addPenny();
                    Console.WriteLine("Penny added!");
                }
 

                else if (i == 3)
                {
                    pb.addNickel();
                    Console.WriteLine("Nickel added!");
                }
 
                else if (i == 4)
                {
                    pb.addDime();
                    Console.WriteLine("Dime added!");
                }
 
                else if (i == 5)
                {
                    pb.addQuarter();
                    Console.WriteLine("Quarter added!");
                }
                else if (i == 6)
                {
                    pb.removeMoneyFromBank();
                    Console.WriteLine("All money is removed!");
                }
                else if (i == 0)
                {
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("Invalid Code");
                }

 
            }
 
        }
    }


Regards,
Eduard
 
Share this answer
 
v2
Comments
wizardzz 6-Dec-11 20:39pm    
Sorry, but this is incorrect. Your if (i == 0) will never be reached because it is within a while i > 0 loop.
wizardzz 6-Dec-11 20:58pm    
Also, the else in the while loop doesn't escape the loop or program or change the value of i, this could be dangerous.
Member 8349150 6-Dec-11 20:42pm    
Thank you very much for all the help but why does when I run my program it displays lets just say "Penny added" 100 times
[no name] 6-Dec-11 20:45pm    
Remove the while loop my friend, because you are doing an infinite loop :P see code above
wizardzz 6-Dec-11 21:02pm    
Actually given the purpose of his program, a while loop is fine. A while loop or recursion could work. I wouldn't sat remove it though, just make sure the user's input is read into i, and that the loop has an escape (invalid i value or '0')
I think your code is so confused here, that you need to start over with a specification, and work from that.

You need:

1. a way to start the application

2. an initial presentation of the menu items

3. establish a condition that will continue the loop within the application, and create a boolean variable for that condition:

// it has to be 'true, initially, so the while loop can start
private bool continueWhile = true;

a. similarly, you want to define what entry will terminate the loop in the application:

int terminateKeyValue = 0;

4. then you enter a while loop which starts with reading a key from the console:

a. check that key to see if it a valid entry: if not use 'continue to restart the while loop.

b. if the key is valid, and it equals in value the key you wish to terminate the application, then set 'continueWhile = false; and use 'continue to cause the while loop to execute one more time and exit.

c. given a valid key in your acceptable range of values that trigger PiggyBank methods: do the right thing to trigger the right method: I'd suggest a Switch statement to handle the different valid cases.

So your pseudo-code might look like this:
private bool continueWhile = true;

int terminateKeyValue = 0;

while(continueWhile)
{
   // get a console keypress
   // convert to integer
   int i = // console key => integer

   // call a validation keypress function
   // if invalid keypress: loop again
   // optionally you could write a message to the console
   // saying 'invalid entry' or whatever
   if(iIsNotOkay(i)) continue;

   // check for termination of the app
   // and quit the loop immediately
   if(i == terminateKeyValue)
   {
     continueWhile = false;
     continue;
   }

   // now your code for handling the
   // different cases of valid entries
   // that trigger PiggyBank Methods
   //
   // I'd suggest using a switch/case statement

}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900