use crate::models::Drink; use std::collections::HashMap; pub struct DrinkReport { pub summary: HashMap, } impl DrinkReport { pub fn into_sorted(self) -> impl Iterator { let mut array: Vec<(u32, u32)> = self.summary.into_iter().collect(); array.sort_by(|(a, _), (b, _)| a.cmp(b)); array.into_iter() } } pub trait GenerateDrinkReport { fn generate_report(&self) -> DrinkReport; } impl GenerateDrinkReport for Vec { fn generate_report(&self) -> DrinkReport { let mut summary: HashMap = HashMap::new(); // init with all default drink prices for price in [2u32, 3u32, 5u32, 8u32, 15u32].into_iter() { summary.insert(price, 0); } for drink in self { summary .entry(drink.price) .and_modify(|v| *v += drink.quantity) .or_insert(drink.quantity); } DrinkReport { summary } } }