Swift beginners tried to implement vending machine logic!

Overview

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

Requirements

Sample implementation requirements

・ 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.

Requirements for referring implemented this time

・ 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.

Sample implementation

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)

Reflections

-It was difficult to name the function. ・ I feel that the code has become monotonous.

Summary

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

Swift beginners tried to implement vending machine logic!
Swift beginners tried to implement microwave logic!
[Swift] I tried to implement the function of the vending machine
[Swift] I tried to implement exception handling for vending machines
[Swift] Easy to implement modal with PanModal
[Swift] How to implement the countdown function
I tried to implement the Iterator pattern
[Swift5] How to implement standby screen using'PKHUD'
[Swift5] How to implement animation using "lottie-ios"
I tried to implement ModanShogi with Kinx
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
I tried to implement deep learning in Java
[For beginners] How to implement the delete function
[Swift] How to implement the fade-in / out function
Rails beginners tried to get started with RSpec
I tried to implement a server using Netty
[Swift] I tried to implement Instagram profile-like UI with UICollectionView only with code without storyboard
How to implement UICollectionView in Swift with code only
How to implement login request processing (Rails / for beginners)
I tried to implement TCP / IP + BIO with JAVA
I tried to implement Firebase push notification in Java
[For beginners] Let's be able to coat like Swift!
I tried to implement Stalin sort with Java Collector
[Java] I tried to implement Yahoo API product search
I tried to implement the Euclidean algorithm in Java
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View