Advertisement
metalni

APS Labs 6 Apteka [feat Acka]

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