A - Add
D - Deposit
W - Withdraw
S - Show all accounts
Q - Quit
When the 'A' key is pressed at the menu screen, the user must enter the account number, then a beginning balance. If there are already 20 accounts added, then an error message pops up and the menu is shown again. If the account number is not 5 digits, the user must re-enter the account number.
When the 'D' key is pressed at the menu screen, the user is prompted to enter an existing account number. If the typed account number doesn't exist, an error message pops up and the user re-enters the account number. Otherwise, it prompts the user for the amount of money to deposit. If the amount entered is negative, an error message pops up and the user re-enters the amount to deposit.
When the 'W' key is pressed at the menu screen, the user is prompted to enter an existing account number. If the typed account number doesn't exist, an error message pops up and the user re-enters the account number. Otherwise, it prompts the user for the amount of money to withdraw. If the amount entered is negative, an error message pops up and the user re-enters the amount to withdraw.
When the 'S' key is pressed at the menu screen, the user is shown the existing account numbers along with their current balances, sorted by account number. If there are no accounts, then an error message pops up and the menu is shown again.
When the 'Q' key is pressed, an exit message prints, then the program closes once a key is pressed.
My questions are: first of all, how can I implement my menu so that the 'Q' key will exit the application instead of pressing any key other than A, D, W, or S? Secondly I'm getting a couple errors when I compile the two files (Program.cs and Account.cs) in Visual Studio 2010:
Program.cs(79,47): error CS0118: Operator '!=' cannot be applied to operands of type 'long' and 'Program3.Account'
Program.cs(116,50): Operator '!=' cannot be applied to operands of type 'long' and 'Program3.Account'
Here is my code for Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program3
{
class Program
{
static void Main(string[] args)
{
long number;
float balance;
float amount;
int accountCounter = 0;
Account[] myAccounts = new Account[20];
char menuChoice;
while (true)
{
Console.WriteLine("Please select an option\n");
Console.WriteLine(" A - Add D - Deposit W-Withdraw S-Show Any other character to Quit\n");
menuChoice = (char)Console.Read(); // reads the menu character entered by the User
switch (menuChoice)
{
// If menu option 1 is selected, call the initialize function.
case 'a':
{
if (accountCounter < 20)
{
Console.WriteLine("Account number to add: ");
number = Console.Read();
while (number < 10000 || number > 99999)
{
Console.WriteLine("Error. Number must be 5 digits: ");
Console.Write("Account number to add: ");
number = Console.Read();
}
Console.WriteLine("Beginning balance: ");
balance = Console.Read();
while (balance < 0)
{
Console.WriteLine("Error: Balance must be non-negative");
Console.Write("Beginning balance: ");
balance = Console.Read();
}
myAccounts[accountCounter] = new Account(number, balance);
Console.WriteLine("Account " + number + " added");
accountCounter++;
}
else
{
Console.WriteLine("There is no more room for accounts:");
}
}
break;
case 'd':
{
if (accountCounter == 0)
Console.WriteLine("There are no accounts to process");
break;
Console.WriteLine("Please enter an existing account number: ");
number = Console.Read();
for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
{
if (number != myAccounts[accountCounter])
{
Console.WriteLine("Account does not exist.");
Console.Write("Please enter an existing account number: ");
number = Console.Read();
}
}
Console.Write("Amount to add: ");
amount = Console.Read();
for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
{
if (amount < balance)
{
Console.WriteLine("Amount is less than current balance.");
Console.Write("Please enter an existing account number: ");
}
balance = Console.Read();
}
myAccounts[accountCounter].Deposit(amount);
}
case 'w':
{
if (accountCounter == 0)
Console.WriteLine("There are no accounts to process");
break;
Console.WriteLine("Please enter an existing account number: ");
number = Console.Read();
for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
{
while (number != Accounts[accountCounter])
Console.WriteLine("Account does not exist.");
Console.Write("Please enter an existing account number: ");
number = Console.Read();
}
Console.Write("Amount to withdraw: ");
amount = Console.Read();
for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
{
while (amount < balance)
Console.WriteLine("Amount is less than current balance.");
Console.Write("Amount to withdraw: ");
balance = Console.Read();
}
myAccounts[accountCounter].Withdraw(amount);
}
case 's':
{
Console.WriteLine("Account Deposit");
Console.WriteLine("--------------------");
for (accountCounter = 0; accountCounter < myAccounts.Length; accountCounter++)
{
Console.Write("{0:2c} {0:2c}", myAccounts[accountCounter].getNumber(), myAccounts[accountCounter].getBalance());
}
}
break;
default:
Console.WriteLine("Bye-bye");
}
}
}
}
}
And here is my Account.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program3
{
public class Account
{
private long number;
private float balance;
public Account(long num, float bal)
{
number = num;
balance = bal;
}
public void setNumber(long num)
{
number = num;
}
public long getNumber()
{
return number;
}
public void setBalance(float bal)
{
balance = bal;
}
public float getBalance()
{
return balance;
}
public void Deposit(float amount)
{
balance += amount;
}
public void Withdraw(float amount)
{
balance -= amount;
}
}
}
How can I fix these errors? I've tried using Accounts[accountCounter] instead, but then these are the errors that are detected:
Program.cs(79,47): error CS0118: 'Program3.Account' is a 'type' but is used like a 'variable'
Program.cs(116,50): error CS0118: 'Program3.Account' is a 'type' but is used like a 'variable'
How can I fix the errors so that my program executes correctly?

New Topic/Question
Reply



MultiQuote





|