Skip to content

Instantly share code, notes, and snippets.

@llxxee
Last active March 21, 2018 10:09
Show Gist options
  • Save llxxee/0e17030383f8a1e1b59ef1103b965c80 to your computer and use it in GitHub Desktop.
Save llxxee/0e17030383f8a1e1b59ef1103b965c80 to your computer and use it in GitHub Desktop.
public class TrashInfo {
// the general category of the trash, should be one of cash-for-trash, e-waste, second-hand-goods
private String trashType;
// detailed price info, with key-value pair of trashName-PriceInfo(unit, pricePerUnit)
private HashMap<String, PriceInfo> trashPrices = new HashMap<>();
public void setTrashType(String trashType) {
this.trashType = trashType;
}
public String getTrashType() {
return trashType;
}
public void addTrashPrice(String trashName, String unit, double pricePerUnit) {
trashPrices.put(trashName, new PriceInfo(unit, pricePerUnit));
}
PriceInfo getTrashPrice(String trashName){
return trashPrices.get(trashName);
}
}
class PriceInfo{
private String unit;
private double pricePerUnit;
PriceInfo(String unit, double pricePerUnit){
this.unit = unit;
this.pricePerUnit = pricePerUnit;
}
public String getUnit() {
return unit;
}
public double getPricePerUnit() {
return pricePerUnit;
}
}
/**
* Example usage, to create an trashInfo object and set trash prices
*/
TrashInfo myAcceptableTrash = new TrashInfo();
// set trash category to cash-for-trash
myAcceptableTrash.setTrashType("cash-for-trash");
// set paper price to 1.2 dollar per kg
myAcceptableTrash.addTrashPrice("paper", "kg", 1.2);
// set metal bin price to 2.5 dollar per kg
myAcceptableTrash.addTrashPrice("metal bin", "kg", 2.5);
// to get the trash category
String trashType = myAcceptableTrash.getTrashType();
// to get the trash price of paper
PriceInfo paperPrice = myAcceptableTrash.getTrashPrice("paper");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment