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;
- /**
- *
- * @author Damjan
- */
- public class Apteka {
- public static void main(String[] args) throws IOException {
- BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
- CBHT<GeneratorKlucLek, Lek> pharmacy = new CBHT<>(3750);
- int numMedicines = Integer.parseInt(data.readLine());
- for (int i = 0; i < numMedicines; ++i) {
- String[] entries = data.readLine().split(" ");
- String ime = entries[0];
- int status = Integer.parseInt(entries[1]);
- int cena = Integer.parseInt(entries[2]);
- int kolicina = Integer.parseInt(entries[3]);
- Lek lek = new Lek(ime, status, cena, kolicina);
- GeneratorKlucLek key = new GeneratorKlucLek(entries[0]);
- pharmacy.insert(key, lek);
- }
- while (true) {
- String imeLek = data.readLine();
- if (imeLek.equalsIgnoreCase("KRAJ")) {
- return;
- }
- String kolicina = data.readLine();
- if (imeLek.equalsIgnoreCase("KRAJ")) {
- return;
- } else {
- GeneratorKlucLek key = new GeneratorKlucLek(imeLek);
- if (pharmacy.search(key) != null) {
- Lek lek = (pharmacy.search(key).element.value);
- if (lek.getKolicina() >= Integer.parseInt(kolicina)) {
- System.out.println(lek.toString());
- System.out.println("Napravena naracka");
- lek.kupiLek(Integer.parseInt(kolicina));
- } else {
- System.out.println(lek.toString());
- System.out.println("Nema dovolno lekovi");
- }
- } else {
- System.out.println("Nema takov lek");
- }
- }
- }
- }
- }
- class Lek {
- private String ime;
- private int status;
- private int cena;
- private int kolicina;
- public Lek(String ime, int status, int cena, int kolicina) {
- this.ime = ime;
- this.status = status;
- this.cena = cena;
- this.kolicina = kolicina;
- }
- public String getIme() {
- return ime;
- }
- public int getStatus() {
- return status;
- }
- public int getCena() {
- return cena;
- }
- public int getKolicina() {
- return kolicina;
- }
- public int kupiLek(int kolicina) {
- int kupeno;
- if ((kolicina > this.kolicina) || (this.kolicina - kolicina <= 0)) {
- kupeno = this.kolicina;
- this.kolicina -= this.kolicina;
- } else {
- kupeno = kolicina;
- this.kolicina -= kolicina;
- }
- return kupeno;
- }
- @Override
- public String toString() {
- StringBuilder output = new StringBuilder();
- output.append(String.format("%s\n", ime.toUpperCase()));
- if (status == 1) {
- output.append("POZ\n");
- } else if (status == 0) {
- output.append("NEG\n");
- } else {
- output.append("NEPOZNAT\n");
- }
- output.append(String.format("%d\n", cena));
- output.append(String.format("%d", kolicina));
- return output.toString();
- }
- }
- class GeneratorKlucLek implements Comparable<GeneratorKlucLek> {
- private String name;
- public GeneratorKlucLek(String name) {
- this.name = name;
- }
- @Override
- public int compareTo(GeneratorKlucLek o) {
- if (name.equalsIgnoreCase(o.name)) {
- return 0;
- } else {
- return -1;
- }
- }
- @Override
- public boolean equals(Object obj) {
- GeneratorKlucLek GenLekObj = (GeneratorKlucLek) obj;
- return this.name.equalsIgnoreCase(GenLekObj.name);
- }
- @Override
- public int hashCode() {
- int hash;
- int asciiC1 = (int) (Character.toUpperCase(name.charAt(0)));
- int asciiC2 = (int) (Character.toUpperCase(name.charAt(1)));
- int asciiC3 = (int) (Character.toUpperCase(name.charAt(2)));
- hash = (29 * (29 * (29 * 0 + asciiC1) + asciiC2) + asciiC3) % 102780;
- return hash;
- }
- }
- 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 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 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();
- }
- }
Add Comment
Please, Sign In to add comment