Wątek przeniesiony 2019-09-05 15:57 z przez Shalom.

Pomocy !!

2019-09-05 15:41
0

Witam. Mam problem z moim programem. Chcialbym aby za pomoca scanner byla mozliwosc stwozenia Listy przedmiotow a nastepnie wyswietlenu ja na ekranie. Do tego zadanai uzywa hash map lecz mam problemy z wyswitleniem powstalej listy bardzo porsze o pomoc.. Program ma na celu twozenie list i oblicznie ich porfitu jaki otrzymamy po wytworzeniu.

import java.util.Locale;
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;
        }
}
Zmień temat - tu wszyscy pytający chcą jakiejś pomocy - AnyKtokolwiek 2019-09-05 15:50

Pozostało 580 znaków

2019-09-05 15:50
0

Jaki masz dokładnie problem? Nie wiesz jak wyświetlić zawartość mapy? Czy dostajesz jakiś błąd?
https://www.samouczekprogrami[...]ku-java/#iterowanie-po-mapach <- zajrzyj tutaj

Garusx napisał(a):
@Override
   public int compareTo(Components o) {
            if(this == o) {
                return 0;
            }

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

           throw new NullPointerException();
       }

??

edytowany 1x, ostatnio: kixe52, 2019-09-05 15:50

Pozostało 580 znaków

Liczba odpowiedzi na stronę

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