คลาส Bank แสดงตัวอย่างการตรวจสอบ Error แล้วทำการ throw
แบบต่างๆ
- docs.microsoft.com – Introduction to classes
Transaction.cs
using System; namespace ConsoleApp1 { class Transaction { public decimal Amount { get; } public DateTime Date { get; } public string Notes { get; } public Transaction(decimal amount, DateTime date, string note) { this.Amount = amount; this.Date = date; this.Notes = note; } } }
BankAccount.cs
using System; using System.Collections.Generic; namespace ConsoleApp1 { class BankAccount { public string Number { get; } public string Owner { get; set; } #region BalanceComputation public decimal Balance { get { decimal balance = 0; foreach (var item in allTransactions) { balance += item.Amount; } return balance; } } #endregion private static int accountNumberSeed = 1234567890; #region Constructor public BankAccount(string name, decimal initialBalance) { this.Number = accountNumberSeed.ToString(); accountNumberSeed++; this.Owner = name; MakeDeposit(initialBalance, DateTime.Now, "Initial balance"); } #endregion #region TransactionDeclaration private List<Transaction> allTransactions = new List<Transaction>(); #endregion #region DepositAndWithdrawal public void MakeDeposit(decimal amount, DateTime date, string note) { if (amount <= 0) { throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive"); } var deposit = new Transaction(amount, date, note); allTransactions.Add(deposit); } public void MakeWithdrawal(decimal amount, DateTime date, string note) { if (amount <= 0) { throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive"); } if (Balance - amount < 0) { throw new InvalidOperationException("Not sufficient funds for this withdrawal"); } var withdrawal = new Transaction(-amount, date, note); allTransactions.Add(withdrawal); } #endregion #region History public string GetAccountHistory() { var report = new System.Text.StringBuilder(); report.AppendLine("Date\t\tAmount\tNote"); foreach (var item in allTransactions) { report.AppendLine($"{item.Date.ToShortDateString()}\t{item.Amount}\t{item.Notes}"); } return report.ToString(); } #endregion } }
Program.cs
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var account = new BankAccount("Jack", 1000); Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance} balance."); account.MakeWithdrawal(500, DateTime.Now, "Rent payment"); Console.WriteLine("Account Balance : " + account.Balance); account.MakeDeposit(100, DateTime.Now, "friend paid me back"); Console.WriteLine("Account Balance : " + account.Balance); Console.WriteLine(account.GetAccountHistory()); // Test that the initial balances must be positive: Console.WriteLine("---Test that the initial balances must be positive:---"); try { var invalidAccount = new BankAccount("invalid", -55); } catch (ArgumentOutOfRangeException e) { Console.WriteLine("Exception caught creating account with negative balance"); Console.WriteLine(e.ToString()); } // Test for a negative balance Console.WriteLine("---Test for a negative balance---"); try { account.MakeWithdrawal(750, DateTime.Now, "Attempt to overdraw"); } catch (InvalidOperationException e) { Console.WriteLine("Exception caught trying to overdraw"); Console.WriteLine(e.ToString()); } } } }
ผลการรัน
> dotnet run Account 1234567890 was created for Jack with 1000 balance. Account Balance : 500 Account Balance : 600 Date Amount Note 6/14/2019 1000 Initial balance 6/14/2019 -500 Rent payment 6/14/2019 100 friend paid me back ---Test that the initial balances must be positive:--- Exception caught creating account with negative balance System.ArgumentOutOfRangeException: Amount of deposit must be positive Parameter name: amount at ConsoleApp1.BankAccount.MakeDeposit(Decimal amount, DateTime date, String note) in C:\Project\ConsoleApp\ConsoleApp1\BankAccount.cs:line 52 at ConsoleApp1.BankAccount..ctor(String name, Decimal initialBalance) in C:\Project\ConsoleApp\ConsoleApp1\BankAccount.cs:line 37 at ConsoleApp1.Program.Main(String[] args) in C:\Project\ConsoleApp\ConsoleApp1\Program.cs:line 23 ---Test for a negative balance--- Exception caught trying to overdraw System.InvalidOperationException: Not sufficient funds for this withdrawal at ConsoleApp1.BankAccount.MakeWithdrawal(Decimal amount, DateTime date, String note) in C:\Project\ConsoleApp\ConsoleApp1\BankAccount.cs:line 66 at ConsoleApp1.Program.Main(String[] args) in C:\Project\ConsoleApp\ConsoleApp1\Program.cs:line 35