[kotlin] coffe project
Viewer
- package machine
- //import kotlin.system.exitProcess
- //top-level input process
- val inp = readln()
- //Machine Supplies Initialized
- var water = 400
- var milk = 540
- var coffeeBeans = 120
- var cups = 9
- var money = 550
- //Variables for espresso min ingredients
- const val espWater = 250
- const val espMilk = 0
- const val espBeans = 16
- const val espCost = 4
- //Variables for latte min ingredients
- const val latWater = 350
- const val latMilk = 75
- const val latBeans = 20
- const val latCost = 7
- //Variables for cappuccino min ingredients
- const val capWater = 200
- const val capMilk = 100
- const val capBeans = 12
- const val capCost = 6
- //Show current state of machine
- //fun currentState() {
- //}
- //functions for processing coffee
- fun espresso() {
- println("I have enough resources, making you a coffee!")
- water -= espWater
- milk -= espMilk
- coffeeBeans -= espBeans
- cups -= 1
- money += espCost
- }
- fun latte () {
- println("I have enough resources, making you a coffee!")
- water -= latWater
- milk -= latMilk
- coffeeBeans -= latBeans
- cups -= 1
- money += latCost
- }
- fun cappuccino() {
- println("I have enough resources, making you a coffee!")
- water -= capWater
- milk -= capMilk
- coffeeBeans -= capBeans
- cups -= 1
- money += capCost
- }
- //functions for checking supplies
- fun waterCheck(): Boolean {
- return (water > espWater && water > latWater && water > capWater)
- }
- fun milkCheck(): Boolean {
- return (milk > espMilk && milk > latMilk && milk > capMilk)
- }
- fun beansCheck(): Boolean {
- return (coffeeBeans > espBeans&& coffeeBeans > latBeans && coffeeBeans > capBeans)
- }
- fun cupsCheck(): Boolean {
- return cups > 0
- }
- //function for buying coffee
- fun buy() {
- val buyInp = readln()
- println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu: $buyInp")
- println()
- if (waterCheck() || milkCheck() || beansCheck() || cupsCheck()) {
- when (inp) {
- "1" -> espresso()
- "2" -> latte()
- "3" -> cappuccino()
- "back" -> mainMenu()
- }
- } else if (!waterCheck()) {
- println("Sorry, not enough water!")
- } else if (!milkCheck()) {
- println("Sorry, not enough milk!")
- } else if (!beansCheck()) {
- println("Sorry, not enough coffee beans!")
- } else if (!cupsCheck()) {
- println("Sorry, not enough cups!")
- }
- }
- //function for filling machine
- fun fill() {
- val waterAdded = readln().toInt()
- val milkAdded = readln().toInt()
- val beansAdded = readln().toInt()
- val cupsAdded = readln().toInt()
- println("Write how many ml of water do you want to add: $waterAdded")
- println("Write how many ml of milk do you want to add: $milkAdded")
- println("Write how many grams of coffee beans do you want to add: $beansAdded")
- println("Write how many disposable cups of coffee do you want to add: $cupsAdded")
- water += waterAdded
- milk += milkAdded
- coffeeBeans += beansAdded
- cups += cupsAdded
- }
- //function for taking monies
- fun take() {
- println("I gave you $$money")
- money = 0
- }
- //function for remaining supplies
- fun remaining() {
- println("The coffee machine has:")
- println("$water ml of water")
- println("$milk ml of milk")
- println("$coffeeBeans g of coffee beans")
- println("$cups disposable cups")
- println("$money of money")
- //mainMenu()
- }
- //function for selecting machine action
- fun mainMenu() {
- //val inp = readln()
- println("Write action (buy, fill, take, remaining, exit): $inp")
- when (inp) {
- "buy" -> buy()
- "fill" -> fill()
- "take" -> take()
- "remaining" -> remaining()
- }
- }
- //main program
- fun main() {
- do {
- mainMenu()
- } while (inp != "exit")
- }
Editor
You can edit this paste and save as new: