1
+ import java .io .IOException ;
1
2
import java .util .HashMap ;
2
3
import java .util .HashSet ;
3
4
import java .util .Iterator ;
4
5
import java .util .Scanner ;
5
6
6
- /*TODO
7
- * Dhmiourgia metavlhtwn max gia swsth diaxeirish tou pinaka
8
- * del kai search: dhmiourgia if gia elegxo an den uparxei kai emfanish katallhlwn mhnumatwn
9
- */
10
7
public class ex01 {
11
8
private static final HashMap <String ,car > cars = new HashMap <>();
12
- public static void main (String [] args ) {
13
- cars .put ("123" ,new car ("toyota" ,"Celica" ,255 ,2000 ,15000 ,"White" ));
14
- cars .put ("555" ,new car ("Fiat" ,"Cinquecento" ,39 ,900 ,700 ,"Black" ));
15
- cars .put ("753" ,new car ("FIAT" ,"Punto" ,45 ,1100 ,500 ,"red" ));
16
- cars .put ("666" ,new car ("Subaru" ,"Impreza WRX STI 1999" ,300 ,2500 ,50000 ,"Blue" ));
17
- cars .put ("359" ,new car ("Toyota" ,"Starlet" ,43 ,1000 ,5000 ,"Gray" ));
9
+ public static void main (String [] args ) throws IOException {
10
+ Scanner in = new Scanner (System .in );
11
+ int option =12 ;
12
+ do {
13
+ System .out .print ("******************************\n *Σύστημα Διαχείρισης CarDealer*\n ******************************\n 0->Εμφάνιση όλων των αυτοκινήτων\n 1->Προσθήκη αυτοκινήτου\n 2->Διαγραφή αυτοκινήτου\n 3->Αναζήτηση αυτοκινήτου\n 4->Εμφάνιση κατασκευαστών\n 5->Εμφάνιση αυτοκινήτου με τη μεγαλύτερη ιπποδύναμη\n 6->Εμφάνιση αυτοκινήτου με τη μικρότερη ιπποδύναμη\n 7->Εμφάνιση αυτοκινήτου με τη μεγαλύτερη ιπποδύναμη συγκεκριμένου κατασκευαστή\n 8->Εμφάνιση αυτοκινήτου με τη μικρότερη ιπποδύναμη συγκεκριμένου κατασκευαστή\n 9->Πλήθος αυτοκινήτων συγκεκριμένου κατασκευαστή\n 10->Εμφάνιση αυτοκινήτων φθηνότερα από συγκεκριμένη τιμή\n 11->Εμφάνιση αυτοκινήτων ακριβότερα από συγκεκριμένη τιμή\n 12->Τερματισμός Λειτουργίας\n Εισάγετε τον αριθμό της επιλογής σας και πατήστε Enter: " );
14
+ option = in .nextInt ();
15
+ if (option !=1 && option !=12 && cars .isEmpty ()) System .out .println ("Δεν υπάρχουν αυτοκίνητα" );
16
+ else if (option ==0 ) displayAll ();
17
+ else if (option ==1 ) add ();
18
+ else if (option ==2 ) del ();
19
+ else if (option ==3 ) search ();
20
+ else if (option ==4 ) brands ();
21
+ else if (option ==5 ) power (1 ,0 );
22
+ else if (option ==6 ) power (0 ,0 );
23
+ else if (option ==7 ) power (1 ,1 );
24
+ else if (option ==8 ) power (0 ,1 );
25
+ else if (option ==9 ) System .out .println ("Αριθμός αυτοκινήτων: " +carsOf ());
26
+ else if (option ==10 ) displayByPrice (0 );
27
+ else if (option ==11 ) displayByPrice (1 );
28
+ if (option !=12 ) { System .out .print ("Πατήστε Enter για να συνεχίσετε..." ); System .in .read (); }
29
+ }while (option !=12 );
18
30
}
19
31
private static void add (){
20
32
Scanner in = new Scanner (System .in );
21
- System .out .println ("Εισάγετε εταιρεία κατασκευής: " );
22
- String brand = in .next ();
23
- System .out .println ("Εισάγετε μοντέλο: " );
24
- String model = in .next ();
25
- System .out .println ("Εισάγετε ιπποδύναμη σε άλογα (hp): " );
33
+ System .out .print ("Εισάγετε εταιρεία κατασκευής: " );
34
+ String brand = in .nextLine ();
35
+ System .out .print ("Εισάγετε μοντέλο: " );
36
+ String model = in .nextLine ();
37
+ System .out .print ("Εισάγετε ιπποδύναμη σε άλογα (hp): " );
26
38
int hp = in .nextInt ();
27
- System .out .println ("Εισάγετε χωρητκότητα σε κυβικά εκατοστά(cc): " );
39
+ System .out .print ("Εισάγετε χωρητκότητα σε κυβικά εκατοστά(cc): " );
28
40
int cc = in .nextInt ();
29
- System .out .println ("Εισάγετε κόστος σε ευρώ(€): " );
41
+ System .out .print ("Εισάγετε κόστος σε ευρώ(€): " );
30
42
int price = in .nextInt ();
31
- System .out .println ("Εισάγετε χρώμα: " );
32
- String color = in .next ();
43
+ System .out .print ("Εισάγετε χρώμα: " );
44
+ in .nextLine ();
45
+ String color = in .nextLine ();
33
46
String randomCode ;
34
47
do {
35
48
int r = (int )(Math .random ()*1000 );
36
49
randomCode = String .valueOf (r /100 %10 ) + String .valueOf (r /10 %10 ) + String .valueOf (r %10 );
37
50
} while (cars .get (randomCode ) != null );
38
51
cars .put (randomCode ,new car (brand ,model ,hp ,cc ,price ,color ));
39
- }
52
+ System .out .println ("Προστέθηκε το αυτοκίνητο με τα παρακάτω στοιχεία" );
53
+ tabTitle ();
54
+ System .out .println (randomCode + ' ' + cars .get (randomCode ).toString ());
55
+ }
40
56
private static void del (){
41
57
Scanner in = new Scanner (System .in );
42
- System .out .println ("Εισάγετε αριθμό προς διαγραφή: " );
43
- String tmp = in .next ();
44
- cars .remove (tmp );
45
- System .out .println ("Το αυτοκίνητο με αριθμό " +tmp +" διαγράφηκε επιτυχώς" );
58
+ System .out .print ("Εισάγετε αριθμό προς διαγραφή: " );
59
+ String tmp = in .nextLine ();
60
+ if (cars .remove (tmp )!=null )
61
+ System .out .println ("Το αυτοκίνητο με ID " +tmp +" διαγράφηκε επιτυχώς" );
62
+ else System .out .println ("Δεν υπάρχει αυτοκίνητο με ID " +tmp );
46
63
}
47
64
private static void search (){
48
65
Scanner in = new Scanner (System .in );
49
- System .out .println ("Εισάγετε αριθμό προς αναζήτηση: " );
50
- String tmp = in .next ();
51
- System .out .println (cars .get (tmp ).toString ());
66
+ System .out .print ("Εισάγετε αριθμό προς αναζήτηση: " );
67
+ String tmp = in .nextLine ();
68
+ if (cars .get (tmp )!=null ){
69
+ tabTitle ();
70
+ System .out .println (tmp + ' ' + cars .get (tmp ).toString ());
71
+ }
72
+ else System .out .println ("Δεν υπάρχει αυτοκίνητο με ID " +tmp );
52
73
}
53
74
private static void brands (){
54
75
HashSet <String > tmp = new HashSet <> ();
55
- for (Iterator <String > it = cars .keySet ().iterator (); it .hasNext ();) {
76
+ for (Iterator <String > it = cars .keySet ().iterator (); it .hasNext ();)
56
77
tmp .add (cars .get (it .next ()).getBrand ());
57
- }
58
- for (String str : tmp ){
78
+ for (String str : tmp )
59
79
System .out .println (str );
60
- }
61
80
}
62
- private static void power (String maxORmin , String brand ){
81
+ private static void power (int maxORmin , int byBrand ){
63
82
int max =0 ,min =9999 ;
64
83
String maxID ="" , minID ="" ;
65
- brand =(brand !=null )?toTitleCase (brand ):null ;
84
+ String brand =" " ;
85
+ if (byBrand ==1 ){
86
+ Scanner in = new Scanner (System .in );
87
+ System .out .print ("Εισάγεται κατασκευαστή: " );
88
+ brand = toTitleCase (in .nextLine ());
89
+ }
66
90
for (Iterator <String > it = cars .keySet ().iterator (); it .hasNext ();) {
67
91
String tmp = it .next ();
68
- if ( cars .get (tmp ).getBrand ().equals (brand ) || brand == null ){
69
- max = Math .max (cars .get (tmp ).getHP (), max );
70
- maxID =( max ==cars .get (tmp ).getHP ())? tmp : maxID ;
71
- min = Math .min (cars .get (tmp ).getHP (), min );
72
- minID =(min == cars .get (tmp ).getHP ())? tmp : minID ;
92
+ if ( cars .get (tmp ).getBrand ().equals (brand ) || byBrand == 0 ){
93
+ max = Math .max ( cars .get (tmp ).getHP () , max );
94
+ maxID = ( max ==cars .get (tmp ).getHP () ) ? tmp : maxID ;
95
+ min = Math .min ( cars .get (tmp ).getHP () , min );
96
+ minID =( min == cars .get (tmp ).getHP () ) ? tmp : minID ;
73
97
}
74
98
}
75
- if (maxORmin .equals ("max" ))
76
- System .out .println ("To autokinhto me th megaluterh ippodunamh einai:\n " +maxID +" " +cars .get (maxID ).toString ());
77
- else if (maxORmin .equals ("min" ))
78
- System .out .println ("To autokinhto me th mikroterh ippodunamh einai:\n " +minID +" " +cars .get (minID ).toString ());
79
- else System .out .println ("Lathos parametros, eisagete min h max gia mikroterh h megaluterh ippodunamh antistoixa" );
99
+ System .out .println ("Το αυτοκίνητο με τη " +((maxORmin ==1 )?"μεγαλύτερη" :"μικρότερη" )+" ιπποδύναμη είναι:" );
100
+ tabTitle ();
101
+ System .out .println (((maxORmin ==1 )?maxID :minID )+' ' +cars .get (((maxORmin ==1 )?maxID :minID )).toString ());
80
102
}
81
- public static void power (String maxORmin ){
82
- power (maxORmin ,null );
83
- }
84
- public static int brandCars (String brand ){
85
- brand =toTitleCase (brand );
103
+ public static int carsOf (){
104
+ Scanner in = new Scanner (System .in );
105
+ System .out .print ("Εισάγεται κατασκευαστή και πατήστε Enter: " );
106
+ String brand = toTitleCase (in .nextLine ());
86
107
int counter =0 ;
87
- for (Iterator <String > it = cars .keySet ().iterator (); it .hasNext ();) {
108
+ for (Iterator <String > it = cars .keySet ().iterator (); it .hasNext ();)
88
109
if (cars .get (it .next ()).getBrand ().equals (brand ))counter ++;
89
- }
90
110
return counter ;
91
111
}
92
- private static void displayByPrice (String maxORmin , int price ){
93
- System .out .format ("%3s %-20s %-20s %-4s %-5s %-7s %-18s\n " , "ID" , "Brand" , "Model" , "HP" ,"CC" ,"Price" ,"Color" );
112
+ private static void displayByPrice (int maxORmin ){
113
+ Scanner in = new Scanner (System .in );
114
+ System .out .print ("Εισάγετε τιμή και πατήστε Enter: " );
115
+ int price = in .nextInt ();
116
+ tabTitle ();
94
117
for (Iterator <String > it = cars .keySet ().iterator (); it .hasNext ();) {
95
118
String cur =it .next ();
96
- Boolean lol = (maxORmin .equals ("max" )?price >cars .get (cur ).getPrice ():price <cars .get (cur ).getPrice ());
97
- if ((maxORmin .equals ("max" )?price >cars .get (cur ).getPrice ():price <cars .get (cur ).getPrice ()))
119
+ if ( maxORmin ==1 ? cars .get (cur ).getPrice () > price : cars .get (cur ).getPrice () < price )
98
120
System .out .println (cur + ' ' + cars .get (cur ).toString ());
99
121
}
100
122
}
101
123
private static void displayAll (){
102
- System . out . format ( "%3s %-20s %-20s %-4s %-5s %-7s %-18s \n " , "ID" , "Brand" , "Model" , "HP" , "CC" , "Price" , "Color" );
124
+ tabTitle ( );
103
125
for (Iterator <String > it = cars .keySet ().iterator (); it .hasNext ();) {
104
126
String cur =it .next ();
105
127
System .out .println (cur + ' ' + cars .get (cur ).toString ());
@@ -108,4 +130,7 @@ private static void displayAll(){
108
130
public static String toTitleCase (String tmp ){
109
131
return tmp .substring (0 ,1 ).toUpperCase () + tmp .substring (1 ).toLowerCase ();
110
132
}
133
+ private static void tabTitle (){
134
+ System .out .format ("%3s %-20s %-25s %-4s %-5s %-7s %-18s\n " , "ID" , "Κατασκευαστής" , "Μοντέλο" , "HP" ,"CC" ,"Τιμή" ,"Χρώμα" );
135
+ }
111
136
}
0 commit comments