DamSi

Untitled

Dec 27th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.80 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6.  *
  7.  * @author Damjan
  8.  */
  9. public class Apteka {
  10.  
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
  13.         CBHT<GeneratorKlucLek, Lek> pharmacy = new CBHT<>(3750);
  14.         int numMedicines = Integer.parseInt(data.readLine());
  15.         for (int i = 0; i < numMedicines; ++i) {
  16.             String[] entries = data.readLine().split(" ");
  17.             String ime = entries[0];
  18.             int status = Integer.parseInt(entries[1]);
  19.             int cena = Integer.parseInt(entries[2]);
  20.             int kolicina = Integer.parseInt(entries[3]);
  21.             Lek lek = new Lek(ime, status, cena, kolicina);
  22.             GeneratorKlucLek key = new GeneratorKlucLek(entries[0]);
  23.             pharmacy.insert(key, lek);
  24.         }
  25.         while (true) {
  26.             String imeLek = data.readLine();
  27.             if (imeLek.equalsIgnoreCase("KRAJ")) {
  28.                 return;
  29.             }
  30.             String kolicina = data.readLine();
  31.             if (imeLek.equalsIgnoreCase("KRAJ")) {
  32.                 return;
  33.             } else {
  34.                 GeneratorKlucLek key = new GeneratorKlucLek(imeLek);
  35.                 if (pharmacy.search(key) != null) {
  36.                     Lek lek = (pharmacy.search(key).element.value);
  37.                     if (lek.getKolicina() >= Integer.parseInt(kolicina)) {
  38.                         System.out.println(lek.toString());
  39.                         System.out.println("Napravena naracka");
  40.                         lek.kupiLek(Integer.parseInt(kolicina));
  41.                     } else {
  42.                         System.out.println(lek.toString());
  43.                         System.out.println("Nema dovolno lekovi");
  44.                     }
  45.                 } else {
  46.                     System.out.println("Nema takov lek");
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52. }
  53.  
  54. class Lek {
  55.  
  56.     private String ime;
  57.     private int status;
  58.     private int cena;
  59.     private int kolicina;
  60.  
  61.     public Lek(String ime, int status, int cena, int kolicina) {
  62.         this.ime = ime;
  63.         this.status = status;
  64.         this.cena = cena;
  65.         this.kolicina = kolicina;
  66.     }
  67.  
  68.     public String getIme() {
  69.         return ime;
  70.     }
  71.  
  72.     public int getStatus() {
  73.         return status;
  74.     }
  75.  
  76.     public int getCena() {
  77.         return cena;
  78.     }
  79.  
  80.     public int getKolicina() {
  81.         return kolicina;
  82.     }
  83.    
  84.     public int kupiLek(int kolicina) {
  85.         int kupeno;
  86.         if ((kolicina > this.kolicina) || (this.kolicina - kolicina <= 0)) {
  87.             kupeno = this.kolicina;
  88.             this.kolicina -= this.kolicina;
  89.         } else {
  90.             kupeno = kolicina;
  91.             this.kolicina -= kolicina;
  92.         }
  93.         return kupeno;
  94.     }
  95.  
  96.     @Override
  97.     public String toString() {
  98.         StringBuilder output = new StringBuilder();
  99.         output.append(String.format("%s\n", ime.toUpperCase()));
  100.         if (status == 1) {
  101.             output.append("POZ\n");
  102.         } else if (status == 0) {
  103.             output.append("NEG\n");
  104.         } else {
  105.             output.append("NEPOZNAT\n");
  106.         }
  107.         output.append(String.format("%d\n", cena));
  108.         output.append(String.format("%d", kolicina));
  109.  
  110.         return output.toString();
  111.     }
  112.  
  113. }
  114.  
  115. class GeneratorKlucLek implements Comparable<GeneratorKlucLek> {
  116.  
  117.     private String name;
  118.  
  119.     public GeneratorKlucLek(String name) {
  120.         this.name = name;
  121.     }
  122.  
  123.     @Override
  124.     public int compareTo(GeneratorKlucLek o) {
  125.         if (name.equalsIgnoreCase(o.name)) {
  126.             return 0;
  127.         } else {
  128.             return -1;
  129.         }
  130.     }
  131.  
  132.     @Override
  133.     public boolean equals(Object obj) {
  134.         GeneratorKlucLek GenLekObj = (GeneratorKlucLek) obj;
  135.         return this.name.equalsIgnoreCase(GenLekObj.name);
  136.     }
  137.  
  138.     @Override
  139.     public int hashCode() {
  140.         int hash;
  141.         int asciiC1 = (int) (Character.toUpperCase(name.charAt(0)));
  142.         int asciiC2 = (int) (Character.toUpperCase(name.charAt(1)));
  143.         int asciiC3 = (int) (Character.toUpperCase(name.charAt(2)));
  144.         hash = (29 * (29 * (29 * 0 + asciiC1) + asciiC2) + asciiC3) % 102780;
  145.  
  146.         return hash;
  147.     }
  148.  
  149. }
  150.  
  151.  
  152. class CBHT<K extends Comparable<K>, E> {
  153.  
  154.     // An object of class CBHT is a closed-bucket hash table, containing
  155.     // entries of class MapEntry.
  156.     private SLLNode<MapEntry<K, E>>[] buckets;
  157.  
  158.     @SuppressWarnings("unchecked")
  159.     public CBHT(int m) {
  160.         // Construct an empty CBHT with m buckets.
  161.         buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
  162.     }
  163.  
  164.     private int hash(K key) {
  165.         // Translate key to an index of the array buckets.
  166.         return Math.abs(key.hashCode()) % buckets.length;
  167.     }
  168.  
  169.     public SLLNode<MapEntry<K, E>> search(K targetKey) {
  170.         // Find which if any node of this CBHT contains an entry whose key is
  171.         // equal
  172.         // to targetKey. Return a link to that node (or null if there is none).
  173.         int b = hash(targetKey);
  174.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  175.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key)) {
  176.                 return curr;
  177.             }
  178.         }
  179.         return null;
  180.     }
  181.  
  182.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  183.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  184.         int b = hash(key);
  185.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  186.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  187.                 // Make newEntry replace the existing entry ...
  188.                 curr.element = newEntry;
  189.                 return;
  190.             }
  191.         }
  192.         // Insert newEntry at the front of the 1WLL in bucket b ...
  193.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  194.     }
  195.     public void delete(K key) {
  196.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  197.         int b = hash(key);
  198.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  199.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  200.                 if (pred == null) {
  201.                     buckets[b] = curr.succ;
  202.                 } else {
  203.                     pred.succ = curr.succ;
  204.                 }
  205.                 return;
  206.             }
  207.         }
  208.     }
  209.  
  210.     public String toString() {
  211.         String temp = "";
  212.         for (int i = 0; i < buckets.length; i++) {
  213.             temp += i + ":";
  214.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  215.                 temp += curr.element.toString() + " ";
  216.             }
  217.             temp += "\n";
  218.         }
  219.         return temp;
  220.     }
  221.  
  222. }
  223. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  224.  
  225.     // Each MapEntry object is a pair consisting of a key (a Comparable
  226.     // object) and a value (an arbitrary object).
  227.     K key;
  228.     E value;
  229.  
  230.     public MapEntry(K key, E val) {
  231.         this.key = key;
  232.         this.value = val;
  233.     }
  234.  
  235.     public int compareTo(K that) {
  236.         // Compare this map entry to that map entry.
  237.         @SuppressWarnings("unchecked")
  238.         MapEntry<K, E> other = (MapEntry<K, E>) that;
  239.         return this.key.compareTo(other.key);
  240.     }
  241.  
  242.     public String toString() {
  243.         return "<" + key + "," + value + ">";
  244.     }
  245. }
  246. class SLLNode<E> {
  247.     protected E element;
  248.     protected SLLNode<E> succ;
  249.  
  250.     public SLLNode(E elem, SLLNode<E> succ) {
  251.         this.element = elem;
  252.         this.succ = succ;
  253.     }
  254.  
  255.     @Override
  256.     public String toString() {
  257.         return element.toString();
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment