40 lines
999 B
Rust
40 lines
999 B
Rust
use crate::models::Drink;
|
|
use std::collections::HashMap;
|
|
|
|
pub struct DrinkReport {
|
|
pub summary: HashMap<u32, u32>,
|
|
}
|
|
|
|
impl DrinkReport {
|
|
pub fn into_sorted(self) -> impl Iterator<Item = (u32, u32)> {
|
|
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<Drink> {
|
|
fn generate_report(&self) -> DrinkReport {
|
|
let mut summary: HashMap<u32, u32> = 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 }
|
|
}
|
|
}
|