From 8403d053c45bf151f9a55c7cbe75a23681f4b2a1 Mon Sep 17 00:00:00 2001 From: Emin Ali <br8bit.n8di5@slmail.me> Date: Thu, 26 Dec 2024 19:00:44 +0400 Subject: [PATCH] Update structs3.rs Refactored the code to encapsulate the cents_per_gram field within the `Package` struct, improving architectural clarity and maintainability. Additionally, updated the related functions and tests to align with this restructuring --- exercises/07_structs/structs3.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/exercises/07_structs/structs3.rs b/exercises/07_structs/structs3.rs index 69e5ced7..419b757e 100644 --- a/exercises/07_structs/structs3.rs +++ b/exercises/07_structs/structs3.rs @@ -6,10 +6,11 @@ struct Package { sender_country: String, recipient_country: String, weight_in_grams: u32, + cents_per_gram: u32, } impl Package { - fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Self { + fn new(sender_country: String, recipient_country: String, weight_in_grams: u32, cents_per_gram: u32) -> Self { if weight_in_grams < 10 { // This isn't how you should handle errors in Rust, but we will // learn about error handling later. @@ -20,6 +21,7 @@ impl Package { sender_country, recipient_country, weight_in_grams, + cents_per_gram, } } @@ -30,7 +32,7 @@ impl Package { } // TODO: Add the correct return type to the function signature. - fn get_fees(&self, cents_per_gram: u32) { + fn get_fees(&self) { // TODO: Calculate the package's fees. } } @@ -49,7 +51,7 @@ mod tests { let sender_country = String::from("Spain"); let recipient_country = String::from("Austria"); - Package::new(sender_country, recipient_country, 5); + Package::new(sender_country, recipient_country, 5, 0); } #[test] @@ -57,7 +59,7 @@ mod tests { let sender_country = String::from("Spain"); let recipient_country = String::from("Russia"); - let package = Package::new(sender_country, recipient_country, 1200); + let package = Package::new(sender_country, recipient_country, 1200, 0); assert!(package.is_international()); } @@ -67,7 +69,7 @@ mod tests { let sender_country = String::from("Canada"); let recipient_country = sender_country.clone(); - let package = Package::new(sender_country, recipient_country, 1200); + let package = Package::new(sender_country, recipient_country, 1200, 0); assert!(!package.is_international()); } @@ -77,11 +79,8 @@ mod tests { let sender_country = String::from("Spain"); let recipient_country = String::from("Spain"); - let cents_per_gram = 3; + let package = Package::new(sender_country, recipient_country, 1500, 3); - let package = Package::new(sender_country, recipient_country, 1500); - - assert_eq!(package.get_fees(cents_per_gram), 4500); - assert_eq!(package.get_fees(cents_per_gram * 2), 9000); + assert_eq!(package.get_fees(), 4500); } }