Last active
July 15, 2016 02:07
-
-
Save KazWolfe/8c71f6d709b6cf8d3ebe5aba70ba8e66 to your computer and use it in GitHub Desktop.
Programmer Milk
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
// Inspired by the joke: | |
// "A programmer was walking out the door for work when | |
// his wife said, 'While you're out, buy some milk.' | |
// The programmer never came home. | |
programmer.getToDoList().getLocationList().addItem(Location.ANYWHERE, () -> {programmer.moveTo(Location.SUPERMARKET);}); | |
programmer.getToDoList().getLocationList().addItem(Location.SUPERMARKET, (neededAmount) -> { | |
// Get the price and inventory from the Economy. | |
double price = Economy.getStore(Store.SUPERMARKET).getInventory().getItemClass(ItemClass.MILK).getPrice(); | |
int milkInv = Economy.getStore(Store.SUPERMARKET).getInventory().getItemClass(ItemClass.MILK).getCount(); | |
// Define the item itself. | |
Item milk; | |
// Define the quantity var | |
int quantity; | |
// Make sure the store HAS milk, and enough. | |
if (milkInv == 0) { | |
throw new IndexOutOfBoundsException("There is no milk to get!"); | |
} else if (milkInv < neededAmount) { | |
// Not enough milk, but get what they have. | |
quantity = milkInv; | |
} else { | |
// There's enough milk | |
quantity = neededAmount; | |
} | |
// Check that the programmer has enough money to buy the requested milk. | |
if (programmer.getMoney() >= price * quantity) { | |
// Actually buy the milk | |
milk = Economy.getStore(Store.SUPERMARKET).buy(ItemClass.MILK, quantity); | |
milk.moveTo(Location.HOME); | |
} else { | |
// The programmer doen't have enough money to buy all the requested milk. Get as much as we can. | |
if (programmer.getMoney() < price) { | |
// The programmer is broke af, or milk is expensive af. | |
throw new NoMoneyException(); | |
} else { | |
// Get what little milk we can get. | |
int maxBuyable = Math.floor(programmer.getMoney() / price); | |
if (maxBuyable < quantity) { | |
quantity = maxBuyable; | |
} | |
milk = Economy.getStore(Store.SUPERMARKET).buy(ItemClass.MILK, quantity); | |
milk.moveTo(Location.HOME); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment