-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDoMApplication.java
202 lines (159 loc) · 6.25 KB
/
DoMApplication.java
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Student: Rustam Zokirov
StudentID: U1910049
Lab #4: DoM Application
*/
import java.util.Scanner; // Import Snanner class
import java.util.Random; // Import Random class
class DoMApplication {
public static void main(String[] args) { // main() method
Scanner input = new Scanner(System.in); // Create a Scanner object
int option, isExit; // Initializing some variables
CityTemperature temperature = new CityTemperature(); // Create object of CityTemperature
CityRainFall rainFall = new CityRainFall(); // Create object of CityRainFall
do { // Loop for reusing the program
option = showMenu(); // The option value
switch (option){
case 1:
temperature.viewCityInfo(); // show city info
break;
case 2:
temperature.viewClimateData(); // show city temperature info
break;
case 3:
rainFall.viewClimateData(); // show city rainfall info
break;
case 4:
int ID;
String name;
System.out.print("Enter the city ID: ");
ID = input.nextInt(); // get user's input
temperature.setCityID(ID); // set city new ID
System.out.print("Enter the city name: ");
name = input.next(); // get user's input
temperature.setCityName(name); // set city new name
break;
case 5:
double[] cityClimateData = new double[24]; // create an array
for(int j=0; j<24; j++) {
System.out.print("Enter climate data for " + j + ":00: ");
cityClimateData[j] = input.nextDouble(); // take user's input
}
temperature.setClimateData(cityClimateData); // set the new temperature data
break;
case 6:
double[] cityRainFallData = new double[12]; // create an array
for(int k=0; k<12; k++) {
System.out.print("Enter rainfall data for month #" + (k+1) + ": ");
cityRainFallData[k] = input.nextDouble(); // take user's inputs
}
rainFall.setClimateData(cityRainFallData); // set the new rainfall data
break;
case 0:
System.out.println("Quitting Program...");
System.exit(0); // program ends
break;
default:
System.out.println("Sorry, please enter valid option!");
} // End of switch
System.out.print("\nDo you want to continue? (0.No 1.Yes) - ");
isExit = input.nextInt(); // validation checking
} while(isExit == 1);
} // End of main
public static int showMenu() {
Scanner input = new Scanner(System.in); // Create a Scanner object
int option = 0;
// Printing menu to screen
System.out.println("\n***** MAIN MENU *****");
System.out.println("1. City information");
System.out.println("2. City temperature (24 hours)");
System.out.println("3. City rainfall (12 months)");
System.out.println("4. Set city information");
System.out.println("5. Set city temperature (24 hours)");
System.out.println("6. Set city rainfall (12 months)");
System.out.println("0. Exit");
// Getting user option from above menu
System.out.print("Your choice: ");
option = input.nextInt();
System.out.println();
return option;
} // End of showMenu
}
class City {
private int cityID; // private = restricted access
private String cityName; // private = restricted access
City() { // Default constructor
cityID = 49;
cityName = "Silicon Valley";
}
City(int cityID, String cityName) { // Parametrized constructor
this.cityID = cityID;
this.cityName = cityName;
}
// Getter for cityID
public int getCityID() {
return cityID;
}
// Setter for cityID
public void setCityID(int cityID) {
this.cityID = cityID;
}
// Getter for cityName
public String getCityName() {
return cityName;
}
// Setter for cityName
public void setCityName(String cityName) {
this.cityName = cityName;
}
// Viewer for CityInfo: Id, Name
public void viewCityInfo() {
System.out.println("City ID: " + getCityID());
System.out.println("City Name: " + getCityName());
}
}
class CityTemperature extends City{
private double[] cityClimateData = {};
CityTemperature() {
cityClimateData = new double[24];
for(int i=0; i<24; i++) { // Set to default 0
cityClimateData[i] = i+20;
}
}
public void setClimateData(double[] cityClimateData) {
this.cityClimateData = cityClimateData;
}
// Getter for cityClimateData
public double[] getClimateData() {
return cityClimateData;
}
public void viewClimateData() {
for(int i=0; i<24; i++) { // Set to default 0
System.out.println("Time: " + i + ":00 = " + cityClimateData[i] + " C");
}
}
}
class CityRainFall extends City {
private double[] cityClimateData = {}; // data member
CityRainFall() { // default constructor
Random random = new Random(); // Create an object of Random class
cityClimateData = new double[12]; // Create an array
for(int i=0; i<12; i++) { // Set to default 0
int rand_num = random.nextInt(200);
cityClimateData[i] = rand_num + 500; // Initialize the elements
}
}
public void setClimateData(double[] cityClimateData) {
this.cityClimateData = cityClimateData;
}
// Getter for cityClimateData
public double[] getClimateData() {
return cityClimateData;
}
// Viewer for ClimateData
public void viewClimateData() {
for(int i=0; i<12; i++) { // Set to default 0
System.out.println("Month #" + (i+1) +": " + cityClimateData[i] + " mm");
}
}
}