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