Currently, I am using the MENTA service and receiving guidance from a Yamataku mentor.
Click here to find out about Yamataku Mentors and services! (https://menta.work/plan/584)
Among them, this time, I did the task of "implementing vending machine logic", so I will output it.
In addition, this issue is a logic created by a Swift beginner, and I think there are many points that cannot be reached. I would appreciate it if you could watch over with warm eyes.
We are always accepting advice.
environment
-Playground
・ You can enter Japanese Yen -Compares the entered Japanese yen with the price of the drink and returns the purchaseable flag if the former exceeds the latter value and the target drink is in stock. ・ Target drinks are "coffee", "water" and "monster" ・ Reduce the inventory of the target drink by 1 when returning the purchase availability judgment of the target drink.
・ Return the amount of change when purchasing a drink ・ Outputs "out of stock" when out of stock ・ Output the number of purchased drinks in stock each time they are purchased -Added a function to output the input Japanese Yen ・ Return the amount of money that was put in when there is no stock ・ Returns ture even when the same price as the drink is put in ・ When the purchase is completed, "Thank you for your purchase" is output.
VirtualVendingMachine.rb
import UIKit
class VirtualVendingMachine {
//Drink type
enum DrinkType {
case coffee
case water
case monster
}
var inputedYen: Int = 0
//price
var coffeePrice: Int = 120
var waterPrice: Int = 100
var monsterPrice: Int = 210
//stock
var coffeeStock: Int = 5
var waterStock: Int = 5
var monsterStock: Int = 5
//Drink purchase
func buyDrink(type: DrinkType, inputedYen: Int) -> Bool {
var isBuyable = false
coinOutput(type: type, inputedYen: inputedYen)
switch type {
case .coffee:
isBuyable = coffeePrice <= inputedYen && 0 < coffeeStock
case .water:
isBuyable = waterPrice <= inputedYen && 0 < waterStock
case .monster:
isBuyable = monsterPrice <= inputedYen && 0 < monsterStock
}
returnDrinkTypeSwich(type: type, inputedYen:inputedYen)
return isBuyable
}
//Drink type
func returnDrinkTypeSwich(type: DrinkType, inputedYen:Int) {
switch type {
case .coffee:
coffeeReturnYenStock(type:type, inputedYen: inputedYen)
case .water:
waterReturnYenStock(type:type, inputedYen: inputedYen)
case .monster:
waterReturnYenStock(type:type, inputedYen: inputedYen)
}
}
//Output Japanese Yen
func coinOutput(type: DrinkType,inputedYen: Int){
print("\(inputedYen)Yen was put in")
}
//When coffee is bought, change and inventory management
func coffeeReturnYenStock(type: DrinkType,inputedYen: Int){
//Return change
if coffeeStock <= 0 {
print("Because there is no coffee in stock\(inputedYen)I will return the yen")
}else if inputedYen >= coffeePrice{
var returnYen1 = inputedYen - coffeePrice
print("Thank you for your purchase")
print("Change\(returnYen1)It's a yen")
}else{
var noenoughcoins = coffeePrice - inputedYen
print("\(noenoughcoins)Not enough yen")
}
//Inventory management
if inputedYen >= coffeePrice && coffeeStock > 0 {
coffeeStock -= 1
print("Current coffee inventory\(coffeeStock)is")
}else{
return
}
}
//When water is bought, change and manage inventory
func waterReturnYenStock(type: DrinkType,inputedYen: Int){
if waterStock <= 0 {
print("Because there is no water in stock\(inputedYen)I will return the yen")
}else if inputedYen >= waterPrice{
var returnYen2 = inputedYen - waterPrice
print("Thank you for your purchase")
print("Change\(returnYen2)It's a yen")
}else{
print("Not enough money")
}
if inputedYen >= waterPrice && waterStock > 0 {
waterStock -= 1
print("Current water inventory\(waterStock)is")
}else{
return
}
}
//Manage change and inventory when monsters are bought
func monsterReturnYenStock(type: DrinkType,inputedYen: Int){
if monsterStock <= 0 {
print("Because there is no monster in stock\(inputedYen)I will return the yen")
}else if inputedYen >= monsterPrice {
var returnYen3 = inputedYen - monsterPrice
print("Thank you for your purchase")
print("Change\(returnYen3)It's a yen")
}else{
print("Not enough money")
}
if inputedYen >= monsterPrice && monsterStock > 0{
monsterStock -= 1
print("The current inventory of monsters\(monsterStock)is")
}else{
return
}
}
}
let virtualVendingMachine = VirtualVendingMachine()
let isSuccessToBuy = virtualVendingMachine.buyDrink(type: .water, inputedYen: 150)
print(isSuccessToBuy)
-It was difficult to name the function. ・ I feel that the code has become monotonous.
I think that thinking about logic is very suitable for the output of basic learning. You can discover various things and notice what you are not good at, so if you are aiming to break away from basic learning, why not give it a try?
Well then!
Recommended Posts