-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarProgram.java
More file actions
316 lines (283 loc) · 10.3 KB
/
CarProgram.java
File metadata and controls
316 lines (283 loc) · 10.3 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import java.util.Scanner;
/**
* Created by Lucas Chapman on 4/23/2020.
* Program designed to allow the user to enter the car they want to purchase.
*/
public class CarProgram {
public static Scanner myScanner;
public static void main(String[] args) {
boolean isValid = false;
init();
while (!isValid) {
try {
int iCar = car();
if (iCar == 1) {
gasCar();
isValid = true;
} else if (iCar == 2) {
electricCar();
isValid = true;
} else {
System.out.println("Error, invalid car type entered\nPress Enter to " +
"try again");
myScanner.nextLine();
}
} catch (Exception e) {
System.out.println("Error, invalid car type entered\nPress Enter to try " +
"again");
myScanner.nextLine();
}
}
}
/**
* Sets up the Scanner and displays a welcome message.
*/
private static void init() {
myScanner = new Scanner(System.in);
System.out.println("Welcome to the Car Program!");
}
/**
* Prompts the user for if they want to get either a gas car or a electric car.
* @return returns the type of car that was selected
*/
private static int car() {
String iData;
System.out.println("Please enter the type of car:\n1 - Gas car\n2 - Electric car");
iData = myScanner.nextLine();
return Integer.parseInt(iData);
}
/**
* Calls other methods to gather enough information to create a GasCar object to be
* used to display formatted data in the console.
*/
private static void gasCar() {
String iMake = make();
String iModel = model();
int iYear = year();
String iColor = color();
int iWeight = weight();
double iTankSize = tankSize();
String iFuelType = fuelType();
GasCar gCar = new GasCar(iMake, iModel, iYear, iColor, iWeight, iTankSize,
iFuelType);
gasCarOutput(gCar);
}
/**
* Prompts the user for a car's make.
* @return returns what was entered
*/
private static String make() {
String data;
System.out.println("Please enter the car's make: ");
data = myScanner.nextLine();
if (data.trim().isEmpty()) {
System.out.println("Nothing entered, defaulted to Honda\nPress Enter to " +
"continue");
myScanner.nextLine();
}
return data;
}
/**
* Prompts the user for the model of a car.
* @return returns what was entered
*/
private static String model() {
String data;
System.out.println("Please enter the model of a car: ");
data = myScanner.nextLine();
if (data.trim().isEmpty()) {
System.out.println("Nothing entered, model defaulted to Civic\nPress Enter to" +
" continue");
myScanner.nextLine();
}
return data;
}
/**
* Prompts to user for the car's year.
* @return returns what was entered
*/
private static int year() {
String input;
int year = 0;
boolean isNumeric = false;
while (!isNumeric) {
try {
System.out.println("Please enter the car's year: ");
input = myScanner.nextLine();
year = Integer.parseInt(input);
// code after formatting will only execute if no exceptions are caught
if (year <= 0) {
System.out.println("Invalid year, year defaulted to 2020\nPress " +
"Enter to continue");
myScanner.nextLine();
}
isNumeric = true;
} catch (Exception e){
System.out.println("Error, data entered is not numeric\nPress Enter to " +
"continue");
myScanner.nextLine();
}
}
return year;
}
/**
* Prompts the user for the color of the car.
* @return returns what was entered
*/
private static String color() {
String data;
System.out.println("Please enter the color of the car: ");
data = myScanner.nextLine();
if (data.trim().isEmpty()) {
System.out.println("Nothing was entered, color defaulted to black\nPress " +
"Enter to continue");
myScanner.nextLine();
}
return data;
}
/**
* Prompts the user for the weight of the car.
* @return returns what was entered
*/
private static int weight() {
String input;
int weight = 0;
boolean isNumeric = false;
while (!isNumeric) {
try {
System.out.println("Please enter how much the car weighs: ");
input = myScanner.nextLine();
weight = Integer.parseInt(input);
// code after formatting will only execute if no exceptions are caught
if (weight <= 0) {
System.out.println("Invalid weight, weight defaulted to 2,875 lbs\n" +
"Press Enter to continue");
myScanner.nextLine();
}
isNumeric = true;
} catch (Exception e){
System.out.println("Error, data entered is not numeric\nPress Enter to " +
"continue");
myScanner.nextLine();
}
}
return weight;
}
/**
* Prompts the user for the size of the car's tank.
* @return returns what was entered
*/
private static double tankSize() {
String input;
double tankSize = 0;
boolean isNumeric = false;
while (!isNumeric) {
try {
System.out.println("Please enter the size of the car's tank: ");
input = myScanner.nextLine();
tankSize = Double.parseDouble(input);
// code after formatting will only execute if no exceptions are caught
if (tankSize <= 0) {
System.out.println("Invalid tank size, data defaulted to 12.4 gal" +
"\nPress Enter to continue");
myScanner.nextLine();
}
isNumeric = true;
} catch (Exception e) {
System.out.println("Error, data entered is not numeric\nPress Enter to " +
"try again");
myScanner.nextLine();
}
}
return tankSize;
}
/**
* Prompts the user for the type of fuel the car uses.
* @return returns what was entered
*/
private static String fuelType() {
String data;
System.out.println("Please enter the type of fuel the car uses: ");
data = myScanner.nextLine();
if (data.trim().isEmpty()) {
System.out.println("Nothing was entered, defaulted to gasoline\nPress Enter " +
"to continue");
myScanner.nextLine();
}
return data;
}
/**
* Displays formatted gas car data in the console.
* @param gCar passes over a GasCar object
*/
private static void gasCarOutput(GasCar gCar) {
System.out.println(gCar.toString() + "\n\nPress Enter to exit the program");
myScanner.nextLine();
}
/**
* Calls other methods to gather enough information to create ElectricCar objects to be
* used to display formatted data back to the user.
*/
private static void electricCar() {
String iMake = make();
String iModel = model();
int iYear = year();
String iColor = color();
int iWeight = weight();
double iBatterySize = batterySize();
String iBatteryType = batteryType();
ElectricCar eCar = new ElectricCar(iMake, iModel, iYear, iColor, iWeight, iBatterySize,
iBatteryType);
electricCarOutput(eCar);
}
/**
* Prompts the user for the size of the car's battery.
* @return returns what was entered
*/
private static double batterySize() {
String input;
double batterySize = 0;
boolean isNumeric = false;
while (!isNumeric) {
try {
System.out.println("Please enter the size of the car's battery: ");
input = myScanner.nextLine();
batterySize = Double.parseDouble(input);
// code after formatting will only execute if no exceptions are caught
if (batterySize <= 0) {
System.out.println("Invalid battery size, data defaulted to 85 kw\n" +
"Press Enter to continue");
myScanner.nextLine();
}
isNumeric = true;
} catch (Exception e) {
System.out.println("Error, data entered is not numeric\nPress Enter to try again");
myScanner.nextLine();
}
}
return batterySize;
}
/**
* Prompts the user for the type of battery the car uses.
* @return returns what was entered
*/
private static String batteryType() {
String data;
System.out.println("Please enter the type of battery the car uses: ");
data = myScanner.nextLine();
if (data.trim().isEmpty()) {
System.out.println("Nothing was entered, defaulted to lithium-ion battery" +
"\nPress Enter to continue");
myScanner.nextLine();
}
return data;
}
/**
* Displays formatted electric car data to the console.
* @param eCar passes over a ElectricCar object
*/
private static void electricCarOutput(ElectricCar eCar) {
System.out.println(eCar.toString() + "\n\nPress Enter to exit the program");
myScanner.nextLine();
}
}