Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
- // Each MapEntry object is a pair consisting of a key (a Comparable
- // object) and a value (an arbitrary object).
- K key;
- E value;
- public MapEntry(K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo(K that) {
- // Compare this map entry to that map entry.
- @SuppressWarnings("unchecked")
- MapEntry<K, E> other = (MapEntry<K, E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString() {
- return String.format("KEY: %s, VALUE: %s", key, value);
- }
- }
- interface DoublyHashable<K> extends Comparable<K> {
- public int hashCode();
- // Return the (raw) hash code to use for this key.
- public int stepCode();
- // Return the (raw) step size to use for this key.
- }
- class DoublyHashedOBHT<K extends DoublyHashable<K>, E> {
- // An object of class DoublyHashedOBHT is an open-bucket hash table,
- // containing entries of class MapEntry.
- private MapEntry<K, E>[] buckets;
- // buckets[b] is null if bucket b has never been occupied.
- // buckets[b] is former if bucket b is formerly-occupied
- // by an entry that has since been deleted (and not yet replaced).
- static final int NONE = -1; // ... distinct from any bucket index.
- @SuppressWarnings({"rawtypes", "unchecked"})
- private static final MapEntry former = new MapEntry(null, null);
- // This guarantees that, for any genuine entry e,
- // e.key.equals(former.key) returns false.
- private int occupancy = 0;
- // ... number of occupied or formerly-occupied buckets in this OBHT.
- @SuppressWarnings("unchecked")
- public DoublyHashedOBHT(int m) {
- // Construct an empty DoublyHashedOBHT with m buckets.
- buckets = (MapEntry<K, E>[]) new MapEntry[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- private int step(K key) {
- // Translate key to a step size of the array buckets.
- return Math.abs(key.stepCode()) % buckets.length;
- }
- public int search(K targetKey) {
- // Find which if any bucket of this OBHT is occupied by an entry whose key
- // is equal to targetKey. Return the index of that bucket.
- int b = hash(targetKey);
- int s = step(targetKey);
- int n_search = 0;
- for (; ; ) {
- MapEntry<K, E> oldEntry = buckets[b];
- if (oldEntry == null)
- return NONE;
- else if (targetKey.equals(oldEntry.key))
- return b;
- else {
- b = (b + s) % buckets.length;
- n_search++;
- if (n_search == buckets.length)
- return NONE;
- }
- }
- }
- public MapEntry<K, E> getNode(K targetKey) {
- // Find which if any bucket of this OBHT is occupied by an entry whose key
- // is equal to targetKey. Return the index of that bucket.
- int b = hash(targetKey);
- int s = step(targetKey);
- int n_search = 0;
- for (; ; ) {
- MapEntry<K, E> oldEntry = buckets[b];
- if (oldEntry == null)
- return null;
- else if (targetKey.equals(oldEntry.key))
- return oldEntry;
- else {
- b = (b + s) % buckets.length;
- n_search++;
- if (n_search == buckets.length)
- return null;
- }
- }
- }
- public void insert(K key, E val) {
- // Insert the entry <key, val> into this OBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- int s = step(key);
- int n_search = 0;
- for (; ; ) {
- MapEntry<K, E> oldEntry = buckets[b];
- if (oldEntry == null) {
- if (++occupancy == buckets.length) {
- System.out.println("Hash tabelata e polna!!!");
- }
- buckets[b] = newEntry;
- return;
- } else if (oldEntry == former
- || key.equals(oldEntry.key)) {
- buckets[b] = newEntry;
- return;
- } else {
- b = (b + s) % buckets.length;
- n_search++;
- if (n_search == buckets.length)
- return;
- }
- }
- }
- @SuppressWarnings("unchecked")
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal to key from this OBHT.
- int b = hash(key);
- int s = step(key);
- int n_search = 0;
- for (; ; ) {
- MapEntry<K, E> oldEntry = buckets[b];
- if (oldEntry == null)
- return;
- else if (key.equals(oldEntry.key)) {
- buckets[b] = former;
- return;
- } else {
- b = (b + s) % buckets.length;
- n_search++;
- if (n_search == buckets.length)
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- if (buckets[i] == null)
- temp += "\n";
- else if (buckets[i] == former)
- temp += "former\n";
- else
- temp += buckets[i] + "\n";
- }
- return temp;
- }
- public DoublyHashedOBHT<K, E> clone() {
- DoublyHashedOBHT<K, E> copy = new DoublyHashedOBHT<K, E>(buckets.length);
- for (int i = 0; i < buckets.length; i++) {
- MapEntry<K, E> e = buckets[i];
- if (e != null && e != former)
- copy.buckets[i] = new MapEntry<K, E>(e.key, e.value);
- else
- copy.buckets[i] = e;
- }
- return copy;
- }
- }
- class Medicine {
- private String name;
- private int onList;
- private int cost;
- private int amount;
- public Medicine(String name, int onList, int cost, int amount) {
- this.name = name.toUpperCase();
- this.onList = onList;
- this.cost = cost;
- this.amount = amount;
- }
- public String getPositive() {
- if (onList == 1)
- return "POZ";
- else
- return "NEG";
- }
- boolean makeOrder(int n) {
- if (amount >= n) {
- amount -= n;
- return true;
- }
- return false;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getOnList() {
- return onList;
- }
- public void setOnList(int onList) {
- this.onList = onList;
- }
- public int getCost() {
- return cost;
- }
- public void setCost(int cost) {
- this.cost = cost;
- }
- public int getAmount() {
- return amount;
- }
- public void setAmount(int amount) {
- this.amount = amount;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Medicine medicine = (Medicine) o;
- if (onList != medicine.onList) return false;
- if (cost != medicine.cost) return false;
- if (amount != medicine.amount) return false;
- return name != null ? name.equals(medicine.name) : medicine.name == null;
- }
- @Override
- public int hashCode() {
- int result = name != null ? name.hashCode() : 0;
- result = 31 * result + onList;
- result = 31 * result + cost;
- result = 31 * result + amount;
- return result;
- }
- @Override
- public String toString() {
- return "Medicine{" +
- "name='" + name + '\'' +
- ", onList=" + onList +
- ", cost=" + cost +
- ", amount=" + amount +
- '}';
- }
- public String ShittyToString() {
- StringBuffer sb = new StringBuffer();
- sb.append(name + System.lineSeparator());
- sb.append(getPositive() + System.lineSeparator());
- sb.append(cost + System.lineSeparator());
- sb.append(amount);
- return sb.toString();
- }
- }
- class MedicineName implements DoublyHashable<MedicineName> {
- private String letters;
- public MedicineName(String letters) {
- this.letters = letters.toUpperCase();
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- MedicineName that = (MedicineName) o;
- return letters != null ? letters.equals(that.letters) : that.letters == null;
- }
- @Override
- public int hashCode() {
- //return letters != null ? letters.hashCode() : 0;
- return (29 * (29 * (29 * 0 + letters.charAt(0)) + letters.charAt(1)) + letters.charAt(2)) % 102780;
- }
- @Override
- public int stepCode() {
- return (29 * (29 * (29 * 0 + letters.charAt(0)) + letters.charAt(1)) + letters.charAt(2)) % 102780;
- }
- @Override
- public int compareTo(MedicineName that) {
- return this.letters.compareTo(that.letters);
- }
- @Override
- public String toString() {
- return "MedicineName{" +
- "letters='" + letters + '\'' +
- '}';
- }
- }
- public class Apteka {
- public static void main(String args[]) {
- Scanner scanner = new Scanner(System.in);
- int numberOfEntries = scanner.nextInt();
- scanner.nextLine();
- DoublyHashedOBHT<MedicineName, Medicine> table = new DoublyHashedOBHT<>(numberOfEntries);
- for (int i = 0; i < numberOfEntries; i++) {
- String name = scanner.next();
- int onList = scanner.nextInt();
- int cost = scanner.nextInt();
- int amount = scanner.nextInt();
- Medicine medicine = new Medicine(name, onList, cost, amount);
- MedicineName medicineName = new MedicineName(name);
- table.insert(medicineName, medicine);
- scanner.nextLine();
- }
- while (scanner.hasNextLine()) {
- String name = scanner.next();
- if (name.equals("KRAJ")) break;
- scanner.nextLine();
- int clientAmount = scanner.nextInt();
- scanner.nextLine();
- MedicineName medicineName = new MedicineName(name);
- int searchIndex = table.search(medicineName);
- MapEntry<MedicineName, Medicine> searchResult = table.getNode(medicineName);
- /*System.out.println(medicineName);
- System.out.println(searchIndex + " " + searchResult);*/
- if (searchResult!=null){
- System.out.println(searchResult.value.ShittyToString());
- if (searchResult.value.makeOrder(clientAmount))
- System.out.println("Napravena naracka");
- else
- System.out.println("Nema dovolno lekovi");
- } else {
- System.out.println("Nema takov lek");
- }
- }
- /*System.out.println("\n=== Table ===");
- System.out.println(table);*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment