Latkoski

Untitled

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