Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class InventoryManager {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Item> Items = new ArrayList<>();
- boolean end = false;
- do {
- String input = scanner.nextLine();
- String[] inputArray = input.split(" ");
- if ("end".equalsIgnoreCase(input)) {
- end = true;
- } else if (input.toLowerCase().startsWith("add")) {
- Items.add(createItem(input));
- } else if (input.toLowerCase().startsWith("filter")) {
- if (inputArray[2].equals("type")) {
- System.out.println(filterByType(Items, inputArray[3]));
- } else if (inputArray[3].equals("to")) {
- System.out.println(filterByPriceTo(Items, Double.parseDouble(inputArray[4])));
- } else if (inputArray[5].equals("to")) {
- System.out.println(filterByPriceFromTo(Items, Double.parseDouble(inputArray[4]), Double.parseDouble(inputArray[6])));
- } else if (inputArray[3].equals("from")) {
- System.out.println(filterByPriceFrom(Items, Double.parseDouble(inputArray[4])));
- }
- }
- } while (!end);
- }
- private static Item createItem(String input) {
- String[] inputLine = input.split(" ");
- Item item = new Item(inputLine[1], Double.parseDouble(inputLine[2]), inputLine[3]);
- System.out.printf("OK: Item %s added successfully%n", item.getName());
- return item;
- }
- private static String filterByType(List<Item> itemList, String type) {
- List<Item> myList = itemList.stream()
- .limit(10)
- .filter(item -> item.type.equals(type))
- .sorted(Comparator.comparing(item -> item.name))
- .collect(Collectors.toList());
- StringBuilder builder = new StringBuilder("OK: ");
- for (Item item : myList) {
- builder.append(String.format("%s (%.2f)", item.getName(), item.getPrice()));
- builder.append(", ");
- }
- return builder.substring(0, builder.length() - 2);
- }
- private static String filterByPriceTo(List<Item> itemList, double maxPrice) {
- List<Item> myList = itemList.stream()
- .limit(10)
- .filter(item -> item.price <= (maxPrice))
- .sorted(Comparator.comparing(item -> item.price))
- .collect(Collectors.toList());
- StringBuilder builder = new StringBuilder("OK: ");
- for (Item item : myList) {
- builder.append(String.format("%s (%.2f)", item.getName(), item.getPrice()));
- builder.append(", ");
- }
- return builder.substring(0, builder.length() - 2);
- }
- private static String filterByPriceFrom(List<Item> itemList, double minPrice) {
- List<Item> myList = itemList.stream()
- .limit(10)
- .filter(item -> item.price >= (minPrice))
- .sorted(Comparator.comparing(item -> item.price))
- .collect(Collectors.toList());
- StringBuilder builder = new StringBuilder("OK: ");
- for (Item item : myList) {
- builder.append(String.format("%s (%.2f)", item.getName(), item.getPrice()));
- builder.append(", ");
- }
- return builder.substring(0, builder.length() - 2);
- }
- private static String filterByPriceFromTo(List<Item> itemList, double minPrice, double maxPrice) {
- List<Item> myList = itemList.stream()
- .limit(10)
- .filter(item -> item.price >= minPrice && item.price <= maxPrice)
- .sorted(Comparator.comparing(item -> item.price))
- .collect(Collectors.toList());
- StringBuilder builder = new StringBuilder("OK: ");
- for (Item item : myList) {
- builder.append(String.format("%s (%.2f)", item.getName(), item.getPrice()));
- builder.append(", ");
- }
- return builder.substring(0, builder.length() - 2);
- }
- public static class Item {
- public String name;
- public double price;
- public String type;
- public Item(String name, double price, String type) {
- this.name = name;
- setPrice(price);
- this.type = type;
- }
- public String getName() {
- return name;
- }
- public void setPrice(double price) {
- if (price <= 0) {
- throw new IllegalArgumentException("");
- }
- this.price = price;
- }
- public double getPrice() {
- return price;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement