diff options
| author | Brad Clawsie <brad@b7j0c.org> | 2026-11-02 15:31:08 -0600 |
|---|---|---|
| committer | Brad Clawsie <brad@b7j0c.org> | 2026-11-02 15:31:08 -0600 |
| commit | 07cb7e7dfa1b2a520e02af70ee924100127a95be (patch) | |
| tree | 5ec4fc034efe1431c594ca18100caa7cb9a56e97 /ex3_12/src | |
| parent | 98fc53dec43f84906f737bb3473bf65c09fc5450 (diff) | |
feat: ex3_12
commit 028ab60ddc9be47264b6aa4c5ceaa592407eee7a
Author: Brad Clawsie <brad@b7j0c.org>
Date: Mon Nov 2 15:30:45 2026 -0600
feat: ex3_12
Diffstat (limited to 'ex3_12/src')
| -rw-r--r-- | ex3_12/src/lib.rs | 1 | ||||
| -rw-r--r-- | ex3_12/src/main.rs | 14 | ||||
| -rw-r--r-- | ex3_12/src/order.rs | 55 |
3 files changed, 70 insertions, 0 deletions
diff --git a/ex3_12/src/lib.rs b/ex3_12/src/lib.rs new file mode 100644 index 0000000..0e3ab35 --- /dev/null +++ b/ex3_12/src/lib.rs @@ -0,0 +1 @@ +pub mod order; diff --git a/ex3_12/src/main.rs b/ex3_12/src/main.rs new file mode 100644 index 0000000..18effb1 --- /dev/null +++ b/ex3_12/src/main.rs @@ -0,0 +1,14 @@ +mod order; + +use order::Order; + +fn main() { + let mut o = match Order::new("p",1,1) { + Ok(_o) => _o, + Err(e) => panic!("{}", e), + }; + o.set_product_name("p2"); + o.set_quantity(2); + o.set_unit_price(2); + println!("{}{}{}{}", o.product_name(), o.quantity(), o.unit_price(), o.total()); +} diff --git a/ex3_12/src/order.rs b/ex3_12/src/order.rs new file mode 100644 index 0000000..d1ac155 --- /dev/null +++ b/ex3_12/src/order.rs @@ -0,0 +1,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 + } +} |
