EDN Admin
Well-known member
Hi,this is my code and i need help to perform the following operation..Can anyone pls help me
Step 1
In GetAccount method, modify its code so that if account is not found in the allAccounts list throw an exception with appropriate error message. The exception thrown from here will bubble up to the caller and has to handled by the caller.
Step 2
Wherever GetAccount method is in use (e.g. Show an Account choice) enclose relevant statements in try block which you suspect, may be affected by exception thrown by GetAccount. Write supporting catch to show user friendly message displaying account is not found.using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace CATS
{
public struct Customer
{
public string CustomerName;
public string CustomerAddress;
}
public enum AccountType
{
Current,
Savings
}
public delegate void BalanceChangeHandler(string message, decimal balanceNow);
public class BankAccount
{
private int _accountNumber;
public Customer Customer;
public decimal _balance;
public static int nextNumber = 1001;
public AccountType TypeAccount;
private static decimal QuickWithdrawAmount = 100;
public event BalanceChangeHandler BalanceChanged;
private Queue<BankTransaction> Transaction;
public int AccountNumber
{
get
{
return _accountNumber;
}
}
public decimal Balance
{
get
{
return _balance;
}
set
{
if (value >= 0)
{
_balance = value;
}
else
{
Console.WriteLine("Balance cannot be nagative");
}
}
}
private BankAccount()
{
this.Transaction = new Queue<BankTransaction>();
this._accountNumber = BankAccount.GetNextAccountNumber();
Console.WriteLine("Your new account number:{0} ", this.AccountNumber);
Console.WriteLine("r");
Console.WriteLine("Enter customer Name: ");
this.Customer.CustomerName = Console.ReadLine();
Console.WriteLine("nEnter customer Address:");
this.Customer.CustomerAddress = Console.ReadLine();
Console.WriteLine("nEnter Opening Balance:");
this.Balance = decimal.Parse(Console.ReadLine());
Console.WriteLine("press c or C for current and s or S for savings");
if ("cC".Contains(Console.ReadLine().Trim()))
{
this.TypeAccount = AccountType.Current;
}
else
{
this.TypeAccount = AccountType.Savings;
}
GC.SuppressFinalize(this);
}
public static BankAccount CreateNewAccount()
{
return new BankAccount();
}
public decimal Deposit(decimal amount)
{
if (amount > 0)
{
this.Balance += amount;
if (BalanceChanged != null)
{
string message = string.Format("nDeposited:{0},New Balance:{1} on{2}", amount, this.Balance, DateTime.Now);
BalanceChanged(message, this.Balance);
this.Transaction.Enqueue(new BankTransaction("Deposited", amount, DateTime.Now));
}
}
return this.Balance;
}
public bool WithDraw(decimal amount)
{
bool sufficientfunds = this.Balance >= amount;
if (sufficientfunds)
{
this.Balance -= amount;
if (BalanceChanged != null)
{
string message = string.Format("nWithdrawn:{0} ,New Balance: {1} on {2}", amount, this.Balance, DateTime.Now);
BalanceChanged(message, this.Balance);
this.Transaction.Enqueue(new BankTransaction("Withdrawn", amount, DateTime.Now));
}
}
return sufficientfunds;
}
public void ShowAccount()
{
Console.WriteLine("Account Number: {0}", this.AccountNumber);
Console.WriteLine("customer name: {0}", this.Customer.CustomerName);
Console.WriteLine("customer add: {0}", this.Customer.CustomerAddress);
Console.WriteLine("customer balance: {0}", this.Balance);
Console.WriteLine("customer type: {0}", this.TypeAccount);
this.GetAccounttrans();
Console.ReadLine();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("n AccountDetails");
sb.Append("n ===================");
sb.Append(string.Format("nAccount Number: {0}n ", this.AccountNumber));
sb.Append(string.Format("CustomerName Number: {0}n ", this.Customer.CustomerName));
sb.Append(string.Format("Account Number: {0}n ", this.TypeAccount.ToString()));
sb.Append(string.Format("Account Number: {0} n", this.Balance));
return sb.ToString();
}
private static int GetNextAccountNumber()
{
return nextNumber++;
}
public void Dispose()
{
Console.WriteLine("nDear customer {0} we are now closing this account" + "nplease collect your cheque of {1} from the bank ", this.Customer.CustomerName, this.Balance);
Console.ReadLine();
}
private void GetAccounttrans()
{
foreach (var bankTransaction in this.Transaction)
{
Console.WriteLine(bankTransaction);
}
}
class BankTransaction
{
string tranDesc;
decimal tranAmount;
DateTime tranWhen;
public string Description
{
get
{
return tranDesc;
}
}
public decimal Amount
{
get
{
return tranAmount;
}
}
public DateTime When
{
get
{
return tranWhen;
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("n TransactionDetails");
sb.Append("n ==================");
sb.Append(string.Format("nTransaction Description: {0}", tranDesc));
sb.Append(string.Format("nTransaction Amount : {0}", tranAmount));
sb.Append(string.Format("nTransaction When: {0}", tranWhen));
return sb.ToString();
}
public BankTransaction(string Desc, decimal amt, DateTime dt)
{
tranDesc = Desc;
tranAmount = Amount;
tranWhen = dt;
}
}
}
class Program
{
static ArrayList allAccounts = new ArrayList();
static void Main(string[] args)
{
if (Login())
{
StartBanking();
Console.WriteLine("Thank You!!!");
Console.ReadLine();
}
else
{
Console.WriteLine("n Invalid Please Try Again");
Console.ReadLine();
return;
}
}
static void StartBanking()
{
int screenChoice = -1;
do
{
Console.WriteLine("r");
Console.WriteLine("1.Teller screen");
Console.WriteLine("r");
Console.WriteLine("2.Customer screen");
Console.WriteLine("r");
Console.WriteLine("0.Quit");
Console.WriteLine("r");
//Console.WriteLine("Enter Your choice or press any key to exit");
bool success = int.TryParse(Console.ReadLine(), out screenChoice);
if (success)
{
switch (screenChoice)
{
case 1:
TellerScreen();
break;
case 2:
CustomerScreen();
break;
default:
Console.WriteLine("nnTHANK U FOR USING CATS:nn");
System.Environment.Exit(0);
break;
}
}
else
{
screenChoice = -1;
Console.WriteLine("nInvalid Please Try Again");
}
Console.ReadLine();
}
while (screenChoice != 0);
}
static void account_BalanceChanged(string message, decimal balanceNow)
{
Console.WriteLine("sms:" + message);
}
static bool Login()
{
bool valid = false;
string username = string.Empty;
string password = string.Empty;
Console.WriteLine("t WELCOME TO CATSt");
Console.WriteLine("n Please Enter Your Username and Passwordn");
Console.WriteLine(" Username: n");
username = Console.ReadLine().Trim();
Console.WriteLine("n Password :n");
Console.WriteLine("r");
password = Console.ReadLine().Trim();
if (username.ToUpper() == "ADMIN" && password.Equals("Pa$$w0rd"))
valid = true;
else
valid = false;
return valid;
}
public static void CustomerScreen()
{
int customerchoice = -1;
BankAccount customerAccount = null;
customerAccount = GetAccount();
if (customerAccount == null)
{
Console.WriteLine("nAccount Does not Exists");
return;
}
do
{
Console.WriteLine("r");
Console.WriteLine("n1.Show my balancen");
Console.WriteLine("n2.Withdrawn");
Console.WriteLine("n3.Quick withdrawn");
Console.WriteLine("n4.Depositn");
Console.WriteLine("n5.Show account detailsn");
Console.WriteLine("n0.Quitn");
Console.WriteLine("nEnter Your Choice or Press Any Key To Exit");
bool success = int.TryParse(Console.ReadLine(), out customerchoice);
if (success)
{
switch (customerchoice)
{
case 1:
customerAccount.ShowAccount();
break;
case 2:
Console.Write("nEnter amount to withdraw: ");
decimal amt1 = decimal.Parse(Console.ReadLine());
customerAccount.WithDraw(amt1);
break;
case 3:
Console.Write("nYou Requested Quick withdraw: ");
customerAccount.WithDraw(100);
break;
case 4:
Console.Write("nEnter amount to deposit ");
decimal amt2 = decimal.Parse(Console.ReadLine());
customerAccount.Deposit(amt2);
break;
case 5:
customerAccount.ShowAccount();
break;
case 0:
return;
default:
Console.WriteLine("nInvalid Please Try Again!!!!!!");
break;
}
}
else
{
customerchoice = -1;
Console.WriteLine("nInvalid Please Try Again!!!!!!");
}
Console.ReadLine();
}
while (customerchoice != 0);
}
public static void TellerScreen()
{
int tellerchoice = -1;
do
{
Console.WriteLine("r");
Console.WriteLine("n1. Create new account: ");
Console.WriteLine("n2. Display account details: ");
Console.WriteLine("n3. Delete an account: ");
Console.WriteLine("n4. Display all accounts: ");
// Console.WriteLine("5.show account details");
Console.WriteLine("n0. Quit: ");
Console.WriteLine("nEnter Your Choice or Press Any Key To Exitn");
bool success = int.TryParse(Console.ReadLine(), out tellerchoice);
if (success)
{
switch (tellerchoice)
{
case 1:
BankAccount account = BankAccount.CreateNewAccount();
if (account != null)
{
Console.WriteLine("nAccount successfully Created!!");
account.BalanceChanged += new BalanceChangeHandler(account_BalanceChanged);
allAccounts.Add(account);
}
break;
case 2:
BankAccount customerAccount = null;
customerAccount = GetAccount();
if (customerAccount == null)
{
Console.Write("nAccount Does not exists");
}
else
{
customerAccount.ShowAccount();
}
break;
case 3:
BankAccount delCustomerAccount = null;
delCustomerAccount = GetAccount();
if (delCustomerAccount == null)
{
Console.Write("nAccount Does not exists");
}
else
{
delCustomerAccount.Dispose();
allAccounts.Remove(delCustomerAccount);
}
break;
case 4:
Console.Write("nAll Account Details...");
Console.Write("n=======================");
foreach (BankAccount current in allAccounts)
{
BankAccount ba = (BankAccount)current;
Console.WriteLine(ba.ToString());
Console.ReadLine();
}
break;
case 0:
return;
default:
Console.WriteLine("nInvalid Please Try Again!!!");
break;
}
}
else
{
tellerchoice = -1;
Console.WriteLine("nInvalid Please Try Again!!!");
}
Console.ReadLine();
}
while (tellerchoice != 0);
}
static BankAccount GetAccount()
{
BankAccount customerAccount = null;
long accountNumber = 0;
Console.WriteLine("nEnter Your Account Number: ");
if (long.TryParse(Console.ReadLine(), out accountNumber))
{
foreach (BankAccount acct in allAccounts)
{
BankAccount current = acct;
if (current.AccountNumber == accountNumber)
{
customerAccount = current;
break;
}
}
}
else
{
Console.WriteLine("nInvalid Please Try Again!!! ");
}
return customerAccount;
}
}
}
View the full article
Step 1
In GetAccount method, modify its code so that if account is not found in the allAccounts list throw an exception with appropriate error message. The exception thrown from here will bubble up to the caller and has to handled by the caller.
Step 2
Wherever GetAccount method is in use (e.g. Show an Account choice) enclose relevant statements in try block which you suspect, may be affected by exception thrown by GetAccount. Write supporting catch to show user friendly message displaying account is not found.using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace CATS
{
public struct Customer
{
public string CustomerName;
public string CustomerAddress;
}
public enum AccountType
{
Current,
Savings
}
public delegate void BalanceChangeHandler(string message, decimal balanceNow);
public class BankAccount
{
private int _accountNumber;
public Customer Customer;
public decimal _balance;
public static int nextNumber = 1001;
public AccountType TypeAccount;
private static decimal QuickWithdrawAmount = 100;
public event BalanceChangeHandler BalanceChanged;
private Queue<BankTransaction> Transaction;
public int AccountNumber
{
get
{
return _accountNumber;
}
}
public decimal Balance
{
get
{
return _balance;
}
set
{
if (value >= 0)
{
_balance = value;
}
else
{
Console.WriteLine("Balance cannot be nagative");
}
}
}
private BankAccount()
{
this.Transaction = new Queue<BankTransaction>();
this._accountNumber = BankAccount.GetNextAccountNumber();
Console.WriteLine("Your new account number:{0} ", this.AccountNumber);
Console.WriteLine("r");
Console.WriteLine("Enter customer Name: ");
this.Customer.CustomerName = Console.ReadLine();
Console.WriteLine("nEnter customer Address:");
this.Customer.CustomerAddress = Console.ReadLine();
Console.WriteLine("nEnter Opening Balance:");
this.Balance = decimal.Parse(Console.ReadLine());
Console.WriteLine("press c or C for current and s or S for savings");
if ("cC".Contains(Console.ReadLine().Trim()))
{
this.TypeAccount = AccountType.Current;
}
else
{
this.TypeAccount = AccountType.Savings;
}
GC.SuppressFinalize(this);
}
public static BankAccount CreateNewAccount()
{
return new BankAccount();
}
public decimal Deposit(decimal amount)
{
if (amount > 0)
{
this.Balance += amount;
if (BalanceChanged != null)
{
string message = string.Format("nDeposited:{0},New Balance:{1} on{2}", amount, this.Balance, DateTime.Now);
BalanceChanged(message, this.Balance);
this.Transaction.Enqueue(new BankTransaction("Deposited", amount, DateTime.Now));
}
}
return this.Balance;
}
public bool WithDraw(decimal amount)
{
bool sufficientfunds = this.Balance >= amount;
if (sufficientfunds)
{
this.Balance -= amount;
if (BalanceChanged != null)
{
string message = string.Format("nWithdrawn:{0} ,New Balance: {1} on {2}", amount, this.Balance, DateTime.Now);
BalanceChanged(message, this.Balance);
this.Transaction.Enqueue(new BankTransaction("Withdrawn", amount, DateTime.Now));
}
}
return sufficientfunds;
}
public void ShowAccount()
{
Console.WriteLine("Account Number: {0}", this.AccountNumber);
Console.WriteLine("customer name: {0}", this.Customer.CustomerName);
Console.WriteLine("customer add: {0}", this.Customer.CustomerAddress);
Console.WriteLine("customer balance: {0}", this.Balance);
Console.WriteLine("customer type: {0}", this.TypeAccount);
this.GetAccounttrans();
Console.ReadLine();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("n AccountDetails");
sb.Append("n ===================");
sb.Append(string.Format("nAccount Number: {0}n ", this.AccountNumber));
sb.Append(string.Format("CustomerName Number: {0}n ", this.Customer.CustomerName));
sb.Append(string.Format("Account Number: {0}n ", this.TypeAccount.ToString()));
sb.Append(string.Format("Account Number: {0} n", this.Balance));
return sb.ToString();
}
private static int GetNextAccountNumber()
{
return nextNumber++;
}
public void Dispose()
{
Console.WriteLine("nDear customer {0} we are now closing this account" + "nplease collect your cheque of {1} from the bank ", this.Customer.CustomerName, this.Balance);
Console.ReadLine();
}
private void GetAccounttrans()
{
foreach (var bankTransaction in this.Transaction)
{
Console.WriteLine(bankTransaction);
}
}
class BankTransaction
{
string tranDesc;
decimal tranAmount;
DateTime tranWhen;
public string Description
{
get
{
return tranDesc;
}
}
public decimal Amount
{
get
{
return tranAmount;
}
}
public DateTime When
{
get
{
return tranWhen;
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("n TransactionDetails");
sb.Append("n ==================");
sb.Append(string.Format("nTransaction Description: {0}", tranDesc));
sb.Append(string.Format("nTransaction Amount : {0}", tranAmount));
sb.Append(string.Format("nTransaction When: {0}", tranWhen));
return sb.ToString();
}
public BankTransaction(string Desc, decimal amt, DateTime dt)
{
tranDesc = Desc;
tranAmount = Amount;
tranWhen = dt;
}
}
}
class Program
{
static ArrayList allAccounts = new ArrayList();
static void Main(string[] args)
{
if (Login())
{
StartBanking();
Console.WriteLine("Thank You!!!");
Console.ReadLine();
}
else
{
Console.WriteLine("n Invalid Please Try Again");
Console.ReadLine();
return;
}
}
static void StartBanking()
{
int screenChoice = -1;
do
{
Console.WriteLine("r");
Console.WriteLine("1.Teller screen");
Console.WriteLine("r");
Console.WriteLine("2.Customer screen");
Console.WriteLine("r");
Console.WriteLine("0.Quit");
Console.WriteLine("r");
//Console.WriteLine("Enter Your choice or press any key to exit");
bool success = int.TryParse(Console.ReadLine(), out screenChoice);
if (success)
{
switch (screenChoice)
{
case 1:
TellerScreen();
break;
case 2:
CustomerScreen();
break;
default:
Console.WriteLine("nnTHANK U FOR USING CATS:nn");
System.Environment.Exit(0);
break;
}
}
else
{
screenChoice = -1;
Console.WriteLine("nInvalid Please Try Again");
}
Console.ReadLine();
}
while (screenChoice != 0);
}
static void account_BalanceChanged(string message, decimal balanceNow)
{
Console.WriteLine("sms:" + message);
}
static bool Login()
{
bool valid = false;
string username = string.Empty;
string password = string.Empty;
Console.WriteLine("t WELCOME TO CATSt");
Console.WriteLine("n Please Enter Your Username and Passwordn");
Console.WriteLine(" Username: n");
username = Console.ReadLine().Trim();
Console.WriteLine("n Password :n");
Console.WriteLine("r");
password = Console.ReadLine().Trim();
if (username.ToUpper() == "ADMIN" && password.Equals("Pa$$w0rd"))
valid = true;
else
valid = false;
return valid;
}
public static void CustomerScreen()
{
int customerchoice = -1;
BankAccount customerAccount = null;
customerAccount = GetAccount();
if (customerAccount == null)
{
Console.WriteLine("nAccount Does not Exists");
return;
}
do
{
Console.WriteLine("r");
Console.WriteLine("n1.Show my balancen");
Console.WriteLine("n2.Withdrawn");
Console.WriteLine("n3.Quick withdrawn");
Console.WriteLine("n4.Depositn");
Console.WriteLine("n5.Show account detailsn");
Console.WriteLine("n0.Quitn");
Console.WriteLine("nEnter Your Choice or Press Any Key To Exit");
bool success = int.TryParse(Console.ReadLine(), out customerchoice);
if (success)
{
switch (customerchoice)
{
case 1:
customerAccount.ShowAccount();
break;
case 2:
Console.Write("nEnter amount to withdraw: ");
decimal amt1 = decimal.Parse(Console.ReadLine());
customerAccount.WithDraw(amt1);
break;
case 3:
Console.Write("nYou Requested Quick withdraw: ");
customerAccount.WithDraw(100);
break;
case 4:
Console.Write("nEnter amount to deposit ");
decimal amt2 = decimal.Parse(Console.ReadLine());
customerAccount.Deposit(amt2);
break;
case 5:
customerAccount.ShowAccount();
break;
case 0:
return;
default:
Console.WriteLine("nInvalid Please Try Again!!!!!!");
break;
}
}
else
{
customerchoice = -1;
Console.WriteLine("nInvalid Please Try Again!!!!!!");
}
Console.ReadLine();
}
while (customerchoice != 0);
}
public static void TellerScreen()
{
int tellerchoice = -1;
do
{
Console.WriteLine("r");
Console.WriteLine("n1. Create new account: ");
Console.WriteLine("n2. Display account details: ");
Console.WriteLine("n3. Delete an account: ");
Console.WriteLine("n4. Display all accounts: ");
// Console.WriteLine("5.show account details");
Console.WriteLine("n0. Quit: ");
Console.WriteLine("nEnter Your Choice or Press Any Key To Exitn");
bool success = int.TryParse(Console.ReadLine(), out tellerchoice);
if (success)
{
switch (tellerchoice)
{
case 1:
BankAccount account = BankAccount.CreateNewAccount();
if (account != null)
{
Console.WriteLine("nAccount successfully Created!!");
account.BalanceChanged += new BalanceChangeHandler(account_BalanceChanged);
allAccounts.Add(account);
}
break;
case 2:
BankAccount customerAccount = null;
customerAccount = GetAccount();
if (customerAccount == null)
{
Console.Write("nAccount Does not exists");
}
else
{
customerAccount.ShowAccount();
}
break;
case 3:
BankAccount delCustomerAccount = null;
delCustomerAccount = GetAccount();
if (delCustomerAccount == null)
{
Console.Write("nAccount Does not exists");
}
else
{
delCustomerAccount.Dispose();
allAccounts.Remove(delCustomerAccount);
}
break;
case 4:
Console.Write("nAll Account Details...");
Console.Write("n=======================");
foreach (BankAccount current in allAccounts)
{
BankAccount ba = (BankAccount)current;
Console.WriteLine(ba.ToString());
Console.ReadLine();
}
break;
case 0:
return;
default:
Console.WriteLine("nInvalid Please Try Again!!!");
break;
}
}
else
{
tellerchoice = -1;
Console.WriteLine("nInvalid Please Try Again!!!");
}
Console.ReadLine();
}
while (tellerchoice != 0);
}
static BankAccount GetAccount()
{
BankAccount customerAccount = null;
long accountNumber = 0;
Console.WriteLine("nEnter Your Account Number: ");
if (long.TryParse(Console.ReadLine(), out accountNumber))
{
foreach (BankAccount acct in allAccounts)
{
BankAccount current = acct;
if (current.AccountNumber == accountNumber)
{
customerAccount = current;
break;
}
}
}
else
{
Console.WriteLine("nInvalid Please Try Again!!! ");
}
return customerAccount;
}
}
}
View the full article