dit_as_91896/food/
mod.rs

1use inflector::Inflector;
2use std::fmt;
3use std::{collections::HashMap, fmt::Display};
4
5#[derive(Clone)]
6pub struct Items {
7    pub food: HashMap<String, f64>,
8    pub drinks: HashMap<String, f64>,
9    pub sides: HashMap<String, f64>,
10}
11
12impl Items {
13    /// # New instance function
14    /// @prams:
15    ///     food: vector of food items
16    ///     drinks: vector of drinks
17    ///     sides: vector of sides
18    /// @return: instance of type Items
19    ///
20    /// # Panics
21    ///
22    /// The function panics if the second argument is zero.
23    ///
24    /// ```rust
25    ///     use std::collections::HashMap;
26    ///     use map_macro::hash_map;
27    ///     use dit_as_91896::food::Items;
28    ///
29    ///     let food: HashMap<String, f64> = hash_map! {
30    ///         String::from("0") => 0.0,
31    ///     };
32    ///     let drinks: HashMap<String, f64> = hash_map! {
33    ///         String::from("1") => 1.0,
34    ///     };
35    ///     let sides: HashMap<String, f64> = hash_map! {
36    ///         String::from("2") => 2.0,
37    ///     };
38    ///
39    ///     let items: Items = Items::new(food.clone(), drinks.clone(), sides.clone());
40    ///
41    ///     assert!(items.food == food);
42    ///     assert!(items.drinks == drinks );
43    ///     assert!(items.sides == sides);
44    /// ```
45    pub fn new(
46        food: HashMap<String, f64>,
47        drinks: HashMap<String, f64>,
48        sides: HashMap<String, f64>,
49    ) -> Self {
50        Self {
51            food,
52            drinks,
53            sides,
54        }
55    }
56
57    /**
58    # view the menu in a prety format
59
60    no params
61
62    returns a `String`
63    */
64    pub fn menu_view(&self) -> String {
65        let mut food: String = String::new();
66        let mut drinks: String = String::new();
67        let mut sides: String = String::new();
68
69        if self.food.keys().len() != 0usize {
70            for (item, price) in &self.food {
71                food.push_str(
72                    format!(
73                        "{}: costs ${:.2},\n    ",
74                        item.as_str().to_title_case(),
75                        price
76                    )
77                    .as_str(),
78                );
79            }
80            food.replace_range((food.len() - 6).., "")
81        }
82
83        if self.drinks.keys().len() != 0usize {
84            for (item, price) in &self.drinks {
85                drinks.push_str(
86                    format!(
87                        "{}: costs ${:.2},\n    ",
88                        item.as_str().to_title_case(),
89                        price
90                    )
91                    .as_str(),
92                )
93            }
94            drinks.replace_range((drinks.len() - 6).., "")
95        }
96
97        if self.sides.keys().len() != 0usize {
98            for (item, price) in &self.sides {
99                sides.push_str(
100                    format!(
101                        "{}: costs ${:.2},\n    ",
102                        item.as_str().to_title_case(),
103                        price
104                    )
105                    .as_str(),
106                );
107            }
108            sides.replace_range((sides.len() - 6).., "")
109        }
110
111        String::from(format!(
112            "Food\n    {food}\n\nDrinks\n    {drinks}\n\nSides\n    {sides}"
113        ))
114    }
115}
116
117impl Display for Items {
118    /// Fmt function allows the struct/type to be printed to the console
119    ///
120    /// ```rust
121    ///     use map_macro::hash_map;
122    ///     use dit_as_91896::food::Items;
123    ///
124    ///     let items: Items = Items {
125    ///     food: hash_map! {
126    ///         String::from("0") => 0.0,
127    ///     },
128    ///     drinks: hash_map! {
129    ///         String::from("1") => 1.0,
130    ///     },
131    ///     sides: hash_map! {
132    ///         String::from("2") => 2.0,
133    ///     },
134    /// };
135    ///
136    /// assert!(format!("{items}").as_str() == "food: {0: costs $0.00}, drinks: {1: costs $1.00}, sides: {2: costs $2.00}");
137    /// ```
138    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139        let mut food: String = String::new();
140        let mut drinks: String = String::new();
141        let mut sides: String = String::new();
142
143        if self.food.keys().len() != 0usize {
144            for (item, price) in &self.food {
145                food.push_str(format!("{}: costs ${:.2},", item.as_str(), price).as_str());
146            }
147            food.replace_range((food.len() - 1).., "")
148        }
149
150        if self.drinks.keys().len() != 0usize {
151            for (item, price) in &self.drinks {
152                drinks.push_str(format!("{}: costs ${:.2},", item.as_str(), price).as_str())
153            }
154            drinks.replace_range((drinks.len() - 1).., "")
155        }
156
157        if self.sides.keys().len() != 0usize {
158            for (item, price) in &self.sides {
159                sides.push_str(format!("{}: costs ${:.2},", item.as_str(), price).as_str());
160            }
161            sides.replace_range((sides.len() - 1).., "")
162        }
163
164        write!(
165            f,
166            "food: {{{food}}}, drinks: {{{drinks}}}, sides: {{{sides}}}"
167        )
168    }
169}