Doubtfire University
Object Oriented Development
OnTrack Submission
Abstract Transactions
Submitted By:
Craig Darryl Peade Tutor:
cpeade Bahadorreza Ofoghi
2022/01/24 02:02
Outcome Weight
Evaluate Code ♦♦
Principles ♦♦
Build Programs ♦♦
Design ♦♦
Justify ♦♦
Task designs, communicates, evaluates, implements and tests simple small object orientated
programming code conforming to planned system solution structures and requirements including
coding conventions, tracing and debugging techniques to identify and correct issues with applied
principles of abstraction, encapsulation, inheritance and polymorphism
January 24, 2022
Produced by Doubtfire
File 1 of 8 Program.cs
1 using System;
2
3 public enum MenuOption
4 {
5 add_account,
6 withdraw,
7 deposit,
8 transfer,
9 print,
10 print_transactions,
11 quit,
12 }
13
14 public class Program
15 {
16 public static void Main()
17 {
18 Bank bank = new Bank();
19 MenuOption userSelection;
20
21 do
22 {
23 userSelection = readUserOption();
24 switch (userSelection)
25 {
26 case MenuOption.add_account:
27 {
28 doAddAccount(bank);
29 break;
30 }
31 case MenuOption.withdraw:
32 {
33 doWithdraw(bank);
34 break;
35 }
36 case MenuOption.deposit:
37 {
38 doDeposit(bank);
39 break;
40 }
41 case MenuOption.transfer:
42 {
43 doTransfer(bank);
44 break;
45 }
46 case MenuOption.print:
47 {
48 doPrint(bank);
49 break;
50 }
51 case MenuOption.print_transactions:
52 {
53 bank.PrintTransactionHistory();
Page 1 of 16
File 1 of 8 Program.cs
54 break;
55 }
56 case MenuOption.quit:
57 {
58 Console.WriteLine("Quitting now...goodbye BYES!");
59 break;
60 }
61 }
62 } while (userSelection != MenuOption.quit);
63 }
64
65 private static Account findAaccount(Bank bank)
66 {
67 Console.Write("Account name?: ");
68 string name = Console.ReadLine();
69 Account result = bank.GetAccount(name);
70
71 if (result == null)
72 {
73 Console.WriteLine($"No such account with name {name}.");
74 }
75
76 return result;
77 }
78
79 private static void doAddAccount(Bank bank)
80 {
81 Console.Write("Account name?: ");
82 string accountName = Console.ReadLine();
83
84 while (true)
85 {
86 try
87 {
88 Console.Write("Opening balance?: ");
89 decimal openingBalance;
90 openingBalance = Convert.ToDecimal(Console.ReadLine());
91
92 if (openingBalance < 0)
93 {
94 throw new Exception();
95 }
96
97 bank.AddAccount(new Account(accountName, openingBalance));
98 break;
99 }
100
101 catch
102 {
103 Console.WriteLine("INVALID opening balance! - Please enter valid
,→ balance! PLEASE!");
104 }
105 }
Page 2 of 16
File 1 of 8 Program.cs
106 }
107
108 private static MenuOption readUserOption()
109 {
110
111 int option;
112 Console.WriteLine("Please select from the following options [1-7]:");
113 Console.WriteLine("------------------------");
114 Console.WriteLine("1: Add Account");
115 Console.WriteLine("2: Withdraw");
116 Console.WriteLine("3: Deposit");
117 Console.WriteLine("4: Transfer");
118 Console.WriteLine("5: Print");
119 Console.WriteLine("6: Print Transactions");
120 Console.WriteLine("7: Quit");
121 Console.WriteLine("------------------------");
122
123 do
124 {
125 try
126 {
127 option = Convert.ToInt32(Console.ReadLine());
128 }
129 catch (Exception ex)
130 {
131 Console.WriteLine("Exceptoin: " + ex.Message);
132 Console.WriteLine("There was a problem parsing your selection.");
133 option = -1;
134 }
135 if (option > 7 || option < 1)
136 {
137 Console.WriteLine("Please select a valid number between 1 and 7.");
138 }
139 } while (option > 7 || option < 1);
140
141 return (MenuOption)(option - 1);
142 }
143
144 private static Account findAccount(Bank fromBank)
145 {
146 Console.Write("Enter Account Name: ");
147 String name = Console.ReadLine();
148 Account result = fromBank.GetAccount(name);
149
150 if (result == null)
151 {
152 Console.WriteLine($"No account found with the name {name}.");
153 }
154
155 return result;
156 }
157
158 public static void doWithdraw(Bank bank)
Page 3 of 16
File 1 of 8 Program.cs
159 {
160 Account account = findAccount(bank);
161 if (account == null) { return; }
162
163 decimal amount;
164 Console.Write("Please enter withdrawal amount: ");
165 try
166 {
167 amount = Convert.ToDecimal(Console.ReadLine());
168 WithdrawTransaction transaction = new WithdrawTransaction(account,
,→ amount);
169
170 bank.ExecuteTransaction(transaction);
171 if (transaction.Succeeded)
172 {
173 throw new Exception("Withdrawal SUCCESSFUL!");
174 }
175 }
176 catch (Exception ex)
177 {
178 Console.WriteLine(ex.Message);
179 }
180 }
181
182 private static void doDeposit(Bank bank)
183
184 {
185 Account account = findAccount(bank);
186 if (account == null) { return; }
187
188 decimal amount;
189 Console.Write("Please enter amount depositing into " + account.Name + " 's
,→ account.");
190 try
191 {
192 amount = Convert.ToDecimal(Console.ReadLine());
193 DepositTransaction depositTransaction = new DepositTransaction(account,
,→ amount);
194
195 bank.ExecuteTransaction(depositTransaction);
196 if (!depositTransaction.Succeeded)
197 {
198 throw new Exception("Deposit FAILED!-/-UNSUCCESSFUL!");
199 }
200 }
201 catch (Exception ex)
202 {
203 Console.WriteLine(ex.Message);
204 }
205 }
206
207 private static void doTransfer(Bank bank)
208 {
Page 4 of 16
File 1 of 8 Program.cs
209 try
210 {
211 Account toAccount = findAccount(bank);
212 if (toAccount == null) { return; }
213
214 Account fromAccount = findAccount(bank);
215 if (fromAccount == null) { return; }
216
217 if (fromAccount == toAccount)
218 {
219 throw new Exception("SAME ACCOUNT!...");
220 }
221
222 Console.Write("Please enter amount transferring into " + toAccount.Name
,→ + " 's account");
223 decimal amount = Convert.ToDecimal(Console.ReadLine());
224 TransferTransaction transferTransaction = new
,→ TransferTransaction(fromAccount, toAccount, amount);
225
226 try
227 {
228 bank.ExecuteTransaction(transferTransaction);
229 if (!transferTransaction.Succeeded)
230 {
231 throw new Exception("Transfer FAILED!-/-UNSUCCESSFUL!");
232 }
233 }
234 catch (Exception ex)
235 {
236 Console.WriteLine(ex.Message);
237 }
238 }
239 catch (Exception ex)
240 {
241 Console.WriteLine(ex.Message);
242 return;
243 }
244 }
245
246 private static void doPrint(Bank bank)
247 {
248 Account account = findAccount(bank);
249 if (account == null)
250 {
251 return;
252 }
253
254 account.Print();
255 }
256 }
257 //private static void doPrintTransactions(Bank bank)
258
Page 5 of 16
File 2 of 8 Bank.cs
1 using System;
2 using System.Collections.Generic;
3
4 public class Bank
5 {
6 private List<Account> _accounts;
7 private List<Transaction> _transactions;
8
9 public Bank()
10 {
11 this._accounts = new List<Account>();
12 this._transactions = new List<Transaction>();
13 }
14
15 public void AddAccount(Account account)
16 {
17 this._accounts.Add(account);
18 }
19
20 public Account GetAccount(string name)
21 {
22 foreach (Account acc in this._accounts)
23 {
24 if (acc.Name.ToLower().Trim() == name.ToLower().Trim())
25 {
26 return acc;
27 }
28 }
29
30 return null;
31 }
32
33 public List<Account> GetAccount_(string name)
34 {
35 List<Account> res = new List<Account>();
36 foreach (Account acc in this._accounts)
37 {
38 if (acc.Name.ToLower().Trim() == name.ToLower().Trim())
39 {
40 res.Add(acc);
41 }
42 }
43
44 return null;
45 }
46
47 public void ExecuteTransaction(Transaction transaction)
48 {
49 this._transactions.Add(transaction);
50 transaction.Execute();
51 }
52
53 public void PrintTransactionHistory()
Page 6 of 16
File 2 of 8 Bank.cs
54 {
55 foreach (Transaction transaction in this._transactions)
56 {
57 Console.Write(transaction.DateStamp + ": ");
58 transaction.Print();
59 }
60 }
61 }
Page 7 of 16
File 3 of 8 Account.cs
1 using System;
2
3 public class Account
4 {
5 private decimal _balance;
6 private string _name;
7
8 public string Name
9 {
10 get
11 {
12 return _name;
13 }
14 }
15
16 public Account(string name, decimal openingBalance)
17 {
18 _name = name;
19 _balance = openingBalance;
20 }
21
22 public bool Deposit(decimal amountToDeposit)
23 {
24 if (amountToDeposit > 0)
25 {
26 _balance += amountToDeposit;
27 return true;
28 }
29 return false;
30 }
31
32
33 public bool Withdraw(decimal amountToWithdraw)
34 {
35 if (amountToWithdraw <= 0 || _balance - amountToWithdraw < 0)
36 {
37 _balance -= amountToWithdraw;
38 return false;
39 }
40 return true;
41 }
42
43
44 public void Print()
45 {
46 Console.WriteLine($"The account name is: {_name}");
47 Console.WriteLine($"The account balance is: {_balance}");
48 }
49 }
50
51
52
53
Page 8 of 16
File 3 of 8 Account.cs
54
55
56
57
58
59
Page 9 of 16
File 4 of 8 Transaction.cs
1 using System;
2
3 public abstract class Transaction
4 {
5 protected decimal _amount;
6 protected bool _executed;
7 protected bool _reversed;
8 private DateTime _dateStamp;
9
10 public bool Executed
11
12 {
13 get { return this._executed; }
14 }
15
16 public bool Reversed
17 {
18 get { return this._reversed; }
19 }
20
21 public DateTime DateStamp
22 {
23 get { return this._dateStamp; }
24 }
25
26 public abstract bool Succeeded
27 {
28 get;
29 }
30
31 public Transaction(decimal amount)
32 {
33 this._amount = amount;
34 }
35
36 public abstract void Print();
37
38 public virtual void Execute()
39 {
40 if (this._executed)
41 {
42 throw new Exception("Already executed previously!");
43 }
44
45 this._executed = true;
46 this._dateStamp = DateTime.Now;
47 }
48
49 public virtual void Rollback()
50 {
51 if (!this._executed)
52 {
53 throw new Exception("Cannot rollback unexecuted transaction!");
Page 10 of 16
File 4 of 8 Transaction.cs
54 }
55
56 if (this._reversed)
57 {
58 throw new Exception("Cannot rollback reversed transaction - reversal
,→ processed");
59 }
60
61 this._reversed = true;
62 this._executed = false;
63 }
64 }
Page 11 of 16
File 5 of 8 WithdrawTransaction.cs
1 using System;
2
3 public class WithdrawTransaction : Transaction
4 {
5 private Account _account;
6 private bool _succeeded = false;
7
8 public override bool Succeeded
9 {
10 get { return this._succeeded; }
11 }
12
13 public WithdrawTransaction(Account account, decimal amount) : base(amount)
14 {
15 this._account = account;
16 }
17
18 public override void Execute()
19 {
20 base.Execute();
21 this._succeeded = this._account.Withdraw(this._amount);
22 }
23
24 public override void Rollback()
25 {
26 base.Rollback();
27 this._succeeded = !this._account.Deposit(this._amount);
28 }
29
30 public override void Print()
31 {
32 if (this._succeeded)
33 Console.WriteLine("A withdrawal of " + this._amount + " from " +
,→ this._account.Name + "'s account was successful");
34 else
35 {
36 Console.WriteLine("Withdrawal unsuccessful!");
37 if (this._reversed)
38 Console.WriteLine("Withdrawal reversed!");
39 }
40 }
41 }
Page 12 of 16
File 6 of 8 DepositTransaction.cs
1 using System;
2
3 public class DepositTransaction : Transaction
4 {
5 private Account _account;
6 private bool _succeeded = false;
7
8 public override bool Succeeded
9 {
10 get { return this._succeeded; }
11 }
12
13 public DepositTransaction(Account account, decimal amount) : base(amount)
14 {
15 this._account = account;
16 }
17
18 public override void Execute()
19 {
20 base.Execute();
21 this._succeeded = this._account.Deposit(this._amount);
22 }
23
24 public override void Rollback()
25 {
26 base.Rollback();
27 this._succeeded = !this._account.Withdraw(this._amount);
28 }
29
30 public override void Print()
31 {
32 if (this._succeeded)
33 Console.WriteLine("A deposit of " + this._amount + " into " +
,→ this._account.Name + "'s account was successful");
34 else
35 {
36 Console.WriteLine("Deposit unsuccessful!");
37 if (this._reversed)
38 Console.WriteLine("Deposit reversed!");
39 }
40 }
41 }
Page 13 of 16
File 7 of 8 TransferTransaction.cs
1 using System;
2
3 public class TransferTransaction : Transaction
4 {
5 private Account _fromAccount;
6 private Account _toAccount;
7 private WithdrawTransaction _theWithdraw;
8 private DepositTransaction _theDeposit;
9
10 public override bool Succeeded
11 {
12 get
13 {
14 if (this._theDeposit.Succeeded && this._theWithdraw.Succeeded)
15 return true;
16 else
17 return false;
18 }
19 }
20 public TransferTransaction(Account fromAccount, Account toAccount, decimal
,→ amount) : base(amount)
21 {
22 this._fromAccount = fromAccount;
23 this._toAccount = toAccount;
24 this._theWithdraw = new WithdrawTransaction(this._fromAccount,
,→ this._amount);
25 this._theDeposit = new DepositTransaction(this._toAccount, this._amount);
26 }
27
28 public override void Execute()
29 {
30 base.Execute();
31 this._theWithdraw.Execute();
32
33 if (this._theWithdraw.Succeeded)
34 {
35 this._theDeposit.Execute();
36 if (!this._theDeposit.Succeeded)
37 {
38 this._theWithdraw.Rollback();
39 this._executed = false;
40 }
41 //else{this._executed = true;}
42 }
43 else
44 {
45 throw new Exception("Cannot execute - withdrawal failed!");
46 }
47 }
48
49 public override void Rollback()
50 {
51 base.Rollback();
Page 14 of 16
File 7 of 8 TransferTransaction.cs
52
53 if (this._theWithdraw.Succeeded)
54 {
55 this._theWithdraw.Rollback();
56 }
57
58 if (this._theDeposit.Succeeded)
59 {
60 this._theDeposit.Rollback();
61 }
62 }
63 public override void Print()
64 {
65 if (this._theWithdraw.Succeeded && this._theDeposit.Succeeded)
66 {
67 Console.WriteLine("Transfer of " + this._amount + " from " +
,→ this._fromAccount.Name + "'s account to " + this._toAccount.Name +
68 "'s account was successful!");
69 Console.Write(" ");
70 this._theDeposit.Print();
71 Console.Write(" ");
72 this._theWithdraw.Print();
73 }
74 else
75 {
76 Console.WriteLine("Transfer FAILED-/-UNSUCCESSFUL!");
77 if (this._reversed)
78 Console.WriteLine("Transfer reversed!");
79 }
80 }
81 }
Page 15 of 16
File 8 of 8 Screenshot
Page 16 of 16