Question
I have created a bank code below and have been told the following issues with my code; 1. even if transaction wasn't successful, you shouldn't
I have created a bank code below and have been told the following issues with my code;
1. even if transaction wasn't successful, you shouldn't be able to re-attempt Execute(), currently your code does allow for that to happen.
2. Your rollback() can happen even if the transaction was unsuccessful.
3. print() should offer more details of the different states of the transaction
4. TransferTransaction rollback() method is incorrect and needs to roll back both parts of the transaction, currently you are calling the withdraw rollback even if the deposit roll back throws an exception
Can you please advise how i could correct my code, this is the whole code files;
using System.Xml.Linq; using BankSystem;
namespace mainAccount { public class Account { private string name; private double balance;
public Account(string name, double balance) { this.name = name; this.balance = balance; }
public void Deposit(double amount) { balance += amount; }
public bool Withdraw(double amount) { if (amount <= balance) { balance -= amount; return true; } else { return false; } }
public double GetBalance() { return balance; }
internal bool Deposit(decimal amount) { throw new NotImplementedException(); } } }
using System; using WESTPAC;
using static WESTPAC.BankSystem; using static WESTPAC.Deposit;
namespace WESTPAC { public class BankSystem {
public class Account { private decimal _balance; public string Name { get; set; } public int AccountNumber { get; set; }
public Account(string name, decimal balance) { Name = name; _balance = balance; AccountNumber = new Random().Next(100000, 999999); }
public decimal GetBalance() { return _balance; }
public void Deposit(decimal amount) { if (amount <= 0) { throw new InvalidOperationException("Invalid deposit amount."); }
_balance += amount; }
public void Withdraw(decimal amount) { if (_balance < amount) { throw new InvalidOperationException("Insufficient funds."); }
_balance -= amount; } }
public class Transaction { public Account Account { get; } public decimal Amount { get; }
public Transaction(Account account, decimal amount) { Account = account; Amount = amount; }
public virtual void Execute() } throw new NotImplementedException(); }
public virtual void Print() { throw new NotImplementedException(); } }
public class WithdrawTransaction : Transaction { public WithdrawTransaction(Account account, decimal amount) : base(account, amount) { }
public override void Execute() { Account.Withdraw(Amount); }
public override void Print() { Console.WriteLine(quot;Withdrew {Amount:C} from account{Account.AccountNumber}"); } }
public class TransferTransaction : Transaction { public Account ToAccount { get; }
public TransferTransaction(Account fromAccount, Account toAccount, decimal amount) : base(fromAccount, amount) { ToAccount = toAccount; }
public override void Execute() { if (Account.GetBalance() < Amount) { throw new InvalidOperationException("Insufficient funds."); }
Account.Withdraw(Amount); ToAccount.Deposit(Amount); }
public override void Print() { Console.WriteLine(quot;Transferred {Amount:C} from account { Account.AccountNumber} to account { ToAccount.AccountNumber} "); } }
public class BankSystem { private static readonly Account[] _accounts = new Account[] { new Account("Ria's Account", 1350), new Account("John's Account", 2500), new Account("Mary's Account", 5000) };
public static Account? GetAccountByNumber(int accountNumber) { foreach (Account account in _accounts) { if (account.AccountNumber == accountNumber) { return account; } }
return null; }
public static int ReadAccountNumber() { Console.Write("Enter account number: "); int accountNumber = int.Parse(Console.ReadLine() ?? ""); return accountNumber; }
public static decimal ReadAmount() { Console.Write("Enter amount: "); decimal amount = decimal.Parse(Console.ReadLine() ?? "");
return amount; }
public static void DoTransfer() { Console.WriteLine("Transfer");
int fromAccountNumber = ReadAccountNumber();
Account fromAccount = GetAccountByNumber(fromAccountNumber);
int toAccountNumber = ReadAccountNumber(); Account toAccount = GetAccountByNumber(toAccountNumber); if (fromAccount == null) { Console.WriteLine(quot;Account with number {fromAccountNumber} not found."); return; } if (toAccount == null) { Console.WriteLine(quot;Account with number {toAccountNumber} not found."); return; }
try { var transaction = new TransferTransaction(fromAccount, toAccount, amount); transaction.Execute(); Console.WriteLine("Transfer successful."); transaction.Print(); } catch (InvalidOperationException ex) { Console.WriteLine(ex.Message); } }
public static void DoWithdraw() { Console.WriteLine("Withdraw");
int accountNumber = ReadAccountNumber(); Account account = GetAccountByNumber(accountNumber);
decimal amount = ReadAmount();
if (account == null) { Console.WriteLine(quot;Account with number {accountNumber} not found."); return; }
try { var transaction = new WithdrawTransaction(account, amount); transaction.Execute(); Console.WriteLine("Withdrawal successful."); transaction.Print();
} catch (InvalidOperationException ex) { Console.WriteLine(ex.Message); } }
public static void Main() { while (true) { Console.WriteLine("Select an option:"); Console.WriteLine("1. Transfer"); Console.WriteLine("2. Withdraw"); Console.WriteLine("3. Exit");
Console.Write("Option: "); string option = (Console.ReadLine() ?? "");
switch (option) { case "1": DoTransfer(); break;
case "2": DoWithdraw(); break;
case "3": return;
default: Console.WriteLine("Invalid option."); break; }
Console.WriteLine(); } } }
using System; using static WESTPAC.Deposit; using static WESTPAC.withDrawTranscation; using System.Security.Principal; using WESTPAC; using static WESTPAC.BankSystem; using static WESTPAC.Deposit;
namespace WESTPAC { public class TransferTranscation { public class Transfer { private readonly DepositTransaction _deposit; private readonly WithdrawTransaction _withdraw;
public bool Success => _deposit.Success && _withdraw.Success;
public Transfer(Account fromAccount, Account toAccount, decimal amount) { _withdraw = new WithdrawTransaction(fromAccount, amount); _deposit = new DepositTransaction(toAccount, amount); }
public void Execute() { if (_deposit.Executed || _withdraw.Executed) { throw new InvalidOperationException("This transaction has already been executed."); }
_withdraw.Execute();
try { _deposit.Execute(); } catch (InvalidOperationException) { // Rollback the withdraw transaction if the deposit transaction fails _withdraw.Rollback(); throw; } }
public void Rollback() { if (!_deposit.Success || !_withdraw.Success) { throw new InvalidOperationException("The transaction was not successful or has not been executed yet."); }
if (_deposit.IsReversed || _withdraw.IsReversed) { throw new InvalidOperationException("The transaction has already been reversed."); }
try { _deposit.Rollback(); } catch (InvalidOperationException) { // Rollback the withdraw transaction if the deposit transaction fails _withdraw.Rollback(); throw; }
_withdraw.Rollback(); } } } }
using System; using System.Security.Principal;
using static WESTPAC.BankSystem;
namespace WESTPAC { public class Deposit { public class DepositTransaction { private readonly Account _account; private readonly decimal _amount; private bool _executed; private bool _success; private bool _reversed;
public bool Executed => _executed; public bool Success => _success; public bool Reversed => _reversed;
public DepositTransaction(Account account, decimal amount) { _account = account; _amount = amount; }
public void Execute() { if (_executed && _success) { throw new Exception("This transaction has already been executed."); }
_executed = true; _success = _account.Deposit((double)_amount); }
public void Rollback() { if (!_executed) { throw new Exception("The transaction has not been executed yet."); }
if (_reversed) { throw new Exception("The transaction has already been reversed."); }
_reversed = true; _success = _account.Withdraw((double)_amount); }
public void Print() { if (_success) { Console.WriteLine(quot;Deposit of {_amount} to {_account.Name}'s account was successful."); } else { Console.WriteLine(quot;Deposit of {_amount} to {_account.Name}'s account failed."); } } } } }
using System.Security.Principal; using WESTPAC;
namespace WESTPAC { public class withDrawTranscation { public class WithdrawTransaction { private readonly Account _account; private readonly decimal _amount; private bool _executed; private bool _success; private bool _reversed;
public bool Executed => _executed; public bool Success => _success; public bool Reversed => _reversed;
public WithdrawTransaction(Account account, decimal amount) { _account = account; _amount = amount; }
public void Execute() { if (_executed && _success) { throw new Exception("This transaction has been done before"); }
_executed = true; _success = _account.Withdraw((double)_amount); }
public void Rollback() { if (!_executed) { throw new Exception("Sorry the Transaction has not been done!"); }
if (_reversed) { throw new Exception("Transaction has not been done!!"); }
_reversed = true; _success = _account.Deposit((double)_amount); }
public void Print() { if (_success) { Console.WriteLine(quot;Withdraw Was Successful {_amount} from {_account.Name}"); } else { Console.WriteLine(quot;The Withdraw has Failed {_amount} from {_account.Name}"); } } } } }
Step by Step Solution
3.52 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
To address the issues in your code lets go through each point and suggest corrections Prevent reattempting Execute after a transaction You can introdu...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started