Last active
August 29, 2015 14:07
-
-
Save mooman219/7d4dfef6dc8f4f11d5c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public class Test { | |
public static void main(String[] args) { | |
Sale sale = new Sale(58); | |
System.out.println(sale.toString()); | |
} | |
public static class Sale { | |
private final int bigBoxes; | |
private final int medBoxes; | |
private final int smlBoxes; | |
public Sale(int bags) { | |
// You have 58 Bags | |
// 58 / 20 = 2.9 | |
// Change 2.9 to an int to get 2 | |
// You need 2 Big Boxes | |
bigBoxes = bags / 20; | |
// You have 2 Big Boxes and 58 Bags | |
// 2 * 20 = 40 | |
// 58 - 40 = 18 | |
// You have 18 Bags leftover. | |
bags -= bigBoxes * 20; | |
// You have 18 Bags | |
// 18 / 10 = 1.8 | |
// Change 18 to an int to get 1 | |
// You need 1 Medium Box | |
medBoxes = bags / 10; | |
// You have 1 Medium Box and 18 Bags | |
// 1 * 10 = 10 | |
// 18 - 10 = 8 | |
// You have 8 bBags leftover | |
bags -= medBoxes * 10; | |
// You have 8 bags | |
// 8 / 5 = 1.6 | |
// Change 1.6 to an int to get 1 | |
// Add 1 to get 2 | |
// You need 2 Small Boxes | |
smlBoxes = (int) (bags / 5d) + 1; | |
} | |
public int getBigBoxes() { | |
return bigBoxes; | |
} | |
public int getMedBoxes() { | |
return medBoxes; | |
} | |
public int getSmlBoxes() { | |
return smlBoxes; | |
} | |
public String toString() { | |
return "Big: " + bigBoxes + " Medium: " + medBoxes + " Small: " + smlBoxes; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment