Swift beginners tried to implement microwave 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 it is the second stage of applied learning, *** I have done the task of "Implementing microwave oven 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.

If you have any advice, I would appreciate it if you could teach me.

environment

-Playground

Implementation requirements

・ The wattage of the microwave oven shall be 900,600,200. ・ This time, for safety reasons, it is not possible to heat for more than 11 minutes.

・ Implementation of timer -Implemented the concept of minutes and seconds ・ Output of remaining time ・ When the remaining time reaches 0 seconds, output "Warming is complete" and stop the timer. ・ If the warming time is 11 minutes or more, "Warming cannot be started!", Output "Please set the timer within 10 minutes 59 seconds"

・ If the timer is set within 10 minutes and 59 seconds, the warming flag is returned and "Warming starts !!" is output. ・ Output the selected wattage

Implementation

VirtualVendingMachine.rb


import UIKit
import PlaygroundSupport

class VirtualMicrowave : UIViewController {

    //Please set the timer
    var timeSet = (min:0,sec:0)
    
    //Initialization of Timer class
    var timer: Timer!
    
    //Determine the wattage
    enum powerConsumptionType{
        
        case bottun900W
        case bottun600W
        case bottun200W
     
        
    var displayName: String{
            
            switch self {
            
            case.bottun900W :
                
                return "900W"
            
            case.bottun600W :
            
                 return "600W"
                
            case.bottun200W :
                
                return "200W"
                
            }
        
    }
        
    }
    
    
    override func viewDidLoad() {
       super.viewDidLoad()
  
        //Call the start timer
        startTimer()
        
   }
   
    
   override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animated)
   }
   
   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
   }
   
   
    //Returns a warmable flag
    func startWarm(type: powerConsumptionType) -> Bool {
    
    
       var isWarmable = false
        
       
        if  timeSet.min < 11 {
        
            print("Start warming! !!:\(type.displayName)")
            
           return true
         
        
        }
        
        
        return isWarmable
        
        //Call the countdown timer
        countDownTimer()
      
 
    }
    
   
@objc func countDownTimer(){
       
  
    if timeSet.min == 0 && timeSet.sec == 0 {
           
           print("It's warming up")
           timer.invalidate()
  
 
   }else if timeSet.min > 10{
     
        
        print("I can't start warming!")
        print("Set the timer within 10 minutes 59 seconds")
        timer.invalidate()
        
    

    }else if timeSet.sec > 0 && timeSet.min < 11 {
        
    //When the number of seconds is 0 or more or 10 minutes or less
     
        timeSet.sec -= 1
        print("\(timeSet.min)Minutes\(timeSet.sec)Seconds")
     
    
    } else if (timeSet.sec == 0) && timeSet.min > 0 {
    
    //When the number of seconds is 0
      
        timeSet.sec += 59
        timeSet.min -= 1
    
        print("\(timeSet.min)Minutes\(timeSet.sec)Seconds")

        
    }
    
        
    }
    
    //Method to start timer
    func startTimer() {
          timer = Timer.scheduledTimer(
               timeInterval: 1,
               target: self,
            selector: #selector(self.countDownTimer),
               userInfo: nil,
               repeats: true)
       }
    }

        
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 300, height: 500))
let viewController = VirtualMicrowave()
viewController.view.backgroundColor = UIColor.gray
window.rootViewController = viewController
window.makeKeyAndVisible()
PlaygroundPage.current.liveView = window

let virtualMicrowave = VirtualMicrowave()
var isSuccessToWarm = virtualMicrowave.startWarm(type:.bottun200W)

print(isSuccessToWarm)

Summary

This time, I learned how to use arguments and how to use the timer method, although it is a Playground environment. Since I thought about logic from scratch, I deepened my understanding of the basic syntax. Considering the additional functions, it may be possible to add not only the range function but also the oven and toast functions.

Recommended Posts

Swift beginners tried to implement microwave logic!
Swift beginners tried to implement vending machine logic!
[Swift] I tried to implement exception handling for vending machines
[Swift] I tried to implement the function of the vending machine
[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
I tried to implement polymorphic related in Nogizaka.
[swift5] How to implement the Twitter share function
[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
How to implement UICollectionView in Swift with code only
How to implement login request processing (Rails / for beginners)
I tried to implement file upload with Spring MVC
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
App development beginners tried to make an Android calculator app
[Java] I tried to implement Yahoo API product search
I tried to implement the Euclidean algorithm in Java
[Swift] Implement swipe processing
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
I tried to implement the like function by asynchronous communication
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
[1st] RSpec beginners tried to write ModelSpec as a beginner
How to implement infinite scrolling (page nate) in Swift TableView
I tried to implement a buggy web application in Kotlin
Implement writing and reading to Property List (.plist) in Swift
[Rails] I tried to implement "Like function" using rails and js