Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package healthyHeaven;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Vegetable potato = new Vegetable("Potato",100);
- Vegetable carrot = new Vegetable("Carrot",120);
- Vegetable lettuce = new Vegetable("Letuce",80);
- Vegetable broccoli = new Vegetable("Broccoli",50);
- Vegetable cucumber = new Vegetable("Cucumber",15);
- Salad shopska = new Salad("Shopska");
- Salad potatoSalad = new Salad("PotatoSalad");
- Salad cucumberSalad = new Salad("CucumberSalad");
- Restaurant kodGazdeta = new Restaurant("Gazdeta");
- shopska.add(cucumber);
- shopska.add(carrot);
- shopska.add(lettuce);
- potatoSalad.add(potato);
- potatoSalad.add(carrot);
- potatoSalad.add(broccoli);
- System.out.println(shopska.getTotalCalories());
- System.out.println(shopska.getProductCount());
- //System.out.println(shopska.toString());
- System.out.println(potatoSalad.toString());
- kodGazdeta.add(potatoSalad);
- kodGazdeta.add(shopska);
- kodGazdeta.add(cucumberSalad);
- kodGazdeta.buy("CucumberSalad");
- System.out.println();
- System.out.println(kodGazdeta.getHealthiestSalad());
- System.out.println(kodGazdeta.generateMenu());
- }
- }
- package healthyHeaven;
- public class Vegetable {
- private String name;
- private int calories;
- public Vegetable(String name,int calories){
- this.name = name;
- this.calories = calories;
- }
- public String getName(){
- return this.name;
- }
- public int getCalories(){
- return this.calories;
- }
- @Override
- public String toString() {
- return String.format(" - %s have %d calories",this.getName(),this.getCalories());
- }
- }
- package healthyHeaven;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Salad {
- private Map<String, Vegetable> products;
- private String name;
- public Salad(String name) {
- this.products = new LinkedHashMap<>();
- this.name = name;
- }
- public String getName() {
- return this.name;
- }
- public void add(Vegetable vegetable) {
- this.products.putIfAbsent(vegetable.getName(), vegetable);
- }
- public int getTotalCalories() {
- int sum = 0;
- for (Vegetable vegetable : this.products.values()) {
- sum += vegetable.getCalories();
- }
- return sum;
- }
- public int getProductCount() {
- return this.products.size();
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(String.format("* Salad %s is %d calories and have %d products:%n",
- this.getName(),
- this.getTotalCalories(),
- this.getProductCount()
- ));
- for (Vegetable vegetable : this.products.values()) {
- sb.append(vegetable.toString()).append(System.lineSeparator());
- }
- return sb.toString().trim();
- }
- }
- package healthyHeaven;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Restaurant {
- private Map<String, Salad> data;
- private String name;
- public Restaurant(String name) {
- this.data = new LinkedHashMap<>();
- this.name = name;
- }
- public String getName() {
- return this.name;
- }
- public void add(Salad salad) {
- this.data.putIfAbsent(salad.getName(), salad);
- }
- public boolean buy(String name) {
- if (this.data.containsKey(name)) {
- this.data.remove(name);
- return true;
- }
- return false;
- }
- public Salad getHealthiestSalad() {
- Salad healthiestSalad = null;
- int calories = Integer.MAX_VALUE;
- for (Salad salad : this.data.values()) {
- int currentCalories = salad.getTotalCalories();
- if (currentCalories < calories) {
- calories = currentCalories;
- healthiestSalad = salad;
- }
- }
- return healthiestSalad;
- }
- public String generateMenu() {
- StringBuilder sb = new StringBuilder();
- sb.append(String.format("%s have %d salads:%n",
- this.getName(),
- this.data.size()
- ));
- for (Salad salad : this.data.values()) {
- sb.append(salad.toString()).append(System.lineSeparator());
- }
- return sb.toString().trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement