2020-12-28 17:30:38 +00:00
## sales
### Instructions
2022-09-15 11:13:05 +00:00
You are going to make a shopping system. It will have a store where the products will be saved, and a cart which will contain items from which a receipt will be generated.
2020-12-28 17:30:38 +00:00
2022-06-14 21:21:50 +00:00
**"Buy three, get one free".**
2020-12-28 17:30:38 +00:00
2022-06-14 21:21:50 +00:00
The store is having a promotion. The cheapest of three items will be free. But there is a problem with the printer interface, it cannot receive any zero values. We can create a workaround. We will reduce all of the values in the cart by a small amount to show the correct total price. You can see the example to see how it works.
2021-03-10 04:18:44 +00:00
2022-06-14 21:21:50 +00:00
You will have to implement for the `Cart` structure the following **functions** :
2022-03-23 18:33:24 +00:00
2022-06-14 21:21:50 +00:00
- `new` : that will initialize the cart.
- `insert_item` : will receive a reference to `Store` and a `String` . Just like the name says, it will insert the item to the cart.
2022-09-15 11:13:05 +00:00
- `generate_receipt` : returns a vector of sorted floats. This function must generate the receipt just like the example below, using the promotion. It should save the result in the `receipt` field.
2020-12-28 17:30:38 +00:00
### Expected Function
```rust
2022-03-23 18:33:24 +00:00
#[derive(Debug, Clone, PartialEq)]
2020-12-28 17:30:38 +00:00
pub struct Store {
pub products: Vec< (String, f32)>,
}
impl Store {
pub fn new(products: Vec< (String, f32)>) -> Store {
Store { products }
}
}
2022-03-23 18:33:24 +00:00
#[derive(Debug, Clone, PartialEq)]
2020-12-28 17:30:38 +00:00
pub struct Cart {
// expected public fields
}
impl Cart {
pub fn new() -> Cart {}
pub fn insert_item(& mut self, s: & Store, ele: String) {}
pub fn generate_receipt(& mut self) -> Vec< f32 > {}
}
```
### Example
2022-06-14 21:21:50 +00:00
```
2022-09-15 11:21:17 +00:00
[1.23, 3.12, 23.1]` => `[1.17, 2.98, 22.06]
2022-06-14 21:21:50 +00:00
```
2020-12-28 17:30:38 +00:00
2022-09-15 11:21:17 +00:00
Because `1.17 + 2.98 + 22.06` == `0 + 3.12 + 23.1`
Floats are rounded with a precision of two decimals which can create small discrepancies as per the example above.
2020-12-28 17:30:38 +00:00
2022-06-14 21:21:50 +00:00
This is a percentage calculation, and it can be applied to a set of three items. If the client purchases 9 items, they will receive three for free, with the discount applied to all items.
2022-03-23 18:33:24 +00:00
2022-06-14 21:21:50 +00:00
```
[1.23, 23.1, 3.12, 9.75, 1.75, 23.75, 2.75, 1.64, 15.23] => [1.16, 1.55, 1.65, 2.6, 2.94, 9.2, 14.38, 21.8, 22.42]
```
2020-12-28 17:30:38 +00:00
2022-03-23 18:33:24 +00:00
2022-06-14 21:21:50 +00:00
```
[3.12, 9.75, 1.75, 23.75, 2.75, 1.64, 15.23] => [1.54, 1.65, 2.59, 2.94, 9.18, 14.34, 22.36]
```
2020-12-28 17:30:38 +00:00
2022-06-14 21:21:50 +00:00
> Hint: Closures are the way.
2020-12-28 17:30:38 +00:00
### Usage
2021-03-10 04:18:44 +00:00
Here is a program to test your function,
2020-12-28 17:30:38 +00:00
```rust
2021-03-10 04:18:44 +00:00
use sales::*;
2020-12-28 17:30:38 +00:00
fn main() {
let store = Store::new(vec![
(String::from("product A"), 1.23),
(String::from("product B"), 23.1),
(String::from("product C"), 3.12)]);
println!("{:?}", store);
let mut cart = Cart::new();
cart.insert_item(& store, String::from("product A"));
cart.insert_item(& store, String::from("product B"));
cart.insert_item(& store, String::from("product C"));
println!("{:?}", cart.generate_receipt());
println!("{:?}", cart);
}
```
And its output:
```console
2021-04-28 09:47:34 +00:00
$ cargo run
2020-12-28 17:30:38 +00:00
Store { products: [("product A", 1.23), ("product B", 23.1), ("product C", 3.12)] }
2022-06-16 13:57:51 +00:00
[1.17, 2.98, 22.06]
2022-09-15 11:21:17 +00:00
Cart { items: [("product A", 1.23), ("product B", 23.1), ("product C", 3.12)], receipt: [1.17, 2.98, 22.06] }
2021-04-28 09:47:34 +00:00
$
2020-12-28 17:30:38 +00:00
```
2022-06-14 21:21:50 +00:00
### Notions
- [closures ](https://doc.rust-lang.org/rust-by-example/fn/closures.html )