tutorial_1/order/
mod.rs

1use crate::customer;
2use crate::customer::*;
3use crate::product;
4use crate::product::*;
5pub struct Order {
6    id: u64,
7    // here we use crate to retrieve the product module and import its Product
8    product: Product,
9    customer: Customer,
10    quantity: u32,
11}
12
13impl Order {
14    pub fn new(
15        id: u64,
16        product: Product,
17        customer: Customer,
18        quantity: u32,
19    ) -> Self {
20        Order {
21            id,
22            product,
23            customer,
24            quantity,
25        }
26    }
27
28    pub fn calculate_discount(&self) -> f64 {
29        println!("discount got quantity: {}", self.quantity);
30
31        if self.quantity > 5 {
32            0.5
33        } else {
34            0.2
35        }
36    }
37
38    pub fn total_bill(&self) -> f64 {
39        let discount = self.calculate_discount();
40        println!(
41            "discount:{}, product_price: {}",
42            discount,
43            self.product.product_price()
44        );
45        let total_before_discount =
46            self.product.product_price() * self.quantity as f64;
47        total_before_discount - (total_before_discount * discount)
48    }
49}