GithubHelp home page GithubHelp logo

35.tipcalculator's Introduction

TipCalculator

一個簡單快速的小費計算工具 App

Features

  • 計算資訊
    • 輸入內容
    • 小費金額
    • 總計結果
  • 小費比例
    • Slider bar 快速調整
    • 按鈕微調
    • 選取 / 加入 常用比例
  • 客製化鍵盤
    • 支援四則運算
    • 支援小數點

Key Skills Gained

將各個鍵盤按鈕以 PressType enum 管理

enum PressType: String {
    case zero = "0"
    case one = "1"
    case two = "2"
    case three = "3"
    case four = "4"
    case five = "5"
    case six = "6"
    case seven = "7"
    case eight = "8"
    case nine = "9"
    case addition = "+"
    case subtraction = "-"
    case multiplication = "×"
    case division = "÷"
    case point = "."
    case delete = ""
}

擴充 PressType 功能,透過 operation 可知按鈕運算類型,state 運算方式

extension PressType {
    var name: String {
        return rawValue
    }
    
    var operation: OperationType {
        switch self {
        case .zero, .one, .two, .three, .four, .five, .six, .seven, .eight, .nine:
            return .number
        case .addition, .subtraction, .multiplication, .division:
            return .command
        case .delete:
            return .delete
        case .point:
            return .point
        }
    }
    
    var state: OperationState {
        switch self {
        case .zero, .one, .two, .three, .four, .five, .six, .seven, .eight, .nine:
            return NumberPress()
        case .addition, .subtraction, .multiplication, .division:
            return CommandPress()
        case .delete:
            return DeletePress()
        case .point:
            return PointPress()
        }
    }
}

實作 pressKeyboard 函式,完成實際運算功能

protocol NumberOperationState: OperationState {}

extension NumberOperationState {
    func pressKeyboard(press: PressType, data: inout PressData) {
        if data.status.last == .null {
            data.inputHistory.append(press.name)
            if press == .zero {
                data.status.append(.zero)
            } else {
                data.status.append(.number)
            }
        } else if data.status.last == .zero, press != .zero {
            let index = data.inputHistory.count - 1
            data.inputHistory[index] = press.name
            data.status.removeLast()
            data.status.append(.number)
        } else if data.status.last == .number {
            let index = data.inputHistory.count - 1
            data.inputHistory[index] += press.name
            data.status.append(.number)
        }
    }
}

Requirements

  • iOS 12
  • Xcode 10

Contact

[email protected]

35.tipcalculator's People

Contributors

littlema0404 avatar hackmd-deploy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.