java - 如何编写 toString() 和 equals() 方法?

标签 java

我试图让这段代码打印实际数字,而不是十六进制位置。

public class MoneyDriver
{
  //This is a driver for testing the class
  public static void main(String[] args)
  {
      final int BEGINNING = 500;
      final Money FIRST_AMOUNT = new Money(10.02);
      final Money SECOND_AMOUNT = new Money(10.02);
      final Money THIRD_AMOUNT = new Money(10.88);

      Money balance = new Money(BEGINNING);
      System.out.println("The current amount is " +
        balance.toString());
      balance = balance.add(SECOND_AMOUNT);
      System.out.println("Adding " + SECOND_AMOUNT +
        " gives " + balance.toString());
      balance = balance.subtract(THIRD_AMOUNT);
      System.out.println("Subtracting " + THIRD_AMOUNT +
        " gives " + balance.toString());



      boolean equal = SECOND_AMOUNT.equals(FIRST_AMOUNT);
      if(equal)
        System.out.println(SECOND_AMOUNT + " equals "
            + FIRST_AMOUNT);
      else
        System.out.println(SECOND_AMOUNT.toString() +
            " does not equal " + FIRST_AMOUNT);

      equal = THIRD_AMOUNT.equals(FIRST_AMOUNT);
      if(equal)
        System.out.println(THIRD_AMOUNT + " equals " +
            FIRST_AMOUNT);
      else
        System.out.println(THIRD_AMOUNT + " does not equal "
            +   FIRST_AMOUNT);
  }
}

这是 Moneydriver 调用的主类

public class Money
{
  private long dollars;
  private long cents;


  public Money(double amount)
  {
    if (amount < 0)
    {
      System.out.println(
             "Error: Negative amounts of money are not allowed.");
      System.exit(0);
    }
    else
    {
      long allCents = Math.round(amount*100);
      dollars = allCents/100;
      cents = allCents%100;
    }
  }

  public Money add(Money otherAmount)
  {
    Money sum = new Money(0);
    sum.cents = this.cents + otherAmount.cents;
    long carryDollars = sum.cents/100;
    sum.cents = sum.cents%100;
    sum.dollars = this.dollars
                 + otherAmount.dollars + carryDollars;
    return sum;
  }

  public Money subtract (Money amount)
  {
    Money difference = new Money(0);
    if (this.cents < amount.cents)
    {
      this.dollars = this.dollars - 1;
      this.cents = this.cents + 100;
    }
    difference.dollars = this.dollars - amount.dollars;
    difference.cents = this.cents - amount.cents;
    return difference;
  }


  public int compareTo(Money amount)
  {
    int value;
    if(this.dollars < amount.dollars)
    {
      value = -1;
    }
    else if (this.dollars > amount.dollars)
    {
      value = 1;
    }
    else if (this.cents < amount.cents)
    {
      value = -1;
    }
    else if (this.cents > amount.cents)
    {
      value = 1;
    }
    else
    {
      value = 0;
    }
    return value;
  }
}

目标是编写 equals 方法(在 main 类上)。该方法比较调用对象的实例变量与参数对象的实例变量是否相等,如果调用对象的美元和美分与参数对象的美元和美分相同,则返回 true。否则,返回 false。

编写toString方法(在main类上)。该方法将返回一个字符串 看起来像钱,包括美元符号。请记住,如果您的金额少于 10 美分,则需要在打印美分之前输入 0,以便正确显示小数点后 2 位。

如果两种方法都正确实现

根据教程,您应该执行以下任一操作

String toString()
static String toString(int i)

但是提供的 Moneydriver 已经具有 tostring 方法,但不显示数字,而是显示变量的十六进制位置。

equals 方法已经在 Moneydriver 中使用了,所以我也有点迷失了。

正确的输出应该是这样的

当前金额为 500.00 美元 添加 10.02 美元得出 510.02 美元 减去 10.88 美元得出 499.1 美元 10.02 美元等于 10.02 美元 10.88 美元不等于 10.02 美元

完全迷失了方向,提前感谢您的帮助。

最佳答案

要在 Money 类上获取字符串输出,请执行类似于以下操作的操作:

public class Money
{
   private long dollars;
   private long cents;

   // suggested approach for constructor
   public Money(long amount) throws IllegalArgumentException
   {
      if (amount < 0) {
        throw new IllegalArgumentException("Amount may not be less than 0");
      }

      // do other stuff
   }

   ...

   public String toString()
   {
     return String.format("%d.%02d", dollars, cents);
   }

   public boolean equals(Object obj)
   {
      boolean equal = false;

      if (obj instanceof Money) {
         Money chk = (Money)obj;
         equal = (chk.dollars == this.dollars &&
                  chk.cents == this.cents);
      }
      return equal;
   }
} // end class Money

关于java - 如何编写 toString() 和 equals() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36732926/

相关文章:

java - 如何从另一个 eclipse 插件项目调用 jar 中的方法?

java - 在我的计算机上安装虚拟 LDAP 服务器 (Windows 2010)

java - 在 Java 程序中的两个 HTTPClient 之间共享 session 的好方法是什么?

java - 如何在 Dialog dismiss Android 上触发事件?

java - 如何从java代码添加环境变量

java - 设置 JLabel 显示图像的正确方法是什么?

java - boolean onClickEventListener 返回的目的

java - 循环泛型(尝试 2)

java - 在 java 中解析 rss 提要时遇到 java.io.FileNotFoundException

java - 由于未事先设置先前的值,嵌套循环未计算正确的金额