Yargi

Orders

Jul 15th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.*;
  3.  
  4. public class Orders {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         List<Stock> warehouse = new ArrayList<>();
  8.  
  9.         String input;
  10.         while (!"buy".equals(input = sc.nextLine())){
  11.             String[] nextStock = input.split("\\s+");
  12.             String name = nextStock[0];
  13.             double price = Double.parseDouble(nextStock[1]);
  14.             int quantity = Integer.parseInt(nextStock[2]);
  15.  
  16.  
  17.             if (listContainsProduct(warehouse, name)){
  18.                 changeProduct(warehouse, name, price, quantity);
  19.             }
  20.             else {
  21.                 warehouse.add(new Stock(name, price, quantity));
  22.             }
  23.         }
  24.         for (Stock o : warehouse){
  25.             System.out.println(o.getName() + " -> " + new DecimalFormat("#.00").format(o.getPrice() * o.getQuantity()));
  26.         }
  27.     }
  28.  
  29.  
  30.     static boolean listContainsProduct(List<Stock> stocks, String name){
  31.         for (Stock o : stocks){
  32.             if(o.getName().equals(name))
  33.             return true;
  34.         }
  35.         return false;
  36.     }
  37.     static List<Stock> changeProduct(List<Stock> stocks, String name, double price, int quantity){
  38.         for (Stock o : stocks){
  39.             if (o.getName().equals(name)){
  40.                 o.setPrice(price);
  41.                 o.setQuantity(o.getQuantity() + quantity);
  42.             }
  43.         }
  44.         return stocks;
  45.     }
  46. }
  47.  
  48. class Stock {
  49.  
  50.     private String name;
  51.     private double price;
  52.     private int quantity;
  53.  
  54.     public Stock(String name, double price, int quantity){
  55.         this.name = name;
  56.         this.price = price;
  57.         this.quantity = quantity;
  58.     }
  59.  
  60.     public void setPrice(double price){
  61.         this.price = price;
  62.     }
  63.     public void setQuantity(int quantity){
  64.         this.quantity = quantity;
  65.     }
  66.  
  67.     public String getName() {return this.name;}
  68.     public int getQuantity() {return this.quantity;}
  69.     public double getPrice() {return this.price;}
  70. }
Advertisement
Add Comment
Please, Sign In to add comment