我是一名學生學習c#。當我做我的任務時,有一些概念困擾着我。這是我的任務。保存並檢查賬戶罰金額和收益餘額
任何時候向「S」-Savings賬戶存入款項時,銀行都應該支付存款的5% 每當退款導致賬戶餘額降至$ 0以下時,就應該受到懲罰。 「C」-Checking和「S」-Savings賬戶的罰款分別爲20美元和30美元。
當帳戶中的餘額低於$ 0時向用戶顯示消息。該消息應說明負面的帳戶。
交易完成後,向用戶顯示兩個帳戶的期末餘額。
abstract class Account
{
private float balance;
private float penalty;
static void Main(string[] args)
{
Console.Write("Please enter your full name: ");
string username = Console.ReadLine();
Console.Write("Please enter your account type to create(C -Checking or S -Savings):");
string accounttype = Console.ReadLine();
Console.Write("Please enter your opening balance: ");
string openingbalance = Console.ReadLine();
Console.Write("Please specify if you are going to deposit or withdrawl(D -Deposit or W -Withdraw):");
string depositwithdrawl = Console.ReadLine();
if (accounttype == "S" && depositwithdrawl == "D")
balance = (1 + 5 %) * openingbalance;
if balance < 0;
get { return balance; }
}
public class SavingAccount: Account
{
public void MakeDeposit(float amount)
{
balance += amount;
}
public void MakeWithdraw(float amount)
{
balance -= amount;
}
}
public class CheckingAccount: Account
{
public void MakeDeposit(float amount)
{
balance += amount;
}
public void MakeWithdraw(float amount)
{
balance -= amount;
}
}
public void ApplyPenalty(float amount)
{
if (balance < 0 && accounttype == "S")
penalty = 30;
if (balance < 0 && accounttype == "C")
penalty = 20;
}
public void ApplyMethod(float amount)
{
if (balance < 0 && accounttype == "S")
balance = balance-30;
if (balance < 0 && accounttype == "C")
balance = balance- 20;
}
public float Balance
{
get { return balance; }
}
}
介紹一個抽象類Account.cs
介紹2班從Account.cs類繼承(SavingsAccount &的CheckingAccount)
有一個抽象方法MakeDeposit()在Account.cs類
實現派生中的MakeDeposit()類
已經在Account.cs類
貫徹ApplyMethod()從帳戶餘額中扣除罰款金額一個抽象方法ApplyPenalty()。
我總是得到「ACCOUNTTYPE」不存在於當前上下文 存在錯誤,並用於可變的平衡,需要用於非靜態字段,方法或屬性「Account.balance對象引用'
非常感謝您的耐心和詳細的指導!對此,我真的非常感激。 – yanma
我的榮幸。如果答案可以接受,請將其標記爲這樣。謝謝! –