我正在开发一个简单的银行程序,但在回滚方法和将 _reversed 设置为 true 时遇到困难。我一直被告知,当且仅当回滚过程成功时,我需要将 _reversed 设置为 true。我觉得我正在这样做,但是如果有人可以审查我的代码,那将非常有帮助。
账户类别:
internal class Account
{
private decimal _balance;
private string _name;
// Constructor Method
public Account(string name, decimal balance)
{
_name = name;
_balance = balance;
}
// Property, which is read only
public string Name
{
get { return _name; }
}
// Public Methods
public bool Deposit(decimal amount)
{
if (amount > 0)
{
_balance += amount;
return true; // Deposit succeeded
}
else
{
Console.WriteLine("Amount to deposit needs to be greater than $0.00.");
return false; // Deposit failed as one cannot deposit a negative amount
}
}
public bool Withdraw(decimal amount)
{
if (amount > 0 && _balance >= amount)
{
_balance -= amount;
Console.WriteLine($"{amount} has been withdrawn from your account.");
return true; // Withdrawal succeeded
}
else if (amount <= 0)
{
Console.WriteLine("Amount to withdraw needs to be greater than $0.00.");
return false; // Withdrawal failed as withdrawal amount is negative number
}
else
{
Console.WriteLine("You have insuffucient funds to make this withdrawal.");
return false; // Withdrawal failed as there is insufficient funds
}
}
public void Print()
{
Console.WriteLine($"Name: {_name}");
Console.WriteLine($"Balance: {_balance}");
}
}
交易类别:
internal abstract class Transaction
{
protected decimal _amount;
protected bool _success;
private bool _executed;
private bool _reversed;
private DateTime _dateStamp;
public Transaction(decimal amount)
{
_amount = amount;
_executed = false;
_success = false;
_reversed = false;
_dateStamp = DateTime.MinValue;
}
public virtual void Execute()
{
if (_executed)
throw new InvalidOperationException("Transaction has already been executed.");
_executed = true;
_dateStamp = DateTime.Now; // Record the current time when Execute is called
}
public virtual void Rollback()
{
if (_executed)
{
if (!_reversed)
{
_reversed = true;
}
}
}
public abstract void Print(); // Placeholder for transaction print details
public bool Executed => _executed;
public abstract bool Success { get; }
public bool Reversed => _reversed;
public DateTime DateStamp => _dateStamp;
}
WithdrawTransaction 类,继承自 Transaction 类:
internal class WithdrawTransaction : Transaction
{
private Account _account;
public WithdrawTransaction(Account account, decimal amount) : base(amount)
{
_account = account;
}
public override void Execute()
{
base.Execute();
_success = _account.Withdraw(_amount);
}
public override void Rollback()
{
if (_success)
{
bool depositResult = _account.Deposit(_amount);
if (depositResult)
{
base.Rollback(); // Will only occur if the rollback is successfully completed
}
else
{
throw new InvalidOperationException("Rollback failed.");
}
}
}
public override void Print()
{
if (_success)
{
Console.WriteLine($"Withdrawal Transaction: {_amount:C} from {_account.Name} on {DateStamp}");
Console.WriteLine($"Executed: {Executed}\nSuccess: {Success}\nReversed {Reversed}");
Console.WriteLine();
}
}
public override bool Success => _success;
}
TransferTransaction类继承自Transaction类:
internal class TransferTransaction : Transaction
{
private Account _fromAccount;
private Account _toAccount;
public TransferTransaction(Account fromAccount, Account toAccount, decimal amount) : base(amount)
{
_fromAccount = fromAccount;
_toAccount = toAccount;
}
public override void Execute()
{
base.Execute();
// Perform withdrawal from source account
bool withdrawalSuccess = _fromAccount.Withdraw(_amount);
if (withdrawalSuccess)
{
// Attempt to deposit into destination account
bool depositSuccess = _toAccount.Deposit(_amount);
_success = depositSuccess;
if (!depositSuccess)
{
// If deposit fails, rollback the withdrawal
_fromAccount.Deposit(_amount);
Console.WriteLine("Deposit failed. Withdrawal has been rolled back.");
}
}
else
{
_success = false;
}
}
public override void Rollback()
{
if (_success && !Reversed)
{
bool withdrawResult = _toAccount.Withdraw(_amount);
if (withdrawResult)
{
bool depositResult = _fromAccount.Deposit(_amount);
if (depositResult)
{
Console.WriteLine("Rollback successful.");
base.Rollback(); // Call base method only if both actions succeed
}
else
{
Console.WriteLine("Rollback failed to deposit back into the source account.");
}
}
else
{
Console.WriteLine("Rollback failed to withdraw from the destination account.");
}
}
else if (Reversed)
{
Console.WriteLine("Transaction has already been reversed.");
}
else if (!_success)
{
Console.WriteLine("Transaction was not successful, so no rollback is needed.");
}
}
public override void Print()
{
Console.WriteLine($"Transfer of {_amount:C} from {_fromAccount.Name}'s account to {_toAccount.Name}'s account");
Console.WriteLine($"Executed: {Executed}\nSuccess: {Success}\nReversed {Reversed}");
Console.WriteLine();
}
public override bool Success => _success;
}
DepositTransaction 类继承自 Transaction 类:
internal class DepositTransaction : Transaction
{
private Account _account;
public DepositTransaction(Account account, decimal amount) : base(amount)
{
_account = account;
}
public override void Execute()
{
base.Execute();
_success = _account.Deposit(_amount);
}
public override void Rollback()
{
if (_success)
{
bool withdrawResult = _account.Withdraw(_amount);
if (withdrawResult)
{
base.Rollback(); // Will only occur if the rollback is successfully completed
}
else
{
throw new InvalidOperationException("rollback failed.");
}
}
}
public override void Print()
{
Console.WriteLine($"Deposit Transaction: {_amount:C} into {_account.Name} on {DateStamp}");
Console.WriteLine($"Executed: {Executed}\nSuccess: {Success}\nReversed {Reversed}");
Console.WriteLine();
}
public override bool Success => _success;
}
我添加了检查以确保在回滚成功时仅引发 base-rollback() 方法
你所描绘的场景并不需要“回滚”,因为没有任何过渡;如果操作无效,您将彻底拒绝“交易”。在其他情况下,您直接“提交”......并且提交后没有“回滚”。
现在,提款然后“撤销”是另一回事(或撤销错误的存款)。在这种情况下,会过帐另一笔具有“相反金额”的“交易”。即“借方”或“贷方”金额。但这些“交易”可能相隔几秒或几天(不涉及这种意义上的“回滚”)。