Advertisement
Ligh7_of_H3av3n

03. Orders

Mar 1st, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package Uprajneniq;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Orders {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         Map<String, Product> products = new LinkedHashMap<>();
  13.  
  14.         while (true) {
  15.             String input = scanner.nextLine();
  16.             if (input.equals("buy")) {
  17.                 break;
  18.             }
  19.  
  20.             String[] tokens = input.split(" ");
  21.             String name = tokens[0];
  22.             double price = Double.parseDouble(tokens[1]);
  23.             int quantity = Integer.parseInt(tokens[2]);
  24.  
  25.             if (!products.containsKey(name)) {
  26.                 products.put(name, new Product(price, quantity));
  27.             } else {
  28.                 Product existingProduct = products.get(name);
  29.                 existingProduct.setPrice(price);
  30.                 existingProduct.addQuantity(quantity);
  31.             }
  32.         }
  33.  
  34.         for (Map.Entry<String, Product> entry : products.entrySet()) {
  35.             String productName = entry.getKey();
  36.             double totalPrice = entry.getValue().getTotalPrice();
  37.             System.out.printf("%s -> %.2f%n", productName, totalPrice);
  38.         }
  39.     }
  40. }
  41.  
  42. class Product {
  43.     private double price;
  44.     private int quantity;
  45.  
  46.     public Product(double price, int quantity) {
  47.         this.price = price;
  48.         this.quantity = quantity;
  49.     }
  50.  
  51.     public double getPrice() {
  52.         return price;
  53.     }
  54.  
  55.     public void setPrice(double price) {
  56.         this.price = price;
  57.     }
  58.  
  59.     public int getQuantity() {
  60.         return quantity;
  61.     }
  62.  
  63.     public void addQuantity(int quantity) {
  64.         this.quantity += quantity;
  65.     }
  66.  
  67.     public double getTotalPrice() {
  68.         return price * quantity;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement