gpt4 book ai didi

java - 余额 -= 10,但减去 20?

转载 作者:搜寻专家 更新时间:2023-11-01 04:04:38 27 4
gpt4 key购买 nike

我正在尝试在类里面使用一种收费的方法。方法是这样的:

  public double chargeFee()
{
balance -= 10;
return balance;
}

但是,它会减去 20。我尝试重新编译它,但找不到导致问题的原因。

完整代码:

public class ManageAccounts
{
public static void main(String[] args)
{
Account acct1, acct2;


//create account1 for Sally with $1000
acct1 = new Account(1000, "Sally", 1111);
acct2 = new Account(500, "Joe", 2222);//create account2 for Joe with $500

System.out.println("Depositing $100 into Account 2222...");
acct2.deposit(100.00);//deposit $100 to Joe's account
System.out.println("New Balance for Account 2222: $" + acct2.getBalance());//print Joe's new balance (use getBalance())
System.out.println();
System.out.println("Withdrawing $50 from Account 1111...");
acct1.withdraw(50);//withdraw $50 from Sally's account

System.out.println("New Balance for Account 1111: $" + acct1.getBalance());//print Sally's new balance (use getBalance())
System.out.println();
acct1.chargeFee();
acct2.chargeFee();//charge fees to both accounts
System.out.println("Charging usage Fees...");
System.out.println("Account balance after fees:");
System.out.println("Account 1111: $" + acct1.chargeFee());
System.out.println("Account 2222: $" + acct2.chargeFee());
System.out.println();
System.out.println("Changing name on Account 2222...");
acct2.changeName("Joseph");//change the name on Joe's account to Joseph
System.out.println();
System.out.println("Printing account summaries...");
System.out.println(acct1.toString());
System.out.println(acct2.toString());//print summary for both accounts

}
}



import java.text.NumberFormat;

public class Account
{
private double balance;
private String name;
private long acctNum;
NumberFormat money = NumberFormat.getCurrencyInstance();
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}

//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}

//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}

//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}


//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return ("Name: " + name + "\tAccount Number: " + acctNum + "\tBalance: " + money.format(balance));
}

//----------------------------------------------
// Deducts $10 service fee
//----------------------------------------------
public double chargeFee()
{
balance -= 10;
return balance;
}

//----------------------------------------------
// Changes the name on the account
//----------------------------------------------
public void changeName(String newName)
{
name = newName;
}

}

一些示例输出:

Depositing $100 into Account 2222...
New Balance for Account 2222: $600.0

Withdrawing $50 from Account 1111...
New Balance for Account 1111: $950.0

Charging usage Fees...

Account balance after fees:

Account 1111: $930.0
Account 2222: $580.0

Changing name on Account 2222...

Printing account summaries...
Name: Sally Account Number: 1111 Balance: $930.00
Name: Joseph Account Number: 2222 Balance: $580.00

最佳答案

您在两个帐户上调用了两次 chargeFee,一次是在您收取实际费用时,另一次是在您打印结果时。

为了将来引用,有一个名为 "Seperate command and query" 的指南, 或 CQS。你可能想阅读它。了解您的情况:

  • 您正在使用 chargeFee 作为查询(即,您返回余额)
  • 然后你使用chargeFee作为命令,扣钱。

更改 chargeFee 以返回一个 void(如果您想要方法链接,则返回 this),您就不会不小心将其用作查询。

http://en.wikipedia.org/wiki/Command-query_separation

关于java - 余额 -= 10,但减去 20?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13638497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com