dit_as_91896/order/mod.rs
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
use crate::food::Items;
use std::{collections::HashMap, fmt::Display};
#[derive(Clone)]
pub struct Order {
pub order: Items,
pub menu_items: Items,
pub cost_for_items: Items,
}
pub struct OrdersVec(pub Vec<Order>);
impl Order {
/**
# This New Func Creates A New Instance
# Example
```rust
use std::collections::HashMap;
use dit_as_91896::{
order::Order,
food::Items
};
let my_order: Order = Order::new(
Items::new(HashMap::new(), HashMap::new(), HashMap::new()),
Items::new(HashMap::new(), HashMap::new(), HashMap::new())
);
```
*/
pub fn new(menu_items: Items, cost_for_items: Items) -> Self {
Self {
order: Items {
food: HashMap::new(),
drinks: HashMap::new(),
sides: HashMap::new(),
},
menu_items,
cost_for_items,
}
}
/**
# Add To The Order
# Example
```rust
use map_macro::hash_map;
use std::collections::HashMap;
use dit_as_91896::{
order::Order,
food::Items
};
let menu = Items::new(
hash_map! {
String::from("test_food") => 2.0
},
HashMap::new(),
HashMap::new()
);
let cost_menu = Items::new(
hash_map! {
String::from("test_food") => 1.0
},
HashMap::new(),
HashMap::new()
);
let mut my_order: Order = Order::new(
menu,
cost_menu
);
// adds two of the food.
my_order.order_add(String::from("test_food")).unwrap();
my_order.order_add(String::from("test_food")).unwrap();
let order_output = Items::new(hash_map! { String::from("test_food") => 4.0 }, HashMap::new(), HashMap::new());
assert!(my_order.order.food == order_output.food);
assert!(my_order.order.drinks == order_output.drinks);
assert!(my_order.order.sides == order_output.sides);
```
*/
pub fn order_add(&mut self, item_name: String) -> Result<(), &'static str> {
if self.menu_items.food.contains_key(&item_name) {
// checks if item_name is in food
if self.order.food.contains_key(&item_name) {
// checks if theres allredy one in order
let price: f64 = self.order.food.get(&item_name).unwrap()
+ *self.menu_items.food.get(&item_name.clone()).unwrap();
self.order.food.insert(item_name.clone(), price);
Ok(())
} else {
self.order.food.insert(
item_name.clone(),
*self.menu_items.food.get(&item_name.clone()).unwrap(),
);
Ok(())
}
} else if self.menu_items.drinks.contains_key(&item_name) {
// checks if item_name is in drinks
if self.order.drinks.contains_key(&item_name) {
// checks if theres allredy one in order
let price: f64 = self.order.drinks.get(&item_name).unwrap()
+ *self.menu_items.drinks.get(&item_name.clone()).unwrap();
self.order.drinks.insert(item_name.clone(), price);
Ok(())
} else {
self.order.drinks.insert(
item_name.clone(),
*self.menu_items.drinks.get(&item_name.clone()).unwrap(),
);
Ok(())
}
} else if self.menu_items.sides.contains_key(&item_name) {
// checks if item_name is in sides
if self.order.sides.contains_key(&item_name) {
// checks if theres allredy one in order
let price: f64 = self.order.sides.get(&item_name).unwrap()
+ *self.menu_items.sides.get(&item_name.clone()).unwrap();
self.order.sides.insert(item_name.clone(), price);
Ok(())
} else {
self.order.sides.insert(
item_name.clone(),
*self.menu_items.sides.get(&item_name.clone()).unwrap(),
);
Ok(())
}
} else {
Err("Error: The Item Could Not Be Found On The Menu.")
}
}
/**
# Calculate the Cost and profit.
Find for the Customer and the profit for the store.\
Returns:
Vec<f64>: [
cost_user: f64,
profit_store: f64
]
# Example
```rust
use map_macro::hash_map;
use std::collections::HashMap;
use dit_as_91896::{
order::Order,
food::Items
};
// cost to user
let menu = Items::new(
hash_map! {
String::from("test_food") => 2.0
},
HashMap::new(),
HashMap::new()
);
// cost of items for eg "the store"
let cost_menu = Items::new(
hash_map! {
String::from("test_food") => 1.0
},
HashMap::new(),
HashMap::new()
);
let mut my_order: Order = Order::new(
menu,
cost_menu
);
// adds two of the food.
my_order.order_add(String::from("test_food")).unwrap();
my_order.order_add(String::from("test_food")).unwrap();
assert!(my_order.cost_profit() == vec![4.0, 2.0]);
```
*/
pub fn cost_profit(&self) -> Vec<f64> {
let food_earnings: Vec<f64> = self.order.food.values().cloned().collect();
let sides_earnings: Vec<f64> = self.order.sides.values().cloned().collect();
let drinks_earnings: Vec<f64> = self.order.drinks.values().cloned().collect();
let cost_user: f64 = food_earnings.iter().sum::<f64>()
+ drinks_earnings.iter().sum::<f64>()
+ sides_earnings.iter().sum::<f64>();
let mut profit_store: f64 = 0.0;
for (key, value) in &self.order.food {
let cost_item: f64 = *self.menu_items.food.get(key).unwrap();
let quantiy: f64 = value / cost_item;
profit_store += quantiy * (cost_item - *self.cost_for_items.food.get(key).unwrap());
}
for (key, value) in &self.order.drinks {
let cost_item: f64 = *self.menu_items.drinks.get(key).unwrap();
let quantiy: f64 = value / cost_item;
profit_store += quantiy * (cost_item - *self.cost_for_items.drinks.get(key).unwrap());
}
for (key, value) in &self.order.sides {
let cost_item: f64 = *self.menu_items.sides.get(key).unwrap();
let quantiy: f64 = value / cost_item;
profit_store += quantiy * (cost_item - *self.cost_for_items.sides.get(key).unwrap());
}
vec![cost_user, profit_store]
}
}
impl Display for Order {
/**
# Allows Order To Be Printed
*/
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.order)
}
}
impl Display for OrdersVec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = String::from("orders: [");
for (x, order) in self.0.iter().enumerate() {
output.push_str(format!("\n {}: {{ {} }},", x, order).as_str())
}
output.replace_range((output.len() - 1).., "\n]");
write!(f, "{output}")
}
}