Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- 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 "<" + key + "," + value + ">";
- }
- }
- class CBHT<K extends Comparable<K>, E> {
- // An object of class CBHT is a closed-bucket hash table, containing
- // entries of class MapEntry.
- private SLLNode<MapEntry<K, E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- // Construct an empty CBHT with m buckets.
- buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K, E>> search(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key is
- // equal
- // to targetKey. Return a link to that node (or null if there is none).
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
- return curr;
- }
- return null;
- }
- public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- // Make newEntry replace the existing entry ...
- curr.element = newEntry;
- return;
- }
- }
- // Insert newEntry at the front of the 1WLL in bucket b ...
- buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
- }
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal to key from this CBHT.
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ;
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp += curr.element.toString() + " ";
- }
- temp += "\n";
- }
- return temp;
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class ImeLek implements Comparable<ImeLek> {
- String imeLek;
- // najprvin implementirame konstruktor, pa hashCode pa equals. hashCodot treba
- // sami da go izmenime
- public ImeLek(String imeLek) {
- super();
- this.imeLek = imeLek;
- }
- @Override
- public int hashCode() {
- /* h(w)=(29∗(29∗(29∗0+ASCII(c1))+ASCII(c2))+ASCII(c3))%102780 */
- int result;
- result = (29 * (29 * (29 * 0 + imeLek.charAt(0)) + imeLek.charAt(1)) + imeLek.charAt(2)) % 102780;
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- ImeLek other = (ImeLek) obj;
- if (imeLek == null) {
- if (other.imeLek != null)
- return false;
- } else if (!imeLek.equals(other.imeLek))
- return false;
- return true;
- }
- @Override
- public int compareTo(ImeLek arg0) {
- // TODO Auto-generated method stub
- return imeLek.compareTo(arg0.imeLek);
- }
- }
- class Lek{
- String imeLek;
- int pozneg;
- int cena;
- int kolichina;
- // najprvin implementirame konstruktor, pa hashCode pa equals. hashCodot treba
- // sami da go izmenime
- public Lek(String imeLek, int pozneg, int cena, int kolichina) {
- super();
- this.imeLek = imeLek;
- this.pozneg = pozneg;
- this.cena = cena;
- this.kolichina = kolichina;
- }
- public String toString() {
- String s = new String();
- s += imeLek + "\n";
- if (pozneg == 1) { // s+=(pozneg==1?"poz\n":"neg\n");
- s += "poz\n";
- } else
- s += "neg\n";
- s += cena + "\n";
- s += kolichina;
- return s;
- }
- }
- public class HashApteka {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N, i, pozneg, cena, kolichina;
- String s, imeLek;
- ImeLek il;
- Lek l;
- SLLNode<MapEntry<ImeLek, Lek>> p = null;
- String[] pom;
- s = br.readLine();
- N = Integer.parseInt(s);
- CBHT<ImeLek, Lek> tabela = new CBHT<ImeLek, Lek>(2 * N + 1);
- for (i = 0; i < N; i++) {
- s = br.readLine();
- pom = s.split(" ");
- imeLek = pom[0].toUpperCase();
- pozneg = Integer.parseInt(pom[1]);
- cena = Integer.parseInt(pom[2]);
- kolichina = Integer.parseInt(pom[3]);
- il = new ImeLek(imeLek);
- l = new Lek(imeLek, pozneg, cena, kolichina);
- tabela.insert(il, l);
- }
- s = br.readLine();
- while (!s.equals("KRAJ")) {
- imeLek = s.toUpperCase();
- s = br.readLine();
- kolichina = Integer.parseInt(s);
- il = new ImeLek(imeLek);
- /* il = new ImeLek(imeLek, kolichina, 0, 0); mozhe i vaka ama kolichina ne ja zema
- bidejki imeLek e keyot*/
- p = tabela.search(il);
- if (p == null) {
- System.out.println("Nema takov lek");
- } else {
- Lek najden = p.element.value;
- /*
- * System.out.println(najden.imeLek); if((najden.pozneg)==1) {
- * System.out.println("poz");
- *
- * } else System.out.println("neg"); System.out.println(najden.cena);
- * System.out.println(najden.kolichina);
- */
- System.out.println(najden);
- if (kolichina <= najden.kolichina) {
- najden.kolichina -= kolichina;
- tabela.insert(il, najden);
- System.out.println("Napravena narachka");
- }
- else
- System.out.println("Nema dovolno lekovi");
- }
- s = br.readLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement