Created
August 28, 2023 16:52
-
-
Save jamespsterling/168b523a97bc797740e3242e21495651 to your computer and use it in GitHub Desktop.
Describe an extensible coffee machine class with can also include a grinder or Wifi reordering module
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
type RoastLevel = 'light' | 'medium' | 'dark'; | |
interface Grinder { | |
/** | |
* Grind specific amount of beans at numeric grind setting | |
* | |
* @param size | |
* @param amount | |
* @returns success boolean | |
*/ | |
grind(size: number, amount: number): boolean; | |
} | |
interface ReorderModule { | |
/** | |
* Reorder coffee using wifi connected module | |
* | |
* @param roast | |
* @param quantity | |
*/ | |
reorder(roast: RoastLevel, quantity: number): boolean; | |
} | |
/** | |
* The basic coffee machine requires water, coffee, and power to brew coffee | |
*/ | |
class CoffeeMachine { | |
power: boolean; | |
water: number = 0; // 0 - 1500 milliliter | |
coffee: number = 0; // 0 - 100 grams | |
constructor() { | |
this.power = false; | |
this.water = 0; | |
this.coffee = 0; | |
} | |
on() { | |
this.power = true; | |
} | |
off() { | |
this.power = false; | |
} | |
addWater(amount: number): void { | |
this.water = amount; | |
} | |
addCoffee(amount: number): void { | |
this.coffee = amount; | |
} | |
/** | |
* Checks to see if the water level is sufficient for brewing | |
* @returns boolean | |
*/ | |
hasWater(): boolean { | |
return this.water > 200 ? true : false; | |
} | |
/** | |
* Checks to see if there is coffee grounds added and ready to brew | |
* @returns boolean | |
*/ | |
hasCoffee(): boolean { | |
return this.coffee > 0 ? true : false; | |
} | |
/** | |
* Brew coffee based on {@link RoastLevel} | |
* | |
* @param roast {@link RoastLevel} | |
* @returns success boolean | |
*/ | |
brew(roast: RoastLevel): boolean { | |
return this.power && this.hasWater() && this.hasCoffee() ? true : false; | |
} | |
/** | |
* Cleans the coffee machine using the cleaning cycle | |
* | |
* @returns done boolean | |
*/ | |
clean(): boolean { | |
return true; | |
} | |
} | |
/** | |
* The AllInOne coffee machine includes a grinder | |
*/ | |
class AllInOne extends CoffeeMachine implements Grinder { | |
grind(size: number, amount: number): boolean { | |
if (this.hasCoffee()) { | |
this.coffee = amount; // assuming zero retention | |
return true; | |
} | |
return false; | |
} | |
} | |
/** | |
* The IoT connected coffee machine includes a Wifi module to reorder coffee | |
*/ | |
class IoTMachine extends CoffeeMachine implements ReorderModule { | |
/** | |
* Reorder a specific coffee based on {@link RoastLevel} | |
* | |
* @param roast | |
* @returns success boolean | |
*/ | |
reorder(roast: RoastLevel, quantity: number): boolean { | |
return this.power ? true : false; | |
} | |
} | |
const basicMachine = new CoffeeMachine(); | |
basicMachine.on(); | |
basicMachine.brew('medium'); // run the brew for roast level 'medium' | |
basicMachine.clean(); | |
basicMachine.off(); | |
const allInOne = new AllInOne(); | |
allInOne.on(); | |
allInOne.grind(7, 50); // grind 50 grams at setting 7 | |
allInOne.brew('dark'); | |
allInOne.off(); | |
const iotMachine = new IoTMachine(); | |
iotMachine.on(); | |
iotMachine.reorder('light', 2); // re-order 2x 'light' roast units |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment