Ad
Abstraction
Logic
IO
Mathematics
Algorithms
Numbers
Data Types

WIP

As a cashier, you must process a sum of money between £1 and £1000 and give back the lowest number of notes and coins available for the amount specified.

Rules

The availables notes are: £50, £20, £10, £5
The available coins are: £2, £1

Output

The method produceChange() will take an int as a parameter, and will return a string describing the given change.

Ex. 1)

produceChange(40)

Should return:

For £40 - change was 2 notes and 0 coins: 2x £20 note

Ex. 2)

produceChange(430)

Should return:

For £430 - change was 10 notes and 3 coins: 8 x £50 note, 1x £20 note, 1X £10 note

Numbers are always formatted correctly so checks for correct input are not required. Assume that the range is inclusively between 1 and 1000.

String output must also be sensibly readable for edge-case scenarios, e.g. "1 coin" is returned instead of "1 coins."

public class Cashier {


    private int fiftyNotes = 0;
    private int twentyNotes = 0;
    private int tenNotes = 0;
    private int fiveNotes = 0;

    private int cash = 0;

    private int twoPoundsCoins = 0;
    private int onePoundCoins = 0;

    private int totalNotes = 0;
    private int totalCoins = 0;

    public Cashier() {
    }


    public String produceChange(int cash) {

        this.cash = cash;

        while ((cash) > 49) {
            cash -= 50;
            fiftyNotes++;
            totalNotes++;
        }
        while ((cash) > 19) {
            cash -= 20;
            twentyNotes++;
            totalNotes++;
        }
        while ((cash) > 9) {
            cash -= 10;
            tenNotes++;
            totalNotes++;
        }
        while ((cash) > 4) {
            cash -= 5;
            fiveNotes++;
            totalNotes++;
        }
        while ((cash) > 1) {
            cash -= 2;
            twoPoundsCoins++;
            totalCoins++;
        }
        onePoundCoins = cash;
        totalCoins += onePoundCoins;

        return this.toString();

    }

    private void clear() {
        fiftyNotes = 0;
        twentyNotes = 0;
        tenNotes = 0;
        fiveNotes = 0;

        twoPoundsCoins = 0;
        onePoundCoins = 0;
        fiftyPence = 0;
        twentyPence = 0;
        tenPence = 0;
        fivePence = 0;
        twoPence = 0;
        onePence = 0;

        totalNotes = 0;
        totalCoins = 0;

    }

    public String toString() {

        StringBuilder sb = new StringBuilder("");

        if (fiftyNotes > 0) {
            sb.append(fiftyNotes + "x £50 note");
        }
        if (twentyNotes > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + twentyNotes + "x £20 note");
        }
        if (tenNotes > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + tenNotes + "x £10 note");
        }
        if (fiveNotes > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + fiveNotes + "x £5 note");
        }
        if (twoPoundsCoins > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + twoPoundsCoins + "x £2 coin");
        }
        if (onePoundCoins > 0) {
            sb.append((sb.length() > 0 ? ", " : "") + onePoundCoins + "x £1 coin");
        }


        String out =  "For £" + cash + " - change was "  + totalNotes  + (totalNotes == 1 ? " note and " : " notes and ")
                + totalCoins + (totalCoins == 1 ? " coin: " : " coins: ");

        clear();
        return out + sb.toString();
    }
}