向银行系统添加利率和透支费
Adding Interest Rate and Overdraft Fee to banking system
这就是我要实现的目标:
如果提款请求超过允许的透支限额 ($100),则不会提款,并且会从余额中扣除固定的固定费用 ($10)。
在下面的代码中,我已尽力做到这一点,但它不起作用,因为当我提取超过余额的金额时,它不会给我失败的交易费用,而是给我一个负余额。我也不确定用户何时单击他们想要查看的帐户详细信息我希望它显示失败的交易费用,如果有的话,如果用户透支的话。
任何帮助将不胜感激。只是无法理解它。
class Program
{
class Customer
{
private string firstName;
private string lastName;
private int phoneNumber;
private string address;
public Customer()
{
firstName = "Tayla";
lastName = "Brown";
phoneNumber = 027493922;
address = "55 Grey Rd";
}
//client info
public string ClientInfo()
{
string clientinfo = ("Account Holder: " + firstName + " " + lastName + " " + "Phone Number: " + phoneNumber + " " + "Address: " + address);
return clientinfo;
}
}
class Account : Customer
{
// Fields
private double accountNumber;
protected string accountType;
protected double balance;
protected double deposit;
protected double withdrawal;
protected double interest;
protected double fee;
protected double overdraft;
// Properties
public string AccountType
{
get { return this.accountType; }
}
public double Withdrawal
{
get { return this.withdrawal; }
set { this.withdrawal = value; }
}
public double Deposit
{
get { return this.deposit; }
set { this.deposit = value; }
}
public double AcctNumber
{ get { return this.accountNumber; } }
public double Bal
{ get { return this.balance; } }
public double f
{ get { return this.fee; } }
public double od
{ get { return this.overdraft; } }
// Creating Random Account Number
public virtual double AccountNumb()
{
Random rand = new Random();
this.accountNumber = rand.Next(100000000, 1000000000);
return accountNumber;
}
public virtual double Fee() // Work out Fee
{
overdraft = 100;
fee = 10;
if (balance < withdrawal)
{
balance -= (overdraft + withdrawal);
}
else if (balance > withdrawal)
{
balance -= withdrawal;
fee -= balance;
}
return balance;
}
//Computes General Balance(resets values)
public virtual double Balance()
{
balance = balance + deposit - withdrawal;
deposit = 0;
withdrawal = 0;
return balance;
}
//Computers Balance when withdrawal equals zero
public virtual double DepositBalance(double input)
{
deposit = input;
withdrawal = 0;
balance = balance + deposit - withdrawal;
return balance;
}
//Computers balance when deposit equals zero
public virtual double WithBalance(double input)
{
withdrawal = input;
deposit = 0;
balance = balance + deposit - withdrawal;
return balance;
}
//displays online banking menu
public virtual void DisplayMenu()
{
Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
Console.WriteLine("");
Console.WriteLine("1.View Client Info");
Console.WriteLine("");
Console.WriteLine("2. View Account Balance:");
Console.WriteLine(" 2A.Everyday\n 2B.Investment\n 2C.Omni");
Console.WriteLine("");
Console.WriteLine("3.Deposit Funds:\n 3A.Everyday\n 3B.Investment\n 3C.Omni");
Console.WriteLine("");
Console.WriteLine("4.Withdraw Funds:\n 4A.Everyday\n 4B.Investment\n 4C.Omni");
Console.WriteLine("");
Console.WriteLine("5.View Everyday Banking Details");
Console.WriteLine("6.View Investment Banking Details");
Console.WriteLine("7.View Omni Banking details");
Console.WriteLine("");
Console.WriteLine("8.Exit");
}
}
// Everyday Account
class Everyday : Account
{
//field
private double minBalance;
private double maxBalance;
//properties
public double MinBalance
{ get { return this.minBalance; } }
public double MaxBalance
{ get { return this.maxBalance; } }
//constructors
public Everyday(double balance) : base()
{
this.minBalance = 0;
this.maxBalance = 1000000000000;
this.balance = balance;
accountType = "Everyday Account";
}
//methods
}
// Investment Account
class Investment : Account
{
//fields
private int investmentInterest;
private int feeInvestment;
//properties
public int InterestInvestment
{
get { return this.investmentInterest; } // Sets Interest
set { investmentInterest = 4; }
}
public int FeeInvestment
{
get { return this.feeInvestment; } // Sets Fee
set { feeInvestment = 10; }
}
//constructors
public Investment(double balance) : base()
{
this.balance = balance;
accountType = "Investment Account";
}
//methods
}
// Omni Account
class Omni : Account // If a request is made to withdraw money over and above the allowed overdraft limit, then no money is withdrawn and
// a fixed flat fee is deducted from the balance
{
//fields
private int interestOmni;
private int overdraft;
//properties
public int InterestOmni
{
get { return this.interestOmni; } // Get interest
set { interestOmni = 4; }
}
public int OverDraftOmni
{
get { return this.overdraft; } // Get overdraft
}
//constructors
public Omni(double balance) : base()
{
interestOmni = 4;
overdraft = 1000; // Overdraft same as min balance?
this.balance = balance;
accountType = "Omni Account";
}
}
static void Main(string[] args)
{
// Object Insantiating
Account client = new Account(); // Our one client
Everyday everyday = new Everyday(2000);
everyday.AccountNumb();
Investment investment = new Investment(500);
investment.AccountNumb();
Omni omni = new Omni(1000);
omni.AccountNumb();
string streamEverydayAccount = ("Account Number: " + everyday.AcctNumber);
string streamInvestmentAccount = ("Account Number: " + investment.AcctNumber);
string streamOmniAccount = ("Account Number: " + omni.AcctNumber);
string streamClientInfo = (client.ClientInfo());
string everydayAccountType = (everyday.AccountType);
string investmentAccountType = (everyday.AccountType);
string omniAccountType = (everyday.AccountType);
// StreamWriter Everyday Account
using (StreamWriter everydaysummary = new StreamWriter(@"EverydaySummary.txt", true))
{
everydaysummary.WriteLine(everydayAccountType);
everydaysummary.WriteLine(streamClientInfo);
everydaysummary.WriteLine(streamEverydayAccount);
}
// StreamWriter Investment Account
using (StreamWriter investmentsummary = new StreamWriter(@"InvestmentSummary.txt", true))
{
investmentsummary.WriteLine(investmentAccountType);
investmentsummary.WriteLine(streamClientInfo);
investmentsummary.WriteLine(streamInvestmentAccount);
}
// StreamWriter Omni Account
using (StreamWriter omnisummary = new StreamWriter(@"OmniSummary.txt", true))
{
omnisummary.WriteLine(omniAccountType);
omnisummary.WriteLine(streamClientInfo);
omnisummary.WriteLine(streamOmniAccount);
}
bool test = false;
do
{
string everydayDepositPara = ($"Transaction: +${everyday.Deposit} at {DateTime.Now.ToString()} Current Balance: ${everyday.Bal}");
string everdayWithdrawalPara = ($"Transaction: -${everyday.Withdrawal} at {DateTime.Now.ToString()} Current Balance: ${everyday.Bal}");
string investmentDepositPara = ($"Transaction: +${investment.Deposit} at {DateTime.Now.ToString()} Current Balance: ${investment.Bal}");
string investmentWithdrawalPara = ($"Transaction: -${investment.Withdrawal} at {DateTime.Now.ToString()} Current Balance: ${investment.Bal}");
string omniDepositPara = ($"Transaction: +${omni.Deposit} at {DateTime.Now.ToString()} Current Balance: ${omni.Bal}");
string omniWithdrawalPara = ($"Transaction: -${omni.Withdrawal} at {DateTime.Now.ToString()} Current Balance: ${omni.Bal}");
//INSTANTIATING STREAMWRITER FILES
// Everyday
using (StreamWriter everdayAccountStreamSummary = new StreamWriter("EverdaySummary.txt", true))
{
if (everyday.Deposit > 0)
{
everdayAccountStreamSummary.WriteLine(everydayDepositPara);
everyday.Deposit = 0;
}
if (everyday.Withdrawal > 0)
{
everdayAccountStreamSummary.WriteLine(everdayWithdrawalPara);
everyday.Withdrawal = 0;
}
}
//Investment
using (StreamWriter investmentAccountStreamSummary = new StreamWriter("InvestmentSummary.txt", true))
{
if (investment.Deposit > 0)
{
investmentAccountStreamSummary.WriteLine(investmentDepositPara);
investment.Deposit = 0;
}
if (investment.Withdrawal > 0)
{
investmentAccountStreamSummary.WriteLine(investmentWithdrawalPara);
investment.Withdrawal = 0;
}
}
//Omni
using (StreamWriter omniAccountStreamSummary = new StreamWriter("OmniSummary.txt", true))
{
if (omni.Deposit > 0)
{
omniAccountStreamSummary.WriteLine(omniDepositPara);
omni.Deposit = 0;
}
if (omni.Withdrawal > 0)
{
omniAccountStreamSummary.WriteLine(omniWithdrawalPara);
omni.Withdrawal = 0;
}
}
client.DisplayMenu();//the online banking menu
string userchoice = Console.ReadLine();//user input from menu options
switch (userchoice.ToUpper())
{
case "1": // Display Client Info
Console.Clear();
Console.WriteLine(client.ClientInfo());
break;
case "2A": // Display Everday Account Balance
Console.Clear();
everyday.Balance();
Console.WriteLine("Everyday Account Balance: $" + everyday.Bal);
break;
case "2B": // Display Investment Account Balance
Console.Clear();
investment.Balance();
Console.WriteLine("Investment Account Balance: $" + investment.Bal);
break;
case "2C": // Display Omni Account Balance
Console.Clear();
omni.Balance();
Console.WriteLine("Omni Account Balance: $" + omni.Bal);
break;
case "3A": // Everyday Account Deposit
Console.Clear();
Console.WriteLine("How much would you like to deposit?");
everyday.Deposit = double.Parse(Console.ReadLine());
Console.WriteLine("You deposited: $" + everyday.Deposit);
everyday.DepositBalance(everyday.Deposit);
break;
case "3B":
Console.Clear(); // Investment Account Deposit
Console.WriteLine("How much would you like to deposit?");
investment.Deposit = double.Parse(Console.ReadLine());
Console.WriteLine("You deposited: $" + investment.Deposit);
investment.DepositBalance(investment.Deposit);
break;
case "3C":
Console.Clear(); // Omni Account Deposit
Console.WriteLine("How much would you like to deposit?");
omni.Deposit = double.Parse(Console.ReadLine());
Console.WriteLine("You deposited: $" + omni.Deposit);
omni.DepositBalance(omni.Deposit);
break;
case "4A": // Everyday account Withdrawal
Console.Clear();
Console.WriteLine("How much would you like to withdraw?");
everyday.Withdrawal = double.Parse(Console.ReadLine());
Console.WriteLine("You withdrew: $" + everyday.Withdrawal);
everyday.WithBalance(everyday.Withdrawal);
break;
case "4B":
Console.Clear(); // Investment Account Withdrawal
Console.WriteLine("How much would you like to withdraw?");
investment.Withdrawal = double.Parse(Console.ReadLine());
Console.WriteLine("You withdrew: $" + investment.Withdrawal);
investment.WithBalance(investment.Withdrawal);
break;
case "4C":
Console.Clear(); // Omni Account Withdrawal
Console.WriteLine("How much would you like to withdraw?");
omni.Withdrawal = double.Parse(Console.ReadLine());
Console.WriteLine("You withdrew: $" + omni.Withdrawal);
omni.WithBalance(omni.Withdrawal);
break;
case "5":
Console.Clear(); // Everyday Details
Console.WriteLine("Everyday Banking Details");
everyday.Balance();
Console.WriteLine("Everyday Account Balance: $" + everyday.Bal);
break;
case "6":
Console.Clear(); // Investment Details
Console.WriteLine("Investment Banking Details");
everyday.Balance();
Console.WriteLine("Investment Account Balance: $" + investment.Bal); // Add Interest Rate and Fee
Console.WriteLine("Interest Rate: " + investment.InterestInvestment + "%");
Console.WriteLine("Fee: "
);
break;
case "7":
Console.Clear(); // Omni Details
Console.WriteLine("Omni Banking Details");
everyday.Balance();
Console.WriteLine("Omni Account Balance: $" + omni.Bal);
Console.WriteLine("Interest Rate: " + omni.InterestOmni + "%");
Console.WriteLine("Fee: ");
break;
case "8":
Console.Clear(); // Exit Banking
Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
Environment.Exit(0);
break;
default: // catch all, breaks the loop
Console.Clear();
test = false;
break;
}
} while (!test);
}
}
我已经对您的代码进行了重构 - 确实很难理解您正在尝试做什么 - 所以这是一个很大的变化。我已经删除了很多重复项,但我无法真正完成所有内容,因为您的代码中也有很多缺失。
我无法回答如何修复代码中的一种方法,因为您更改帐户状态的方式 类 真的很难理解。
在我的重构中,我总是这样计算 Balance
:
public decimal Balance => this.Opening + this.Deposits - this.Withdrawals + this.Interest - this.Fees;
所以现在只需要正确设置 Balance
的各个组件即可。
以下是 Withdraw
现在的工作方式:
public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Withdrawal amount must be positive");
}
decimal fee = 0;
if (amount > this.Overdraft)
{
amount = 0m;
fee = 10m;
}
else if (this.Balance < amount)
{
amount = this.Balance;
}
this.Withdrawals -= amount;
return (amount, fee);
}
如果我没理解错的话,当您尝试取款超过 Overdraft
金额时,您会收到 </code> 费用,但不会取款。我添加了一张支票,以确保您提取的金额不能超过余额。</p>
<p>这是我重构的其余部分。让我知道您需要讨论的内容:</p>
<pre><code>class Program
{
public class Client
{
private string _firstName;
private string _lastName;
private string _phoneNumber;
private string _address;
public Client(string firstName, string lastName, string phoneNumber, string address)
{
_firstName = firstName;
_lastName = lastName;
_phoneNumber = phoneNumber;
_address = address;
}
public string ClientInfo =>
$"Account Holder: {_firstName} {_lastName} Phone Number: {_phoneNumber} Address: {_address}";
}
public abstract class Account
{
private Client _client;
public Account(Client client, int accountNumber, decimal opening)
{
_client = client;
this.AccountNumber = accountNumber;
this.Opening = opening;
}
public Account(Client client, decimal opening) : this(client, Account.GenerateAccountNumber(), opening)
{ }
public int AccountNumber { get; private set; }
public string AccountType => $"{this.GetType().Name} Account";
public abstract decimal Overdraft { get; protected set; }
public decimal Opening { get; protected set; }
public decimal Deposits { get; protected set; }
public decimal Withdrawals { get; protected set; }
public decimal Interest { get; protected set; }
public decimal Fees { get; protected set; }
public decimal InterestRate { get; protected set; } = 4m;
public decimal Balance => this.Opening + this.Deposits - this.Withdrawals + this.Interest - this.Fees;
private static Random _random = new Random();
public static int GenerateAccountNumber() => _random.Next(100000000, 1000000000);
public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Withdrawal amount must be positive");
}
decimal fee = 0;
if (amount > this.Overdraft)
{
amount = 0m;
fee = 10m;
}
else if (this.Balance < amount)
{
amount = this.Balance;
}
this.Withdrawals += amount;
this.Fees += fee;
return (amount, fee);
}
public decimal Deposit(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Deposit amount must be positive");
}
this.Deposits += amount;
return amount;
}
public IEnumerable<string> GetTransactions()
{
var now = DateTime.Now;
if (this.Deposits > 0m)
{
yield return $"Deposits: +${this.Deposits} at {now.ToString()} Current Balance: ${this.Balance}";
}
if (this.Withdrawals > 0m)
{
yield return $"Withdrawals: +${this.Withdrawals} at {now.ToString()} Current Balance: ${this.Balance}";
}
}
public string Summary => String.Join(Environment.NewLine, this.AccountType, _client.ClientInfo, "Account Number: " + this.AccountNumber);
}
public class Everyday : Account
{
public decimal MinBalance { get; private set; } = 0m;
public decimal MaxBalance { get; private set; } = 1000000000000m;
public override decimal Overdraft { get; protected set; } = 100m;
public Everyday(Client client, decimal opening) : base(client, opening)
{ }
public Everyday(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
public class Investment : Account
{
public decimal InvestmentFee { get; private set; } = 10m;
public override decimal Overdraft { get; protected set; } = 100m;
public Investment(Client client, decimal opening) : base(client, opening)
{ }
public Investment(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
class Omni : Account
{
public override decimal Overdraft { get; protected set; } = 1000m;
public Omni(Client client, decimal opening) : base(client, opening)
{ }
public Omni(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
private static void DisplayBalance(Account account)
{
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
}
private static void DepositAmount(Account account)
{
Console.WriteLine("How much would you like to deposit?");
decimal deposited = account.Deposit(decimal.Parse(Console.ReadLine()));
Console.WriteLine($"You deposited: {deposited:$#,##0.00} into your {account.AccountType}");
}
private static void WithdrawAmount(Account account)
{
Console.WriteLine("How much would you like to withdraw?");
var result = account.Withdraw(decimal.Parse(Console.ReadLine()));
Console.WriteLine($"You withdrew: {result.Withdrawn:$#,##0.00}");
if (result.Fee != 0m)
{
Console.WriteLine($"With fee: {result.Fee:$#,##0.00}");
}
}
private static void DisplayDetails(Everyday account)
{
Console.WriteLine("Everyday Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
}
private static void DisplayDetails(Investment account)
{
Console.WriteLine("Investment Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
Console.WriteLine("Fee: ");
}
private static void DisplayDetails(Omni account)
{
Console.WriteLine("Omni Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
Console.WriteLine("Fee: ");
}
private static void Main(string[] args)
{
bool test = false;
Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd"); // Our one client
Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 2000m);
Investment investment = new Investment(client, 500m);
Omni omni = new Omni(client, 1000m);
File.WriteAllText(@"EverydaySummary.txt", everyday.Summary);
File.WriteAllText(@"InvestmentSummary.txt", investment.Summary);
File.WriteAllText(@"OmniSummary.txt", omni.Summary);
do
{
File.WriteAllLines("EverdaySummary.txt", everyday.GetTransactions());
File.WriteAllLines("InvestmentSummary.txt", investment.GetTransactions());
File.WriteAllLines("OmniSummary.txt", omni.GetTransactions());
DisplayMenu();
string userchoice = Console.ReadLine();//user input from menu options
switch (userchoice.ToUpper())
{
case "1": // Display Client Info
//Console.Clear();
Console.WriteLine(client.ClientInfo);
break;
case "2A": // Display Everday Account Balance
//Console.Clear();
DisplayBalance(everyday);
break;
case "2B": // Display Investment Account Balance
//Console.Clear();
DisplayBalance(investment);
break;
case "2C": // Display Omni Account Balance
//Console.Clear();
DisplayBalance(omni);
break;
case "3A": // Everyday Account Deposit
//Console.Clear();
DepositAmount(everyday);
break;
case "3B":
//Console.Clear(); // Investment Account Deposit
DepositAmount(investment);
break;
case "3C":
//Console.Clear(); // Omni Account Deposit
DepositAmount(omni);
break;
case "4A": // Everyday account Withdrawal
//Console.Clear();
WithdrawAmount(everyday);
break;
case "4B":
//Console.Clear(); // Investment Account Withdrawal
WithdrawAmount(investment);
break;
case "4C":
//Console.Clear(); // Omni Account Withdrawal
WithdrawAmount(omni);
break;
case "5":
//Console.Clear(); // Everyday Details
DisplayDetails(everyday);
break;
case "6":
//Console.Clear(); // Investment Details
DisplayDetails(investment);
break;
case "7":
//Console.Clear(); // Omni Details
DisplayDetails(omni);
break;
case "8":
//Console.Clear(); // Exit Banking
Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
test = true;
break;
default: // catch all, breaks the loop
//Console.Clear();
test = false;
break;
}
} while (!test);
}
//displays online banking menu
private static void DisplayMenu()
{
Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
Console.WriteLine("");
Console.WriteLine("1.View Client Info");
Console.WriteLine("");
Console.WriteLine("2. View Account Balance:");
Console.WriteLine(" 2A.Everyday\n 2B.Investment\n 2C.Omni");
Console.WriteLine("");
Console.WriteLine("3.Deposit Funds:\n 3A.Everyday\n 3B.Investment\n 3C.Omni");
Console.WriteLine("");
Console.WriteLine("4.Withdraw Funds:\n 4A.Everyday\n 4B.Investment\n 4C.Omni");
Console.WriteLine("");
Console.WriteLine("5.View Everyday Banking Details");
Console.WriteLine("6.View Investment Banking Details");
Console.WriteLine("7.View Omni Banking details");
Console.WriteLine("");
Console.WriteLine("8.Exit");
}
}
这是一些测试代码,表明现在应该可以正常工作了。
private static void Main(string[] args)
{
Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd");
Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 30m);
Console.WriteLine($"Balance: {everyday.Balance}");
var r1 = everyday.Withdraw(15m);
Console.WriteLine($"Withdrawn: {r1.Withdrawn}, Fee: {r1.Fee}, Balance: {everyday.Balance}");
var r2 = everyday.Withdraw(1000m);
Console.WriteLine($"Withdrawn: {r2.Withdrawn}, Fee: {r2.Fee}, Balance: {everyday.Balance}");
var r3 = everyday.Withdraw(10m);
Console.WriteLine($"Withdrawn: {r3.Withdrawn}, Fee: {r3.Fee}, Balance: {everyday.Balance}");
}
给出:
Balance: 30
Withdrawn: 15, Fee: 0, Balance: 15
Withdrawn: 0, Fee: 10, Balance: 5
Withdrawn: 5, Fee: 0, Balance: 0
这是一个带有交易的版本。
class Program
{
public class Client
{
private string _firstName;
private string _lastName;
private string _phoneNumber;
private string _address;
public Client(string firstName, string lastName, string phoneNumber, string address)
{
_firstName = firstName;
_lastName = lastName;
_phoneNumber = phoneNumber;
_address = address;
}
public string ClientInfo =>
$"Account Holder: {_firstName} {_lastName} Phone Number: {_phoneNumber} Address: {_address}";
}
public abstract class Account
{
private Client _client;
public Account(Client client, int accountNumber, decimal opening)
{
_client = client;
this.AccountNumber = accountNumber;
this.Transactions.Add(Transaction.Create(DateTime.Now, opening, TransactionType.Opening));
}
public Account(Client client, decimal opening) : this(client, Account.GenerateAccountNumber(), opening)
{ }
public int AccountNumber { get; private set; }
public string AccountType => $"{this.GetType().Name} Account";
public abstract decimal Overdraft { get; protected set; }
public List<Transaction> Transactions { get; private set; } = new List<Transaction>();
public decimal InterestRate { get; protected set; } = 4m;
public decimal Balance => this.Transactions.Sum(t => t.Amount);
private static Random _random = new Random();
public static int GenerateAccountNumber() => _random.Next(100000000, 1000000000);
public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Withdrawal amount must be positive");
}
decimal fee = 0;
if (amount > this.Overdraft)
{
amount = 0m;
fee = 10m;
}
else if (this.Balance < amount)
{
amount = this.Balance;
}
if (amount > 0m)
{
this.Transactions.Add(Transaction.Create(DateTime.Now, amount, TransactionType.Withdrawal));
}
if (fee > 0m)
{
this.Transactions.Add(Transaction.Create(DateTime.Now, fee, TransactionType.Fee));
}
return (amount, fee);
}
public decimal Deposit(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Deposit amount must be positive");
}
this.Transactions.Add(Transaction.Create(DateTime.Now, amount, TransactionType.Deposit));
return amount;
}
public string Summary => String.Join(Environment.NewLine, this.AccountType, _client.ClientInfo, "Account Number: " + this.AccountNumber);
}
public class Everyday : Account
{
public decimal MinBalance { get; private set; } = 0m;
public decimal MaxBalance { get; private set; } = 1000000000000m;
public override decimal Overdraft { get; protected set; } = 100m;
public Everyday(Client client, decimal opening) : base(client, opening)
{ }
public Everyday(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
public class Investment : Account
{
public decimal InvestmentFee { get; private set; } = 10m;
public override decimal Overdraft { get; protected set; } = 100m;
public Investment(Client client, decimal opening) : base(client, opening)
{ }
public Investment(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
class Omni : Account
{
public override decimal Overdraft { get; protected set; } = 1000m;
public Omni(Client client, decimal opening) : base(client, opening)
{ }
public Omni(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
public class Transaction
{
public DateTime When { get; private set; }
public decimal Amount { get; private set; }
public TransactionType Type { get; private set; }
public static Transaction Create(DateTime when, decimal amount, TransactionType type)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Transaction amount must be positive");
}
return new Transaction(when, (type == TransactionType.Withdrawal || type == TransactionType.Fee) ? -amount : amount, type);
}
private Transaction(DateTime when, decimal amount, TransactionType type)
{
this.When = when;
this.Amount = amount;
this.Type = type;
}
}
public enum TransactionType { Opening, Deposit, Withdrawal, Fee, Interest }
private static void DisplayBalance(Account account)
{
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
}
private static void DepositAmount(Account account)
{
Console.WriteLine("How much would you like to deposit?");
decimal deposited = account.Deposit(decimal.Parse(Console.ReadLine()));
Console.WriteLine($"You deposited: {deposited:$#,##0.00} into your {account.AccountType}");
}
private static void WithdrawAmount(Account account)
{
Console.WriteLine("How much would you like to withdraw?");
var result = account.Withdraw(decimal.Parse(Console.ReadLine()));
Console.WriteLine($"You withdrew: {result.Withdrawn:$#,##0.00}");
if (result.Fee != 0m)
{
Console.WriteLine($"With fee: {result.Fee:$#,##0.00}");
}
}
private static void DisplayDetails(Everyday account)
{
Console.WriteLine("Everyday Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
}
private static void DisplayDetails(Investment account)
{
Console.WriteLine("Investment Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
Console.WriteLine("Fee: ");
}
private static void DisplayDetails(Omni account)
{
Console.WriteLine("Omni Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
Console.WriteLine("Fee: ");
}
private static void Main(string[] args)
{
bool test = false;
Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd"); // Our one client
Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 2000m);
Investment investment = new Investment(client, 500m);
Omni omni = new Omni(client, 1000m);
File.WriteAllText(@"EverydaySummary.txt", everyday.Summary);
File.WriteAllText(@"InvestmentSummary.txt", investment.Summary);
File.WriteAllText(@"OmniSummary.txt", omni.Summary);
do
{
File.WriteAllLines("EverdaySummary.txt", everyday.Transactions.Select(t => $"{t.When} {t.Type} {t.Amount}"));
File.WriteAllLines("InvestmentSummary.txt", investment.Transactions.Select(t => $"{t.When} {t.Type} {t.Amount}"));
File.WriteAllLines("OmniSummary.txt", omni.Transactions.Select(t => $"{t.When} {t.Type} {t.Amount}"));
DisplayMenu();
string userchoice = Console.ReadLine();//user input from menu options
switch (userchoice.ToUpper())
{
case "1": // Display Client Info
//Console.Clear();
Console.WriteLine(client.ClientInfo);
break;
case "2A": // Display Everday Account Balance
//Console.Clear();
DisplayBalance(everyday);
break;
case "2B": // Display Investment Account Balance
//Console.Clear();
DisplayBalance(investment);
break;
case "2C": // Display Omni Account Balance
//Console.Clear();
DisplayBalance(omni);
break;
case "3A": // Everyday Account Deposit
//Console.Clear();
DepositAmount(everyday);
break;
case "3B":
//Console.Clear(); // Investment Account Deposit
DepositAmount(investment);
break;
case "3C":
//Console.Clear(); // Omni Account Deposit
DepositAmount(omni);
break;
case "4A": // Everyday account Withdrawal
//Console.Clear();
WithdrawAmount(everyday);
break;
case "4B":
//Console.Clear(); // Investment Account Withdrawal
WithdrawAmount(investment);
break;
case "4C":
//Console.Clear(); // Omni Account Withdrawal
WithdrawAmount(omni);
break;
case "5":
//Console.Clear(); // Everyday Details
DisplayDetails(everyday);
break;
case "6":
//Console.Clear(); // Investment Details
DisplayDetails(investment);
break;
case "7":
//Console.Clear(); // Omni Details
DisplayDetails(omni);
break;
case "8":
//Console.Clear(); // Exit Banking
Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
test = true;
break;
default: // catch all, breaks the loop
//Console.Clear();
test = false;
break;
}
} while (!test);
}
//displays online banking menu
private static void DisplayMenu()
{
Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
Console.WriteLine("");
Console.WriteLine("1.View Client Info");
Console.WriteLine("");
Console.WriteLine("2. View Account Balance:");
Console.WriteLine(" 2A.Everyday\n 2B.Investment\n 2C.Omni");
Console.WriteLine("");
Console.WriteLine("3.Deposit Funds:\n 3A.Everyday\n 3B.Investment\n 3C.Omni");
Console.WriteLine("");
Console.WriteLine("4.Withdraw Funds:\n 4A.Everyday\n 4B.Investment\n 4C.Omni");
Console.WriteLine("");
Console.WriteLine("5.View Everyday Banking Details");
Console.WriteLine("6.View Investment Banking Details");
Console.WriteLine("7.View Omni Banking details");
Console.WriteLine("");
Console.WriteLine("8.Exit");
}
}
这就是我要实现的目标:
如果提款请求超过允许的透支限额 ($100),则不会提款,并且会从余额中扣除固定的固定费用 ($10)。
在下面的代码中,我已尽力做到这一点,但它不起作用,因为当我提取超过余额的金额时,它不会给我失败的交易费用,而是给我一个负余额。我也不确定用户何时单击他们想要查看的帐户详细信息我希望它显示失败的交易费用,如果有的话,如果用户透支的话。
任何帮助将不胜感激。只是无法理解它。
class Program
{
class Customer
{
private string firstName;
private string lastName;
private int phoneNumber;
private string address;
public Customer()
{
firstName = "Tayla";
lastName = "Brown";
phoneNumber = 027493922;
address = "55 Grey Rd";
}
//client info
public string ClientInfo()
{
string clientinfo = ("Account Holder: " + firstName + " " + lastName + " " + "Phone Number: " + phoneNumber + " " + "Address: " + address);
return clientinfo;
}
}
class Account : Customer
{
// Fields
private double accountNumber;
protected string accountType;
protected double balance;
protected double deposit;
protected double withdrawal;
protected double interest;
protected double fee;
protected double overdraft;
// Properties
public string AccountType
{
get { return this.accountType; }
}
public double Withdrawal
{
get { return this.withdrawal; }
set { this.withdrawal = value; }
}
public double Deposit
{
get { return this.deposit; }
set { this.deposit = value; }
}
public double AcctNumber
{ get { return this.accountNumber; } }
public double Bal
{ get { return this.balance; } }
public double f
{ get { return this.fee; } }
public double od
{ get { return this.overdraft; } }
// Creating Random Account Number
public virtual double AccountNumb()
{
Random rand = new Random();
this.accountNumber = rand.Next(100000000, 1000000000);
return accountNumber;
}
public virtual double Fee() // Work out Fee
{
overdraft = 100;
fee = 10;
if (balance < withdrawal)
{
balance -= (overdraft + withdrawal);
}
else if (balance > withdrawal)
{
balance -= withdrawal;
fee -= balance;
}
return balance;
}
//Computes General Balance(resets values)
public virtual double Balance()
{
balance = balance + deposit - withdrawal;
deposit = 0;
withdrawal = 0;
return balance;
}
//Computers Balance when withdrawal equals zero
public virtual double DepositBalance(double input)
{
deposit = input;
withdrawal = 0;
balance = balance + deposit - withdrawal;
return balance;
}
//Computers balance when deposit equals zero
public virtual double WithBalance(double input)
{
withdrawal = input;
deposit = 0;
balance = balance + deposit - withdrawal;
return balance;
}
//displays online banking menu
public virtual void DisplayMenu()
{
Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
Console.WriteLine("");
Console.WriteLine("1.View Client Info");
Console.WriteLine("");
Console.WriteLine("2. View Account Balance:");
Console.WriteLine(" 2A.Everyday\n 2B.Investment\n 2C.Omni");
Console.WriteLine("");
Console.WriteLine("3.Deposit Funds:\n 3A.Everyday\n 3B.Investment\n 3C.Omni");
Console.WriteLine("");
Console.WriteLine("4.Withdraw Funds:\n 4A.Everyday\n 4B.Investment\n 4C.Omni");
Console.WriteLine("");
Console.WriteLine("5.View Everyday Banking Details");
Console.WriteLine("6.View Investment Banking Details");
Console.WriteLine("7.View Omni Banking details");
Console.WriteLine("");
Console.WriteLine("8.Exit");
}
}
// Everyday Account
class Everyday : Account
{
//field
private double minBalance;
private double maxBalance;
//properties
public double MinBalance
{ get { return this.minBalance; } }
public double MaxBalance
{ get { return this.maxBalance; } }
//constructors
public Everyday(double balance) : base()
{
this.minBalance = 0;
this.maxBalance = 1000000000000;
this.balance = balance;
accountType = "Everyday Account";
}
//methods
}
// Investment Account
class Investment : Account
{
//fields
private int investmentInterest;
private int feeInvestment;
//properties
public int InterestInvestment
{
get { return this.investmentInterest; } // Sets Interest
set { investmentInterest = 4; }
}
public int FeeInvestment
{
get { return this.feeInvestment; } // Sets Fee
set { feeInvestment = 10; }
}
//constructors
public Investment(double balance) : base()
{
this.balance = balance;
accountType = "Investment Account";
}
//methods
}
// Omni Account
class Omni : Account // If a request is made to withdraw money over and above the allowed overdraft limit, then no money is withdrawn and
// a fixed flat fee is deducted from the balance
{
//fields
private int interestOmni;
private int overdraft;
//properties
public int InterestOmni
{
get { return this.interestOmni; } // Get interest
set { interestOmni = 4; }
}
public int OverDraftOmni
{
get { return this.overdraft; } // Get overdraft
}
//constructors
public Omni(double balance) : base()
{
interestOmni = 4;
overdraft = 1000; // Overdraft same as min balance?
this.balance = balance;
accountType = "Omni Account";
}
}
static void Main(string[] args)
{
// Object Insantiating
Account client = new Account(); // Our one client
Everyday everyday = new Everyday(2000);
everyday.AccountNumb();
Investment investment = new Investment(500);
investment.AccountNumb();
Omni omni = new Omni(1000);
omni.AccountNumb();
string streamEverydayAccount = ("Account Number: " + everyday.AcctNumber);
string streamInvestmentAccount = ("Account Number: " + investment.AcctNumber);
string streamOmniAccount = ("Account Number: " + omni.AcctNumber);
string streamClientInfo = (client.ClientInfo());
string everydayAccountType = (everyday.AccountType);
string investmentAccountType = (everyday.AccountType);
string omniAccountType = (everyday.AccountType);
// StreamWriter Everyday Account
using (StreamWriter everydaysummary = new StreamWriter(@"EverydaySummary.txt", true))
{
everydaysummary.WriteLine(everydayAccountType);
everydaysummary.WriteLine(streamClientInfo);
everydaysummary.WriteLine(streamEverydayAccount);
}
// StreamWriter Investment Account
using (StreamWriter investmentsummary = new StreamWriter(@"InvestmentSummary.txt", true))
{
investmentsummary.WriteLine(investmentAccountType);
investmentsummary.WriteLine(streamClientInfo);
investmentsummary.WriteLine(streamInvestmentAccount);
}
// StreamWriter Omni Account
using (StreamWriter omnisummary = new StreamWriter(@"OmniSummary.txt", true))
{
omnisummary.WriteLine(omniAccountType);
omnisummary.WriteLine(streamClientInfo);
omnisummary.WriteLine(streamOmniAccount);
}
bool test = false;
do
{
string everydayDepositPara = ($"Transaction: +${everyday.Deposit} at {DateTime.Now.ToString()} Current Balance: ${everyday.Bal}");
string everdayWithdrawalPara = ($"Transaction: -${everyday.Withdrawal} at {DateTime.Now.ToString()} Current Balance: ${everyday.Bal}");
string investmentDepositPara = ($"Transaction: +${investment.Deposit} at {DateTime.Now.ToString()} Current Balance: ${investment.Bal}");
string investmentWithdrawalPara = ($"Transaction: -${investment.Withdrawal} at {DateTime.Now.ToString()} Current Balance: ${investment.Bal}");
string omniDepositPara = ($"Transaction: +${omni.Deposit} at {DateTime.Now.ToString()} Current Balance: ${omni.Bal}");
string omniWithdrawalPara = ($"Transaction: -${omni.Withdrawal} at {DateTime.Now.ToString()} Current Balance: ${omni.Bal}");
//INSTANTIATING STREAMWRITER FILES
// Everyday
using (StreamWriter everdayAccountStreamSummary = new StreamWriter("EverdaySummary.txt", true))
{
if (everyday.Deposit > 0)
{
everdayAccountStreamSummary.WriteLine(everydayDepositPara);
everyday.Deposit = 0;
}
if (everyday.Withdrawal > 0)
{
everdayAccountStreamSummary.WriteLine(everdayWithdrawalPara);
everyday.Withdrawal = 0;
}
}
//Investment
using (StreamWriter investmentAccountStreamSummary = new StreamWriter("InvestmentSummary.txt", true))
{
if (investment.Deposit > 0)
{
investmentAccountStreamSummary.WriteLine(investmentDepositPara);
investment.Deposit = 0;
}
if (investment.Withdrawal > 0)
{
investmentAccountStreamSummary.WriteLine(investmentWithdrawalPara);
investment.Withdrawal = 0;
}
}
//Omni
using (StreamWriter omniAccountStreamSummary = new StreamWriter("OmniSummary.txt", true))
{
if (omni.Deposit > 0)
{
omniAccountStreamSummary.WriteLine(omniDepositPara);
omni.Deposit = 0;
}
if (omni.Withdrawal > 0)
{
omniAccountStreamSummary.WriteLine(omniWithdrawalPara);
omni.Withdrawal = 0;
}
}
client.DisplayMenu();//the online banking menu
string userchoice = Console.ReadLine();//user input from menu options
switch (userchoice.ToUpper())
{
case "1": // Display Client Info
Console.Clear();
Console.WriteLine(client.ClientInfo());
break;
case "2A": // Display Everday Account Balance
Console.Clear();
everyday.Balance();
Console.WriteLine("Everyday Account Balance: $" + everyday.Bal);
break;
case "2B": // Display Investment Account Balance
Console.Clear();
investment.Balance();
Console.WriteLine("Investment Account Balance: $" + investment.Bal);
break;
case "2C": // Display Omni Account Balance
Console.Clear();
omni.Balance();
Console.WriteLine("Omni Account Balance: $" + omni.Bal);
break;
case "3A": // Everyday Account Deposit
Console.Clear();
Console.WriteLine("How much would you like to deposit?");
everyday.Deposit = double.Parse(Console.ReadLine());
Console.WriteLine("You deposited: $" + everyday.Deposit);
everyday.DepositBalance(everyday.Deposit);
break;
case "3B":
Console.Clear(); // Investment Account Deposit
Console.WriteLine("How much would you like to deposit?");
investment.Deposit = double.Parse(Console.ReadLine());
Console.WriteLine("You deposited: $" + investment.Deposit);
investment.DepositBalance(investment.Deposit);
break;
case "3C":
Console.Clear(); // Omni Account Deposit
Console.WriteLine("How much would you like to deposit?");
omni.Deposit = double.Parse(Console.ReadLine());
Console.WriteLine("You deposited: $" + omni.Deposit);
omni.DepositBalance(omni.Deposit);
break;
case "4A": // Everyday account Withdrawal
Console.Clear();
Console.WriteLine("How much would you like to withdraw?");
everyday.Withdrawal = double.Parse(Console.ReadLine());
Console.WriteLine("You withdrew: $" + everyday.Withdrawal);
everyday.WithBalance(everyday.Withdrawal);
break;
case "4B":
Console.Clear(); // Investment Account Withdrawal
Console.WriteLine("How much would you like to withdraw?");
investment.Withdrawal = double.Parse(Console.ReadLine());
Console.WriteLine("You withdrew: $" + investment.Withdrawal);
investment.WithBalance(investment.Withdrawal);
break;
case "4C":
Console.Clear(); // Omni Account Withdrawal
Console.WriteLine("How much would you like to withdraw?");
omni.Withdrawal = double.Parse(Console.ReadLine());
Console.WriteLine("You withdrew: $" + omni.Withdrawal);
omni.WithBalance(omni.Withdrawal);
break;
case "5":
Console.Clear(); // Everyday Details
Console.WriteLine("Everyday Banking Details");
everyday.Balance();
Console.WriteLine("Everyday Account Balance: $" + everyday.Bal);
break;
case "6":
Console.Clear(); // Investment Details
Console.WriteLine("Investment Banking Details");
everyday.Balance();
Console.WriteLine("Investment Account Balance: $" + investment.Bal); // Add Interest Rate and Fee
Console.WriteLine("Interest Rate: " + investment.InterestInvestment + "%");
Console.WriteLine("Fee: "
);
break;
case "7":
Console.Clear(); // Omni Details
Console.WriteLine("Omni Banking Details");
everyday.Balance();
Console.WriteLine("Omni Account Balance: $" + omni.Bal);
Console.WriteLine("Interest Rate: " + omni.InterestOmni + "%");
Console.WriteLine("Fee: ");
break;
case "8":
Console.Clear(); // Exit Banking
Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
Environment.Exit(0);
break;
default: // catch all, breaks the loop
Console.Clear();
test = false;
break;
}
} while (!test);
}
}
我已经对您的代码进行了重构 - 确实很难理解您正在尝试做什么 - 所以这是一个很大的变化。我已经删除了很多重复项,但我无法真正完成所有内容,因为您的代码中也有很多缺失。
我无法回答如何修复代码中的一种方法,因为您更改帐户状态的方式 类 真的很难理解。
在我的重构中,我总是这样计算 Balance
:
public decimal Balance => this.Opening + this.Deposits - this.Withdrawals + this.Interest - this.Fees;
所以现在只需要正确设置 Balance
的各个组件即可。
以下是 Withdraw
现在的工作方式:
public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Withdrawal amount must be positive");
}
decimal fee = 0;
if (amount > this.Overdraft)
{
amount = 0m;
fee = 10m;
}
else if (this.Balance < amount)
{
amount = this.Balance;
}
this.Withdrawals -= amount;
return (amount, fee);
}
如果我没理解错的话,当您尝试取款超过 Overdraft
金额时,您会收到 </code> 费用,但不会取款。我添加了一张支票,以确保您提取的金额不能超过余额。</p>
<p>这是我重构的其余部分。让我知道您需要讨论的内容:</p>
<pre><code>class Program
{
public class Client
{
private string _firstName;
private string _lastName;
private string _phoneNumber;
private string _address;
public Client(string firstName, string lastName, string phoneNumber, string address)
{
_firstName = firstName;
_lastName = lastName;
_phoneNumber = phoneNumber;
_address = address;
}
public string ClientInfo =>
$"Account Holder: {_firstName} {_lastName} Phone Number: {_phoneNumber} Address: {_address}";
}
public abstract class Account
{
private Client _client;
public Account(Client client, int accountNumber, decimal opening)
{
_client = client;
this.AccountNumber = accountNumber;
this.Opening = opening;
}
public Account(Client client, decimal opening) : this(client, Account.GenerateAccountNumber(), opening)
{ }
public int AccountNumber { get; private set; }
public string AccountType => $"{this.GetType().Name} Account";
public abstract decimal Overdraft { get; protected set; }
public decimal Opening { get; protected set; }
public decimal Deposits { get; protected set; }
public decimal Withdrawals { get; protected set; }
public decimal Interest { get; protected set; }
public decimal Fees { get; protected set; }
public decimal InterestRate { get; protected set; } = 4m;
public decimal Balance => this.Opening + this.Deposits - this.Withdrawals + this.Interest - this.Fees;
private static Random _random = new Random();
public static int GenerateAccountNumber() => _random.Next(100000000, 1000000000);
public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Withdrawal amount must be positive");
}
decimal fee = 0;
if (amount > this.Overdraft)
{
amount = 0m;
fee = 10m;
}
else if (this.Balance < amount)
{
amount = this.Balance;
}
this.Withdrawals += amount;
this.Fees += fee;
return (amount, fee);
}
public decimal Deposit(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Deposit amount must be positive");
}
this.Deposits += amount;
return amount;
}
public IEnumerable<string> GetTransactions()
{
var now = DateTime.Now;
if (this.Deposits > 0m)
{
yield return $"Deposits: +${this.Deposits} at {now.ToString()} Current Balance: ${this.Balance}";
}
if (this.Withdrawals > 0m)
{
yield return $"Withdrawals: +${this.Withdrawals} at {now.ToString()} Current Balance: ${this.Balance}";
}
}
public string Summary => String.Join(Environment.NewLine, this.AccountType, _client.ClientInfo, "Account Number: " + this.AccountNumber);
}
public class Everyday : Account
{
public decimal MinBalance { get; private set; } = 0m;
public decimal MaxBalance { get; private set; } = 1000000000000m;
public override decimal Overdraft { get; protected set; } = 100m;
public Everyday(Client client, decimal opening) : base(client, opening)
{ }
public Everyday(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
public class Investment : Account
{
public decimal InvestmentFee { get; private set; } = 10m;
public override decimal Overdraft { get; protected set; } = 100m;
public Investment(Client client, decimal opening) : base(client, opening)
{ }
public Investment(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
class Omni : Account
{
public override decimal Overdraft { get; protected set; } = 1000m;
public Omni(Client client, decimal opening) : base(client, opening)
{ }
public Omni(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
private static void DisplayBalance(Account account)
{
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
}
private static void DepositAmount(Account account)
{
Console.WriteLine("How much would you like to deposit?");
decimal deposited = account.Deposit(decimal.Parse(Console.ReadLine()));
Console.WriteLine($"You deposited: {deposited:$#,##0.00} into your {account.AccountType}");
}
private static void WithdrawAmount(Account account)
{
Console.WriteLine("How much would you like to withdraw?");
var result = account.Withdraw(decimal.Parse(Console.ReadLine()));
Console.WriteLine($"You withdrew: {result.Withdrawn:$#,##0.00}");
if (result.Fee != 0m)
{
Console.WriteLine($"With fee: {result.Fee:$#,##0.00}");
}
}
private static void DisplayDetails(Everyday account)
{
Console.WriteLine("Everyday Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
}
private static void DisplayDetails(Investment account)
{
Console.WriteLine("Investment Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
Console.WriteLine("Fee: ");
}
private static void DisplayDetails(Omni account)
{
Console.WriteLine("Omni Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
Console.WriteLine("Fee: ");
}
private static void Main(string[] args)
{
bool test = false;
Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd"); // Our one client
Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 2000m);
Investment investment = new Investment(client, 500m);
Omni omni = new Omni(client, 1000m);
File.WriteAllText(@"EverydaySummary.txt", everyday.Summary);
File.WriteAllText(@"InvestmentSummary.txt", investment.Summary);
File.WriteAllText(@"OmniSummary.txt", omni.Summary);
do
{
File.WriteAllLines("EverdaySummary.txt", everyday.GetTransactions());
File.WriteAllLines("InvestmentSummary.txt", investment.GetTransactions());
File.WriteAllLines("OmniSummary.txt", omni.GetTransactions());
DisplayMenu();
string userchoice = Console.ReadLine();//user input from menu options
switch (userchoice.ToUpper())
{
case "1": // Display Client Info
//Console.Clear();
Console.WriteLine(client.ClientInfo);
break;
case "2A": // Display Everday Account Balance
//Console.Clear();
DisplayBalance(everyday);
break;
case "2B": // Display Investment Account Balance
//Console.Clear();
DisplayBalance(investment);
break;
case "2C": // Display Omni Account Balance
//Console.Clear();
DisplayBalance(omni);
break;
case "3A": // Everyday Account Deposit
//Console.Clear();
DepositAmount(everyday);
break;
case "3B":
//Console.Clear(); // Investment Account Deposit
DepositAmount(investment);
break;
case "3C":
//Console.Clear(); // Omni Account Deposit
DepositAmount(omni);
break;
case "4A": // Everyday account Withdrawal
//Console.Clear();
WithdrawAmount(everyday);
break;
case "4B":
//Console.Clear(); // Investment Account Withdrawal
WithdrawAmount(investment);
break;
case "4C":
//Console.Clear(); // Omni Account Withdrawal
WithdrawAmount(omni);
break;
case "5":
//Console.Clear(); // Everyday Details
DisplayDetails(everyday);
break;
case "6":
//Console.Clear(); // Investment Details
DisplayDetails(investment);
break;
case "7":
//Console.Clear(); // Omni Details
DisplayDetails(omni);
break;
case "8":
//Console.Clear(); // Exit Banking
Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
test = true;
break;
default: // catch all, breaks the loop
//Console.Clear();
test = false;
break;
}
} while (!test);
}
//displays online banking menu
private static void DisplayMenu()
{
Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
Console.WriteLine("");
Console.WriteLine("1.View Client Info");
Console.WriteLine("");
Console.WriteLine("2. View Account Balance:");
Console.WriteLine(" 2A.Everyday\n 2B.Investment\n 2C.Omni");
Console.WriteLine("");
Console.WriteLine("3.Deposit Funds:\n 3A.Everyday\n 3B.Investment\n 3C.Omni");
Console.WriteLine("");
Console.WriteLine("4.Withdraw Funds:\n 4A.Everyday\n 4B.Investment\n 4C.Omni");
Console.WriteLine("");
Console.WriteLine("5.View Everyday Banking Details");
Console.WriteLine("6.View Investment Banking Details");
Console.WriteLine("7.View Omni Banking details");
Console.WriteLine("");
Console.WriteLine("8.Exit");
}
}
这是一些测试代码,表明现在应该可以正常工作了。
private static void Main(string[] args)
{
Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd");
Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 30m);
Console.WriteLine($"Balance: {everyday.Balance}");
var r1 = everyday.Withdraw(15m);
Console.WriteLine($"Withdrawn: {r1.Withdrawn}, Fee: {r1.Fee}, Balance: {everyday.Balance}");
var r2 = everyday.Withdraw(1000m);
Console.WriteLine($"Withdrawn: {r2.Withdrawn}, Fee: {r2.Fee}, Balance: {everyday.Balance}");
var r3 = everyday.Withdraw(10m);
Console.WriteLine($"Withdrawn: {r3.Withdrawn}, Fee: {r3.Fee}, Balance: {everyday.Balance}");
}
给出:
Balance: 30
Withdrawn: 15, Fee: 0, Balance: 15
Withdrawn: 0, Fee: 10, Balance: 5
Withdrawn: 5, Fee: 0, Balance: 0
这是一个带有交易的版本。
class Program
{
public class Client
{
private string _firstName;
private string _lastName;
private string _phoneNumber;
private string _address;
public Client(string firstName, string lastName, string phoneNumber, string address)
{
_firstName = firstName;
_lastName = lastName;
_phoneNumber = phoneNumber;
_address = address;
}
public string ClientInfo =>
$"Account Holder: {_firstName} {_lastName} Phone Number: {_phoneNumber} Address: {_address}";
}
public abstract class Account
{
private Client _client;
public Account(Client client, int accountNumber, decimal opening)
{
_client = client;
this.AccountNumber = accountNumber;
this.Transactions.Add(Transaction.Create(DateTime.Now, opening, TransactionType.Opening));
}
public Account(Client client, decimal opening) : this(client, Account.GenerateAccountNumber(), opening)
{ }
public int AccountNumber { get; private set; }
public string AccountType => $"{this.GetType().Name} Account";
public abstract decimal Overdraft { get; protected set; }
public List<Transaction> Transactions { get; private set; } = new List<Transaction>();
public decimal InterestRate { get; protected set; } = 4m;
public decimal Balance => this.Transactions.Sum(t => t.Amount);
private static Random _random = new Random();
public static int GenerateAccountNumber() => _random.Next(100000000, 1000000000);
public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Withdrawal amount must be positive");
}
decimal fee = 0;
if (amount > this.Overdraft)
{
amount = 0m;
fee = 10m;
}
else if (this.Balance < amount)
{
amount = this.Balance;
}
if (amount > 0m)
{
this.Transactions.Add(Transaction.Create(DateTime.Now, amount, TransactionType.Withdrawal));
}
if (fee > 0m)
{
this.Transactions.Add(Transaction.Create(DateTime.Now, fee, TransactionType.Fee));
}
return (amount, fee);
}
public decimal Deposit(decimal amount)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Deposit amount must be positive");
}
this.Transactions.Add(Transaction.Create(DateTime.Now, amount, TransactionType.Deposit));
return amount;
}
public string Summary => String.Join(Environment.NewLine, this.AccountType, _client.ClientInfo, "Account Number: " + this.AccountNumber);
}
public class Everyday : Account
{
public decimal MinBalance { get; private set; } = 0m;
public decimal MaxBalance { get; private set; } = 1000000000000m;
public override decimal Overdraft { get; protected set; } = 100m;
public Everyday(Client client, decimal opening) : base(client, opening)
{ }
public Everyday(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
public class Investment : Account
{
public decimal InvestmentFee { get; private set; } = 10m;
public override decimal Overdraft { get; protected set; } = 100m;
public Investment(Client client, decimal opening) : base(client, opening)
{ }
public Investment(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
class Omni : Account
{
public override decimal Overdraft { get; protected set; } = 1000m;
public Omni(Client client, decimal opening) : base(client, opening)
{ }
public Omni(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
{ }
}
public class Transaction
{
public DateTime When { get; private set; }
public decimal Amount { get; private set; }
public TransactionType Type { get; private set; }
public static Transaction Create(DateTime when, decimal amount, TransactionType type)
{
if (amount <= 0m)
{
throw new System.InvalidOperationException("Transaction amount must be positive");
}
return new Transaction(when, (type == TransactionType.Withdrawal || type == TransactionType.Fee) ? -amount : amount, type);
}
private Transaction(DateTime when, decimal amount, TransactionType type)
{
this.When = when;
this.Amount = amount;
this.Type = type;
}
}
public enum TransactionType { Opening, Deposit, Withdrawal, Fee, Interest }
private static void DisplayBalance(Account account)
{
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
}
private static void DepositAmount(Account account)
{
Console.WriteLine("How much would you like to deposit?");
decimal deposited = account.Deposit(decimal.Parse(Console.ReadLine()));
Console.WriteLine($"You deposited: {deposited:$#,##0.00} into your {account.AccountType}");
}
private static void WithdrawAmount(Account account)
{
Console.WriteLine("How much would you like to withdraw?");
var result = account.Withdraw(decimal.Parse(Console.ReadLine()));
Console.WriteLine($"You withdrew: {result.Withdrawn:$#,##0.00}");
if (result.Fee != 0m)
{
Console.WriteLine($"With fee: {result.Fee:$#,##0.00}");
}
}
private static void DisplayDetails(Everyday account)
{
Console.WriteLine("Everyday Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
}
private static void DisplayDetails(Investment account)
{
Console.WriteLine("Investment Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
Console.WriteLine("Fee: ");
}
private static void DisplayDetails(Omni account)
{
Console.WriteLine("Omni Banking Details");
Console.WriteLine($"{account.AccountType} Balance: {account.Balance:$#,##0.00}");
Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
Console.WriteLine("Fee: ");
}
private static void Main(string[] args)
{
bool test = false;
Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd"); // Our one client
Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 2000m);
Investment investment = new Investment(client, 500m);
Omni omni = new Omni(client, 1000m);
File.WriteAllText(@"EverydaySummary.txt", everyday.Summary);
File.WriteAllText(@"InvestmentSummary.txt", investment.Summary);
File.WriteAllText(@"OmniSummary.txt", omni.Summary);
do
{
File.WriteAllLines("EverdaySummary.txt", everyday.Transactions.Select(t => $"{t.When} {t.Type} {t.Amount}"));
File.WriteAllLines("InvestmentSummary.txt", investment.Transactions.Select(t => $"{t.When} {t.Type} {t.Amount}"));
File.WriteAllLines("OmniSummary.txt", omni.Transactions.Select(t => $"{t.When} {t.Type} {t.Amount}"));
DisplayMenu();
string userchoice = Console.ReadLine();//user input from menu options
switch (userchoice.ToUpper())
{
case "1": // Display Client Info
//Console.Clear();
Console.WriteLine(client.ClientInfo);
break;
case "2A": // Display Everday Account Balance
//Console.Clear();
DisplayBalance(everyday);
break;
case "2B": // Display Investment Account Balance
//Console.Clear();
DisplayBalance(investment);
break;
case "2C": // Display Omni Account Balance
//Console.Clear();
DisplayBalance(omni);
break;
case "3A": // Everyday Account Deposit
//Console.Clear();
DepositAmount(everyday);
break;
case "3B":
//Console.Clear(); // Investment Account Deposit
DepositAmount(investment);
break;
case "3C":
//Console.Clear(); // Omni Account Deposit
DepositAmount(omni);
break;
case "4A": // Everyday account Withdrawal
//Console.Clear();
WithdrawAmount(everyday);
break;
case "4B":
//Console.Clear(); // Investment Account Withdrawal
WithdrawAmount(investment);
break;
case "4C":
//Console.Clear(); // Omni Account Withdrawal
WithdrawAmount(omni);
break;
case "5":
//Console.Clear(); // Everyday Details
DisplayDetails(everyday);
break;
case "6":
//Console.Clear(); // Investment Details
DisplayDetails(investment);
break;
case "7":
//Console.Clear(); // Omni Details
DisplayDetails(omni);
break;
case "8":
//Console.Clear(); // Exit Banking
Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
test = true;
break;
default: // catch all, breaks the loop
//Console.Clear();
test = false;
break;
}
} while (!test);
}
//displays online banking menu
private static void DisplayMenu()
{
Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
Console.WriteLine("");
Console.WriteLine("1.View Client Info");
Console.WriteLine("");
Console.WriteLine("2. View Account Balance:");
Console.WriteLine(" 2A.Everyday\n 2B.Investment\n 2C.Omni");
Console.WriteLine("");
Console.WriteLine("3.Deposit Funds:\n 3A.Everyday\n 3B.Investment\n 3C.Omni");
Console.WriteLine("");
Console.WriteLine("4.Withdraw Funds:\n 4A.Everyday\n 4B.Investment\n 4C.Omni");
Console.WriteLine("");
Console.WriteLine("5.View Everyday Banking Details");
Console.WriteLine("6.View Investment Banking Details");
Console.WriteLine("7.View Omni Banking details");
Console.WriteLine("");
Console.WriteLine("8.Exit");
}
}