dit_as_91896/food/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
use inflector::Inflector;
use std::fmt;
use std::{collections::HashMap, fmt::Display};
#[derive(Clone)]
pub struct Items {
pub food: HashMap<String, f64>,
pub drinks: HashMap<String, f64>,
pub sides: HashMap<String, f64>,
}
impl Items {
/// # New instance function
/// @prams:
/// food: vector of food items
/// drinks: vector of drinks
/// sides: vector of sides
/// @return: instance of type Items
///
/// # Panics
///
/// The function panics if the second argument is zero.
///
/// ```rust
/// use std::collections::HashMap;
/// use map_macro::hash_map;
/// use dit_as_91896::food::Items;
///
/// let food: HashMap<String, f64> = hash_map! {
/// String::from("0") => 0.0,
/// };
/// let drinks: HashMap<String, f64> = hash_map! {
/// String::from("1") => 1.0,
/// };
/// let sides: HashMap<String, f64> = hash_map! {
/// String::from("2") => 2.0,
/// };
///
/// let items: Items = Items::new(food.clone(), drinks.clone(), sides.clone());
///
/// assert!(items.food == food);
/// assert!(items.drinks == drinks );
/// assert!(items.sides == sides);
/// ```
pub fn new(
food: HashMap<String, f64>,
drinks: HashMap<String, f64>,
sides: HashMap<String, f64>,
) -> Self {
Self {
food,
drinks,
sides,
}
}
/**
# view the menu in a prety format
no params
returns a `String`
*/
pub fn menu_view(&self) -> String {
let mut food: String = String::new();
let mut drinks: String = String::new();
let mut sides: String = String::new();
if self.food.keys().len() != 0usize {
for (item, price) in &self.food {
food.push_str(
format!(
"{}: costs ${:.2},\n ",
item.as_str().to_title_case(),
price
)
.as_str(),
);
}
food.replace_range((food.len() - 6).., "")
}
if self.drinks.keys().len() != 0usize {
for (item, price) in &self.drinks {
drinks.push_str(
format!(
"{}: costs ${:.2},\n ",
item.as_str().to_title_case(),
price
)
.as_str(),
)
}
drinks.replace_range((drinks.len() - 6).., "")
}
if self.sides.keys().len() != 0usize {
for (item, price) in &self.sides {
sides.push_str(
format!(
"{}: costs ${:.2},\n ",
item.as_str().to_title_case(),
price
)
.as_str(),
);
}
sides.replace_range((sides.len() - 6).., "")
}
String::from(format!(
"Food\n {food}\n\nDrinks\n {drinks}\n\nSides\n {sides}"
))
}
}
impl Display for Items {
/// Fmt function allows the struct/type to be printed to the console
///
/// ```rust
/// use map_macro::hash_map;
/// use dit_as_91896::food::Items;
///
/// let items: Items = Items {
/// food: hash_map! {
/// String::from("0") => 0.0,
/// },
/// drinks: hash_map! {
/// String::from("1") => 1.0,
/// },
/// sides: hash_map! {
/// String::from("2") => 2.0,
/// },
/// };
///
/// assert!(format!("{items}").as_str() == "food: {0: costs $0.00}, drinks: {1: costs $1.00}, sides: {2: costs $2.00}");
/// ```
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut food: String = String::new();
let mut drinks: String = String::new();
let mut sides: String = String::new();
if self.food.keys().len() != 0usize {
for (item, price) in &self.food {
food.push_str(format!("{}: costs ${:.2},", item.as_str(), price).as_str());
}
food.replace_range((food.len() - 1).., "")
}
if self.drinks.keys().len() != 0usize {
for (item, price) in &self.drinks {
drinks.push_str(format!("{}: costs ${:.2},", item.as_str(), price).as_str())
}
drinks.replace_range((drinks.len() - 1).., "")
}
if self.sides.keys().len() != 0usize {
for (item, price) in &self.sides {
sides.push_str(format!("{}: costs ${:.2},", item.as_str(), price).as_str());
}
sides.replace_range((sides.len() - 1).., "")
}
write!(
f,
"food: {{{food}}}, drinks: {{{drinks}}}, sides: {{{sides}}}"
)
}
}