-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarsLeeWeek4.java
367 lines (336 loc) · 14.3 KB
/
carsLeeWeek4.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
Seyed Program Request:
write an app that can keep track of the car's inventory in a dealership. Here is the UML
Car Class
//list of the instance variables
-make: String
-model: String
-color: String
-stockNum:int
-year: int
-mileage: int
-salePrice: double
-purchasePrice: double
-cost: double
//list of the methods
+Car (String make, String model, String color, int stockNum, int year, int mileage, double salePrice, double purchadePrice, double cost) //constructor
+getMake() : String
+getModel() : String
+getColor(): String
+getStockNum(): int
+getYear(): int
+getMileage(): int
+getSalePrice(): double
+getPurchasePrice() : double
+getCost() : double
+setMake() : String
+setModel() : String
+setColor(): String
+setStockNum(): int
+setYear(): int
+setMileage(): int
+setSalePrice(): double
+setPurchasePrice() : double
+setCost() : double
+getProfit(): double
+toString(): String
Author: Jonathan Lee
Professor: Gita Faroughi
Class: Sierra College CSCI13
Date: Feb 18 2022
NOTES:
!!!!
This program will run on its own or with the driver file named LeeShowGridLayout
The reason for this program is to help with learning Objects. The GUI based file is additonal and optional to use to help you see
that file only calls the setter methods from object instantiation needs it must be in the same location as this file
!!!!
*/
import java.util.*;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.text.*;
import java.io.*;
public class carsLeeWeek4 implements Serializable
{
private String make, model, color, dateTime;
private int stockNum, year, mileage;
private double salePrice, purchasePrice, cost;
private static int count = 0;
public carsLeeWeek4 (String make, String model, String color, int stockNum, int year, int mileage, double salePrice, double purchasePrice, String dateTime)
{
this.make = make;
this.model = model;
this.color = color;
this.stockNum = stockNum;
this.year = year;
this.mileage = mileage;
this.salePrice = salePrice;
this.purchasePrice = purchasePrice;
this.dateTime = dateTime;
count ++;
}
/* Getter Methods */
public String getMake()
{
return this.make;
}
public String getModel()
{
return this.model;
}
public String getColor()
{
return this.color;
}
public int getStockNum()
{
return this.stockNum;
}
public int getYear()
{
return this.year;
}
public int getMileage()
{
return this.mileage;
}
public double getSalePrice()
{
return this.salePrice;
}
public static int getCount()
{
return count;
}
public double getPurchasePrice()
{
return this.purchasePrice;
}
public String getdateTime()
{
return this.dateTime;
}
/* Setter Methods*/
public void setMake(String make)
{
this.make = make;
}
public void setModel(String model)
{
this.model = model;
}
public void setColor(String color)
{
this.color = color;
}
public void setStockNum(int stockNum)
{
this.stockNum = stockNum;
}
public void setYear(int year)
{
this.year = year;
}
public void setMileage(int mileage)
{
this.mileage = mileage;
}
public void setSalePrice(double salePrice)
{
this.salePrice = salePrice;
}
public void setdateTime(String time) //overrider for sales time
{
this.dateTime = time;
}
/* This method creates a new list of cars based on user entry */
public static carsLeeWeek4[] Arr(Scanner console, String time)
{
String name = "";
String confirm = "";
int currcount = carsLeeWeek4.getCount();
System.out.println(time);
System.out.print("How many new entries are needed for inventory system? ");
while (!console.hasNextInt()) //data validation system will only accept int based numbers
{
System.out.println("Numerical entry only");
System.out.print("Enter a number only: --> ");
console.next();
}
int entry2 = 0; //for temp array for object array add
int entry = console.nextInt(); // start size
carsLeeWeek4[] carsArray = new carsLeeWeek4[entry]; //array of objects created based on variable entry
for (int i = 0; i < carsArray.length; i++) //iteration over array of cars that was created for entry entry 2 will control location of iteration count based on this while loop grow if needed
{
do{ // do this until the entry is confirmed and after iterator to next array element
console.nextLine();
System.out.print("Entry #"+(i+1)+", Enter automobile \"MAKE\": ");
String make = console.nextLine();
System.out.print("Enter "+make+" \"MODEL\": ");
String model = console.next();
System.out.print("Enter "+make+" "+model+" \"COLOR\": ");
String color = console.next();
System.out.print("Enter "+color+" "+make+" "+model+" \"STOCK NUMBER\": ");
int stockNum = console.nextInt();
System.out.print("Enter Stock# "+stockNum+" "+color+" "+make+" "+model+" \"YEAR\": ");
int year = console.nextInt();
System.out.print("Enter Stock# "+stockNum+" "+color+" "+year+" "+make+" "+model+" \"MILEAGE\": ");
int mileage = console.nextInt();
System.out.print("Enter Stock # "+stockNum+" "+color+" "+year+" "+make+" "+model+" \"SALE PRICE\": ");
double salePrice = console.nextDouble();
System.out.print("Enter Stock# "+stockNum+" "+color+" "+year+" "+make+" "+model+" \"DEALER PURCHASE PRICE\": ");
double purchasePrice = console.nextDouble();
carsArray[i] = new carsLeeWeek4(make, model, color, stockNum, year, mileage, salePrice, purchasePrice, time); // array of object creations that calls the car class constructors, instanceation clause
System.out.println("\n*******************");
System.out.println(carsArray[i]);
System.out.println("Confirm entry is correct Y/y ?"); // confirmation required before i is incremented or it will continue with object array position as the same and replace orginal values
confirm = console.next();
} while (!confirm.equalsIgnoreCase("y")); //confirmation of each item
}
System.out.println("\n*******************************************************************");
System.out.println("*"+time+"*"); // display current time I want to add to each entry fix me
System.out.println("*******************************************************************");
System.out.println();
System.out.println("Printout of current inventory in system: "); //printout of all cars
System.out.println("\n*******************");
for (int i = 0; i< carsArray.length; i++) //iteration of over new cars for printout display total inventory
{
System.out.println(carsArray[i]);
System.out.println("\n*******************");
}
System.out.println();
return carsArray;
}
/* this method is called recursively inside of class cars when growarray is ran */
public static carsLeeWeek4[] addInventory(Scanner console, String time, int entry2, carsLeeWeek4[] carsArray)
{
String name = "";
String confirm = "";
for (int i = (carsLeeWeek4.getCount()); i < carsArray.length; i++) //iteration over array of cars that was created for entry entry 2 will control location of iteration count based on this while loop grow if needed
{
do{ // do this until the entry is confirmed and after iterator to next array element
console.nextLine();
System.out.print("Entry #"+(i+1)+", Enter automobile \"MAKE\": ");
String make = console.nextLine();
System.out.print("Enter "+make+" \"MODEL\": ");
String model = console.next();
System.out.print("Enter "+make+" "+model+" \"COLOR\": ");
String color = console.next();
System.out.print("Enter "+color+" "+make+" "+model+" \"STOCK NUMBER\": ");
int stockNum = console.nextInt();
System.out.print("Enter Stock# "+stockNum+" "+color+" "+make+" "+model+" \"YEAR\": ");
int year = console.nextInt();
System.out.print("Enter Stock# "+stockNum+" "+color+" "+year+" "+make+" "+model+" \"MILEAGE\": ");
int mileage = console.nextInt();
System.out.print("Enter Stock # "+stockNum+" "+color+" "+year+" "+make+" "+model+" \"SALE PRICE\": ");
double salePrice = console.nextDouble();
System.out.print("Enter Stock# "+stockNum+" "+color+" "+year+" "+make+" "+model+" \"DEALER PURCHASE PRICE\": ");
double purchasePrice = console.nextDouble();
carsArray[i] = new carsLeeWeek4(make, model, color, stockNum, year, mileage, salePrice, purchasePrice, time); // array of object creations that calls the car class constructors, instanceation clause
System.out.println("\n*******************");
System.out.println(carsArray[i]);
System.out.println("Confirm entry is correct Y/y ?"); // confirmation required before i is incremented or it will continue with object array position as the same and replace orginal values
confirm = console.next();
} while (!confirm.equalsIgnoreCase("y")); //confirmation of each item
}
System.out.println("\n*******************************************************************");
System.out.println("*"+time+"*"); // display current time I want to add to each entry fix me
System.out.println("*******************************************************************");
System.out.println();
System.out.println("UPDATED----->\n\tInventory in system: "); //printout of all cars
System.out.println("\n*******************");
for (int i = 0; i< carsArray.length; i++) //iteration of over new cars for printout display total inventory
{
System.out.println(carsArray[i]);
System.out.println("\n*******************");
}
System.out.println();
return carsArray;
}
/* This method will grow the array without deleting the data in it based on how many cars you need added */
public static carsLeeWeek4[] growArray(carsLeeWeek4[] carsArray, Scanner console, String time)
{
System.out.print("How many new entries are needed for inventory system? "); // new entry needed?
while (!console.hasNextInt()) //data validation system will only accept int based numbers
{
System.out.println("Numerical entry only"); //data validation
System.out.print("Enter a number only: --> ");
console.next();
}
int entry2 = console.nextInt(); // entry for temp array
carsLeeWeek4[] temp = new carsLeeWeek4[carsArray.length + entry2];
for (int i = 0; i < carsArray.length; i++) // loop to grow original array with new size and all values stay the same except null for new lines
{
temp[i] = carsArray[i];
}
carsArray = temp;
carsArray = carsLeeWeek4.addInventory(console, time, entry2, carsArray);
return carsArray;
}
/* This method saves all inventory to String s */
public static String getInventory(carsLeeWeek4[] inventory, String time)
{
String s = ("Time this report was generated \n"+time);
s += ("\n*******************\n");
for (int i = 0; i< inventory.length; i++) //iteration of over new cars for printout display total inventory
{
s += (inventory[i]);
s += ("\n*******************\n");
}
return s;
}
/* This method is tostring */
public String toString()
{
String s = ("Dealer Acquisition Date: " +this.dateTime+ "\nStock Number: " +this.stockNum+ "\nYear: " +this.year+ "\nColor: " +this.color+ "\nMake: " +this.make+ "\nModel: " +this.model+ "\nMileage: " +this.mileage+ "\nSale Price: $" +this.salePrice+ "\nPurchase Price: $" +this.purchasePrice);
return s;
}
}
/* Driver Class */
class carsDriver
{
public static void main (String[] args)
throws FileNotFoundException {
{
String menu = "";
Scanner console = new Scanner(System.in); //Scanner object
GregorianCalendar calendar = new GregorianCalendar(); //calendar object
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);//formatter object
TimeZone timezone = TimeZone.getTimeZone("PST");//timezone object
formatter.setTimeZone(timezone);
String time = (formatter.format(calendar.getTime())); //Time creation call string object to be passed
carsLeeWeek4 inventory[] = carsLeeWeek4.Arr(console, time); //create a new array of objects named inventory for object cars
while(!menu.equalsIgnoreCase("q")) //menu program
{
System.out.println("\tEnter q/Q to quit");
//System.out.println("\tEnter 0 to create a new inventory"); //fix me need new code for dual reference calls new array
System.out.println("\tEnter 1 to add to inventory");
System.out.println("\tEnter 2 to print current inventory");
System.out.println("\tEnter 3 to save to text file");
menu = console.next();
/*if(menu.equals("0"))
{
carsLeeWeek4 OtherInventory[] = carsLeeWeek4.Arr(console, time); //pass scanner and time
continue;
}*/
if (menu.equals("1"))
{
inventory = carsLeeWeek4.growArray(inventory, console, time); //pass array of objects scanner and time
continue;
}
else if (menu.equals("2"))
{
System.out.println(carsLeeWeek4.getInventory(inventory, time)); //pass array of objects and time
System.out.println();
continue;
}
else if (menu.equals("3"))
{
PrintStream output = new PrintStream(new File("Inventory.txt")); // outputs file to where you have the .java file saved
output.println(carsLeeWeek4.getInventory(inventory, time)); // calls inventory from cars and sends the inventory and time for print out
System.out.println("File Saved in Current working Directory"); //display message
}
}
}
}
}