Hi Guys, I am trying to make a console based ATM Machine simulator, I have removed all the code that was not relevant to this challenge I am having.

What I am trying to do in display the balance which I can do, opening balance is 94.37, then I need to either do a Deposit or a Withdraw and have that amount change when i redisplay the balance.

I know the problem is that when I display the balance whether it be before or after I make a deposit or withdraw it takes the balance from the global variable, what I don't know is how not to use a global variable but still get a starting value and then return the Deposit/Withdraw balance to the existing balance and overwrite it.

I have no experience with classes/methods in C#, I called everything static cause thats how it was done in a book I started to read.

I have done a kpl paper so do know about methods/functions but not in this context.

Someone recommended making another class and calling that or making my methods private and using those values, I tryed the latter but it threw up errors in my menu switch statement because it didn't recognise the:
WithDraw(balance) and the Deposit(balance) in the switch statment because they were variables not declared in global yet i was trying to run/call them in global/main method,

Hope you can understand this, I spent about 2/3 hours writing the main bits of code and over 6 hours trying to get this 1 bit working, any help would be much appreciated.

thank you.

using System;
using System.Collections.Generic;
using System.Text;

namespace ATMMachine
{
    class MyClass
    {
        static void Main(string[] args)
        {
            
            int option = 0;
            string output = "";
            bool done = false;
            int choice = 0;
            double balance = 94.37;
            //double balance = startBalance;
            //double withDrawBalance = 0;
            Console.Write("This an Atm Machine simulation program\n");

            while (!done)
            {
                if (choice == 5)
                {
                    option = 5;
                }
                else if (choice == 7)
                {
                    option = AtmInterface();
                }
                else
                {
                    option = AtmInterface();
                }


                switch (option)
                {
                    case 1:
                        ChangePin();
                        break;
                    case 2:
                        //output = " Display Balance";
                        //UnderConstruction(output);
                        Console.WriteLine("Balance is: $" + AccountBalance(newBalance) + "c\n");
                        System.Threading.Thread.Sleep(2000);
                        break;
                    case 3:
                        // output = " \tWithdraw";
                        //UnderConstruction(output);
                        WithDraw();
                        break;
                    case 4:
                        //output = " \t\tDeposit";
                        //UnderConstruction(output);
                        Deposit();
                        break;
                    case 5:
                        Console.WriteLine("\nTransaction ended, card is been returned");
                        done = true;
                        break;
                    case 6:
                        output = " \tTest Pin";
                        UnderConstruction(output);
                        break;
                    default:
                        AtmInterface();
                        break;
                }// end switch
            } // end while


            // pause the console window
            Console.WriteLine("\nPress any key to continue...");
            Console.WriteLine("Program complete");
            Console.ReadLine();
           
        }// end static
       

      
    
        
        static double WithDraw()
        {
            double withDrawAmount = 0;
            //double withDrawBalance = 0;
            bool done = false;
            bool zero = false;
            double balance = 94.37;
            double newBalance = 0;

            while (!zero)
            {
                if (balance == 0)
                {
                    Console.WriteLine("Insufficient funds to withdraw, withdraw cancelled\n");
                    System.Threading.Thread.Sleep(4000);
                    zero = true;
                }
                else
                {
                    Console.WriteLine("Withdraws must be $20 or multiples of $20\n");
                 while (!done)
                    {
                        Console.Write("Amount to Withdraw: $");
                        try
                        {
                            withDrawAmount = int.Parse(Console.ReadLine());
                        }
                        catch (FormatException)
                        {
                        }
                        if (withDrawAmount % 20 != 0.00 || withDrawAmount == 0)
                        {
                            Console.WriteLine("\nAmount of Withdraw must in multiples of $20\n");
                                                        
                            string keyEntered = "n";
                            bool correctKey = false;

                            while (!correctKey)
                            {
                                Console.Write("Do you wish to continue 'y' or 'n': ");
                                try
                                {
                                    keyEntered = Console.ReadLine();
                                }
                                catch (FormatException)
                                {
                                }
                                keyEntered = keyEntered.ToLower();
                                if (keyEntered == "n")
                                {
                                    Console.WriteLine("\nReturning to Main Options Menu");
                                    done = true;
                                    zero = true;
                                    correctKey = true;
                                    System.Threading.Thread.Sleep(4000);
                                }
                                else if (keyEntered != "n" && keyEntered != "y")
                                {
                                    Console.WriteLine("Invalid selection");
                                }
                                else
                                {
                                    correctKey = true;
                                }
                            }// end while
                            
                            
                        }
                        else if (withDrawAmount < balance)
                        {
                            Console.WriteLine("\nWithdraw was successful\n");
                           balance -= withDrawAmount;
                           newBalance = balance;
                           //  System.Threading.Thread.Sleep(4000);
                            done = true;
                            zero = true;
                        }
                        else
                        {
                            Console.WriteLine("\nInsufficient funds available, withdraw cancelled\n");
                            System.Threading.Thread.Sleep(4000);
                            done = true;
                            zero = true;
                        }
                    }// end while
                }// end outer if
            }// end out while 
            return newBalance;
         }// end static void withDraw

       

       

