summaryrefslogtreecommitdiff
path: root/ex3_12/src/order.rs
blob: d1ac1553e364865c6a8985524fe71c7cc91d93fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
pub struct Order {
    product_name: String,
    quantity: u32,
    unit_price: u32,
}

impl Order {
    pub fn new(product_name: &str, quantity: u32, unit_price: u32) -> Result<Self, &'static str> {
        if product_name.is_empty() {
            return Err("product_name is empty");
        }
        if product_name.len() > 300 {
            return Err("product_name len is greater than 300");
        }
        if quantity == 0 {
            return Err("quantity is 0");
        }
        if unit_price == 0 {
            return Err("unit price is 0");
        }
        Ok(Self {
            product_name: String::from(product_name),
            quantity,
            unit_price,
        })
    }

    pub fn total(&self) -> u32 {
        self.quantity * self.unit_price
    }

    pub fn product_name(&self) -> String {
        self.product_name.clone()
    }

    pub fn quantity(&self) -> u32 {
        self.quantity
    }

    pub fn unit_price(&self) -> u32 {
        self.unit_price
    }

    pub fn set_product_name(&mut self, product_name: &str) {
        self.product_name = String::from(product_name);
    }

    pub fn set_quantity(&mut self, quantity:u32) {
        self.quantity = quantity
    }

    pub fn set_unit_price(&mut self, unit_price: u32) {
        self.unit_price = unit_price
    }
}