2016-08-04 73 views
0

我試圖使當前餘額方法更新其餘額後撤出方法被調用,以便它成爲任何後續withdraw.I我不知道我在做什麼錯誤的起始餘額。謝謝。更新atm餘額

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Threading.Tasks; 


public class Customer 
{ 
    private double deposit; 
    private double balance; 
    static double bankCharge = 0.50; 



    public Customer(double depo) 
    { 
     if (depo > 2000) 
     { 
      Console.WriteLine("deposit cannot be morethan 2000"); 
     } 
     else 
     { 

      this.deposit += depo; 
     } 

    } 

    public void setDeposit(double depo) 
    { 
     if (depo > 2000) 
     { 
      Console.WriteLine(" Sorry, you cannot deposit morethan 2000"); 
     } 
     else 
     { 
      this.deposit+= depo; 
     } 
    } 

    public double currentBalance() 
    { 
     this.balance= this.deposit; 
     //this.balance+=this.balance; 

     return this.balance; 
    } 

    public double Withdrawal() 
    { 
     int amount; 
     bool passed; 
     do 
     { 
      Console.WriteLine("How much do you want to withdraw?"); 

      passed = int.TryParse(Console.ReadLine(), out amount); 
      if ((!passed) || (amount % 5 != 0)) 

      { 
       Console.WriteLine("Wrong input,No decimals please,Enter in multiples of 5, Try again"); 

      } 
     } while (!passed || amount % 5 != 0); 

     double charges = amount + bankCharge; // amount to be withdrawn + the bank charge 
     Console.WriteLine("charges are {0}", charges); 
     // Console.WriteLine("Your current balance is {0}", this.currentBalance()); 
     if (charges > this.currentBalance()) 
     { 
      Console.WriteLine("Sorry, you do not have enough money to perform this transaction"); 
     } 
     else 
     { 
     Console.WriteLine("Your current balance is {0}", this.balance); 
     this.balance-= charges; // withdrawal done 
     Console.WriteLine(" balance after transaction/charges is={0} ",this.balance); 
     } 

     return this.balance; 

    } 




} 





class Program 
{ 

    static void Main() 
    { 
     Customer lee = new Customer(500); 
     lee.setDeposit(2000); 
     lee.setDeposit(400); 

     //Console.WriteLine("the withdraw function returns this amount {0}", lee.Withdrawal()); 
     Console.WriteLine(lee.Withdrawal()); 
     Console.WriteLine(lee.currentBalance()); 
    } 
} 
+0

好吧,我們也不知道你在做什麼錯,因爲你沒有告訴我們你不喜歡的事情。 –

+0

在Main中可以看到,當前餘額爲2900.可以說我想撤回40,撤回後撤回方法返回2859.5,這是正確的,因爲0.50銀行費用。問題是:當我再次調用取款功能而不是從2859.5開始的當前餘額時,它從2900開始。換句話說,我希望每次客戶調用取款方法時,起始餘額應該是返回的餘額通過最後一筆交易的提款方式。 – gasperino

+0

請勿在評論區域包含實質細節。修改你的問題,明確你的問題。目前,還不清楚你的問題出了什麼問題,它可能會被關閉。 –

回答

0

代碼現在工作我想它。我擺脫了在currentBalance method.I實施的設置this.balance其中更新每隔一個存款是由時間平衡構造和setDeposit方法存款的方式。每次取款方法被調用和取款時,餘額都會同樣更新。這是我最初的挑戰,取款後餘額沒有更新,現在是固定的。您可能會關閉。

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Threading.Tasks; 
public class Customer 
{ 
    private double deposit; 
    private double balance; 
    static double bankCharge = 0.50; 
    public Customer(double depo) 
    { 
     if (depo > 2000) 
     { 
      Console.WriteLine("deposit cannot be morethan 2000"); 
     } 
     else 
     { 

      this.deposit += depo; 
      this.balance = this.deposit; 
     } 

    } 
    public void setDeposit(double depo) 
    { 
     if (depo > 2000) 
     { 
      Console.WriteLine(" Sorry, you cannot deposit morethan 2000"); 
     } 
     else 
     { 
      this.deposit += depo; 
      this.balance = this.deposit; 
     } 
    } 

    public double currentBalance() 
    {   
    return this.balance; 
    } 

    public double Withdrawal() 
    { 

     int amount; 
     bool passed; 
     do 
     { 
      Console.WriteLine("How much do you want to withdraw?"); 

      passed = int.TryParse(Console.ReadLine(), out amount); 
      if ((!passed) || (amount % 5 != 0)) 

      { 
       Console.WriteLine("Wrong input,No decimals please,Enter in multiples of 5, Try again"); 

      } 
     } while (!passed || amount % 5 != 0); 

     double charges = amount + bankCharge; 
     Console.WriteLine("charges are {0}", charges); 
     Console.WriteLine("Your current balance is {0}", this.balance); 
     if (charges > this.balance) 
     { 
      Console.WriteLine("Sorry, you do not have enough money to perform this transaction"); 
     } 
     else 
     { 

      this.balance = this.balance - charges; 

     } 

     return this.balance; 
    } 

} 
class Program 
{ 

    static void Main() 
    { 
     Customer lee = new Customer(500); 
     lee.setDeposit(2000); 
     lee.setDeposit(400); 
     lee.setDeposit(250); 
     lee.setDeposit(1850); 
     // Console.WriteLine(lee.Withdrawal()); 
     //Console.WriteLine(lee.Withdrawal()); 
     Console.WriteLine("Balance before withdawal is {0}", lee.currentBalance()); 
     Console.WriteLine(" Your balance after withdawal is {0}",lee.Withdrawal()); 
     Console.WriteLine("Starting balance for next transaction is {0}",lee.currentBalance()); 
     Console.WriteLine("*******************************************************"); 
     Console.WriteLine("Balance before withdawal is {0}", lee.currentBalance()); 
     Console.WriteLine(" Your balance after withdawal is {0}", lee.Withdrawal()); 
     Console.WriteLine("Starting balance for next transaction is {0}", lee.currentBalance()); 

    } 
}