For the WithdrawTransaction class, in the Execute method, notice
that the code repeats what the code in Withdraw in Account class
does. Therefore, we can simply call that one instead! Capture
the result into the _success variable, then use it to decide when
to throw the InvalidOperationException.
The same can be implemented for the RollBack method in the same
class, it can call the deposit method.
Then, do the same in DepositTransaction and TransferTransaction
(where you can).
Try and catch the thrown errors from the method that makes the
call in BankSystem, design wise, it is not very useful to try and
catch from the same block that throws the error.
After each successful transaction, give the user the option to
rollback the transaction.
For example, you could ask the user after each transaction if
they want to rollback their transaction, if yes, then reverse,
otherwise, transaction executes as normal
Account.cs
BankSystem.cs
DepositTransaction.cs
TransferTransaction.cs
using System; 1 2 3 4 5 6 7 8 9, namespace BankSystem { 참조 22개 class Account { private decimal _balance; private String _name: 10 11 12 13 14 15 참조 3개 public Account (String name, decimal balance) { this._balance = balance: this._name = name; } 16 17 18 19 20 21 참조 3개 public void Print { Console.WriteLine("Account name: " + this._name); Console.WriteLine("Available balance: " + this._balance); 참조 4개 public String Name() => this._name: 22 23 24 참조 4개 public Decimal Balance() => this._balance: 참조 4개 public void setBalace(decimal amount) { this._balance = amount; 25 26 27 28 29 30 31 32 참조 0개 public bool Deposit(decimal ammount) { bool deposited = false;
if (ammount < 0 { Console.WriteLine("Invalid number. Please re-enter a valid number. "); deposited = false; 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 else if (ammount >= 0) { this._balance += ammount; deposited = true; return deposited; 8mm和印配时如明加印加卵mmmmmBB = 48 49 50 51 52 53 참조 0개 public bool Withdraw(decimal ammount) { bool withdraw = false; if (ammount < 0) { Console.WriteLine("Invalid number. Please re-enter a valid number. "); withdraw = false; 54 else if (ammount > this._balance) Console.WriteLine("You can not withdraw more than your balance!"); 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 else { this._balance -= ammount: withdraw = true; } return withdraw;
using System; 1 1 2 3 4. 5 6 7 8 9 10 11 12 13 참조 10개 Opublic enum MenuOptions { Deposit = 1, Withdraw = 2. Transfer = 3 Print = 4, Quit = 5 14 15 BBFB9%n%阳阳阳阳9mm配留的困 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 namespace BankSystem { 참조 0개 class BankSystem { 참조 0개 static void Main(string[] args) { Account A = new Account("Fred", 1000); Account B = new Account ("Karl", 3000); Account C = new Account ("Dana", 6000); int Option; do { { Console.WriteLine("Bank System Options are: "); foreach (MenuOptions choices in Enum. GetValues(typeof (MenuOptions))) { Console.WriteLine("-{0}. {1}", (int) choices, choices); } Console.Write("Select an option: "); Option = Convert.Tolnt32 (Console.ReadLine(); MenuOptions option = (MenuOptions) Option; switch (option) { I case MenuOptions. Deposit:
Console.WriteLine(Convert.ToString (option)); DoDeposit(A); break; } 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 case MenuOptions. Withdraw: { Console.WriteLine(Convert.ToString (option)); DOW i thdraw (A); break; } case MenuOptions. Transfer: { Console.WriteLine(Convert.ToString (option)); DoTransfer (B, C); 588加4%的卵耶耶耶耶mm配的田8588mmugg阳阳mg昭和初对比 break; case MenuOptions.Print: { Console.WriteLine(Convert.ToString (option)); DOPrint(A,B,C);. break; 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 case MenuOptions. Quit: { Console.WriteLine(Convert.ToString (option)); break; default: {
Console.WriteLine("Invaild option. Please try again."); break; } } while (Option <= 0 || Option < Enum. GetValues(typeof (MenuOptions)).Length); 75 76 77 78 79 80 81 82 83 84 85 86 Console.Read(); 참조 1개 static void DoDeposit(Account account) { Console.WriteLine("Input amount"); decimal amount = Convert.ToDecimal(Console.ReadLine()); 西阳江阳时5%的的的的的的95%的明明明明明明 wwwggggwg 87 88 89 90 91 92 93 94 95 96 97 if (amount < 0) { Console.WriteLine("Invalid number. Please re-enter a valid number."); return; } Deposit Transaction DT = new Deposit Transaction(account, amount); DL Execute(); DL printO; 참조 1개 static void DoWithdraw(Account account) { Console.WriteLine("Input amount"); decimal amount = Convert.ToDecimal(Console.ReadLine(); = 100 101 102 103 104 105 106 107 108 109 110 111 if (amount < 0) { Console.WriteLine("Invalid number. Please re-enter a valid number. "); return; } WithdrawTransaction WT = new WithdrawTransaction (account, amount); WT.Execute(); WL.printo;
112 참조 1개 static void DoTransfer (Account fromáccount, Account tokccount) { Console.WriteLine("Input amount"); decimal amount = Convert.ToDecimal(Console.ReadLine(); 113 114 115 116 117 118 119 120 121 122 123 124 125 if (amount < 0) { Console.WriteLine("Invalid number. Please re-enter a valid number."); return; } Transfer Transaction IT = new Transfer Transaction(fromAccount, toaccount, amount); IL.Execute(); IL.print; 참조 1개 static void DoPrint (Account A, Account B, Account C) 126 127 128 129 130 131 132 133 A. Print(); B.Print); C.Print();
using Systemi 1 2 3 4. 5 6 7 8 9 9 10 11 12 namespace BankSystem { 참조 5개 class Withdraw Transaction { private Account _account: private decimal „amount: private Boolean _executed; private Boolean _success; private Boolean _reversed; 참조 개 public Boolean Executed -> this._executed; 13 14 참조 2개 public Boolean Success () => this._success; 15 16 참조 1개 public Boolean Rever esedo => this._reversed: 17 186 19 20 21 22 23 24 참조 2개 public Withdraw Transaction (Account account, decimal amount) { this._account = account: this._amount = amount; } 25 26 27 28 29 30 31 32 33 참조 1개 public void print () { if (success) { Console.WriteLine( _amount + was withdrawn from ' + _account. Name + "H's account"); +
34 참조 2개 public void Execute() { decimal balance = _account. Balance 0); try { if (executed) { throw new InvalidOperationException ("Transaction has already been attempted"); } if (_amount > balance) { _success = false; throw new InvalidOperationException("Insufficient balance! You can not withdraw more than your balance!"); } else { balance = balance - _amount: _account.setBalace(balance); 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 郎困困m距朗加到犯时时距矿狙即加58mm田田58mmaw的阳阳688 _success = true; _executed = true; } catch (InvalidOperationException ex) { { Console.WriteLine(ex); 59 60 61 62 63 64 65 66 67 참조 1개 public void Rollback { decimal balance = _account. Balance 0); try { if (Success && !Rever esedo) { 69 70
balance . balance + _amount: _account.setBalace(balance); _reversed = true; else { throw new Invalid OperationException("Original transaction not completed"); 门把河过河相沿mmwwmm 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 catch (Exception ex) Console.WriteLine(ex);
using System; 1 2 3 4 5 6 7 8 9 10 11 12 namespace BankSystem { 참조 5개 class Deposit Transaction { private Account account: private decimal amount: private Boolean _executed: private Boolean _success; private Boolean _reversed; 참조 0개 public Boolean Executedo => this._executed: 13 14. 참조 2개 public Boolean Success () => this._success; 15 16 17 18 참조 1개 public Boolean Reveresed( => this._reversed; 19 20 21 22 23 참조 2개 public Depos itTransaction (Account account, decimal amount) { this._account = account: this._amount = amount: 240 참조 1개 public void print () { if (success) { Console.WriteLine( _amount + 25 26 27 28 29 30 31 32 È II was Deposit to " + _account.Name() + "Y's account"); 참조 2개
33 34 35 참조 2개 public void Execute0 { decimal balance = _account. Balance(; try { if (_executed) { throw new InvalidOperationException ("Transaction has already been attempted"); } else { balance = balance + _amount: _account.setBalace(balance); _success = true; } _executed = true; 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 昭明明距印%即如以昭昭阳阳阳明mmmmmmmmmwu8 catch (InvalidOperationException ex) { Console.WriteLine(ex); 56 57 58 59 60 61 62 63 64 65 66 67 68 69 참조 1개 public void Rollback() { decimal balance = _account. Balance 0; try { if (Success && !Reveresed() { balance = balance - _amount: _account.setBalace(balance); _reversed = true; } else { throw new Invalid OperationException("Original transaction not completed"); TUSSUUNTIOLA
} catch (Exception ex) 和可视对对对相相 Console.WriteLine(ex); 71 72 73 74 75 76 77 78
using System; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 namespace BankSystem { 참조 3개 class Transfer Transaction { private Account fromáccount; private Account _toAccount: private decimal _amount: private Deposit Transaction depositi private WithdrawTransaction withdrawi private Boolean _executed; private Boolean _success: private Boolean _reversed; 참조 0개 public Boolean Executed => this._executed: 16 17 참조 1개 public Boolean Success () => this._success; 18 196 참조 1개 public Boolean Reveresed() => this._reversed; 20 21 22 23 24 25 26 27 28 29 30 참조 1개 public Transfer Transaction (Account fromáccount, Account toaccount, decimal amount) { this._fromáccount = fromáccount; this._tosccount = tosccount; this._amount = amount; _deposit = new Deposit Transaction (toaccount, amount); _withdraw = new WithdrawTransaction(fromaccount, amount); 31 32 33 참조 1개 public void print { Console.WriteLine("Transferred " + _amount + " from " + _fromAccount Name() + "'s account To + _fromáccount.Name() + "'s account To " + _toaccount. Name() + "H's account");
} 34 35 36 37 38 39 40 41 42 43 44 45 참조 1개 public void Execute() { _withdraw.Execute(); _deposit. Execute(); if (_withdraw. Success () && _deposit. Success () _success = true; _executed = true; F 叫阳阳mmmmmw昭阳阳阳阳mmBB叫阳阳mmmmm&Bg 참조 개 public void Rollback { try { if (Successo && !Reveresed() { throw new InvalidOperationException("Original transaction not completed"); 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 deposit. Rollback O; _withdraw. Rollback O; _reversed = true; } catch (Exception ex) { Console.WriteLine(ex);
For the WithdrawTransaction class, in the Execute method, notice that the code repeats what the code in Withdraw in Acco
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
For the WithdrawTransaction class, in the Execute method, notice that the code repeats what the code in Withdraw in Acco
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!