Advertisement
Guest User

apteka

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