收银机计划

时间:2018-03-20 02:13:42

标签: java currency

我正在写这个收银机计划有问题。具体来说就是output2方法。我试图找到一种方法来输出用户将在更改时收到的特定美元金额而不将其写为:

system.out.println(“你的改变将是百元钞票上的”+百+“ “+50 +”五十美元钞票ETC“);

但是,如果它符合用户将接收的更改,则包含美元值。

我希望这不会让人感到困惑。

int ranNum, hundred, fifty, twenty, ten, five, one, quarter, dime, nickel, penny;
double original, discount, savings, salesprice, tax, total, payment, change, quarterV = .25, dimeV = .10, //V as in value
        nickelV = .05, pennyV = .01;

DecimalFormat percent = new DecimalFormat("0%");
DecimalFormat money = new DecimalFormat("$0.00");

public void input()
{
    System.out.println("Hello, this program will ask you for a price of an item you would like to purchase");
    System.out.println("and return a random discount from 5-75 (multiple of 5). Then return the following:");
    System.out.println("original price, discount percent, discount amount, sales price, tax and total price with 7% tax.\n");
    System.out.println("Please give me the price of an item you would like to purchase");
    original = scan.nextDouble();
    scan.nextLine();
}

public void calculations()
{
    //This will be used to find the random discount given to the user:
    ranNum = random.nextInt(15)+1;
    discount = ((ranNum*5)*.10)*.10;

    //This will be used to find the amount the user will save:
    savings = (discount*original);

    //This will be used to find the salesprice of the item being purchased:
    salesprice = original - savings;

    //This will be used to find the total price of the item after 7% tax deductions
    tax = (salesprice*7)/100;

    //This will be used to find the final total the customer must pay
    total = salesprice + tax;
}

public void change()
{
    change = payment - total;

    hundred = (int) Math.floor(change/100);

    fifty = (int) Math.floor((change - hundred * 100)/50);

    twenty = (int) Math.floor((change - hundred * 100 - fifty * 50) / 20);

    ten = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20) / 10);

    five = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10) / 5);

    one = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5) / 1);

    quarter = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1)
            / quarterV);

    dime = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1 
            - quarter * quarterV) / dimeV);

    nickel = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1 
            - quarter * quarterV - dime * dimeV) / nickelV);

    penny = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1 
            - quarter * quarterV - dime * dimeV - nickel * nickelV) / penny);

}


public void output1()
{
    System.out.println("The original price of your item was "+money.format(original));
    System.out.println("You will be granted a " +percent.format(discount)+ " discount on your purchase.");
    System.out.println("Your discount amount (amount you are saving) is "+money.format(savings)+".");
    System.out.println("The sales price of your item is "+money.format(salesprice));
    System.out.println("The 7% tax payment will come out to be "+money.format(tax));
    System.out.println("Thus your total will be "+money.format(total)+"\n");
    System.out.println("How much money are you using to purchase your item?");
    payment = scan.nextDouble();
    scan.nextLine();
}

public void output2()
{
    System.out.println("Your change is");
    if (change>=100)
    {
        System.out.println(hundred+" one hundred dollar bills");
    }
    else
    {
        if (change>=50)
        {
            System.out.println(fifty+" fifty dollar bills");

        }
        else
        {
            if (change>=20)
            {
                System.out.println(twenty+" twenty dollar bills");
            }
            else
            {
                if (change>=5)
                {
                    System.out.println(five+" five dollar bills");
                }
                else
                {
                    System.out.println(one+" one dollar bills");
                }
            }
        }
    }


}


public void run()
{
    input();
    calculations();
    output1();
    change();
    output2();
}

public static void main(String[] args)
{
    CashRegister phill = new CashRegister();
    phill.run();
}

}

2 个答案:

答案 0 :(得分:0)

试试这个。我只收取100,50和20美元钞票的收银机,因为你可以从这里得到它的挂起。所有这些代码都是通过最高面额的账单到最低,从变更金额中减去账单数量以获得剩余的变化金额。

public void output2()
{
    int remainder = change; //make a copy of change variable so it remains final.
    int count=0;
    System.out.println("Your change is");
    if(remainder>=100){
        hundred = (int)remainder/100;
        remainder %= 100;
    }
    if(remainder>=50){
        fifty = (int)remainder/50;
        remainder %= 50;
    }
    if(remainder>=20){
        twenty = (int)remainder/20;
        remainder %= 20;
    }
    ...
    System.out.print(hundred + " one hundred dollar bills,");
    System.out.print(fifty + " fifty dollar bills,");
    System.out.print(twenty + " twenty dollar bills,");
    ...
}

答案 1 :(得分:0)

这可以解决问题

public void output2(){
    String changeMessage = "";
    double _change = change; //In case you need the change value somewhere else

    int numberOfNotesInHundred = (int)(_change / 100);
    if (numberOfNotesInHundred > 0){
        changeMessage += (numberOfNotesInHundred + " one hundred dollar bills ");
        _change = _change % 100;
    }

    int numberOfNotesInFifty = (int)(_change / 50);
    if (numberOfNotesInFifty > 0){
        changeMessage += (numberOfNotesInFifty + " fifty dollar bills ");
        _change = _change % 50;
    }

    // And so on for 20, 5 and 1

    System.out.println("Your change will be: " + changeMessage);
}