繁体   English   中英

美元和美分柜台

[英]Dollars and cents counter

因此,我得到了一个写一个计数器的任务,该计数器将给定的美元和美分加起来。 给了我们一个用于测试功能的测试类。

我们得到了以下提示:

public int dollars () //The dollar count. 
ensure: this.dollars () >= 0

public int cents () //The cents count. 
ensure: 0 <= this.cents() && this.cents() <= 99

和:

public void add (int dollars, int cents) //Add the specified dollars and cents to this Counter. 
public void reset () //Reset this Counter to 0. 
ensure: this .dollars() == 0 && this.cents() == 0 

这是我当前的代码:

public class Counter {

    private float count;

    public Counter() {
        count = 0;
    }

    public int dollars() {
        if (this.dollars() >= 0) {
            count = count + Float.parseFloat(this.dollars() + "." + 0);
        } return 0;
    }

    public int cents() {
        if (0 <= this.cents() && this.cents() <= 99) {
            count = count + Float.parseFloat(+0 + "." + this.cents());
        } else if (100 <= this.cents()) {
            count = count + Float.parseFloat(+1 + "." + (this.cents() - 100));
        }
        return 0;
    }

    public void add(int dollars, int cents) {
        dollars = this.dollars();
        cents = this.cents();
    }

    public void reset() {
        count = 0;  
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
}

我意识到我在这里犯了错误(应该是在浮动汇率上,并试图计算浮动汇率的美元和美分部分)。 但是我无法确切指出失败的地方。

public final class Counter {

    private int cents;

    public int dollars() {
        return cents / 100;
    }

    public int cents() {
        return cents;    // if you want to retrun all centes
        // return cents % 100;    // if you want to return cents less than dollar
    }

    public void add(int dollars, int cents) {
        this.cents = dollars * 100 + cents;
    }

    public void reset() {
        cents = 0;
    }
}

金融编程的一个非常重要的规则: 永远不要将浮点数用作对付货币 您肯定很快或什至很快就会遇到问题。 看看我的例子,实际上,您可以很容易地实现计数器,只需将美分作为int即可(如我所见,您没有美分的一部分)。

PS未来的绝招

假设您需要一个浮点值并支持所有标准数学运算,例如+,-,/,* 例如,美元和整数美分(如您的示例中所示),您就不能(或不想)使用浮点运算。 你该怎么办?

只需以整数值存储两个Lows digis作为小数部分。 让我们以价格$ 12为例:

int price = 1200;    // 00 is reserverd for centes, , price is $12
price += 600;        // add $6, price is $18
price += 44;         // add $0.44, price is $18.55

int dollars = price / 100;   // retrieve total dollars - $18     
int cents = cents % 100;     // retrieve cents less than dollars - 44  
public class Counter {

    private int dollars = 0;
    private int cents = 0;

    public Counter(int dollars, int cents) {
        this.dollars = dollars;
        this.cents = cents;
    }

    Counter reset() {
        return new Counter(0, 0);
    }

    Counter add(int dollars, int cents) {
        if (dollars < 0 || cents < 0) {
            throw new IllegalArgumentException();
        }

        int dollarsToAdd = this.dollars + dollars;
        int centsToAdd = this.cents + cents;

        if (centsToAdd > 99) {
            dollarsToAdd += centsToAdd / 100;
            centsToAdd = centsToAdd % 100;
        }
        return new Counter(dollarsToAdd, centsToAdd);
    }

    public void print() {
        System.out.println(this.dollars + "." + this.cents);
    }

}

1)使用两个计数器而不是一个。 它使计算变得容易,并避免了浮点数的问题。 (检查出System.out.println(1.03 - .42);

2)验证输入。 无法添加负数。

3)美分/ 100将返还完整的美元数。 由于用int除以int会除去小数点。

4)cents%100将返回余数-转换为整美元后剩下的美分。

5)让我们使其不变。 它将避免并发问题。

一些有用的答案。 我还意识到我对分配的理解不正确(以为我需要总金额,而不是两个单独的美元和美分计数器),因此我变得比实际需要的要难。

我的解决方案(已通过使用提供的测试类验证为可以工作)如下:

public final class Counter {

    private int dollars;
    private int cents;

    public int dollars() {

        return dollars;
    }

    public int cents() {
        return cents;
    }

    public void add(int dollars, int cents) {
        if (dollars >= 0) {
            if (0 <= cents && cents <= 99) {
                this.dollars += dollars;
                this.cents += cents;
            }
            if (this.cents > 100 | cents > 100) {
                this.dollars += this.cents / 100;
                this.cents = this.cents % 100;
            }
            if (this.cents == 100 | cents == 100) {
                this.dollars++;
                this.cents = this.cents - 100;
            }

        }
    }

    public void reset() {
        dollars = 0;
        cents = 0;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM