C# 银行系统存取款
我对C#不熟悉,尤其是OOP及其原理。因此,我有一个家庭作业,从我的大学实施一个“银行系统”。我有存款、抵押和贷款账户,也有客户,应该是个人或公司。我有一个计算每个账户利率的公式,但目前它并不重要,因为我认为目前这种方法很有效。我的问题是存款功能。在这里您可以看到我的层次结构: Account.csC# 银行系统存取款,c#,oop,C#,Oop,我对C#不熟悉,尤其是OOP及其原理。因此,我有一个家庭作业,从我的大学实施一个“银行系统”。我有存款、抵押和贷款账户,也有客户,应该是个人或公司。我有一个计算每个账户利率的公式,但目前它并不重要,因为我认为目前这种方法很有效。我的问题是存款功能。在这里您可以看到我的层次结构: Account.cs public abstract class Account { private Customer customer; private double balance; priv
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;
}
}
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;
}
}
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;
}
}
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;
}
}
存款账户.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;
}
}
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;
}
}
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;
}
}
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;
}
}
MortgageAccount.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;
}
}
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;
}
}
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;
}
}
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;
}
}
公共类抵押账户:账户
{
公共抵押账户(客户、双余额、双利率):基础(客户、余额、利率)
{ }
公共覆盖双计算InterestAmount(整数月数)
{
双倍利息=0.0;
如果(该客户是公司客户)
{
如果(月数)
问题是,当我向任何帐户添加一些金额时,它返回的值与旧帐户相同。我的意思是,当我调试时,它实际上是10999,但我打印10000。问题出在哪里
您的存款方法正在返回一个值(由于您没有指定它的任何用途,该值将被丢弃)。相反,您希望存款方法存储您计算的值。它不需要返回任何内容
同样,对于方法draw,只允许depostccounts进行取款,当我实例化账户时,如account depo=new depostcount(个人,400,0.7);函数draw不存在
您不能对帐户调用draw,因此,如果您有一个帐户变量秘密地保存着一个存款帐户对象,您需要向存款帐户键入cast以执行提款。您向帐户添加金额的方法基本上是返回两个输入的总和,而不是存储数据
您应该指定新值,然后按如下方式返回:
this.Balance = this.Balance + amount;
return this.Balance;
可以使用+=
运算符将其简化为一行:
return this.Balance += amount;
对于每个其他操作,当您要存储该操作时,您应该执行相同的操作。因此,当您从余额中减去金额时,您可以执行以下操作:
return this.Balance -= amount;
请注意,这是太多的代码。您至少可以(应该)删除所有感兴趣的内容。但仍然比代码太少更好。一个突出的技术问题是平衡上的private
setter.For draw()为了工作,它必须是代码>保护< /代码>。对于您的设计选择,考虑在CoDeVIEW站点上发布。您将获得更少的票价。