2017-08-07 123 views
0

我是一名學生學習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; } 
    } 
} 
  1. 介紹一個抽象類Account.cs

  2. 介紹2班從Account.cs類繼承(SavingsAccount &的CheckingAccount)

  3. 有一個抽象方法MakeDeposit()在Account.cs類

  4. 實現派生中的MakeDeposit()類

  5. 已經在Account.cs類

  6. 貫徹ApplyMethod()從帳戶餘額中扣除罰款金額一個抽象方法ApplyPenalty()。

我總是得到「ACCOUNTTYPE」不存在於當前上下文 存在錯誤,並用於可變的平衡,需要用於非靜態字段,方法或屬性「Account.balance對象引用'

回答

1

這是你第一次進入編程嗎?

首先,你在這裏得到編譯錯誤:

if (balance < 0 && accounttype == "S") 

因爲「ACCOUNTTYPE」未聲明爲Account類的成員;相反,你在Main()方法中將它聲明爲一個局部變量。

爲了克服這一點,添加ACCOUNTTYPE的一員,就像平衡(我們會得到點球后):

abstract class Account 
{ 
    private float balance; 
    private float accounttype; 

現在,在我們得到你的第二個錯誤,你應該重新安排你的代碼一點點。我假設你想要你創建一個C#控制檯應用程序,它應該有一個靜態無效的Main(string [] args)方法作爲你的程序的起點。問題在於你已經在你的Account類中封裝了Main()方法,我不認爲這就是你想要做的。

,請注意花括號,然後移動Main()方法下你的賬戶類的末尾以下:

噢,還有一件事......我建議你使用十進制而不是浮於貨幣價值。前面陳述的解釋超出了本教材的範圍。:)現在就相信我吧。

abstract class Account 
{ 
    private decimal balance; 
    private decimal penalty; 
    private string accounttype; 

    public void MakeDeposit(decimal amount) 
    { 
     balance += amount; 
    } 

    // ... and so on 

    public decimal Balance 
    { 
     get { return balance; } 
     // This is new too... you'll need this 'set' method here in your Balance property. More on that later. 
     set { balance = value; } 
    } 
} 

// Now we put the Main() method 
static void Main(string [] args) 
{ 
    // Your main program code goes here. 
} 

行!現在我們已經弄清楚了,你的第二個錯誤是因爲你需要實例化(花哨的詞用於「創建」)你派生的Account類之一的對象(你還沒有實現);否則,沒有任何東西給出「平衡」一詞有任何意義。這是一個帳戶,畢竟有一個平衡......不是您的計算機科學101計劃的主要功能。

當然,由於Account是摘要類,因此無法實例化Account對象(您將收到編譯錯誤)。所以,你需要編寫代碼,這些派生類,如SavingsAccount:

// This can probably go after your Account class definition, but just before your Main() method: 
class SavingsAccount : Account 
{ 
    // More stuff will go here later 
} 

現在,在Main()方法,您可以創建類型SavingsAccount的一個實例(一個「對象」),和設置它的平衡......一旦你清理了一些數據類型和操作符的語法問題。

// Bad things here... balance=(1+5%)*openingbalance; 

您不能將數字和字符串相乘。您需要將字符串轉換爲數字,但請注意!良好的編程需要我們從用戶那裏拯救用戶。您應該檢查他們爲期初餘額輸入的值實際上是一個有效的小數。如果不是,我們向用戶發出警告,要求他們再試一次:

bool isBalanceValid = false; 
    decimal decOpeningBalance = 0; 
    Console.Write("Please enter your opening balance: "); 

    while (! isBalanceValid) 
    { 
     string openingbalance = Console.ReadLine(); 
     if (! decimal.TryParse(openingbalance, out decOpeningBalance)) 
     { 
      Console.WriteLine("You must enter a valid decimal value, please try again"); 
     } 
     else 
     { 
      isBalanceValid = true; 
     } 
    } 

好了,回跌到哪裏,我們分配平衡儲蓄帳戶... C#不使用「%」簽收小數,所以使用0.05爲5%:

SavingsAccount savingsAccount = new SavingsAccount(); 

    if (accounttype == "S" && depositwithdrawl == "D") 
     savingsAccount.Balance = (1 + 0.05M) * decOpeningBalance; 

我可能已經走得太遠了這個解釋,但我寧願幫助別人瞭解爲什麼,而不是代碼,只是如何代碼。

+0

非常感謝您的耐心和詳細的指導!對此,我真的非常感激。 – yanma

+0

我的榮幸。如果答案可以接受,請將其標記爲這樣。謝謝! –