Object subclass: MyCalculator [
MyCalculator class >> getInput: prompt [
| userInput |
userInput := FileStream stdin nextLine.
^userInput isEmpty ifTrue: [nil] ifFalse: [userInput asInteger].
]
MyCalculator class >> isPositive: number [
^number > 0.
]
MyCalculator class >> isNegative: number [
^number < 0.
]
MyCalculator class >> areNumbersEqual: num1 and: num2 [
^num1 = num2.
]
MyCalculator class >> calculate: num1 and: num2 [
| result |
result := Dictionary new.
result at: 'Addition' put: (num1 + num2).
result at: 'Subtraction' put: (num1 - num2).
result at: 'Multiplication' put: (num1 * num2).
result at: 'Division' put: (num1 / num2).
^result.
]
MyCalculator class >> main [
| num1 num2 |
FileStream stdout nextPutAll: 'Enter the first integer: '; flush.
num1 := self getInput: 'Enter the first integer: '.
FileStream stdout nextPutAll: 'Enter the second integer: '; flush.
num2 := self getInput: 'Enter the second integer: '.
FileStream stdout nextPutAll: 'First number is ', (self isPositive: num1) ifTrue: ['positive'] ifFalse: ['negative'], '.'; nl; flush.
FileStream stdout nextPutAll: 'Second number is ', (self isPositive: num2) ifTrue: ['positive'] ifFalse: ['negative'], '.'; nl; flush.
FileStream stdout nextPutAll: 'Numbers are equal: ', (self areNumbersEqual: num1 and: num2) asString, '.'; nl; flush.
| results |
results := self calculate: num1 and: num2.
FileStream stdout nextPutAll: 'Results:'; nl; flush.
results keysAndValuesDo: [ :key :value |
FileStream stdout nextPutAll: key, ': ', value asString; nl; flush.
].
]
]
MyCalculator main.