mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-01 08:19:18 +00:00
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
This commit is contained in:
parent
26cf4989a2
commit
8403d053c4
@ -6,10 +6,11 @@ struct Package {
|
|||||||
sender_country: String,
|
sender_country: String,
|
||||||
recipient_country: String,
|
recipient_country: String,
|
||||||
weight_in_grams: u32,
|
weight_in_grams: u32,
|
||||||
|
cents_per_gram: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Package {
|
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 {
|
if weight_in_grams < 10 {
|
||||||
// This isn't how you should handle errors in Rust, but we will
|
// This isn't how you should handle errors in Rust, but we will
|
||||||
// learn about error handling later.
|
// learn about error handling later.
|
||||||
@ -20,6 +21,7 @@ impl Package {
|
|||||||
sender_country,
|
sender_country,
|
||||||
recipient_country,
|
recipient_country,
|
||||||
weight_in_grams,
|
weight_in_grams,
|
||||||
|
cents_per_gram,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +32,7 @@ impl Package {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add the correct return type to the function signature.
|
// 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.
|
// TODO: Calculate the package's fees.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,7 +51,7 @@ mod tests {
|
|||||||
let sender_country = String::from("Spain");
|
let sender_country = String::from("Spain");
|
||||||
let recipient_country = String::from("Austria");
|
let recipient_country = String::from("Austria");
|
||||||
|
|
||||||
Package::new(sender_country, recipient_country, 5);
|
Package::new(sender_country, recipient_country, 5, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -57,7 +59,7 @@ mod tests {
|
|||||||
let sender_country = String::from("Spain");
|
let sender_country = String::from("Spain");
|
||||||
let recipient_country = String::from("Russia");
|
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());
|
assert!(package.is_international());
|
||||||
}
|
}
|
||||||
@ -67,7 +69,7 @@ mod tests {
|
|||||||
let sender_country = String::from("Canada");
|
let sender_country = String::from("Canada");
|
||||||
let recipient_country = sender_country.clone();
|
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());
|
assert!(!package.is_international());
|
||||||
}
|
}
|
||||||
@ -77,11 +79,8 @@ mod tests {
|
|||||||
let sender_country = String::from("Spain");
|
let sender_country = String::from("Spain");
|
||||||
let recipient_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(), 4500);
|
||||||
|
|
||||||
assert_eq!(package.get_fees(cents_per_gram), 4500);
|
|
||||||
assert_eq!(package.get_fees(cents_per_gram * 2), 9000);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user