        static double Deposit()
       {
            double deposit_Amount = 0;
            bool done = false;
            double balance = 94.37;;
            double newBalance;

            Console.WriteLine("Deposits must be $10 or in multilpes of $10\n");

            while (!done)
            {
                Console.Write("How much do you wish to deposit: $");
                try
                {
                    deposit_Amount = double.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                }
                if (deposit_Amount % 10 != 0 || deposit_Amount ==0)
                {
                    Console.WriteLine("\nDeposits must be in multiples of $10\n");
                    Console.WriteLine("Deposit transaction failed, returning to main menu\n");
                    System.Threading.Thread.Sleep(4000);
                    done = true;
                }
                else
                {
                    Console.WriteLine("\nDeposit of $" + deposit_Amount + " was successful\n");
                    balance += deposit_Amount;
                    System.Threading.Thread.Sleep(4000);
                    done = true;
                }

            }// ends while
            return balance;
       }// ends static double deposit
       
        static double AccountBalance(double newBalance)
        {
            return newBalance;
        }
        
    }
}

Some of the code has been butchered abit due to my many attempts to fix the problem, Static Deposit has not really been worked too much because I know if I fix Withdraw then the same solution can be used for Deposit also.

Basically I need to go, Display Balance of 94.37 > withdraw any amount less then Balance> then display new balance and have the new balance display correctly.

cheers

case 3:
// output = " \tWithdraw";
//UnderConstruction(output);
WithDraw();
break;

WithDraw should return something to a "global" variable. I know the compiler should give you an error here, but it doesn't...

Everytime you enter the Deposit() method you are setting the deposit amount to 94.37 :. its not really ever going to change. I mean you will return a different balance from that method but as soon as you attempt to deposit again you will be starting at 94.37$ again.

You have to pass around the deposit amount as a parameter to the method

static double Deposit(double balance)

do that and keep track of the balance in the main method, Is
perhaps the simplest way.

What you really should do is split the problem up into classes etc. but anyhow.

Also there are no global variables in c# but you can create a class static public class var and then use that ( MyClassName.staticVarName)

But it is nicer ( i think ) to pass it around.

For multiple reasons, One being that the problem its self does not lend its self IMHO to a static var, If you assume that more then one person will use your ATM then you will be managing multiple balances :. you wouldn't expect that the balanace would be one global var but would be a variables attached to a class that contains that persons account information.

So in conclusion you could say

namespace ATMMachine
{
    class MyClass
    {
     public static double balance;              //each time i use this now I have to type MyClass.balance;
     ......
     }
}

then only set it to 94 in the main method (before its first used), not in the withdraw and deposit methods.

heres the 2 bits that are causing the problem

using System;
using System.Collections.Generic;
using System.Text;

namespace Sec05ATMMoneyOnly
{
    class Program
    {
        static void Main(string[] args)
        {
             
            int option = 0;
            string output = "";
            bool done = false;
            int choice = 0;
            //double newBalance = 0;
            double balance = 94.37;
            //double balance = startBalance;
            //double withDrawBalance = 0;
            Console.Write("This an Atm Machine simulation program\n");

           
            while (!done)
            {
                if (choice == 5)
                {
                    option = 5;
                }
                else if (choice == 7)
                {
                    option = AtmInterface();
                }
                else
                {
                    option = AtmInterface();
                }


                switch (option)
                {
                    case 1:
                        //ChangePin();
                        break;
                    case 2:
                        //output = " Display Balance";
                        //UnderConstruction(output);
                        Console.WriteLine("Balance is: $" + AccountBalance(balance) + "c\n");
                        System.Threading.Thread.Sleep(2000);
                        break;
                    case 3:
                        // output = " \tWithdraw";
                        //UnderConstruction(output);
                        WithDraw(balance);
                        break;
                    case 4:
                        //output = " \t\tDeposit";
                        //UnderConstruction(output);
                        Deposit();
                        break;
                    case 5:
                        Console.WriteLine("\nTransaction ended, card is been returned");
                        done = true;
                        break;
                    case 6:
                        output = " \tTest Pin";
                        //UnderConstruction(output);
                        break;
                    default:
                        AtmInterface();
                        break;
                }// end switch
            } // end while


            // pause the console window
            Console.WriteLine("\nPress any key to continue...");
            Console.WriteLine("Program complete");
            Console.ReadLine();
           
        }// end static
       
       
    
