我是C#的新手,尤其是OOP及其原则。所以我从我的大学做了一个家庭作业来实施一个银行系统"。我有存款,抵押贷款和贷款账户,也有客户,应该是个人或公司。我有一个计算每个账户利率的公式,但目前并不重要,因为我觉得这个时候工作正常。我的问题是存款功能。在这里,您可以看到我的层次结构:
Account.cs
public abstract class Account
{
private Customer customer;
private double balance;
private double interestRate;
public Customer Customer { get; private set; }
public double Balance { get; private set; }
public double InterestRate { get; private set; }
public Account(Customer customer, double balance, double interestRate)
{
this.Customer = customer;
this.Balance = balance;
this.InterestRate = interestRate;
}
public abstract double CalculateInterestAmount(int numberOfMonths);
public virtual double Deposit(double amount)
{
return this.Balance + amount;
}
}
DepositAccount.cs
public class DepositAccount : Account
{
public DepositAccount(Customer customer, double balance, double interestRate) : base(customer, balance, interestRate)
{ }
public override double CalculateInterestAmount(int numberOfMonths)
{
double interest = 0.0;
if (this.Balance < 1000)
{
interest = 0.0;
}
else
{
interest = numberOfMonths * this.InterestRate;
}
return interest;
}
public double Withdraw(double amount)
{
double result = 0.0;
if (amount > this.Balance)
{
return result = 0.0;
}
else
{
result = this.Balance - amount;
}
return result;
}
}
LoanAccount.cs
public class LoanAccount : Account
{
public LoanAccount(Customer customer, double balance, double interestRate) : base(customer, balance, interestRate) { }
public override double CalculateInterestAmount(int numberOfMonths)
{
double interest = 0.0;
if (this.Customer is IndividualCustomer)
{
if (numberOfMonths > 3)
{
interest = (numberOfMonths - 3) * this.InterestRate;
}
else
{
interest = 0.0;
}
}
if (this.Customer is CompanyCustomer)
{
if (numberOfMonths > 2)
{
interest = (numberOfMonths - 2) * this.InterestRate;
}
else
{
interest = 0.0;
}
}
return interest;
}
}
MortgageAccount.cs
public class MortgageAccount : Account
{
public MortgageAccount(Customer customer, double balance, double interestRate) : base(customer, balance, interestRate)
{ }
public override double CalculateInterestAmount(int numberOfMonths)
{
double interest = 0.0;
if (this.Customer is CompanyCustomer)
{
if (numberOfMonths <= 12)
{
interest = (numberOfMonths * this.InterestRate) / 2;
}
else
{
interest = (((12 * this.InterestRate) / 2) + ((numberOfMonths - 12) * this.InterestRate));
}
}
if (this.Customer is IndividualCustomer)
{
if (numberOfMonths <= 6)
{
interest = 0.0;
}
else
{
interest = (numberOfMonths - 6) * this.InterestRate;
}
}
return interest;
}
}
主要
static void Main(string[] args)
{
//Customers
Customer individual = new IndividualCustomer();
Customer company = new CompanyCustomer();
//Accounts
Account mort = new MortgageAccount(individual, 2500.0, 0.2);
Account depo = new DepositAccount(individual, 400, 0.7);
Account loan = new LoanAccount(company, 10000, 3.2);
loan.Deposit(999); // this is not working. Balance should be 10999 but it is still 10000
Console.WriteLine(loan.Balance);
List<Account> accounts = new List<Account>();
accounts.Add(mort);
accounts.Add(depo);
accounts.Add(loan);
Bank b = new Bank(accounts);
Console.WriteLine(b.ToString());
}
问题是,当我向任何帐户添加一些金额时,它返回与旧帐户相同的金额。我的意思是,当我调试它真的是10999但我打印10000.问题出在哪里?同样对于Withdraw方法,只允许DepositCccounts撤销,当我实例化这个Account depo = new DepositAccount(individual, 400, 0.7);
帐户时,函数Withdraw消失了。我应该Withdraw
虚拟吗?但我只需要它存入DepositAccounts。我很困惑。
答案 0 :(得分:1)
您向帐户添加金额的方法基本上是返回两个输入的总和而不是存储数据。
您应该分配新值,然后像这样返回:
this.Balance = this.Balance + amount;
return this.Balance;
可以使用+=
运算符将其简化为一行:
return this.Balance += amount;
对于每个其他操作,您应该在存储该操作时执行相同的操作。因此,当您从余额中减去金额时,您可以这样做:
return this.Balance -= amount;
答案 1 :(得分:0)
问题是,当我向任何帐户添加一些金额时,它返回与旧帐户相同的金额。我的意思是,当我调试它真的是10999但我打印10000.问题出在哪里?
您的Deposit方法返回一个值(由于您未指定任何用途,因此会将其丢弃。)相反,您希望Deposit方法存储您计算的值。它不需要返回任何东西。
同样对于方法Withdraw,只允许DepositCccounts撤销,当我实例化帐户时,如此帐户depo = new DepositAccount(个人,400,0.7);函数Withdraw消失了。
您不能在帐户上调用提款,因此如果您有一个秘密持有DepositAccount对象的帐户变量,您需要将其转型为DepositAccount才能执行提款。