Skip to content

Commit a323495

Browse files
committed
coupling cohesion before
1 parent 16b6e08 commit a323495

File tree

6 files changed

+231
-1
lines changed

6 files changed

+231
-1
lines changed

better-rust/Cargo.lock

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

better-rust/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
workspace = { members = ["solid", "macros"] }
1+
workspace = { members = ["solid", "macros", "coupling_and_cohesion"] }
22
[package]
33
name = "better-rust"
44
version = "0.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "coupling_and_cohesion"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rand = "0.8.5"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use rand::{self, Rng};
2+
3+
fn generate_random_string(length: usize, digits: bool) -> String {
4+
rand::thread_rng()
5+
.sample_iter(rand::distributions::Alphanumeric)
6+
.map(char::from)
7+
.filter(|c| if digits { c.is_digit(10) } else { !c.is_digit(10) })
8+
.map(|c| c.to_ascii_uppercase())
9+
.take(length)
10+
.collect()
11+
}
12+
13+
pub struct VehicleRegistry;
14+
15+
impl VehicleRegistry {
16+
pub fn generate_vehicle_id(&self, length: usize) -> String {
17+
generate_random_string(length, false)
18+
}
19+
20+
pub fn generate_vehicle_license(&self, id: &str) -> String {
21+
let front = &id[0..2];
22+
let middle = generate_random_string(2, true);
23+
let last = generate_random_string(2, false);
24+
25+
format!("{front}-{middle}-{last}")
26+
}
27+
}
28+
29+
pub struct Application;
30+
31+
impl Application {
32+
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);
38+
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);
42+
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:?}");
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use rand::{self, Rng};
2+
3+
fn generate_random_string(length: usize, digits: bool) -> String {
4+
rand::thread_rng()
5+
.sample_iter(rand::distributions::Alphanumeric)
6+
.map(char::from)
7+
.filter(|c| if digits { c.is_digit(10) } else { !c.is_digit(10) })
8+
.map(|c| c.to_ascii_uppercase())
9+
.take(length)
10+
.collect()
11+
}
12+
13+
pub struct VehicleRegistry;
14+
15+
impl VehicleRegistry {
16+
pub fn generate_vehicle_id(&self, length: usize) -> String {
17+
generate_random_string(length, false)
18+
}
19+
20+
pub fn generate_vehicle_license(&self, id: &str) -> String {
21+
let front = &id[0..2];
22+
let middle = generate_random_string(2, true);
23+
let last = generate_random_string(2, false);
24+
25+
format!("{front}-{middle}-{last}")
26+
}
27+
}
28+
29+
pub struct Application;
30+
31+
impl Application {
32+
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);
38+
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);
42+
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:?}");
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mod coupling_cohesion_before;
2+
3+
fn main() {
4+
let app = coupling_cohesion_before::Application;
5+
6+
app.register_vehicle("Volkswagen ID3");
7+
}

0 commit comments

Comments
 (0)