-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP_Final_Project.java
More file actions
2197 lines (1893 loc) · 86.1 KB
/
Copy pathOOP_Final_Project.java
File metadata and controls
2197 lines (1893 loc) · 86.1 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.Stage;
import javafx.collections.*;
import javafx.scene.control.cell.*;
interface Diagnosable {
public double calculateDamage();
public String generateReport();
}
class Engine{
private int capacity;
private int health;
public Engine(){
capacity = 1600;
health = 100;
}
public Engine(int capacity, int health){
this.capacity = capacity;
this.health = health;
}
public int getCapacity(){
return capacity;
}
public int getHealth(){
return health;
}
public void restore(){
health = 100;
}
public void reduce(int amount) {
health = Math.max(0, health - amount);
}
}
abstract class Vehicle{
protected String make;
protected String model;
protected double oilLevel;
protected double airPressure;
protected double tyres;
protected double suspension;
protected double brakes;
protected Boolean isBroken;
protected Engine engine;
public Vehicle(String make, String model, double oilLevel, double airPressure, double tyres, double suspension, int capacity, int health, double brakes){
this.make = make;
this.model = model;
this.oilLevel = oilLevel;
this.airPressure = airPressure;
this.tyres = tyres;
this.suspension = suspension;
this.brakes = brakes;
this.isBroken = false;
this.engine = new Engine(capacity, health);
}
// main abstract methods
public abstract void showHealth();
public abstract void displayDetails();
public abstract double CalculateScore();
public void checkStatus() {
if (engine.getHealth() <= 0) {
isBroken = true;
System.out.println("Vehicle is BROKEN");
} else if (engine.getHealth() < 30) {
System.out.println("WARNING: Engine Low");
} else {
System.out.println("Engine is Healthy");
}
}
public void serviceEngine() {
engine.restore();
isBroken = false;
}
public void resetStats() {
oilLevel = 10.0;
airPressure = 10.0;
tyres = 10.0;
suspension = 10.0;
brakes = 10.0;
isBroken = false;
engine.restore();
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getOilLevel() {
return oilLevel;
}
public void setOilLevel(double oilLevel) {
this.oilLevel = oilLevel;
}
public double getAirPressure() {
return airPressure;
}
public void setAirPressure(double airPressure) {
this.airPressure = airPressure;
}
public double getTyres() {
return tyres;
}
public void setTyres(double tyres) {
this.tyres = tyres;
}
public double getSuspension() {
return suspension;
}
public void setSuspension(double suspension) {
this.suspension = suspension;
}
public double getBrakes() {
return brakes;
}
public void setBrakes(double brakes) {
this.brakes = brakes;
}
public Engine getEngine() {
return engine;
}
public boolean isBroken() {
return isBroken;
}
}
class Car extends Vehicle implements Diagnosable{
private boolean HasAC;
Car(String make, String model, double brakes, double oilLevel, double airPressure, double tyres, double suspension, boolean HasAC, int capacity, int health){
super(make,model,oilLevel, airPressure,tyres, suspension, capacity, health, brakes);
this.HasAC = HasAC;
}
public boolean isHasAC() {
return HasAC;
}
public double calculateDamage(){
return (100-CalculateScore());
}
public String generateReport(){
return "--------Car Health Report--------\n"+
"Overall Score: " + CalculateScore()+ "\n"+
"Total Damage: " + calculateDamage()+ "\n"+
"Engine Health: "+ engine.getHealth()+ "\n"+
"Oil Level: " + oilLevel+ "\n"+
"Tyre Health: " + tyres+ "\n"+
"Brake Health: "+ brakes +"\n"+
"Suspension "+ suspension+ "\n"+
"Has Ac: " + HasAC;
}
public void displayDetails(){
System.out.println("============================");
System.out.println("CAR INFORMATION");
System.out.println("============================");
System.out.println("Make: "+make);
System.out.println("Model: "+model);
System.out.println("Engine Power: "+ engine.getCapacity()+"cc");
System.out.println("Air Conditioner Variant: "+ HasAC);
System.out.println("---------------------------");
showHealth();
}
public void showHealth(){
System.out.println("CAR HEALTH ANALYTICS");
System.out.println("------------------------");
System.out.println("Oil Level: "+oilLevel);
System.out.println("Air Pressure: "+airPressure);
System.out.println("Tyre Health: "+ tyres);
System.out.println("Suspension Health: "+suspension);
}
public double CalculateScore() {
return (engine.getHealth() / 100.0 * 50) + // Max 50
(oilLevel / 10.0 * 20) + // Max 20
(tyres / 10.0 * 10) + // Max 10
(suspension / 10.0 * 10) + // Max 10
(airPressure / 10.0 * 5)+ // Max 5
(brakes/10 *5); //Max 5
// Total Max = 100
}
}
class Truck extends Vehicle implements Diagnosable{
private double loadCapacity;
Truck(String make, String model, double brakes, double oilLevel, double airPressure, double tyres, double suspension, int capacity, int health, double loadCapacity){
super(make,model,oilLevel, airPressure,tyres, suspension, capacity, health, brakes);
this.loadCapacity = loadCapacity;
}
public double getLoadCapacity() {
return loadCapacity;
}
public double calculateDamage(){
return (100-CalculateScore());
}
public String generateReport(){
return "--------Truck Health Report--------\n"+
"Overall Score: " + CalculateScore()+ "\n"+
"Total Damage: " + calculateDamage()+ "\n"+
"Engine Health: "+ engine.getHealth()+ "\n"+
"Oil Level: " + oilLevel+ "\n"+
"Tyre Health: " + tyres+ "\n"+
"Brake Health: "+ brakes +"\n"+
"Suspension "+ suspension+ "\n"+
"Load Capacity: " + loadCapacity;
}
public void displayDetails(){
System.out.println("============================");
System.out.println("TRUCK INFORMATION");
System.out.println("============================");
System.out.println("Make: "+make);
System.out.println("Model: "+model);
System.out.println("Engine Power: "+ engine.getCapacity()+"cc");
System.out.println("Load Capacity in Kg: "+ loadCapacity);
System.out.println("---------------------------");
showHealth();
}
public void showHealth(){
System.out.println("TRUCK HEALTH ANALYTICS");
System.out.println("------------------------");
System.out.println("Oil Level: "+oilLevel);
System.out.println("Air Pressure: "+airPressure);
System.out.println("Tyre Health: "+ tyres);
System.out.println("Suspension Health: "+suspension);
}
public double CalculateScore() {
return (engine.getHealth() / 100.0 * 50) + // Max 50
(oilLevel / 10.0 * 20) + // Max 20
(tyres / 10.0 * 10) + // Max 10
(suspension / 10.0 * 10) + // Max 10
(airPressure / 10.0 * 5)+ // Max 5
(brakes/10 *5); //Max 5
// Total Max = 100
}
}
class Motorcycle extends Vehicle implements Diagnosable{
private double chainSprocketHealth;
Motorcycle(String make, String model,double brakes, double oilLevel, double airPressure, double tyres, double suspension, double chainSprocketHealth, int engineCapacity, int EngineHealth){
super(make, model, oilLevel, airPressure, tyres, suspension, engineCapacity, EngineHealth, brakes);
this.chainSprocketHealth= chainSprocketHealth;
}
public double getChainSprocketHealth() {
return chainSprocketHealth;
}
public void setChainSprocketHealth(double chainSprocketHealth) {
this.chainSprocketHealth = chainSprocketHealth;
}
public double calculateDamage(){
return (100-CalculateScore());
}
public String generateReport(){
return "--------Car Health Report--------\n"+
"Overall Score: " + CalculateScore()+ "\n"+
"Engine Health: "+ engine.getHealth()+"\n"+
"Oil Level: " + oilLevel+"\n"+
"Tyre Health: " + tyres+"\n"+
"Brake Health: "+ brakes +"\n"+
"Suspension "+ suspension+"\n"+
"Chain Sprocket Health: " + chainSprocketHealth;
}
public void displayDetails(){
System.out.println("============================");
System.out.println("Motorcycle Information System");
System.out.println("============================");
System.out.println("Make: "+make);
System.out.println("Model: "+model);
System.out.println("Engine Power: "+ engine.getCapacity()+"cc");
System.out.println("---------------------------");
showHealth();
}
public void showHealth(){
System.out.println("Motorcycle HEALTH ANALYTICS");
System.out.println("------------------------");
System.out.println("Oil Level: "+oilLevel);
System.out.println("Air Pressure: "+airPressure);
System.out.println("Tyre Health: "+ tyres);
System.out.println("Suspension Health: "+suspension);
System.out.println("Chain-Sprocket Health: "+ chainSprocketHealth);
}
public double CalculateScore() {
return (engine.getHealth() / 100.0 * 40) + // Max 40 pointa for Engine
(oilLevel / 10.0 * 15) + // Max 15
(tyres / 10.0 * 15) + // Max 15
(suspension / 10.0 * 10) + // Max 10
(airPressure / 10.0 * 5) + // Max 5
(chainSprocketHealth / 10.0 * 10)+ // Max 10
(brakes /10 * 5); //Max 5
// Total Max = 100
}
@Override
public void resetStats() {
super.resetStats();
chainSprocketHealth = 10.0;
}
}
class DriverBehavior{
private boolean hardBraking;
private boolean overspeeding;
private boolean aggressiveDriving;
private boolean longDrivingHours;
DriverBehavior(boolean hardBraking, boolean overspeeding, boolean aggressiveDriving, boolean longDrivingHours){
this.hardBraking = hardBraking;
this.overspeeding = overspeeding;
this.aggressiveDriving = aggressiveDriving;
this.longDrivingHours = longDrivingHours;
}
public boolean isHardBraking() {
return hardBraking;
}
public boolean isOverspeeding() {
return overspeeding;
}
public boolean isAggressiveDriving() {
return aggressiveDriving;
}
public boolean isLongDrivingHours() {
return longDrivingHours;
}
}
class Trip{
private double distance;
private String terrain;
private double load;
private Vehicle vehicle;
private DriverBehavior behavior;
public Trip(double distance, String terrain, double load, Vehicle vehicle, DriverBehavior behavior) {
this.distance = distance;
this.terrain = terrain;
this.load = load;
this.vehicle = vehicle;
this.behavior = behavior;
}
public void showDetails(){
System.out.println("=========================");
System.out.println(" TRIP SUMMARY");
System.out.println("=========================");
System.out.println("Total Distance: "+distance);
System.out.println("Terrain: "+terrain);
System.out.println("Vehicle Used: "+ vehicle.getModel());
if (behavior.isAggressiveDriving()){
System.out.println("Aggressive Driving Detected");
}
if (behavior.isHardBraking()){
System.out.println("Hard Braking Detected");
}
if (behavior.isLongDrivingHours()){
System.out.println("Long Driving Hours Detected");
}
if (behavior.isOverspeeding()){
System.out.println("Overspeeding Detected");
}
System.out.println();
}
public Vehicle getVehicle() {
return vehicle;
}
public double getDistance() {
return distance;
}
public String getTerrain() {
return terrain;
}
public double getLoad() {
return load;
}
public DriverBehavior getDriverBehavior(){
return behavior;
}
}
class User{
private String username;
private String password;
User(String username, String password){
this.username = username;
this.password = password;
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
}
class VehicleManager{
private ArrayList<Vehicle> vehicles;
public VehicleManager(){
vehicles = new ArrayList<Vehicle>();
}
public void addVehicle(Vehicle vehicleObj){
vehicles.add(vehicleObj);
}
public void removeVehicle(int index){
try{
vehicles.remove(index);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Invalid Array Index! Please Enter a Valid Index");
}
}
public ArrayList<Vehicle> getVehicles() {
return vehicles;
}
public void displayAll(){
for (Vehicle vehicle : vehicles) {
vehicle.displayDetails();
}
}
}
class LoginManager{
private ArrayList<User> users;
private User currentUser;
LoginManager() {
users = new ArrayList<>();
currentUser = null;
}
public boolean register(String username, String password){
for(User u : users){
if(u.getUsername().equals(username)){
return false;
}
}
users.add(new User(username, password));
return true;
}
public boolean login(String username, String password){
for(User u : users){
if (u.getUsername().equals(username) && u.getPassword().equals(password)) {
currentUser = u;
return true;
}
}
return false;
}
public void logout(){
currentUser = null;
}
public User getCurrentUser(){
return currentUser;
}
public ArrayList<User> getUsers(){
return users;
}
public void setUsers(ArrayList<User> users) {
this.users = users;
}
}
class AdviceManager{
public String getScoreAdvice(double score){
if (score >= 90){
return "Score: " + score + "/100\n✓ EXCELLENT — Vehicle is in peak condition. Safe for any trip.";
}
else if (score >= 80){
return "Score: " + score + "/100\n✓ GOOD — Minor checks recommended. Verify oil and tyre pressure before departure.";
}
else if (score >= 70){
return "Score: " + score + "/100\n⚠ MODERATE — Professional inspection recommended before this trip.";
}
else if (score >= 60){
return "Score: " + score + "/100\n⚠ CONCERNING — Avoid long trips. Service the vehicle soon.";
}
else
return "Score: " + score + "/100\n🔴 DANGEROUS — Do NOT take this trip. Immediate workshop visit required.";
}
public String getBehaviorWarnings(DriverBehavior behavior){
StringBuilder sb = new StringBuilder();
if (behavior.isHardBraking()){
sb.append("⚠ Hard braking: accelerates brake pad and tyre wear. Will reduce brake health over time.\n");
}
if (behavior.isOverspeeding()){
sb.append("⚠ Overspeeding: increases engine strain, fuel consumption, and accident risk on this terrain.\n");
}
if (behavior.isAggressiveDriving()){
sb.append("⚠ Aggressive driving: damages suspension and drivetrain. Reduce on hilly terrain.\n");
}
if (behavior.isLongDrivingHours()){
sb.append("⚠ Long driving hours: vehicle needs rest breaks. Check oil and temperature every 2 hours.\n");
}
if (sb.length() == 0){
sb.append("✓ No driver behavior concerns detected.");
}
return sb.toString();
}
public String getFullAdvice(Trip trip){
double score = trip.getVehicle().CalculateScore();
String vehicleAdvice = getScoreAdvice(score);
String behaviorAdvice = getBehaviorWarnings(trip.getDriverBehavior());
return vehicleAdvice + "\n\n" + behaviorAdvice;
}
public String getSummaryReport(Trip trip){
String dateTime = LocalDateTime.now().toString();
String advice = getFullAdvice(trip);
StringBuilder sb = new StringBuilder();
sb.append("\n===== VEHICLE TRIP REPORT =====\n\n");
sb.append("Generated: "+ dateTime + "\n\n");
sb.append("Vehicle: " + trip.getVehicle().getMake() + " " + trip.getVehicle().getModel() + "\n");
sb.append("Distance: " + trip.getDistance() + "\n");
sb.append("Terrain: " + trip.getTerrain() + "\n");
sb.append("Load: " + trip.getLoad() + "\n\n");
sb.append("===== ADVICE =====\n" + advice);
return sb.toString();
}
}
class WearManager {
// Call this after a trip is confirmed complete
public void applyWear(Trip trip) {
Vehicle v = trip.getVehicle();
String terrain = trip.getTerrain();
double distance = trip.getDistance();
double load = trip.getLoad();
DriverBehavior b = trip.getDriverBehavior();
// Base wear per 100km (small amounts)
double baseTyre = (distance / 100) * 0.3;
double baseSusp = (distance / 100) * 0.2;
double baseBrake = (distance / 100) * 0.2;
double baseOil = (distance / 100) * 0.3;
double baseEngine = (distance / 100) * 2; // engine is 0-100 scale
// Terrain multipliers
double tyreMult = 1.0, suspMult = 1.0, brakeMult = 1.0,
oilMult = 1.0, engineMult = 1.0;
if (terrain.equals("City")) {
brakeMult = 1.8; // lots of stopping
tyreMult = 1.3;
} else if (terrain.equals("Highway")) {
engineMult = 1.5; // high RPM sustained
oilMult = 1.4;
brakeMult = 0.6; // barely braking
} else if (terrain.equals("Hilly")) {
suspMult = 2.0; // up and down stress
brakeMult = 1.8; // downhill braking
engineMult = 1.5; // climbing effort
} else if (terrain.equals("Off-road")) {
tyreMult = 2.5; // rough surface
suspMult = 2.5;
brakeMult = 1.2;
} else if (terrain.equals("Mixed")) {
tyreMult = 1.4;
suspMult = 1.4;
brakeMult = 1.3;
}
// Driver behavior additions
if (b.isHardBraking()) {
brakeMult += 0.8;
tyreMult += 0.5;
}
if (b.isOverspeeding()) {
engineMult += 0.8;
oilMult += 0.5;
}
if (b.isAggressiveDriving()) {
suspMult += 0.7;
tyreMult += 0.4;
}
if (b.isLongDrivingHours()) {
oilMult += 0.8;
engineMult += 0.4;
}
// Load penalty (mainly for trucks, works for all)
if (load > 200) {
suspMult += 0.5;
tyreMult += 0.5;
}
// Calculate final wear amounts
double tyreDrop = baseTyre * tyreMult;
double suspDrop = baseSusp * suspMult;
double brakeDrop = baseBrake * brakeMult;
double oilDrop = baseOil * oilMult;
double engineDrop = baseEngine * engineMult;
// Apply wear — clamp to 0 minimum
v.setTyres( Math.max(0, v.getTyres() - tyreDrop));
v.setSuspension(Math.max(0, v.getSuspension() - suspDrop));
v.setBrakes( Math.max(0, v.getBrakes() - brakeDrop));
v.setOilLevel( Math.max(0, v.getOilLevel() - oilDrop));
// Engine needs its own reduce method (see Step 2)
reduceEngineHealth(v, (int) Math.round(engineDrop));
// Motorcycle-specific: chain sprocket wears on hilly/off-road
if (v instanceof Motorcycle) {
Motorcycle m = (Motorcycle) v;
double chainDrop = baseTyre * suspMult * 0.6;
m.setChainSprocketHealth(Math.max(0, m.getChainSprocketHealth() - chainDrop));
}
// Check if vehicle is now broken
v.checkStatus();
}
// Helper — reduces engine health by given amount
private void reduceEngineHealth(Vehicle v, int amount) {
v.getEngine().reduce(amount);
}
// Returns a summary of what was reduced (show this to user after trip)
public String getWearSummary(Vehicle v, double oldTyre, double oldSusp,
double oldBrake, double oldOil, int oldEngine) {
StringBuilder sb = new StringBuilder();
sb.append("=== WEAR APPLIED AFTER TRIP ===\n\n");
sb.append(String.format("Engine Health : %.0f → %.0f\n", (double)oldEngine, (double)v.getEngine().getHealth()));
sb.append(String.format("Oil Level : %.1f → %.1f\n", oldOil, v.getOilLevel()));
sb.append(String.format("Tyre Health : %.1f → %.1f\n", oldTyre, v.getTyres()));
sb.append(String.format("Suspension : %.1f → %.1f\n", oldSusp, v.getSuspension()));
sb.append(String.format("Brake Health : %.1f → %.1f\n", oldBrake, v.getBrakes()));
if (v instanceof Motorcycle) {
sb.append("Chain-Sprocket also reduced.\n");
}
sb.append("\nNew Overall Score: " + String.format("%.1f", v.CalculateScore()) + "/100");
return sb.toString();
}
}
class FileManager {
private static final String USERS_FILE = "users.txt";
private static final String VEHICLES_FILE = "vehicles.txt";
// ==================== USERS ====================
public void saveUsers(ArrayList<User> users) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(USERS_FILE))) {
for (User u : users) {
bw.write(u.getUsername() + "," + u.getPassword());
bw.newLine();
}
} catch (IOException e) {
System.out.println("Error saving users: " + e.getMessage());
}
}
public ArrayList<User> loadUsers() {
ArrayList<User> users = new ArrayList<>();
File f = new File(USERS_FILE);
if (!f.exists()) return users;
try (Scanner sc = new Scanner(f)) {
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.isEmpty()) continue;
String[] parts = line.split(",");
if (parts.length == 2) {
users.add(new User(parts[0], parts[1]));
}
}
} catch (IOException e) {
System.out.println("Error loading users: " + e.getMessage());
}
return users;
}
// ==================== VEHICLES ====================
// Format for CAR:
// CAR,make,model,oilLevel,airPressure,tyres,suspension,brakes,engineCapacity,engineHealth,hasAC
// Format for MOTORCYCLE:
// MOTORCYCLE,make,model,oilLevel,airPressure,tyres,suspension,brakes,engineCapacity,engineHealth,chainSprocketHealth
// Format for TRUCK:
// TRUCK,make,model,oilLevel,airPressure,tyres,suspension,brakes,engineCapacity,engineHealth,loadCapacity
public void saveVehicles(ArrayList<Vehicle> vehicles) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(VEHICLES_FILE))) {
for (Vehicle v : vehicles) {
String line = "";
if (v instanceof Car) {
Car c = (Car) v;
line = "CAR," + v.getMake() + "," + v.getModel() + ","
+ v.getOilLevel() + "," + v.getAirPressure() + ","
+ v.getTyres() + "," + v.getSuspension() + ","
+ v.getBrakes() + ","
+ v.getEngine().getCapacity() + "," + v.getEngine().getHealth() + ","
+ c.isHasAC();
} else if (v instanceof Motorcycle) {
Motorcycle m = (Motorcycle) v;
line = "MOTORCYCLE," + v.getMake() + "," + v.getModel() + ","
+ v.getOilLevel() + "," + v.getAirPressure() + ","
+ v.getTyres() + "," + v.getSuspension() + ","
+ v.getBrakes() + ","
+ v.getEngine().getCapacity() + "," + v.getEngine().getHealth() + ","
+ m.getChainSprocketHealth();
} else if (v instanceof Truck) {
Truck t = (Truck) v;
line = "TRUCK," + v.getMake() + "," + v.getModel() + ","
+ v.getOilLevel() + "," + v.getAirPressure() + ","
+ v.getTyres() + "," + v.getSuspension() + ","
+ v.getBrakes() + ","
+ v.getEngine().getCapacity() + "," + v.getEngine().getHealth() + ","
+ t.getLoadCapacity();
}
if (!line.isEmpty()) {
bw.write(line);
bw.newLine();
}
}
} catch (IOException e) {
System.out.println("Error saving vehicles: " + e.getMessage());
}
}
public ArrayList<Vehicle> loadVehicles() {
ArrayList<Vehicle> vehicles = new ArrayList<>();
File f = new File(VEHICLES_FILE);
if (!f.exists()) return vehicles;
try (Scanner sc = new Scanner(f)) {
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.isEmpty()) continue;
String[] p = line.split(",");
// p[0]=type, p[1]=make, p[2]=model,
// p[3]=oil, p[4]=air, p[5]=tyres, p[6]=suspension, p[7]=brakes,
// p[8]=engineCap, p[9]=engineHealth, p[10]=extraField
String type = p[0];
if (type.equals("CAR") && p.length >= 11) {
vehicles.add(new Car(
p[1], p[2],
Double.parseDouble(p[7]), // brakes
Double.parseDouble(p[3]), // oil
Double.parseDouble(p[4]), // air
Double.parseDouble(p[5]), // tyres
Double.parseDouble(p[6]), // suspension
Boolean.parseBoolean(p[10]),// hasAC
Integer.parseInt(p[8]), // engineCapacity
Integer.parseInt(p[9]) // engineHealth
));
} else if (type.equals("MOTORCYCLE") && p.length >= 11) {
vehicles.add(new Motorcycle(
p[1], p[2],
Double.parseDouble(p[7]), // brakes
Double.parseDouble(p[3]), // oil
Double.parseDouble(p[4]), // air
Double.parseDouble(p[5]), // tyres
Double.parseDouble(p[6]), // suspension
Double.parseDouble(p[10]), // chainSprocketHealth
Integer.parseInt(p[8]), // engineCapacity
Integer.parseInt(p[9]) // engineHealth
));
} else if (type.equals("TRUCK") && p.length >= 11) {
vehicles.add(new Truck(
p[1], p[2],
Double.parseDouble(p[7]), // brakes
Double.parseDouble(p[3]), // oil
Double.parseDouble(p[4]), // air
Double.parseDouble(p[5]), // tyres
Double.parseDouble(p[6]), // suspension
Integer.parseInt(p[8]), // engineCapacity
Integer.parseInt(p[9]), // engineHealth
Double.parseDouble(p[10]) // loadCapacity
));
}
}
} catch (IOException e) {
System.out.println("Error loading vehicles: " + e.getMessage());
}
return vehicles;
}
// ==================== REPORT ====================
public void saveReport(String content, String filename) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
bw.write(content);
} catch (IOException e) {
System.out.println("Error saving report: " + e.getMessage());
}
}
}
class Theme {
static final String BASE = "#1a1f2e";
static final String SURFACE = "#242938";
static final String CARD = "#2d3348";
static final String NAV = "#1e2436";
static final String BORDER = "#3a4060";
static final String TEXT = "#e2e8f0";
static final String MUTED = "#8892a4";
static final String ACCENT = "#6c8ef7";
static final String GREEN = "#2d6a4f";
static final String GREEN_TEXT = "#95d5b2";
static final String RED = "#6b1f1f";
static final String RED_TEXT = "#ffb3b3";
static final String PURPLE = "#3d2d6e";
static final String PURPLE_TEXT= "#c4b5fd";
static final String SCORE_GREEN_BG = "#1a3d2b";
static final String SCORE_GREEN_FG = "#09c457";
static final String SCORE_AMBER_BG = "#3d2e0a";
static final String SCORE_AMBER_FG = "#ffcf32";
static final String SCORE_RED_BG = "#3d1515";
static final String SCORE_RED_FG = "#f35151";
static final String NAV_STYLE =
"-fx-background-color: " + NAV + ";" +
"-fx-padding: 10 16 10 16;" +
"-fx-border-color: " + BORDER + ";" +
"-fx-border-width: 0 0 1 0;";
static final String INPUT_STYLE =
"-fx-background-color: " + CARD + ";" +
"-fx-border-color: " + BORDER + ";" +
"-fx-border-radius: 6;" +
"-fx-background-radius: 6;" +
"-fx-text-fill: " + TEXT + ";" +
"-fx-font-size: 13;" +
"-fx-prompt-text-fill: " + MUTED + ";";
static final String LABEL_STYLE =
"-fx-text-fill: " + TEXT + ";" +
"-fx-font-size: 13;";
static String btn(String bg, String fg) {
return "-fx-background-color: " + bg + ";" +
"-fx-text-fill: " + fg + ";" +
"-fx-background-radius: 6;" +
"-fx-border-radius: 6;" +
"-fx-cursor: hand;" +
"-fx-padding: 7 18 7 18;" +
"-fx-font-size: 13;";
}
static String ghostBtn() {
return "-fx-background-color: " + CARD + ";" +
"-fx-text-fill: " + MUTED + ";" +
"-fx-border-color: " + BORDER + ";" +
"-fx-border-width: 1;" +
"-fx-border-radius: 6;" +
"-fx-background-radius: 6;" +
"-fx-cursor: hand;" +
"-fx-padding: 7 18 7 18;" +
"-fx-font-size: 13;";
}
static String sectionLabel() {
return "-fx-font-size: 11;" +
"-fx-text-fill: " + ACCENT + ";" +
"-fx-font-weight: bold;";
}
}
class SceneManager {
private Stage stage;
private LoginManager loginManager;
private VehicleManager vehicleManager;
private FileManager fileManager;
private AdviceManager adviceManager;
SceneManager(Stage stage, LoginManager loginManager,
VehicleManager vehicleManager, FileManager fileManager,
AdviceManager adviceManager) {
this.stage = stage;
this.loginManager = loginManager;
this.vehicleManager= vehicleManager;
this.fileManager = fileManager;
this.adviceManager = adviceManager;
}
public void showSplash() {
SplashScreen splash = new SplashScreen(this);
stage.setScene(new Scene(splash, 780, 540));
}
public void showLogin() {
LoginPanel panel = new LoginPanel(this, loginManager, fileManager);
stage.setScene(new Scene(panel, 780, 540));
}
public void showRegister() {
RegisterPanel panel = new RegisterPanel(this, loginManager, fileManager);
stage.setScene(new Scene(panel, 780, 540));
}
public void showDashboard() {
DashboardPanel panel = new DashboardPanel(this, vehicleManager, loginManager, fileManager);
stage.setScene(new Scene(panel, 780, 540));
}
public void showAddVehicle() {
AddVehiclePanel panel = new AddVehiclePanel(this, vehicleManager, fileManager);
stage.setScene(new Scene(panel, 780, 540));
}
public void showVehicleDetails(Vehicle v) {
VehicleDetailsPanel panel = new VehicleDetailsPanel(this, fileManager, vehicleManager);
panel.loadVehicle(v);
stage.setScene(new Scene(panel, 780, 540));
}
public void showTrip() {
TripPanel panel = new TripPanel(this, vehicleManager, adviceManager);
stage.setScene(new Scene(panel, 780, 540));
}
public void showAdvice(Trip trip) {