当我更新数组中某些记录的余额时,使用存款和取款金额,该特定记录的余额会随着数组中记录的余额而变化。
如何解决?
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));
}
}
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]);
}
答案 0 :(得分:3)
您尚未显示定义balance
字段的位置,但是您可以通过static
方法deposit
和withdraw
来访问它会猜测它本身就是一个静态变量,比如说
private static double balance;
现在,static
在这里意味着什么?如果你弄明白了,你会知道你的程序中的错误是什么,以及为什么在一个对象中更改它会改变它所有