Ad
Abstraction
Logic
IO
Mathematics
Algorithms
Numbers
Data Types
Code
Diff
  • import java.util.Arrays;
    
    public class Cashier {
    
        private int cash = 0;
        private String text[] = {"x £50 note", "x £20 note", "x £10 note", "x £5 note","x £2 coin","x £1 coin"}; //can be adjusted if new types of bills/coins are put into circulation
        private int notes[] = {50, 20, 10, 5, 2, 1};
        private int totals[] = {0,0,0,0,0,0};
    
        public Cashier() {}
    
        public String produceChange(int cash) {
            this.cash = cash;
          
          for(int i=0;i<notes.length;i++){
            while(cash>(notes[i]-1)){
              cash -= notes[i];
              totals[i]++;
            }
          }
            return this.toString();
        }
    
        public String toString() {
          int totalNotes =  Arrays.stream(Arrays.copyOfRange(totals, 0, 4)).sum();  //can be adjusted if new types of bills/coins are put into circulation
          int totalCoins = Arrays.stream(Arrays.copyOfRange(totals, 4, 6)).sum();
          String out ="For £" + cash + " - change was "+ totalNotes  + (totalNotes == 1 ? " note and " : " notes and ") + totalCoins + (totalCoins == 1 ? " coin: " : " coins: ");
          boolean commaFlag = false;
          for(int i=0;i<notes.length;i++){
             if(totals[i] != 0){
               out+=(commaFlag?", ":"")+totals[i]+text[i]; 
               //uncomment next line to propperly add 's' at end of bill/coin counts, example: "3x £50 note" --> "3x £50 notes"
               //totals[i]>1?out+="s":"";
               commaFlag = true;
             }
          }
          
            Arrays.fill(totals, 0);
            return out;
        }
    }
    
    • public class Cashier {
    • import java.util.Arrays;
    • private int fiftyNotes = 0;
    • private int twentyNotes = 0;
    • private int tenNotes = 0;
    • private int fiveNotes = 0;
    • public class Cashier {
    • private int cash = 0;
    • private String text[] = {"x £50 note", "x £20 note", "x £10 note", "x £5 note","x £2 coin","x £1 coin"}; //can be adjusted if new types of bills/coins are put into circulation
    • private int notes[] = {50, 20, 10, 5, 2, 1};
    • private int totals[] = {0,0,0,0,0,0};
    • private int twoPoundsCoins = 0;
    • private int onePoundCoins = 0;
    • private int totalNotes = 0;
    • private int totalCoins = 0;
    • public Cashier() {
    • }
    • 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++;
    • for(int i=0;i<notes.length;i++){
    • while(cash>(notes[i]-1)){
    • cash -= notes[i];
    • totals[i]++;
    • }
    • 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();
    • int totalNotes = Arrays.stream(Arrays.copyOfRange(totals, 0, 4)).sum(); //can be adjusted if new types of bills/coins are put into circulation
    • int totalCoins = Arrays.stream(Arrays.copyOfRange(totals, 4, 6)).sum();
    • String out ="For £" + cash + " - change was "+ totalNotes + (totalNotes == 1 ? " note and " : " notes and ") + totalCoins + (totalCoins == 1 ? " coin: " : " coins: ");
    • boolean commaFlag = false;
    • for(int i=0;i<notes.length;i++){
    • if(totals[i] != 0){
    • out+=(commaFlag?", ":"")+totals[i]+text[i];
    • //uncomment next line to propperly add 's' at end of bill/coin counts, example: "3x £50 note" --> "3x £50 notes"
    • //totals[i]>1?out+="s":"";
    • commaFlag = true;
    • }
    • }
    • Arrays.fill(totals, 0);
    • return out;
    • }
    • }