W jaki sposób wyswietlic mape za pomoca Scanner ?

0

Witam mam problem z moin porgramem. Chcialbym aby uzytkownik za pomoca Scanner wprowadzajac nazwe Recipe mógl sobie ja wyswietlic na ekranie. NIe mam pomyslu jak to zrobic class Recipe ma juz zaprojktowany to string ktory wyswietla i oblicz totalny koszt przedmiotow. Oto kod nad ktorym pracuje. case 2( jest odpowiedzialny za twozenie Recipe ) case 1 za twozenie listy skladnikow tu nie mam porblemu wyswietla sie bez problemu. Prosze o podpowiedz w tej kwestji.

import java.util.Scanner;

public class Main {
   private static ComponentsList componentsList = new ComponentsList();

   public static void main(String[] args) throws Exception {

       Scanner scanner = new Scanner(System.in);
       boolean quit = false;
       System.out.println("Welcome \n"
           + "Choose one of the options.\n"
           + "1 - Add Component to the listz.\n"
           + "2 - Add new Recipe.\n"
           + "3 - Print Components List.\n"
           + "4 - Print Recipe.\n"
           + "0 - Quit");

       while(!quit){
           System.out.println("Chooce an option: ");
           int choice = scanner.nextInt();
           scanner.nextLine();
           switch (choice){
               case 0:
                   quit = true;
                   break;
               case 1:
                       System.out.println("Component Name: ");
                       String name = scanner.next();
                       name = name.toUpperCase(Locale.ENGLISH);
                       System.out.println("Component Price: ");
                       int price = scanner.nextInt();


                       Components temp = new Components(name, price);
                       componentsList.addComponents(temp);
                       break;
               case 2:
                   System.out.println("Recipe name: ");
                   String recipeName = scanner.next();
                   recipeName = recipeName.toUpperCase(Locale.ENGLISH);
                   System.out.println("Number of Components: ");
                   int componentsNumber = scanner.nextInt();
                   Recipe tempOne = new Recipe(recipeName);
                   for(int i = 1; i <= componentsNumber; i++){
                       System.out.println( i + " Component name:");
                       String itemAddedName = scanner.next();
                       itemAddedName = itemAddedName.toUpperCase(Locale.ENGLISH);
                       System.out.println( i  + " number of Component:");
                       int itemAddedQuantity = scanner.nextInt();
                       addItemToRecipe(tempOne, itemAddedName, itemAddedQuantity);
                   }
                   break;
               case 3:
                   System.out.println(componentsList);
                   break;
               case 4:
                   System.out.println("Enter name: ");
                   String recypiToPrint = scanner.next();
                   System.out.println(tempOne);

           }
       }

   }

   public static int addItemToRecipe(Recipe recipe, String componentIn, int quantity) {

       Components component = componentsList.get(componentIn);
       if(component == null) {
           System.out.println(" Ingredient " + componentIn + " not added.");
           return 0;
       }

       if(quantity>0){
           return recipe.addNewRecipe(component,quantity);
       }

       return 0;
   }

   public static void removeItemFromRecipe(Recipe recipe, String ccomponentOut){
       Components component = componentsList.get(ccomponentOut);
       if(component == null) {
           System.out.println(" Ingredient " + ccomponentOut + " not added.");
       }

       recipe.removeItemFromRecipe(component);

   }

   public static void calculateProfit(Recipe recipe, int marketPrice){
       System.out.println("You earn " + recipe.profit(marketPrice));
   }

}
import java.util.Map;
        import java.util.TreeMap;

public class ComponentsList {
    private final Map<String, Components> list;

    public ComponentsList() {
        this.list = new TreeMap<>();
    }

    public Components get(String key) {
        return list.get(key);
    }

    public int addComponents(Components component){
        if(component != null){
            list.put(component.getName(), component);
            return 1;
        }
        return 0;

    }

    public int removeComponent(Components component){
        if(component != null){
            list.remove(component.getName(), component);
            return 1;
        }
        return 0;
    }


    @Override
    public String toString() {
        String s = "\nComponents List\n";
        for (Map.Entry<String, Components> component : list.entrySet()) {
            s = s + component.getKey() + " ingredient price " + component.getValue().getPrice() + "\n";
        }
        return s;
    }
}
public class Components implements Comparable<Components> {

    private final String name;
    private int price;
    private int quantity;

    public Components(String name, int price) {
        this.name = name;
        this.price = price;
        this.quantity = 0;

    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        if(price > 0.0){
            this.price = price;}
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == this) {
            return true;
        }

        if((obj == null) || (obj.getClass() != this.getClass())) {
            return false;
        }

        String objName = ((Components) obj).getName();
        return this.name.equals(objName);
    }

    @Override
    public int compareTo(Components o) {
            if(this == o) {
                return 0;
            }

            if(o != null) {
                return this.name.compareTo(o.getName());
            }

            throw new NullPointerException();
        }


    @Override
    public int hashCode() {
        return this.name.hashCode() + 31;
    }

    @Override
    public String toString() {
        return this.name + " : price " + this.price ;
    }
}
import java.util.Map;
import java.util.TreeMap;

public class Recipe {
    private final String name;
    private final Map<Components, Integer> list;

    public Recipe(String name) {
        this.name = name;
        this.list = new TreeMap<>();
    }

    public int addNewRecipe(Components component, int quantity){
        component.setQuantity(quantity);
        if(quantity > 0){
            int inRecipe = list.getOrDefault(component, 0);
            int newPricw = inRecipe + (component.getPrice() * quantity);


            list.put(component, newPricw);
            return quantity;

            }

        return  0;
    }

    public void removeItemFromRecipe(Components component){
        list.remove(component);
    }

    public int profit(int marketPrice){
        int totalCost = 0;
        for (Map.Entry<Components, Integer> component : list.entrySet()) {
            totalCost += component.getValue();
        }


        return marketPrice - totalCost;

    }

    @Override
    public String toString() {
            String s = name + " contains " + list.size() + ((list.size() == 1) ? " item" : " items") + "\n";
            int totalCost = 0;
            for (Map.Entry<Components, Integer> component : list.entrySet()) {
                s = s + component.getKey() + " in an amount " + component.getKey().getQuantity() + "\n";
                totalCost += component.getValue();
            }

            return s + "Total cost " + totalCost;
        }
}
0

Trafiłeś w sedno. Jak widzisz nawet nie potrafiłam sprecyzować problemu który mam. Czy mógłbyś rozwinąć swoją myśl co dokładnie mylę pomoże mi to w rozwiązaniu tego problemy. I jakie wartości masz na myśli które musze pobrać z b) rozumiem ze to nazwa c) a kolejna? Czy powinienem stworzyć metodę która wyszukuje objęły po nazwie.

0
  1. Dziwne nazewnictwo. Nie nazywaj zmiennych "list" a już na pewno nie w sytuacji gdy to jest mapa.
  2. Jak wyświetlić zawartość mapy? Iterując po niej.
    a) zwykła pętlą: https://www.samouczekprogramisty.pl/kolekcje-w-jezyku-java/#iterowanie-po-mapach
    b) streamami:
yourCustomMap.entrySet().stream().forEach(mapEntry-> System.out.println(mapEntry));

1 użytkowników online, w tym zalogowanych: 0, gości: 1