[kotlin] coffe project

Viewer

copydownloadembedprintName: coffe project
  1. package machine
  2. //import kotlin.system.exitProcess
  3.  
  4. //top-level input process
  5. val inp = readln()
  6.  
  7. //Machine Supplies Initialized
  8. var water = 400
  9. var milk = 540
  10. var coffeeBeans = 120
  11. var cups = 9
  12. var money = 550
  13.  
  14. //Variables for espresso min ingredients
  15. const val espWater = 250
  16. const val espMilk = 0
  17. const val espBeans = 16
  18. const val espCost = 4
  19.  
  20.  
  21. //Variables for latte min ingredients
  22. const val latWater = 350
  23. const val latMilk = 75
  24. const val latBeans = 20
  25. const val latCost = 7
  26.  
  27. //Variables for cappuccino min ingredients
  28. const val capWater = 200
  29. const val capMilk = 100
  30. const val capBeans = 12
  31. const val capCost = 6
  32.  
  33. //Show current state of machine
  34. //fun currentState() {
  35.  
  36. //}
  37.  
  38. //functions for processing coffee
  39. fun espresso() {
  40.     println("I have enough resources, making you a coffee!")
  41.     water -= espWater
  42.     milk -= espMilk
  43.     coffeeBeans -= espBeans
  44.     cups -= 1
  45.     money += espCost
  46. }
  47.  
  48. fun latte () {
  49.     println("I have enough resources, making you a coffee!")
  50.     water -= latWater
  51.     milk -= latMilk
  52.     coffeeBeans -= latBeans
  53.     cups -= 1
  54.     money += latCost
  55. }
  56.  
  57. fun cappuccino() {
  58.     println("I have enough resources, making you a coffee!")
  59.     water -= capWater
  60.     milk -= capMilk
  61.     coffeeBeans -= capBeans
  62.     cups -= 1
  63.     money += capCost
  64. }
  65.  
  66. //functions for checking supplies
  67. fun waterCheck(): Boolean {
  68.     return (water > espWater && water > latWater && water > capWater)
  69. }
  70.  
  71. fun milkCheck(): Boolean {
  72.     return (milk > espMilk && milk > latMilk && milk > capMilk)
  73. }
  74.  
  75. fun beansCheck(): Boolean {
  76.     return (coffeeBeans > espBeans&& coffeeBeans > latBeans && coffeeBeans > capBeans)
  77. }
  78.  
  79. fun cupsCheck(): Boolean {
  80.     return cups > 0
  81. }
  82.  
  83. //function for buying coffee
  84. fun buy() {
  85.     val buyInp = readln()
  86.     println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu: $buyInp")
  87.     println()
  88.     if (waterCheck() || milkCheck() || beansCheck() || cupsCheck()) {
  89.         when (inp) {
  90.             "1" -> espresso()
  91.             "2" -> latte()
  92.             "3" -> cappuccino()
  93.             "back" -> mainMenu()
  94.         }
  95.     } else if (!waterCheck()) {
  96.         println("Sorry, not enough water!")
  97.     } else if (!milkCheck()) {
  98.         println("Sorry, not enough milk!")
  99.     } else if (!beansCheck()) {
  100.         println("Sorry, not enough coffee beans!")
  101.     } else if (!cupsCheck()) {
  102.         println("Sorry, not enough cups!")
  103.     }
  104. }
  105.  
  106. //function for filling machine
  107. fun fill() {
  108.     val waterAdded = readln().toInt()
  109.     val milkAdded = readln().toInt()
  110.     val beansAdded = readln().toInt()
  111.     val cupsAdded = readln().toInt()
  112.  
  113.     println("Write how many ml of water do you want to add: $waterAdded")
  114.     println("Write how many ml of milk do you want to add: $milkAdded")
  115.     println("Write how many grams of coffee beans do you want to add: $beansAdded")
  116.     println("Write how many disposable cups of coffee do you want to add: $cupsAdded")
  117.  
  118.     water += waterAdded
  119.     milk += milkAdded
  120.     coffeeBeans += beansAdded
  121.     cups += cupsAdded
  122.  
  123. }
  124.  
  125. //function for taking monies
  126. fun take() {
  127.     println("I gave you $$money")
  128.     money = 0
  129. }
  130.  
  131. //function for remaining supplies
  132. fun remaining() {
  133.     println("The coffee machine has:")
  134.     println("$water ml of water")
  135.     println("$milk ml of milk")
  136.     println("$coffeeBeans g of coffee beans")
  137.     println("$cups disposable cups")
  138.     println("$money of money")
  139.     //mainMenu()
  140.  
  141. }
  142.  
  143.  
  144. //function for selecting machine action
  145. fun mainMenu() {
  146.     //val inp = readln()
  147.     println("Write action (buy, fill, take, remaining, exit): $inp")
  148.     when (inp) {
  149.         "buy" -> buy()
  150.         "fill" -> fill()
  151.         "take" -> take()
  152.         "remaining" -> remaining()
  153.     }
  154. }
  155.  
  156. //main program
  157. fun main() {
  158.     do {
  159.         mainMenu()
  160.     } while (inp != "exit")
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  

Editor

You can edit this paste and save as new:


File Description
  • coffe project
  • Paste Code
  • 28 Sep-2022
  • 3.86 Kb
You can Share it: