Java addition subtraction with money

霸气de小男生 提交于 2019-12-10 20:27:42

问题


I am trying to add and subtract dollars and cents but I am having trouble with going over 100 cents and under 0 cents. My code works fine for adding anything until I need to convert 100 cents into a dollar. I'm having trouble putting my words into code, but I understand what needs to be done to convert cents into a dollar.

FYI this is for a class so that is why I have code for static method addition/subtraction and class method addition/subtraction

My code:

package moneyapp;

public class MoneyApp {

    public static void main(String[] args)
    {
        Money money1=new Money(99,99);
        Money money6=new Money(100,00);
        Money money7=new Money(0,1);

        add(money1,money7);
        System.out.println("The sum of "+money1+" and "+money7+" is "+money1.add(money7));
        subtract(money6,money7);
        System.out.println("The difference of "+money6+" and "+money7+" is "+money6.subtract(money7));
    }

    static Money add(Money money, Money money2)
    {
        int adddollars=money.dollars+money2.dollars;
        int addcents=money.cents+money2.cents;
        Money addmoney=new Money(adddollars,addcents);
        System.out.println(addmoney.toString());
        return addmoney;
    }
    static Money subtract(Money money, Money money2)
    {
        int subtractdollars=money.dollars-money2.dollars;
        int subtractcents=money.cents-money2.cents;
        Money subtractmoney=new Money(subtractdollars,subtractcents);
        System.out.println(subtractmoney.toString());
        return subtractmoney;
    }
}

Class code:

package moneyapp;

public class Money
{
    int dollars;
    int cents;

    public Money()
    {
        dollars=0;
        cents=0;
    }

    public Money(int dollar, int cent)
    {
        dollars=dollar;
        cents=cent;
    }

    public Money(int dollar)
    {
        dollars=dollar;
        cents=00;
    }

    public String toString()
    {
        if(cents<10)
        {
            return "$"+dollars+"."+"0"+cents;
        }
        else
        {
            return "$"+dollars+"."+cents;
        }
    }

    public int getDollars()
    {
        return dollars;
    }

    public int getCents()
    {
        return cents;
    }

    public void setDollars(int dollars)
    {
        this.dollars=dollars;
    }

    public void setCents(int cents)
    {
        this.cents=cents;
    }

    public Money add(Money other)
    {
        int dol=dollars+other.dollars;
        int cen=cents+other.cents;
        Money answer=new Money(dol,cen);
        return answer;
    }
    public Money subtract(Money other)
    {
        int dol=dollars-other.dollars;
        int cen=cents-other.cents;
        Money answer=new Money(dol,cen);
        return answer;
    }
}

回答1:


Consider this instead:

public class Money {

    private int m;

    public Money(int m) {
        this.m = m;
    }

    public int getDollars() {
        return m / 100;
    }

    public int getCents() {
        return m % 100;
    }

    public int get() {
        return m;
    }

    public Money add(Money other) {
        return new Money(m + other.get());
    }

    public Money subtract(Money other) {
        return new Money(m - other.get());
    }
}



回答2:


I would put an if statement saying that if cents are >= 100 then add to the dollars static Money add(Money money, Money money2) { int adddollars=money.dollars+money2.dollars; int addcents=money.cents+money2.cents;

    if(addcents >= 100){
        adddollars++;
    }

    Money addmoney=new Money(adddollars,addcents);
    System.out.println(addmoney.toString());
    return addmoney;
}

As for your subtraction method i would put something like static Money subtract(Money money, Money money2) { int subtractdollars=money.dollars-money2.dollars; int subtractcents=money.cents-money2.cents;

    if(subtractcents < 0)
        money.dollars--;
    money.cents=money.cents + 100;
    subtractcents= money.cents-money2.cents;


    Money subtractmoney=new Money(subtractdollars,subtractcents);
    System.out.println(subtractmoney.toString());
    return subtractmoney;
}  



回答3:


I updated your money class to fix the problem.

Money addition

100 cents is 1 dollar, so you can initialize dollar as:

this.dollars = dollar+cent / 100;

The amount of cents is than equal to the remainder of the division by 100

this.cents = cents % 100

In this way, cents will always be between 0-99.

Money subtraction

While your amount of cents is lower than zero, you can borrow 100 cents by decreasing the amount of dollars.

Updated Money Class

public class Money {
    public final int dollars;
    public final int cents;

    public Money() {
        dollars = 0;
        cents = 0;
    }

    public Money(int dollar, int cent) {
        dollars = dollar + cent / 100;
        cents = cent % 100;
    }

    public Money(int dollar) {
        dollars = dollar;
        cents = 0;
    }

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

    public Money add(Money other) {
        int dol = dollars + other.dollars;
        int cen = cents + other.cents;
        Money answer = new Money(dol, cen);
        return answer;
    }

    public Money subtract(Money other) {
        int dol = dollars - other.dollars;
        int cen = cents - other.cents;

        while (cents < 0) {
            --dol;
            cen += 100;
        }
        Money answer = new Money(dol, cen);
        return answer;
    }
}


来源:https://stackoverflow.com/questions/27930330/java-addition-subtraction-with-money

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!