prometheus800

Аптека АПС

Dec 20th, 2020
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.ArrayList;
  3. import java.util.Hashtable;
  4. import java.util.Scanner;
  5.  
  6. enum posNeg{
  7.     NEG,
  8.     POZ;
  9.  
  10.     public static posNeg fromInteger(int x){
  11.         switch (x){
  12.             case 0:
  13.                 return NEG;
  14.             case 1:
  15.                 return POZ;
  16.         }
  17.         return null;
  18.     }
  19. }
  20.  
  21. class Product {
  22.     private final String name;
  23.     private final posNeg status;
  24.     private final int price;
  25.     private final int stock;
  26.  
  27.     public Product(String name, int status, int price, int stock) {
  28.         this.name = name;
  29.         this.status = posNeg.fromInteger(status);
  30.         this.price = price;
  31.         this.stock = stock;
  32.     }
  33.  
  34.     public String toString(){
  35.         return String.format("%s\n%s\n%d\n%d\n", this.name, this.status, this.price, this.stock);
  36.     }
  37.  
  38.     public int getStock(){
  39.         return this.stock;
  40.     }
  41.  
  42.     public String getName(){
  43.         return this.name;
  44.     }
  45.  
  46.     public int getStatus() {
  47.         return this.status == posNeg.NEG ? 0:1;
  48.     }
  49.  
  50.     public int getPrice() {
  51.         return price;
  52.     }
  53.  
  54.     @Override
  55.     public boolean equals(Object o) {
  56.         if (this == o) return true;
  57.         if (!(o instanceof Product)) return false;
  58.  
  59.         Product product = (Product) o;
  60.  
  61.         if (price != product.price) return false;
  62.         if (stock != product.stock) return false;
  63.         if (name != null ? !name.equals(product.name) : product.name != null) return false;
  64.         return status == product.status;
  65.     }
  66.  
  67.     @Override
  68.     public int hashCode() {
  69.         int value = 0;
  70.         for (int i = 0; i < this.name.length(); i++) {
  71.             value += 29 * i + this.name.charAt(i);
  72.         }
  73.         return (29 * (29 * value)) % 102780;
  74.     }
  75. }
  76.  
  77. class DrugStore{
  78.     public final ArrayList<Product> products;
  79.  
  80.     DrugStore() {
  81.         this.products = new ArrayList<>();
  82.     }
  83.  
  84.     public int getOptimalSize(int x){
  85.         return (int)(x / 0.75);
  86.     }
  87.  
  88.     public void handleProductsAndCustomers(InputStream inputStream){
  89.         Scanner scanner = new Scanner(inputStream);
  90.         int numDrugs = scanner.nextInt();
  91.         scanner.nextLine();
  92.  
  93.         Hashtable<String, Product> drugStoreTable = new Hashtable<>(getOptimalSize(numDrugs));
  94.  
  95.         for (int i = 0; i < numDrugs; i++) {
  96.             String line = scanner.nextLine();
  97.             String[] parts = line.split(" ");
  98.             products.add(new Product(parts[0].toUpperCase(), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3])));
  99.             drugStoreTable.put(parts[0].toUpperCase(), products.get(i));
  100.         }
  101.  
  102.         handleCustomers(scanner, drugStoreTable);
  103.     }
  104.  
  105.     private void handleCustomers(Scanner scanner, Hashtable<String, Product> drugStoreTable) {
  106.         String line = scanner.nextLine();
  107.         while(!line.equals("KRAJ")) {
  108.             int amountAskedFor = scanner.nextInt();
  109.             scanner.nextLine(); //Consume line
  110.             if (!drugStoreTable.containsKey(line.toUpperCase())) {
  111.                 System.out.println("Nema takov lek");
  112.             }
  113.             else {
  114.                 Product product = drugStoreTable.get(line.toUpperCase());
  115.                 if (amountAskedFor > product.getStock()) {
  116.                     System.out.println(product + "Nema dovolno lekovi");
  117.                 }
  118.                 //Update product. . .
  119.                 else {
  120.                     updateProduct(drugStoreTable, line, amountAskedFor, product);
  121.                 }
  122.             }
  123.             line = scanner.nextLine();
  124.         }
  125.     }
  126.  
  127.     private void updateProduct(Hashtable<String, Product> drugStoreTable, String line, int amountAskedFor, Product product) {
  128.         Product updated = new Product(product.getName(), product.getStatus(), product.getPrice(), product.getStock() - amountAskedFor);
  129.         drugStoreTable.remove(line.toUpperCase());
  130.         drugStoreTable.put(product.getName().toUpperCase(), updated);
  131.         System.out.println(product + "Napravena naracka");
  132.     }
  133. }
  134.  
  135. public class Apteka {
  136.     //Runner
  137.     public static void main(String[] args) {
  138.         DrugStore drugStore = new DrugStore();
  139.         drugStore.handleProductsAndCustomers(System.in);
  140.     }
  141. }
Add Comment
Please, Sign In to add comment