        static double WithDraw(double balance)
        {
            double withDrawAmount = 0;
            //double withDrawBalance = 0;
            bool done = false;
            bool zero = false;
            //double balance = 94.37;
            double newBalance = 0;

            while (!zero)
            {
                if (balance == 0)
                {
                    Console.WriteLine("Insufficient funds to withdraw, withdraw cancelled\n");
                    System.Threading.Thread.Sleep(4000);
                    zero = true;
                }
                else
                {
                    Console.WriteLine("Withdraws must be $20 or multiples of $20\n");
                 while (!done)
                    {
                        Console.Write("Amount to Withdraw: $");
                        try
                        {
                            withDrawAmount = int.Parse(Console.ReadLine());
                        }
                        catch (FormatException)
                        {
                        }
                        if (withDrawAmount % 20 != 0.00 || withDrawAmount == 0)
                        {
                            Console.WriteLine("\nAmount of Withdraw must in multiples of $20\n");
                                                        
                            string keyEntered = "n";
                            bool correctKey = false;

                            while (!correctKey)
                            {
                                Console.Write("Do you wish to continue 'y' or 'n': ");
                                try
                                {
                                    keyEntered = Console.ReadLine();
                                }
                                catch (FormatException)
                                {
                                }
                                keyEntered = keyEntered.ToLower();
                                if (keyEntered == "n")
                                {
                                    Console.WriteLine("\nReturning to Main Options Menu");
                                    done = true;
                                    zero = true;
                                    correctKey = true;
                                    System.Threading.Thread.Sleep(4000);
                                }
                                else if (keyEntered != "n" && keyEntered != "y")
                                {
                                    Console.WriteLine("Invalid selection");
                                }
                                else
                                {
                                    correctKey = true;
                                }
                            }// end while
                            
                            
                        }
                        else if (withDrawAmount < balance)
                        {
                            Console.WriteLine("\nWithdraw was successful\n");
                           balance -= withDrawAmount;
                           newBalance = balance;
                           //  System.Threading.Thread.Sleep(4000);
                            done = true;
                            zero = true;
                        }
                        else
                        {
                            Console.WriteLine("\nInsufficient funds available, withdraw cancelled\n");
                            System.Threading.Thread.Sleep(4000);
                            done = true;
                            zero = true;
                        }
                    }// end while
                }// end outer if
            }// end out while 
            balance = newBalance;
            return newBalance;
         }// end static void withDraw

          
       
        static double AccountBalance(double balance)
        {
            return balance;
        }
    }
}
case 3:
                        // output = " \tWithdraw";
                        //UnderConstruction(output);
                        WithDraw(balance);

should read

case 3:
                        // output = " \tWithdraw";
                        //UnderConstruction(output);
                        balance = WithDraw(balance);

Everytime you enter the Deposit() method you are setting the deposit amount to 94.37 :. its not really ever going to change. I mean you will return a different balance from that method but as soon as you attempt to deposit again you will be starting at 94.37$ again.

You have to pass around the deposit amount as a parameter to the method

static double Deposit(double balance)

do that and keep track of the balance in the main method, Is
perhaps the simplest way.

What you really should do is split the problem up into classes etc. but anyhow.

Also there are no global variables in c# but you can create a class static public class var and then use that ( MyClassName.staticVarName)

But it is nicer ( i think ) to pass it around.

For multiple reasons, One being that the problem its self does not lend its self IMHO to a static var, If you assume that more then one person will use your ATM then you will be managing multiple balances :. you wouldn't expect that the balanace would be one global var but would be a variables attached to a class that contains that persons account information.

So in conclusion you could say

namespace ATMMachine
{
    class MyClass
    {
     public static double balance;              //each time i use this now I have to type MyClass.balance;
     ......
     }
}

then only set it to 94 in the main method (before its first used), not in the withdraw and deposit methods.

Thanks heaps dude, I didn't even really know about declaring before method main.

problem solved.

This question has already been answered. Start a new discussion instead.