Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov committed Jun 6, 2024
1 parent ed2feb4 commit bebf619
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
15 changes: 15 additions & 0 deletions Calcium/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ enum Operation {
case multiply
case divide
case equals

func calculateValue(lhs: Int, rhs: Int) -> Int {
switch self {
case .plus:
lhs + rhs
case .minus:
lhs - rhs
case .multiply:
lhs * rhs
case .divide:
lhs / rhs
case .equals:
0
}
}
}

extension Operation: CalculatorButtonRepresentable {
Expand Down
13 changes: 5 additions & 8 deletions Calcium/MainScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ struct MainScreen: View {
onOperationButtonPressed(calculatorButton: calculatorButton)
case .equals:
switch latestOperationButton {
case let .operation(operation) where operation == .minus:
displayingText = String(Int(leftValue)! - Int(displayingText)!)
case let .operation(operation) where operation == .plus:
displayingText = String(Int(leftValue)! + Int(displayingText)!)
case let .operation(operation) where operation == .multiply:
displayingText = String(Int(leftValue)! * Int(displayingText)!)
case let .operation(operation) where operation == .divide:
displayingText = String(Int(leftValue)! / Int(displayingText)!)
case let .operation(operation):
displayingText = String(operation.calculateValue(
lhs: Int(leftValue)!,
rhs: Int(displayingText)!)
)
default:
break
}
Expand Down

0 comments on commit bebf619

Please sign in to comment.