2012-03-28 13 views
1

配列内の特定のレコードの残高を更新すると、入金金額と引き出し金額を使用すると、その個別レコードの残高が、配列のレコードの残高と共に変更されます。複数の配列で調整された値

修正方法?

private String name; 
private int pin; 
private int account; 
private static double balance; 

public void setBalance(double amount) 
{ 
    balance = amount; 
} 

public static void deposit(double aDeposit) 
{ 
balance = balance + aDeposit; 
} 
public static void withdraw(double aWithdraw) 
{ 
    if 
    (balance >= aWithdraw) 
     balance = balance - aWithdraw; 
    else if 
    (balance < aWithdraw) 
     System.out.println("Cannot withdarw amount."); 
} 

public double getBalance() 
{ 
    return balance; 
} 
public boolean equal(CustomerRecord otherObject) 
{ 
    return (name.equalsIgnoreCase(otherObject.name) && 
      (pin == otherObject.pin) && 
      (account == otherObject.account) && 
      (balance == otherObject.balance)); 

     } 
    } 

あなたが balanceフィールドを定義していますが static方法 depositwithdraw Iからそれにアクセスできるようにしているという事実で行くあなたは示されていない
do{ 


     System.out.println("Enter the name"); 
     String aName; 
     Scanner keyboard = new Scanner(System.in); 
     aName = keyboard.nextLine(); 

     System.out.println("Enter the pin"); 
     int aPin; 
     aPin = keyboard.nextInt(); 


      for 

      (int i = 0; i < index; i++) { 
       CustomerRecord record = anotherArray[i]; 
       if 
       ((record.getName().equalsIgnoreCase(aName)) && (record.getPin() == (aPin))) 

      { 
       System.out.println(record); 

      System.out.println("Enter the amount you wish to Deposit"); 
      double aDeposit; 
      aDeposit = keyboard.nextDouble(); 
      CustomerRecord.deposit(aDeposit); 

      System.out.println("Enter the amount you wish to withdraw"); 
      double aWithdraw; 
      aWithdraw = keyboard.nextDouble(); 
      CustomerRecord.withdraw(aWithdraw); 


      record.getBalance(); 

    } 
      } 

     System.out.println("\nAnother Transaction? (y for yes) (n for no)"); 
     repeat = keyboard.next().charAt(0); 

    } 
    while 
     (

     repeat == 'y' || repeat == 'Y') ; 

     //Print the records on screen 

    { for (int i = 0; i < index; i++) 
     System.out.print(anotherArray[i]); 
    } 

答えて

3

それがそれ自体が静的変数であると推測するでしょう。

private static double balance; 

ここでstaticは何を意味していますか?それを理解すれば、あなたのプログラムのエラーは何か分かり、あるオブジェクトでそれを変更するとすべてが変わるのです。

+0

わかりました。静的な意味は何ですか? – Jake

+0

それは*静的です。それは驚くべきことではありません。 'フィールド'が '静的'なのはどういう意味ですか? –

+1

haha​​!ブリリアント。本当にありがとう! – Jake

関連する問題