mardi 24 août 2021

Problems with the task about if statement

I am a beginner in Java, please be easy to me.

I've got a task : People in the line are going to buy tickets. They have money three tipes: 25,50,100. Ticket costs - 25. If cashier has odd money - he gives a ticket, if not- doesn't.

How to make if for situation when in cash box are enough money with tipe 25 (25+25+25 ), but no one with tipe 50, and cashier get 100 from somebody?

My code:

public static void line(Integer... people) {
    ArrayList<Integer> cashbox = new ArrayList<>();
    
    for (int money : people) {
        if (money == 25) {
            cashbox.add(money);
            System.out.println("Here is your ticket!");
            continue;
        } else if (money == 50 && cashbox.contains(25)) {
            System.out.println("Here is your ticket!");
            cashbox.remove(cashbox.indexOf(25));
            continue;
        } else if (money == 100 && cashbox.contains(50) && cashbox.contains(25)) {
            System.out.println("Here is your ticket!");
            cashbox.remove(cashbox.indexOf(50));
            cashbox.remove(cashbox.indexOf(25));
            continue;
        }
        else {System.out.println("Sorry, I haven't odd money for you!");}
    }
}

Aucun commentaire:

Enregistrer un commentaire