Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajneniq;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Orders {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Product> products = new LinkedHashMap<>();
- while (true) {
- String input = scanner.nextLine();
- if (input.equals("buy")) {
- break;
- }
- String[] tokens = input.split(" ");
- String name = tokens[0];
- double price = Double.parseDouble(tokens[1]);
- int quantity = Integer.parseInt(tokens[2]);
- if (!products.containsKey(name)) {
- products.put(name, new Product(price, quantity));
- } else {
- Product existingProduct = products.get(name);
- existingProduct.setPrice(price);
- existingProduct.addQuantity(quantity);
- }
- }
- for (Map.Entry<String, Product> entry : products.entrySet()) {
- String productName = entry.getKey();
- double totalPrice = entry.getValue().getTotalPrice();
- System.out.printf("%s -> %.2f%n", productName, totalPrice);
- }
- }
- }
- class Product {
- private double price;
- private int quantity;
- public Product(double price, int quantity) {
- this.price = price;
- this.quantity = quantity;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public int getQuantity() {
- return quantity;
- }
- public void addQuantity(int quantity) {
- this.quantity += quantity;
- }
- public double getTotalPrice() {
- return price * quantity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement