Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.*;
- public class Orders {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- List<Stock> warehouse = new ArrayList<>();
- String input;
- while (!"buy".equals(input = sc.nextLine())){
- String[] nextStock = input.split("\\s+");
- String name = nextStock[0];
- double price = Double.parseDouble(nextStock[1]);
- int quantity = Integer.parseInt(nextStock[2]);
- if (listContainsProduct(warehouse, name)){
- changeProduct(warehouse, name, price, quantity);
- }
- else {
- warehouse.add(new Stock(name, price, quantity));
- }
- }
- for (Stock o : warehouse){
- System.out.println(o.getName() + " -> " + new DecimalFormat("#.00").format(o.getPrice() * o.getQuantity()));
- }
- }
- static boolean listContainsProduct(List<Stock> stocks, String name){
- for (Stock o : stocks){
- if(o.getName().equals(name))
- return true;
- }
- return false;
- }
- static List<Stock> changeProduct(List<Stock> stocks, String name, double price, int quantity){
- for (Stock o : stocks){
- if (o.getName().equals(name)){
- o.setPrice(price);
- o.setQuantity(o.getQuantity() + quantity);
- }
- }
- return stocks;
- }
- }
- class Stock {
- private String name;
- private double price;
- private int quantity;
- public Stock(String name, double price, int quantity){
- this.name = name;
- this.price = price;
- this.quantity = quantity;
- }
- public void setPrice(double price){
- this.price = price;
- }
- public void setQuantity(int quantity){
- this.quantity = quantity;
- }
- public String getName() {return this.name;}
- public int getQuantity() {return this.quantity;}
- public double getPrice() {return this.price;}
- }
Advertisement
Add Comment
Please, Sign In to add comment