1
+ use std:: collections:: HashMap ;
1
2
use rand:: { self , Rng } ;
2
3
3
4
fn generate_random_string ( length : usize , digits : bool ) -> String {
@@ -10,9 +11,71 @@ fn generate_random_string(length: usize, digits: bool) -> String {
10
11
. collect ( )
11
12
}
12
13
13
- pub struct VehicleRegistry ;
14
+ #[ derive( Clone ) ]
15
+ pub struct VehicleInfo {
16
+ pub brand : String ,
17
+ pub electric : bool ,
18
+ pub catalogue_price : u32 ,
19
+ }
20
+
21
+ impl VehicleInfo {
22
+ pub fn new ( brand : & str , electric : bool , catalogue_price : u32 ) -> Self {
23
+ Self { brand : brand. to_string ( ) , electric, catalogue_price }
24
+ }
25
+
26
+ pub fn compute_tax ( & self ) -> f32 {
27
+ let mut tax_percentage = 0.05 ;
28
+ if self . electric {
29
+ tax_percentage = 0.02 ;
30
+ }
31
+
32
+ tax_percentage * self . catalogue_price as f32
33
+ }
34
+
35
+ pub fn print ( & self ) {
36
+ println ! ( "Brand: {}" , self . brand) ;
37
+ println ! ( "Payable tax: {:#?}" , self . compute_tax( ) ) ;
38
+ }
39
+ }
40
+
41
+ pub struct Vehicle {
42
+ pub id : String ,
43
+ pub license_plate : String ,
44
+ pub info : VehicleInfo ,
45
+ }
46
+
47
+ impl Vehicle {
48
+ pub fn new ( id : & str , license_plate : String , info : VehicleInfo ) -> Self {
49
+ Self { id : id. to_string ( ) , license_plate, info }
50
+ }
51
+
52
+ pub fn print ( & self ) {
53
+ println ! ( "Registration complete. Vehicle information:" ) ;
54
+ println ! ( "Id: {}" , self . id) ;
55
+ println ! ( "License plate: {}" , self . license_plate) ;
56
+ self . info . print ( ) ;
57
+ }
58
+ }
59
+ pub struct VehicleRegistry {
60
+ pub vehicle_info : HashMap < String , VehicleInfo > ,
61
+ }
14
62
15
63
impl VehicleRegistry {
64
+ pub fn new ( ) -> Self {
65
+ let mut vehicle_registry = Self { vehicle_info : HashMap :: new ( ) } ;
66
+
67
+ vehicle_registry. add_vehicle_info ( "Tesla Model 3" , true , 60000 ) ;
68
+ vehicle_registry. add_vehicle_info ( "Volkswagen ID3" , true , 35000 ) ;
69
+ vehicle_registry. add_vehicle_info ( "BMW 5" , false , 45000 ) ;
70
+ vehicle_registry. add_vehicle_info ( "Tesla Model Y" , true , 75000 ) ;
71
+
72
+ vehicle_registry
73
+ }
74
+
75
+ fn add_vehicle_info ( & mut self , brand : & str , electric : bool , catalogue_price : u32 ) {
76
+ self . vehicle_info . insert ( brand. to_string ( ) , VehicleInfo :: new ( brand, electric, catalogue_price) ) ;
77
+ }
78
+
16
79
pub fn generate_vehicle_id ( & self , length : usize ) -> String {
17
80
generate_random_string ( length, false )
18
81
}
@@ -24,48 +87,25 @@ impl VehicleRegistry {
24
87
25
88
format ! ( "{front}-{middle}-{last}" )
26
89
}
90
+
91
+ pub fn create_vehicle ( & self , brand : & str ) -> Vehicle {
92
+ let id = self . generate_vehicle_id ( 12 ) ;
93
+ let license_plate = self . generate_vehicle_license ( & id) ;
94
+
95
+ Vehicle :: new ( & id, license_plate, self . vehicle_info . get ( brand) . unwrap ( ) . clone ( ) )
96
+ }
27
97
}
28
98
29
99
pub struct Application ;
30
100
31
101
impl Application {
32
102
pub fn register_vehicle ( & self , brand : & str ) {
33
- // create a registry instance
34
- let registry = VehicleRegistry ;
35
-
36
- // generate a vehicle id of length 12
37
- let vehicle_id = registry. generate_vehicle_id ( 12 ) ;
103
+ // create a registry instance
104
+ let registry = VehicleRegistry :: new ( ) ;
38
105
39
- // now generate a license plate for the vehicle
40
- // using the first two characters of the vehicle id
41
- let license_plate = registry. generate_vehicle_license ( & vehicle_id) ;
106
+ let vehicle = registry. create_vehicle ( brand) ;
42
107
43
- // compute the catalogue price
44
- let mut catalogue_price = 0 ;
45
-
46
- if brand == "Tesla Model 3" {
47
- catalogue_price = 60000 ;
48
- } else if brand == "Volkswagen ID3" {
49
- catalogue_price = 35000 ;
50
- } else if brand == "BMW 5" {
51
- catalogue_price = 45000 ;
52
- }
53
-
54
- // compute the tax percentage (default 5% of the catalogue price, except for electric cars where it is 2%)
55
- let mut tax_percentage = 0.05 ;
56
-
57
- if brand == "Tesla Model 3" || brand == "Volkswagen ID3" {
58
- tax_percentage = 0.02 ;
59
- }
60
-
61
- // compute the payable tax
62
- let payable_tax = tax_percentage * catalogue_price as f32 ;
63
-
64
- // print out the vehicle registration information
65
- println ! ( "Registration complete. Vehicle information:" ) ;
66
- println ! ( "Brand: {brand}" ) ;
67
- println ! ( "Id: {vehicle_id}" ) ;
68
- println ! ( "License plate: {license_plate}" ) ;
69
- println ! ( "Payable tax: {payable_tax:?}" ) ;
108
+ // print out the vehicle information
109
+ vehicle. print ( ) ;
70
110
}
71
111
}
0 commit comments