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