Advertisement
elvirynwa

healthyHeaven

Jun 22nd, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. package healthyHeaven;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         Vegetable potato = new Vegetable("Potato",100);
  11.         Vegetable carrot = new Vegetable("Carrot",120);
  12.         Vegetable lettuce = new Vegetable("Letuce",80);
  13.         Vegetable broccoli = new Vegetable("Broccoli",50);
  14.         Vegetable cucumber = new Vegetable("Cucumber",15);
  15.  
  16.         Salad shopska = new Salad("Shopska");
  17.         Salad potatoSalad = new Salad("PotatoSalad");
  18.         Salad cucumberSalad = new Salad("CucumberSalad");
  19.  
  20.         Restaurant kodGazdeta = new Restaurant("Gazdeta");
  21.  
  22.         shopska.add(cucumber);
  23.         shopska.add(carrot);
  24.         shopska.add(lettuce);
  25.  
  26.         potatoSalad.add(potato);
  27.         potatoSalad.add(carrot);
  28.         potatoSalad.add(broccoli);
  29.  
  30.         System.out.println(shopska.getTotalCalories());
  31.         System.out.println(shopska.getProductCount());
  32.         //System.out.println(shopska.toString());
  33.         System.out.println(potatoSalad.toString());
  34.  
  35.         kodGazdeta.add(potatoSalad);
  36.         kodGazdeta.add(shopska);
  37.         kodGazdeta.add(cucumberSalad);
  38.  
  39.         kodGazdeta.buy("CucumberSalad");
  40.         System.out.println();
  41.  
  42.         System.out.println(kodGazdeta.getHealthiestSalad());
  43.  
  44.         System.out.println(kodGazdeta.generateMenu());
  45.  
  46.     }
  47. }
  48.  
  49. package healthyHeaven;
  50.  
  51. public class Vegetable {
  52.     private String name;
  53.     private int calories;
  54.  
  55.     public Vegetable(String name,int calories){
  56.         this.name = name;
  57.         this.calories = calories;
  58.     }
  59.  
  60.     public String getName(){
  61.         return this.name;
  62.     }
  63.  
  64.     public int getCalories(){
  65.         return this.calories;
  66.     }
  67.  
  68.     @Override
  69.     public String toString() {
  70.         return String.format(" - %s have %d calories",this.getName(),this.getCalories());
  71.     }
  72. }
  73.  
  74.  
  75. package healthyHeaven;
  76.  
  77. import java.util.LinkedHashMap;
  78. import java.util.Map;
  79.  
  80. public class Salad {
  81.     private Map<String, Vegetable> products;
  82.     private String name;
  83.  
  84.     public Salad(String name) {
  85.         this.products = new LinkedHashMap<>();
  86.         this.name = name;
  87.     }
  88.  
  89.     public String getName() {
  90.         return this.name;
  91.     }
  92.  
  93.     public void add(Vegetable vegetable) {
  94.         this.products.putIfAbsent(vegetable.getName(), vegetable);
  95.     }
  96.  
  97.     public int getTotalCalories() {
  98.         int sum = 0;
  99.         for (Vegetable vegetable : this.products.values()) {
  100.             sum += vegetable.getCalories();
  101.         }
  102.         return sum;
  103.     }
  104.  
  105.     public int getProductCount() {
  106.         return this.products.size();
  107.     }
  108.  
  109.     @Override
  110.     public String toString() {
  111.         StringBuilder sb = new StringBuilder();
  112.  
  113.         sb.append(String.format("* Salad %s is %d calories and have %d products:%n",
  114.                 this.getName(),
  115.                 this.getTotalCalories(),
  116.                 this.getProductCount()
  117.         ));
  118.         for (Vegetable vegetable : this.products.values()) {
  119.             sb.append(vegetable.toString()).append(System.lineSeparator());
  120.         }
  121.         return sb.toString().trim();
  122.     }
  123. }
  124.  
  125.  
  126. package healthyHeaven;
  127.  
  128. import java.util.LinkedHashMap;
  129. import java.util.Map;
  130.  
  131. public class Restaurant {
  132.     private Map<String, Salad> data;
  133.     private String name;
  134.  
  135.     public Restaurant(String name) {
  136.         this.data = new LinkedHashMap<>();
  137.         this.name = name;
  138.     }
  139.  
  140.     public String getName() {
  141.         return this.name;
  142.     }
  143.  
  144.     public void add(Salad salad) {
  145.         this.data.putIfAbsent(salad.getName(), salad);
  146.     }
  147.  
  148.     public boolean buy(String name) {
  149.         if (this.data.containsKey(name)) {
  150.             this.data.remove(name);
  151.             return true;
  152.         }
  153.         return false;
  154.     }
  155.  
  156.     public Salad getHealthiestSalad() {
  157.         Salad healthiestSalad = null;
  158.         int calories = Integer.MAX_VALUE;
  159.  
  160.         for (Salad salad : this.data.values()) {
  161.             int currentCalories = salad.getTotalCalories();
  162.             if (currentCalories < calories) {
  163.                 calories = currentCalories;
  164.                 healthiestSalad = salad;
  165.             }
  166.         }
  167.         return healthiestSalad;
  168.     }
  169.  
  170.     public String generateMenu() {
  171.         StringBuilder sb = new StringBuilder();
  172.  
  173.         sb.append(String.format("%s have %d salads:%n",
  174.                 this.getName(),
  175.                 this.data.size()
  176.         ));
  177.         for (Salad salad : this.data.values()) {
  178.             sb.append(salad.toString()).append(System.lineSeparator());
  179.         }
  180.         return sb.toString().trim();
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement