Becasue your Main method does very little:
static void Main(string[] args)
{
Console.WriteLine("Welcome!");
Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
int input = Convert.ToInt32(Console.ReadLine());
if (input >= 1 && input <= 3)
{
Console.WriteLine("You have entered " + input);
Console.ReadLine();
}
else if (input == 4)
{
Console.WriteLine("Goodbye.");
}
}
As soon as you enter the number it exits and the window closes so you can't see anything.
So start by changing that and adding a loop:
static void Main(string[] args)
{
int input = -1;
do
{
Console.WriteLine("Welcome!");
Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
if (int32.TryParse(Console.ReadLine(), out input))
{
if (input >= 1 && input <= 3)
{
Console.WriteLine("You have entered " + input);
Console.ReadLine();
}
}
} while input != 4)
Console.WriteLine("Goodbye.");
}
I've also replaced the Convert with TryParse - which means your app doesn't crash if you type the wrong thing!
You other problem is the "I want to use Account here" bit - it's a bit muddled as to exactly what you need but try:
Account acc = new Account();
acc.menu();
But you shouldn't really be putting the console stuff in the Account class - it should be concerned only with manipulating accounts, not handling user input. You main method should handle user input, and pass data to the Account class, and call methods inside it as the user asks for things to happen.
"The account is concerned with the interface because it needs to know what method to call. The ATM needs to be able to call the account and then later choose an action, by pressing the key. So I need to move the interface to the ATM section because it contains the main method?"
The ATM is "an interface" in the same way that login via the internet is - but you don't expect the two to be the same just because they access the same Account, do you? For one thing, where are you going to insert the bank card in your computer? :laugh:
The ATM constructs an instance of the account (which has the right account number) by asking the Account List (or whatever) for the Account itself and passing it the account number.
Disclaimer: you may not want to use this code (in fact I'd recommend you don't) but it may explain what I mean:
public class Account
{
private static Account[] accounts = new Account[3];
private decimal balance = 0.0M;
public Account(decimal openingDeposit, int accountNumber)
{
if (accountNumber >= 0 && accountNumber < accounts.Length)
{
balance = openingDeposit;
accounts[accountNumber] = this;
}
}
public Account GetAccount(int index)
{
if (index >= 0 && index < accounts.Length)
{
return accounts[index];
}
return null;
}
public decimal MakeDeposit(decimal deposit)
{
balance += deposit;
return balance;
}
public decimal MakeWithdrawl(decimal deposit)
{
balance -= deposit;
return balance;
}
public decimal GetBalance()
{
return balance;
}
}
Now, your Main method can "fill in the blanks":
public static void Main(string[] args)
{
Account a = new Account(123.88M, 0);
Account b = new Account(456.99M, 1);
Account c = new Account(1.24M, 2);
...
if (int.TryParse(Console.ReadLine(), out input))
{
if (input >= 1 && input <= 3)
{
Account acc = Account.GetAccount(input - 1);
Console.WriteLine("The account balance is: {0}", acc.GetBalance());
}
}
}
Here, the Account is dealing with itself, which means it "knows" which account it's got to access - this is called an
instance
of the Account class, and each instance is separate from each other. They have different account numbers, different balances - and one account can't affect another.
There's probably quite a lot there that doesn't make much sense, but try to gloss over it for a bit and get a "feel" for what happens.
The ATM (your Main method) knows how to deal with the user via a screen and a couple of keys, but it doesn't affect your account itself - it asks your bank's main systems to do that and that "real world" separation is reflected in the software.
Have a think, and see if your get what I mean - I'm off now as Herself is insisting I "get a life" and the consequences for disobedience here could get serious! :laugh: