Merge 'upstream/main' into main

This commit is contained in:
lukaszKielar 2020-06-30 20:55:02 +02:00
commit 5d17c5aaa4

View File

@ -5,31 +5,39 @@
#[derive(Debug)] #[derive(Debug)]
struct Package { struct Package {
from: String, sender_country: String,
to: String, recipient_country: String,
weight: f32, weight_in_grams: i32,
} }
impl Package { impl Package {
fn new(from: String, to: String, weight: f32) -> Package { fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
if weight <= 0.0 { if weight_in_grams <= 0 {
let msg = format!("Weight must be greater than 0, {} specified", weight); let msg = format!(
"Weight must be greater than 0, {} specified",
weight_in_grams
);
panic!(msg) panic!(msg)
} else { } else {
return Package { from, to, weight }; return Package {
sender_country,
recipient_country,
weight_in_grams,
};
} }
} }
fn is_international(&self) -> bool { fn is_international(&self) -> bool {
if self.from == self.to { if self.sender_country == self.recipient_country {
false false
} else { } else {
true true
} }
} }
fn get_fees(&self, cost_per_kg: f32) -> f32 { fn get_fees(&self, cents_per_kg: i32) -> i32 {
self.weight * cost_per_kg let weight_in_kgs = (self.weight_in_grams as f32) / 1000.;
(weight_in_kgs * (cents_per_kg as f32)) as i32
} }
} }
@ -40,28 +48,28 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn fail_creating_weightless_package() { fn fail_creating_weightless_package() {
let country_from = String::from("Spain"); let sender_country = String::from("Spain");
let country_to = String::from("Austria"); let recipient_country = String::from("Austria");
Package::new(country_from, country_to, -2.21); Package::new(sender_country, recipient_country, -2210);
} }
#[test] #[test]
fn create_international_package() { fn create_international_package() {
let country_from = String::from("Spain"); let sender_country = String::from("Spain");
let country_to = String::from("Russia"); let recipient_country = String::from("Russia");
let package = Package::new(country_from, country_to, 1.2); let package = Package::new(sender_country, recipient_country, 1200);
assert!(package.is_international()); assert!(package.is_international());
} }
#[test] #[test]
fn calculate_transport_fees() { fn calculate_transport_fees() {
let country_from = String::from("Spain"); let sender_country = String::from("Spain");
let country_to = String::from("Spain"); let recipient_country = String::from("Spain");
let country_fee = 8.; let cents_per_kg = 3000;
let package = Package::new(country_from, country_to, 22.0); let package = Package::new(sender_country, recipient_country, 1500);
assert_eq!(package.get_fees(country_fee), 176.0); assert_eq!(package.get_fees(cents_per_kg), 4500);
} }
} }