summaryrefslogtreecommitdiff
path: root/ex3_12
diff options
context:
space:
mode:
Diffstat (limited to 'ex3_12')
-rw-r--r--ex3_12/Cargo.lock7
-rw-r--r--ex3_12/Cargo.toml6
-rw-r--r--ex3_12/src/lib.rs1
-rw-r--r--ex3_12/src/main.rs14
-rw-r--r--ex3_12/src/order.rs55
-rw-r--r--ex3_12/tests/test.rs16
6 files changed, 99 insertions, 0 deletions
diff --git a/ex3_12/Cargo.lock b/ex3_12/Cargo.lock
new file mode 100644
index 0000000..3767028
--- /dev/null
+++ b/ex3_12/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "ex3_12"
+version = "0.1.0"
diff --git a/ex3_12/Cargo.toml b/ex3_12/Cargo.toml
new file mode 100644
index 0000000..4e8f6c3
--- /dev/null
+++ b/ex3_12/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "ex3_12"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
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
+ }
+}
diff --git a/ex3_12/tests/test.rs b/ex3_12/tests/test.rs
new file mode 100644
index 0000000..8e5f1d8
--- /dev/null
+++ b/ex3_12/tests/test.rs
@@ -0,0 +1,16 @@
+#[cfg(test)]
+mod tests {
+ use ex3_12::order::Order;
+
+ #[test]
+ fn t0() {
+ assert!(Order::new("p",1,1).is_ok());
+ }
+
+ #[test]
+ fn t1() {
+ assert!(Order::new("",1,1).is_err());
+ assert!(Order::new("p",0,1).is_err());
+ assert!(Order::new("p",1,0).is_err());
+ }
+}