Skip to content

Commit 1c98122

Browse files
committed
Add coupling cohesion after
1 parent a323495 commit 1c98122

File tree

4 files changed

+98
-37
lines changed

4 files changed

+98
-37
lines changed

Diff for: better-rust/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: better-rust/coupling_and_cohesion/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77

88
[dependencies]
99
rand = "0.8.5"
10+
macros = { path = "../macros" }
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use rand::{self, Rng};
23

34
fn generate_random_string(length: usize, digits: bool) -> String {
@@ -10,9 +11,71 @@ fn generate_random_string(length: usize, digits: bool) -> String {
1011
.collect()
1112
}
1213

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+
}
1462

1563
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+
1679
pub fn generate_vehicle_id(&self, length: usize) -> String {
1780
generate_random_string(length, false)
1881
}
@@ -24,48 +87,25 @@ impl VehicleRegistry {
2487

2588
format!("{front}-{middle}-{last}")
2689
}
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+
}
2797
}
2898

2999
pub struct Application;
30100

31101
impl Application {
32102
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();
38105

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);
42107

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();
70110
}
71111
}

Diff for: better-rust/coupling_and_cohesion/src/main.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
mod coupling_cohesion_before;
2+
mod coupling_cohesion_after;
23

34
fn main() {
4-
let app = coupling_cohesion_before::Application;
5+
coupling_cohesion_before();
6+
7+
coupling_cohesion_after();
8+
}
9+
10+
#[macros::example]
11+
fn coupling_cohesion_before() {
12+
use coupling_cohesion_before::*;
13+
14+
let app = Application;
15+
16+
app.register_vehicle("Volkswagen ID3");
17+
}
18+
19+
#[macros::example]
20+
fn coupling_cohesion_after() {
21+
use coupling_cohesion_after::*;
22+
23+
let app = Application;
524

625
app.register_vehicle("Volkswagen ID3");
726
}

0 commit comments

Comments
 (0)