3
2

More than 3 years have passed since last update.

Swift初学者が電子レンジロジックを実装してみた!

Last updated at Posted at 2020-11-13

概要

現在、私はMENTAサービスを利用し、ヤマタクメンターにご指導いただいています。

ヤマタクメンターやサービスについて知りたい方はこちら!
(https://menta.work/plan/584)

その中で、今回は、応用学習の第2段ということで、
「電子レンジロジックの実装」の課題を行ったので、アウトプットしていきます。

なお、今回の課題はSwift初学者が作ったロジックであり、至らぬ点が多々あると思います。どうか、温かい目で見守っていただけると幸いです。

アドバイス等ありましたら、ご教授いただけると幸いです。

環境

-Playground

実装の要件

・電子レンジのワット数は900,600,200とする
・今回は、安全のために11分以上は加熱できないものとする

・タイマーの実装
  ・分と秒の概念を実装
  ・ 残り時間の出力
  ・残り時間が0秒になったら、「温め終了です」と出力し、タイマーを停止
  ・温める時間が11分以上だと「温めを開始できません!」、
   「タイマーを10分59秒以内に設定してください」と出力

・タイマーが10分59秒以内にセットされたら、温め可能フラグを返し、「温め開始!!」を出力
・選択されたワット数を出力

実装

VirtualVendingMachine.rb
import UIKit
import PlaygroundSupport

class VirtualMicrowave : UIViewController {

    //タイマーをセットしてください
    var timeSet = (min:0,sec:0)

    //Timerクラスの初期化
    var timer: Timer!

    //ワット数を決める
    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()

        //スタートタイマーを呼び出す
        startTimer()

   }


   override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animated)
   }

   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
   }


    //温め可能フラグを返す
    func startWarm(type: powerConsumptionType) -> Bool {


       var isWarmable = false


        if  timeSet.min < 11 {

            print("温め開始!!:\(type.displayName)")

           return true


        }


        return isWarmable

        //カウントダウンタイマーを呼び出す
        countDownTimer()


    }


@objc func countDownTimer(){


    if timeSet.min == 0 && timeSet.sec == 0 {

           print("温め終了です")
           timer.invalidate()


   }else if timeSet.min > 10{


        print("温めを開始できません!")
        print("タイマーを10分59秒以内に設定してください")
        timer.invalidate()



    }else if timeSet.sec > 0 && timeSet.min < 11 {

    //秒数が0以上の時または10分以下の時

        timeSet.sec -= 1
        print("\(timeSet.min)\(timeSet.sec)秒")


    } else if (timeSet.sec == 0) && timeSet.min > 0 {

    //秒数が0の時

        timeSet.sec += 59
        timeSet.min -= 1

        print("\(timeSet.min)\(timeSet.sec)秒")


    }


    }

    //タイマーを起動するメソッド
    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)

まとめ

今回は、引数の使い方やPlaygroundの環境ではあるが、タイマーメソッドの使い方を学んだ。
ゼロからロジックを考えたので、基礎構文の理解が深まった。
追加機能を考えるとすれば、レンジ機能だけではなくオーブン、トースト機能などを追加してもいいかもしれません。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2