• + 0 comments

    Why not ?

    static int numberOfChocolates(int money, int price, int bonus) {
        int chocolades = money / price;
        MainAndRest accumulated = accumulate(chocolades, bonus);
        if (accumulated.rest >= bonus) {
            MainAndRest accumulatedRests = accumulate(accumulated.rest, bonus);
            int result = chocolades + accumulated.main + accumulatedRests.main;
            return result;
        } else {
            int result = chocolades + accumulated.main;
            return result;
        }
    }
    
    static class MainAndRest {
    
        int main;
        int rest;
    
        public MainAndRest(int main, int rest) {
            this.main = main;
            this.rest = rest;
        }
    
    }
    
    static MainAndRest accumulate(int subMoney, int bonus) {
        int main = subMoney / bonus;
        int rest = subMoney % bonus;
        if ((main + rest) < bonus) {
            return new MainAndRest(main, rest);
        } else {
            MainAndRest sub = accumulate(main, bonus);
            return new MainAndRest(main + sub.main, rest + sub.rest);
        }
    }