Skip to content

Commit c39da38

Browse files
committed
Add single_responsibility
1 parent 4b07419 commit c39da38

9 files changed

+167
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# misc
22
.DS_Store
3-
*.pyc
3+
*.pyc
4+
/better-rust/target

better-rust/Cargo.lock

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

better-rust/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
workspace = { members = ["solid"] }
2+
[package]
3+
name = "better-rust"
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

better-rust/solid/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "solid"
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]

better-rust/solid/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod single_responsibility_before;
2+
pub mod single_responsibility_after;

better-rust/solid/src/main.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
fn main() {
3+
run_single_responsibility_before();
4+
5+
run_single_responsibility_after();
6+
}
7+
8+
fn run_single_responsibility_before() {
9+
println!("running `run_single_responsibility_before`:\n");
10+
11+
use solid::single_responsibility_before::*;
12+
13+
let mut order = Order::new();
14+
15+
order.add_item("Keyboard", 1, 50.0);
16+
order.add_item("SSD", 1, 150.0);
17+
order.add_item("USB cable", 2, 5.0);
18+
19+
println!("{}", order.total_price());
20+
order.pay("debit", "0372846");
21+
22+
println!("------------");
23+
}
24+
25+
fn run_single_responsibility_after() {
26+
println!("running `run_single_responsibility_after`:\n");
27+
28+
use solid::single_responsibility_after::*;
29+
30+
let mut order = Order::new();
31+
32+
order.add_item("Keyboard", 1, 50.0);
33+
order.add_item("SSD", 1, 150.0);
34+
order.add_item("USB cable", 2, 5.0);
35+
36+
println!("{}", order.total_price());
37+
38+
let processor = PaymentProcessor {};
39+
40+
processor.pay_debit(&mut order, "0372846");
41+
42+
println!("------------");
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
pub struct Order<'a> {
3+
pub items: Vec<&'a str>,
4+
pub quantities: Vec<u32>,
5+
pub prices: Vec<f32>,
6+
pub status: &'a str,
7+
}
8+
9+
impl<'a> Order<'a> {
10+
pub fn new() -> Self {
11+
Self { items: vec![], quantities: vec![], prices: vec![], status: "open" }
12+
}
13+
14+
pub fn add_item(&mut self, name: &'a str, quantity: u32, price: f32) {
15+
self.items.push(name);
16+
self.quantities.push(quantity);
17+
self.prices.push(price);
18+
}
19+
20+
pub fn total_price(&self) -> f32 {
21+
let mut total: f32 = 0.0;
22+
23+
for (i, price) in self.prices.iter().enumerate() {
24+
total += self.quantities[i] as f32 * price;
25+
}
26+
27+
total
28+
}
29+
}
30+
31+
pub struct PaymentProcessor;
32+
33+
impl PaymentProcessor {
34+
pub fn pay_debit(&self, order: &mut Order, security_code: &str) {
35+
println!("Processing debit payment type");
36+
println!("Verifying security code: {security_code}");
37+
order.status = "paid";
38+
}
39+
40+
pub fn pay_credit(&self, order: &mut Order, security_code: &str) {
41+
println!("Processing credit payment type");
42+
println!("Verifying security code: {security_code}");
43+
order.status = "paid";
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
pub struct Order<'a> {
3+
pub items: Vec<&'a str>,
4+
pub quantities: Vec<u32>,
5+
pub prices: Vec<f32>,
6+
pub status: &'a str,
7+
}
8+
9+
impl<'a> Order<'a> {
10+
pub fn new() -> Self {
11+
Self { items: vec![], quantities: vec![], prices: vec![], status: "open" }
12+
}
13+
14+
pub fn add_item(&mut self, name: &'a str, quantity: u32, price: f32) {
15+
self.items.push(name);
16+
self.quantities.push(quantity);
17+
self.prices.push(price);
18+
}
19+
20+
pub fn total_price(&self) -> f32 {
21+
let mut total: f32 = 0.0;
22+
23+
for (i, price) in self.prices.iter().enumerate() {
24+
total += self.quantities[i] as f32 * price;
25+
}
26+
27+
total
28+
}
29+
30+
pub fn pay(&mut self, payment_type: &str, security_code: &str) {
31+
if payment_type == "debit" {
32+
println!("Processing debit payment type");
33+
println!("Verifying security code: {security_code}");
34+
self.status = "paid";
35+
} else if payment_type == "credit" {
36+
println!("Processing credit payment type");
37+
println!("Verifying security code: {security_code}");
38+
self.status = "paid";
39+
} else {
40+
panic!("Unknown payment type: {payment_type}");
41+
}
42+
}
43+
}

better-rust/src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
fn main() {
3+
println!("Hello, world!");
4+
}

0 commit comments

Comments
 (0)