DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #309 - Coffee Customization

You're an employee at a local cafe.

You can serve 3 coffee recipes:

  • Black = Black Coffee
  • Cubano = Cubano coffee + Brown sugar
  • Americano = Americano coffee + Milk with 0.5% fat

... and you can add a lot of extra sugar and milk in any coffee, for example:

  • Black coffee + Milk with 3.2% fat + Brown sugar
  • Cubano coffee + Brown sugar + Brown sugar + Cane sugar
  • Americano coffee + Milk with 3.2% fat + Cane sugar

With the following code provided for you, can you create a Coffee by implementing a CoffeeBuilder struct/class?

You can start from scratch or use the following code as a starting off point:

C++
struct Milk {
  float fat;
};

struct Sugar {
  std::string sort;
};

struct Coffee {
  std::string sort;
  std::vector<Milk> milk;
  std::vector<Sugar> sugar;
};
Enter fullscreen mode Exit fullscreen mode
Rust
#[derive(Debug)]
struct Coffee {
    sort: String,
    milk: Vec<Milk>,
    sugar: Vec<Sugar>,
}

#[derive(Debug)]
struct Milk {
    fat: f32,
}

#[derive(Debug)]
struct Sugar {
    sort: String,
}

struct CoffeeBuilder {
    sort: String,
    milk: Vec<Milk>,
    sugar: Vec<Sugar>,
}
Enter fullscreen mode Exit fullscreen mode

Tests

  1. A Black coffee with sugar and milk with 3.2% fat, please.
  2. A Cubano coffee, please.
  3. An Americano with two brown sugars, please.

Good luck!


This challenge comes from arkada38 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (2)

Collapse
 
aminnairi profile image
Amin

JavaScript

const count = (count, item) => ({ ...count, [item]: (count[item] || 0) + 1 });
const announce = callback => (announcements, [item, count]) => [...announcements, callback(item, count)];
const pluralize = (count, text) => count > 1 ? `${text}s` : text;
const coffee = (kind, sugars = [], milks = []) => ({
  addSugar: sugar => coffee(kind, [...sugars, sugar]),
  addMilk: milk => coffee(kind, sugars, [...milks, milk]),
  announce: () => {
    const sugarCount = sugars.reduce(count, {});
    const sugarAnnouncement = Object.entries(sugarCount).reduce(announce((sugar, count) => `with ${count} ${sugar} ${pluralize(count, "sugar")}`), []);
    const milkCount = milks.reduce(count, {});
    const milkAnnouncement = Object.entries(milkCount).reduce(announce((fat, count) => `with ${count} ${fat}% fat ${pluralize(count, "milk")}`), []);
    const announcements = [...sugarAnnouncement, ...milkAnnouncement];
    return announcements.length > 0 ? `One ${kind} ${announcements.join(", ")}, please.` : `One ${kind}, please.`;
  }
});
Enter fullscreen mode Exit fullscreen mode

Usage

const firstOrder = 
  coffee("black coffee")
    .addSugar("regular")
    .addMilk(3.2)
    .announce();

const secondOrder = coffee("Cubano coffee").announce();

const thirdOrder = 
  coffee("Americano")
    .addSugar("brown")
    .addSugar("brown")
    .announce();

console.log(firstOrder);  // One black coffee with 1 regular sugar, with 1 3.2% fat milk, please.
console.log(secondOrder); // One Cubano coffee, please.
console.log(thirdOrder);  // One Americano with 2 brown sugars, please.
Enter fullscreen mode Exit fullscreen mode
Collapse
 
qm3ster profile image
Mihail Malo

Not gonna lie,

struct Milk {
    fat: f32,
}
Enter fullscreen mode Exit fullscreen mode

got me giggling