Filip_Markoski

[ADS] Аптека

Nov 22nd, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.46 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  4.  
  5.     // Each MapEntry object is a pair consisting of a key (a Comparable
  6.     // object) and a value (an arbitrary object).
  7.     K key;
  8.     E value;
  9.  
  10.     public MapEntry(K key, E val) {
  11.         this.key = key;
  12.         this.value = val;
  13.     }
  14.  
  15.     public int compareTo(K that) {
  16.         // Compare this map entry to that map entry.
  17.         @SuppressWarnings("unchecked")
  18.         MapEntry<K, E> other = (MapEntry<K, E>) that;
  19.         return this.key.compareTo(other.key);
  20.     }
  21.  
  22.     public String toString() {
  23.         return String.format("KEY: %s, VALUE: %s", key, value);
  24.     }
  25. }
  26.  
  27. interface DoublyHashable<K> extends Comparable<K> {
  28.  
  29.     public int hashCode();
  30.     // Return the (raw) hash code to use for this key.
  31.  
  32.     public int stepCode();
  33.     // Return the (raw) step size to use for this key.
  34.  
  35. }
  36.  
  37. class DoublyHashedOBHT<K extends DoublyHashable<K>, E> {
  38.  
  39.     // An object of class DoublyHashedOBHT is an open-bucket hash table,
  40.     // containing entries of class MapEntry.
  41.     private MapEntry<K, E>[] buckets;
  42.  
  43.     // buckets[b] is null if bucket b has never been occupied.
  44.     // buckets[b] is former if bucket b is formerly-occupied
  45.     // by an entry that has since been deleted (and not yet replaced).
  46.  
  47.     static final int NONE = -1; // ... distinct from any bucket index.
  48.  
  49.     @SuppressWarnings({"rawtypes", "unchecked"})
  50.     private static final MapEntry former = new MapEntry(null, null);
  51.     // This guarantees that, for any genuine entry e,
  52.     // e.key.equals(former.key) returns false.
  53.  
  54.     private int occupancy = 0;
  55.     // ... number of occupied or formerly-occupied buckets in this OBHT.
  56.  
  57.     @SuppressWarnings("unchecked")
  58.     public DoublyHashedOBHT(int m) {
  59.         // Construct an empty DoublyHashedOBHT with m buckets.
  60.         buckets = (MapEntry<K, E>[]) new MapEntry[m];
  61.     }
  62.  
  63.  
  64.     private int hash(K key) {
  65.         // Translate key to an index of the array buckets.
  66.         return Math.abs(key.hashCode()) % buckets.length;
  67.     }
  68.  
  69.  
  70.     private int step(K key) {
  71.         // Translate key to a step size of the array buckets.
  72.         return Math.abs(key.stepCode()) % buckets.length;
  73.     }
  74.  
  75.  
  76.     public int search(K targetKey) {
  77.         // Find which if any bucket of this OBHT is occupied by an entry whose key
  78.         // is equal to targetKey. Return the index of that bucket.
  79.         int b = hash(targetKey);
  80.         int s = step(targetKey);
  81.         int n_search = 0;
  82.         for (; ; ) {
  83.             MapEntry<K, E> oldEntry = buckets[b];
  84.             if (oldEntry == null)
  85.                 return NONE;
  86.             else if (targetKey.equals(oldEntry.key))
  87.                 return b;
  88.             else {
  89.                 b = (b + s) % buckets.length;
  90.                 n_search++;
  91.                 if (n_search == buckets.length)
  92.                     return NONE;
  93.             }
  94.         }
  95.     }
  96.  
  97.     public MapEntry<K, E> getNode(K targetKey) {
  98.         // Find which if any bucket of this OBHT is occupied by an entry whose key
  99.         // is equal to targetKey. Return the index of that bucket.
  100.         int b = hash(targetKey);
  101.         int s = step(targetKey);
  102.         int n_search = 0;
  103.         for (; ; ) {
  104.             MapEntry<K, E> oldEntry = buckets[b];
  105.             if (oldEntry == null)
  106.                 return null;
  107.             else if (targetKey.equals(oldEntry.key))
  108.                 return oldEntry;
  109.             else {
  110.                 b = (b + s) % buckets.length;
  111.                 n_search++;
  112.                 if (n_search == buckets.length)
  113.                     return null;
  114.             }
  115.         }
  116.     }
  117.  
  118.  
  119.     public void insert(K key, E val) {
  120.         // Insert the entry <key, val> into this OBHT.
  121.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  122.         int b = hash(key);
  123.         int s = step(key);
  124.         int n_search = 0;
  125.         for (; ; ) {
  126.             MapEntry<K, E> oldEntry = buckets[b];
  127.             if (oldEntry == null) {
  128.                 if (++occupancy == buckets.length) {
  129.                     System.out.println("Hash tabelata e polna!!!");
  130.                 }
  131.                 buckets[b] = newEntry;
  132.                 return;
  133.             } else if (oldEntry == former
  134.                     || key.equals(oldEntry.key)) {
  135.                 buckets[b] = newEntry;
  136.                 return;
  137.             } else {
  138.                 b = (b + s) % buckets.length;
  139.                 n_search++;
  140.                 if (n_search == buckets.length)
  141.                     return;
  142.             }
  143.         }
  144.     }
  145.  
  146.  
  147.     @SuppressWarnings("unchecked")
  148.     public void delete(K key) {
  149.         // Delete the entry (if any) whose key is equal to key from this OBHT.
  150.         int b = hash(key);
  151.         int s = step(key);
  152.         int n_search = 0;
  153.         for (; ; ) {
  154.             MapEntry<K, E> oldEntry = buckets[b];
  155.             if (oldEntry == null)
  156.                 return;
  157.             else if (key.equals(oldEntry.key)) {
  158.                 buckets[b] = former;
  159.                 return;
  160.             } else {
  161.                 b = (b + s) % buckets.length;
  162.                 n_search++;
  163.                 if (n_search == buckets.length)
  164.                     return;
  165.             }
  166.         }
  167.     }
  168.  
  169.  
  170.     public String toString() {
  171.         String temp = "";
  172.         for (int i = 0; i < buckets.length; i++) {
  173.             temp += i + ":";
  174.             if (buckets[i] == null)
  175.                 temp += "\n";
  176.             else if (buckets[i] == former)
  177.                 temp += "former\n";
  178.             else
  179.                 temp += buckets[i] + "\n";
  180.         }
  181.         return temp;
  182.     }
  183.  
  184.  
  185.     public DoublyHashedOBHT<K, E> clone() {
  186.         DoublyHashedOBHT<K, E> copy = new DoublyHashedOBHT<K, E>(buckets.length);
  187.         for (int i = 0; i < buckets.length; i++) {
  188.             MapEntry<K, E> e = buckets[i];
  189.             if (e != null && e != former)
  190.                 copy.buckets[i] = new MapEntry<K, E>(e.key, e.value);
  191.             else
  192.                 copy.buckets[i] = e;
  193.         }
  194.         return copy;
  195.     }
  196.  
  197. }
  198.  
  199. class Medicine {
  200.     private String name;
  201.     private int onList;
  202.     private int cost;
  203.     private int amount;
  204.  
  205.     public Medicine(String name, int onList, int cost, int amount) {
  206.         this.name = name.toUpperCase();
  207.         this.onList = onList;
  208.         this.cost = cost;
  209.         this.amount = amount;
  210.     }
  211.  
  212.     public String getPositive() {
  213.         if (onList == 1)
  214.             return "POZ";
  215.         else
  216.             return "NEG";
  217.     }
  218.  
  219.     boolean makeOrder(int n) {
  220.         if (amount >= n) {
  221.             amount -= n;
  222.             return true;
  223.         }
  224.         return false;
  225.     }
  226.  
  227.     public String getName() {
  228.         return name;
  229.     }
  230.  
  231.     public void setName(String name) {
  232.         this.name = name;
  233.     }
  234.  
  235.     public int getOnList() {
  236.         return onList;
  237.     }
  238.  
  239.     public void setOnList(int onList) {
  240.         this.onList = onList;
  241.     }
  242.  
  243.     public int getCost() {
  244.         return cost;
  245.     }
  246.  
  247.     public void setCost(int cost) {
  248.         this.cost = cost;
  249.     }
  250.  
  251.     public int getAmount() {
  252.         return amount;
  253.     }
  254.  
  255.     public void setAmount(int amount) {
  256.         this.amount = amount;
  257.     }
  258.  
  259.     @Override
  260.     public boolean equals(Object o) {
  261.  
  262.         if (this == o) return true;
  263.         if (o == null || getClass() != o.getClass()) return false;
  264.  
  265.         Medicine medicine = (Medicine) o;
  266.  
  267.         if (onList != medicine.onList) return false;
  268.         if (cost != medicine.cost) return false;
  269.         if (amount != medicine.amount) return false;
  270.         return name != null ? name.equals(medicine.name) : medicine.name == null;
  271.     }
  272.  
  273.     @Override
  274.     public int hashCode() {
  275.         int result = name != null ? name.hashCode() : 0;
  276.         result = 31 * result + onList;
  277.         result = 31 * result + cost;
  278.         result = 31 * result + amount;
  279.         return result;
  280.     }
  281.  
  282.     @Override
  283.     public String toString() {
  284.         return "Medicine{" +
  285.                 "name='" + name + '\'' +
  286.                 ", onList=" + onList +
  287.                 ", cost=" + cost +
  288.                 ", amount=" + amount +
  289.                 '}';
  290.     }
  291.  
  292.     public String ShittyToString() {
  293.         StringBuffer sb = new StringBuffer();
  294.         sb.append(name + System.lineSeparator());
  295.         sb.append(getPositive() + System.lineSeparator());
  296.         sb.append(cost + System.lineSeparator());
  297.         sb.append(amount);
  298.         return sb.toString();
  299.     }
  300. }
  301.  
  302. class MedicineName implements DoublyHashable<MedicineName> {
  303.     private String letters;
  304.  
  305.     public MedicineName(String letters) {
  306.         this.letters = letters.toUpperCase();
  307.     }
  308.  
  309.     @Override
  310.     public boolean equals(Object o) {
  311.         if (this == o) return true;
  312.         if (o == null || getClass() != o.getClass()) return false;
  313.  
  314.         MedicineName that = (MedicineName) o;
  315.  
  316.         return letters != null ? letters.equals(that.letters) : that.letters == null;
  317.     }
  318.  
  319.     @Override
  320.     public int hashCode() {
  321.         //return letters != null ? letters.hashCode() : 0;
  322.         return (29 * (29 * (29 * 0 + letters.charAt(0)) + letters.charAt(1)) + letters.charAt(2)) % 102780;
  323.     }
  324.  
  325.     @Override
  326.     public int stepCode() {
  327.         return (29 * (29 * (29 * 0 + letters.charAt(0)) + letters.charAt(1)) + letters.charAt(2)) % 102780;
  328.     }
  329.  
  330.     @Override
  331.     public int compareTo(MedicineName that) {
  332.         return this.letters.compareTo(that.letters);
  333.     }
  334.  
  335.     @Override
  336.     public String toString() {
  337.         return "MedicineName{" +
  338.                 "letters='" + letters + '\'' +
  339.                 '}';
  340.     }
  341. }
  342.  
  343. public class Apteka {
  344.  
  345.     public static void main(String args[]) {
  346.         Scanner scanner = new Scanner(System.in);
  347.  
  348.         int numberOfEntries = scanner.nextInt();
  349.         scanner.nextLine();
  350.         DoublyHashedOBHT<MedicineName, Medicine> table = new DoublyHashedOBHT<>(numberOfEntries);
  351.         for (int i = 0; i < numberOfEntries; i++) {
  352.             String name = scanner.next();
  353.             int onList = scanner.nextInt();
  354.             int cost = scanner.nextInt();
  355.             int amount = scanner.nextInt();
  356.             Medicine medicine = new Medicine(name, onList, cost, amount);
  357.  
  358.             MedicineName medicineName = new MedicineName(name);
  359.  
  360.             table.insert(medicineName, medicine);
  361.  
  362.             scanner.nextLine();
  363.         }
  364.  
  365.         while (scanner.hasNextLine()) {
  366.             String name = scanner.next();
  367.             if (name.equals("KRAJ")) break;
  368.             scanner.nextLine();
  369.             int clientAmount = scanner.nextInt();
  370.             scanner.nextLine();
  371.  
  372.             MedicineName medicineName = new MedicineName(name);
  373.  
  374.             int searchIndex = table.search(medicineName);
  375.             MapEntry<MedicineName, Medicine> searchResult = table.getNode(medicineName);
  376.             /*System.out.println(medicineName);
  377.             System.out.println(searchIndex + " " + searchResult);*/
  378.  
  379.             if (searchResult!=null){
  380.                 System.out.println(searchResult.value.ShittyToString());
  381.                 if (searchResult.value.makeOrder(clientAmount))
  382.                     System.out.println("Napravena naracka");
  383.                 else
  384.                     System.out.println("Nema dovolno lekovi");
  385.             } else {
  386.                 System.out.println("Nema takov lek");
  387.             }
  388.  
  389.         }
  390.         /*System.out.println("\n=== Table ===");
  391.         System.out.println(table);*/
  392.     }
  393. }
Advertisement
Add Comment
Please, Sign In to add comment