-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperature.go
136 lines (90 loc) · 3.36 KB
/
temperature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package weathermodule_v2
//
import (
"fmt"
)
//
type TemperatureScale int
//
const (
Kelvin TemperatureScale = iota
Celsius
Fahrenheit
)
//
func (tempScale TemperatureScale) String() string {
return [...]string{"Kelvin", "Celsius", "Fahrenheit"}[tempScale]
}
//
type Temperature struct {
//
temperatureValue float64
currentTemperatureScale TemperatureScale
temperatureScaleSymbol string
}
// Defining the Temperature initializer
func (temp *Temperature) InitializeTemperature(value float64) {
temp.temperatureValue = value
temp.currentTemperatureScale = Kelvin
temp.temperatureScaleSymbol = " K"
}
// 'temperatureValue' attribute getter
func (temp *Temperature) GetTemperatureValue() float64 {
return temp.temperatureValue
}
// 'currentTemperatureScale' attribute getter
func (temp *Temperature) GetCurrentTemperatureScale() TemperatureScale {
return temp.currentTemperatureScale
}
// 'temperatureScaleSymbol' attribute getter
func (temp *Temperature) GetTemperatureScaleSymbol() string {
return temp.temperatureScaleSymbol
}
// Temperature conversion to Kelvin (°K)
func (temp *Temperature) SetTemperatureAsKelvin() {
if temp.currentTemperatureScale == Celsius {
temp.temperatureValue = temp.temperatureValue + 273.15
temp.currentTemperatureScale = Kelvin
temp.temperatureScaleSymbol = " K"
fmt.Printf(Green() + "Temperature converted in Kelvin (K) successfully." + Reset() + "\n")
} else if temp.currentTemperatureScale == Fahrenheit {
temp.temperatureValue = (temp.temperatureValue - 32) * (5.0/9.0) + 273.15
temp.currentTemperatureScale = Kelvin
temp.temperatureScaleSymbol = " K"
fmt.Printf(Green() + "Temperature converted in Kelvin (K) successfully." + Reset() + "\n")
} else {
fmt.Printf(Red() + "Temperature already in Kelvin (K)." + Reset() + "\n")
}
}
// Temperature conversion to Celsius (°C)
func (temp *Temperature) SetTemperatureAsCelsius() {
if temp.currentTemperatureScale == Kelvin {
temp.temperatureValue = temp.temperatureValue - 273.15
temp.currentTemperatureScale = Celsius
temp.temperatureScaleSymbol = " °C"
fmt.Printf(Green() + "Temperature converted in Celsius (°C) successfully." + Reset() + "\n")
} else if temp.currentTemperatureScale == Fahrenheit {
temp.temperatureValue = (temp.temperatureValue - 32) * (5.0/9.0)
temp.currentTemperatureScale = Celsius
temp.temperatureScaleSymbol = " °C"
fmt.Printf(Green() + "Temperature converted in Celsius (°C) successfully." + Reset() + "\n")
} else {
fmt.Printf(Red() + "Temperature already in Celsius (°C)." + Reset() + "\n")
}
}
// Temperature conversion to Fahrenheit (°F)
func (temp *Temperature) SetTemperatureAsFahrenheit() {
if temp.currentTemperatureScale == Kelvin {
temp.temperatureValue = (temp.temperatureValue - 273.15) * (9.0/5.0) + 32
temp.currentTemperatureScale = Fahrenheit
temp.temperatureScaleSymbol = " °F"
fmt.Printf(Green() + "Temperature converted in Fahrenheit (°F) successfully." + Reset() + "\n")
} else if temp.currentTemperatureScale == Celsius {
temp.temperatureValue = (temp.temperatureValue * (9.0/5.0)) + 32
temp.currentTemperatureScale = Fahrenheit
temp.temperatureScaleSymbol = " °F"
fmt.Printf(Green() + "Temperature converted in Fahrenheit (°F) successfully." + Reset() + "\n")
} else {
fmt.Printf(Red() + "Temperature already in Fahrenheit (°F)." + Reset() + "\n")
}
}