Andziev

Аптека

Nov 23rd, 2016
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Key {
  4.     private String name;
  5.  
  6.     public Key (String name) {
  7.         this.name = name;
  8.     }
  9.     public boolean equals(Object obj) {
  10.         if(obj==null||this.getClass()!=obj.getClass())
  11.             return false;
  12.         Key key=(Key)obj;
  13.         return name.equals(key.name);
  14.     }
  15.     public int hashCode() {
  16.         int sum = 0;
  17.         for (int i = 0; i < name.length(); i++)
  18.             sum += (int) name.charAt(i);
  19.         return ((29 * (29 * (29 + sum))) % 102780);
  20.     }
  21. }
  22. class Lek {
  23.     private String name;
  24.     private String type;
  25.     private int price;
  26.     private int n;
  27.  
  28.     public Lek (String name, int type, int price, int n) {
  29.         this.name = name;
  30.         if(type == 0)
  31.             this.type = "NEG";
  32.         else
  33.             this.type = "POZ";
  34.         this.price = price;
  35.         this.n = n;
  36.     }
  37.     public int shop (int amount) {
  38.         if(amount <= n) {
  39.             n -= amount;
  40.             return n + amount;
  41.         }
  42.         else return -1;
  43.     }
  44.     public String toStringHave () {
  45.         return String.format("%s\n%s\n%d",name,type,price);
  46.     }
  47.     public String toString () {
  48.         return String.format("%s\n%s\n%d\n%d",name,type,price,n);
  49.     }
  50. }
  51.  
  52. public class Apteka {
  53.  
  54.     public static void main (String [] args) {
  55.         Scanner sc = new Scanner (System.in);
  56.         HashMap <Key,Lek> map = new HashMap <> ();
  57.         int N = Integer.parseInt(sc.nextLine());
  58.  
  59.         for(int i=0; i<N; i++) {
  60.             String [] parts = sc.nextLine().split("\\s++");
  61.             Lek l = new Lek (parts[0].toUpperCase(),Integer.parseInt(parts[1]),
  62.                              Integer.parseInt(parts[2]),Integer.parseInt(parts[3]));
  63.             map.put(new Key(parts[0].toUpperCase()),l);
  64.         }
  65.         String line = sc.nextLine();
  66.  
  67.         while(!line.equals("KRAJ")) {
  68.             int amount = Integer.parseInt(sc.nextLine());
  69.             if(!map.containsKey(new Key(line.toUpperCase())))
  70.                 System.out.println("Nema takov lek");
  71.             else {
  72.                 Lek l = map.get(new Key(line.toUpperCase()));
  73.                 int tester = l.shop(amount);
  74.                 if(tester == -1)
  75.                     System.out.println(l.toString() + "\n" + "Nema dovolno lekovi");
  76.                 else
  77.                     System.out.println(l.toStringHave() + "\n" + tester + "\n" + "Napravena naracka");
  78.             }
  79.             line = sc.nextLine();
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment