我正在创建一个具有这两种方法的程序,我无法弄清楚.它们是"撤回"和"存款",它们位于CheckingAccount类中.在这些方法中,我想最初将值设为0然后添加到它.然后我想取新数字并从中减去.我想"存款"250美元.然后我想'退出'98美元.我不确定在哪里存储这些值以及如何执行它们.我有一个显示器应该看到的结果,而我离开撤销和存款方法是空的.
账户类别:
class Account
{
protected string firstName;
protected string lastName;
protected long number;
public string FirstName
{
set
{
firstName = value;
}
}
public string LastName
{
set
{
lastName = value;
}
}
public long Number
{
set
{
number = value;
}
}
public override string ToString()
{
return firstName + " " + lastName + "\nAccount #: " + number;
}
}
}
Run Code Online (Sandbox Code Playgroud)
检查帐户类别:
class CheckingAccount : Account
{
private decimal balance;
public CheckingAccount(string firstName, string lastName, long number, decimal initialBalance)
{
FirstName = firstName;
LastName = lastName;
Number = number;
Balance = initialBalance;
}
public decimal Balance
{
get
{
return balance;
}
set
{
balance = value;
}
}
public void deposit(decimal amount)
{
//initial value should be 0 and should be adding 250 to it.
}
public void withdraw(decimal amount)
{
//this takes the 250 amount and subtracts 98 from it
}
public void display()
{
Console.WriteLine(ToString());
Console.WriteLine("Balance: ${0}", Balance);
}
}
}
Run Code Online (Sandbox Code Playgroud)
显示类:
class Display
{
static void Main(string[] args)
{
CheckingAccount check = new CheckingAccount("John", "Smith", 123456, 0M);
Console.WriteLine("After Account Creation...");
check.display();
Console.WriteLine("After Depositing $250...");
//constructor
Console.WriteLine("After Withdrawing $98...");
//constructor
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望我的输出看起来像这样:
创建
帐户后...
John Smith
帐户#:123456
余额:0
存款250美元后......
约翰史密斯
账号#:123456
余额:250
撤回98美元后......
约翰史密斯
账号#:123456
余额:152
简单的答案是
public void deposit(decimal amount)
{
balance += amount;
}
public void withdraw(decimal amount)
{
balance -= amount;
}
Run Code Online (Sandbox Code Playgroud)
随意添加必要的验证(透支?试图存入负数?)