Latkoski

Аптека

Jan 10th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;import java.util.Map;
  4.  
  5. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  6.  
  7.     // Each MapEntry object is a pair consisting of a key (a Comparable
  8.     // object) and a value (an arbitrary object).
  9.     K key;
  10.     E value;
  11.  
  12.     public MapEntry (K key, E val) {
  13.         this.key = key;
  14.         this.value = val;
  15.     }
  16.    
  17.     public int compareTo (K that) {
  18.     // Compare this map entry to that map entry.
  19.         @SuppressWarnings("unchecked")
  20.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  21.         return this.key.compareTo(other.key);
  22.     }
  23.  
  24.     public String toString () {
  25.         return "<" + key + "," + value + ">";
  26.     }
  27. }
  28.  
  29. class SLLNode<E> {
  30.     protected E element;
  31.     protected SLLNode<E> succ;
  32.  
  33.     public SLLNode(E elem, SLLNode<E> succ) {
  34.         this.element = elem;
  35.         this.succ = succ;
  36.     }
  37.  
  38.     @Override
  39.     public String toString() {
  40.         return element.toString();
  41.     }
  42. }
  43.  
  44.  
  45. class CBHT<K extends Comparable<K>, E> {
  46.  
  47.     // An object of class CBHT is a closed-bucket hash table, containing
  48.     // entries of class MapEntry.
  49.     private SLLNode<MapEntry<K,E>>[] buckets;
  50.  
  51.     @SuppressWarnings("unchecked")
  52.     public CBHT(int m) {
  53.         // Construct an empty CBHT with m buckets.
  54.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  55.     }
  56.  
  57.     private int hash(K key) {
  58.         // Translate key to an index of the array buckets.
  59.     //return Math.abs(key.hashCode()) % buckets.length;
  60.     String s = (String)key;
  61.     char c1 = s.charAt(0);//prvata bukva od lekot
  62.     char c2 = s.charAt(1);//vtora
  63.     char c3 = s.charAt(2);//treta
  64.     char ASCIIc1 = c1;//gi stavame vo ASCII za hash funkcijata
  65.     char ASCIIc2 = c2;
  66.     char ASCIIc3 = c3;
  67.    
  68.     return ((29*(29*(29*0 + ASCIIc1) + ASCIIc2) + ASCIIc3)%102780) % buckets.length;
  69.     }
  70.  
  71.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  72.         // Find which if any node of this CBHT contains an entry whose key is
  73.         // equal
  74.         // to targetKey. Return a link to that node (or null if there is none).
  75.         int b = hash(targetKey);
  76.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  77.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  78.                 return curr;
  79.         }
  80.         return null;
  81.     }
  82.  
  83.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  84.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  85.         int b = hash(key);
  86.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  87.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  88.                 // Make newEntry replace the existing entry ...
  89.                 curr.element = newEntry;
  90.                 return;
  91.             }
  92.         }
  93.         // Insert newEntry at the front of the 1WLL in bucket b ...
  94.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  95.     }
  96.  
  97.     public void delete(K key) {
  98.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  99.         int b = hash(key);
  100.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  101.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  102.                 if (pred == null)
  103.                     buckets[b] = curr.succ;
  104.                 else
  105.                     pred.succ = curr.succ;
  106.                 return;
  107.             }
  108.         }
  109.     }
  110.  
  111.     public String toString() {
  112.         String temp = "";
  113.         for (int i = 0; i < buckets.length; i++) {
  114.             temp += i + ":";
  115.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  116.                 temp += curr.element.toString() + " ";
  117.             }
  118.             temp += "\n";
  119.         }
  120.         return temp;
  121.     }
  122.  
  123. }
  124.  
  125. class Lek{
  126.     String ime;
  127.     int lista;
  128.     int cena;
  129.     int parcinja;
  130.    
  131.     public Lek(String ime, int lista, int cena, int parcinja)
  132.     {
  133.         this.ime = ime;
  134.         this.lista = lista;
  135.         this.cena = cena;
  136.         this.parcinja = parcinja;
  137.     }
  138. }
  139.  
  140. public class Apteka {
  141.    
  142.     public static void main(String[] args) throws NumberFormatException, IOException
  143.     {
  144.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  145.         int N = Integer.parseInt(br.readLine());
  146.         CBHT<String,Lek> table = new CBHT<String,Lek>(N);
  147.         for(int i = 0 ; i < N ; i++)//gi vnesvime site lekoj
  148.         {
  149.             String tmp = br.readLine();
  150.             String[]del = tmp.split(" ");
  151.             String imeLek = del[0].toUpperCase();
  152.             int lista = Integer.parseInt(del[1]);
  153.             int cena = Integer.parseInt(del[2]);
  154.             int parcinja = Integer.parseInt(del[3]);
  155.            
  156.             Lek lek = new Lek(imeLek,lista,cena,parcinja);//nov objekt za da go stajme vo hash
  157.             table.insert(imeLek,lek);//imelek->key,lek->value
  158.         }
  159.         System.out.println("");
  160.         String red = br.readLine();//go vnesvime lekot so go sakame
  161.        
  162.         while(!red.equals("KRAJ"))
  163.         {
  164.             int kolicina = Integer.parseInt(br.readLine());
  165.             SLLNode<MapEntry<String,Lek>> node = table.search(red.toUpperCase());
  166.             if(node==null){
  167.                 System.out.println("Nema takov lek");
  168.             red = br.readLine();
  169.             continue;
  170.             }
  171.            
  172.             int zaliha = node.element.value.parcinja;
  173.             if(kolicina > zaliha)
  174.             System.out.println("Nema dovolno lekovi");
  175.             else{
  176.                 System.out.println(node.element.value.ime);
  177.                 if(node.element.value.lista== 1)
  178.                     System.out.println("POZ");//pozitivna ili negativna lista
  179.                 else
  180.                     System.out.println("NEG");
  181.             }
  182.             System.out.println(node.element.value.cena);
  183.             System.out.println(node.element.value.parcinja);
  184.            
  185.             int noviParcinja = zaliha - kolicina;//update na zalihata poradi poracanite parcinja
  186.             node.element.value.parcinja = noviParcinja;
  187.             System.out.println("Napravena naracka");
  188.         }
  189.         red = br.readLine();
  190.     }
  191.  
  192.    
  193. }
Advertisement
Add Comment
Please, Sign In to add comment