DamSi

Untitled

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