Advertisement
IvaAnd

Ex04_Orders

Jul 15th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Ex04_Orders {
  6.  
  7. static class Products {
  8. private String currentProduct;
  9. private double price;
  10. private int quantity;
  11.  
  12.  
  13. public Products(String currentProduct , double price, int quantity) {
  14. this.currentProduct = currentProduct;
  15. this.price = price;
  16. this.quantity = quantity;
  17. }
  18.  
  19. public void setProduct(String product) {
  20. this.currentProduct = product;
  21. }
  22.  
  23. public void setPrice(double price) {
  24. this.price = price;
  25. }
  26.  
  27. public void setQuantity(int quantity) {
  28. this.quantity = quantity;
  29. }
  30.  
  31. public String getProduct() {
  32. return currentProduct;
  33. }
  34.  
  35. public double getPrice() {
  36. return price;
  37. }
  38.  
  39. public int getQuantity() {
  40. return quantity;
  41. }
  42.  
  43. // @Override
  44. // public String toString() {
  45. // return String.format("%s -> %.2f", product, price * quantity);
  46. // }
  47. }
  48.  
  49.  
  50. public static void main(String[] args) {
  51. Scanner scanner = new Scanner(System.in);
  52.  
  53.  
  54. List<Products> productsList = new ArrayList<>();
  55. String command = scanner.nextLine();
  56. while (!command.equals("buy")) {
  57. String[] productDetails = command.split(" ");
  58.  
  59. String currentProduct = productDetails[0];
  60. double price = Double.parseDouble(productDetails[1]);
  61. int quantity = Integer.parseInt(productDetails[2]);
  62.  
  63. Products product = new Products(currentProduct, price, quantity);
  64. if (!productsList.contains(currentProduct)){
  65. productsList.add(product);
  66. }
  67. else if (productsList.contains(currentProduct)) {
  68. int index = productsList.indexOf(currentProduct);
  69.  
  70. // тук искам да взема съществуващото количество на продукта, на този индекс
  71. // да си добавя новото количество и да направя productsList.set(index, quantity)
  72. quantity = quantity+ productsList.get(index,quantity);
  73. productsList.set(index,quantity);
  74.  
  75. // currentQuantity = currentQuantity+quantity;
  76. // productsList.setQuantity(index,quantity);
  77.  
  78.  
  79. }
  80. command = scanner.nextLine();
  81. }
  82.  
  83. System.out.println(productsList);
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement