Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Apteka {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int N = Integer.parseInt(input.nextLine());
- CBHT<MedicaneKeyGenerator, Medicine> table = new CBHT(100);
- for(int i = 0; i<N; i++){
- String line = input.nextLine();
- String[] parts = line.split(" ");
- Medicine newMedicine = new Medicine(parts[0], Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]));
- MedicaneKeyGenerator keyGenerator = new MedicaneKeyGenerator(parts[0]);
- table.insert(keyGenerator, newMedicine);
- }
- while(true){
- String line = input.nextLine();
- if(line.equals("KRAJ"))
- return;
- String line2 = input.nextLine();
- if(line.equals("KRAJ")){
- return;
- }
- else{
- MedicaneKeyGenerator keyToSearchFor = new MedicaneKeyGenerator(line);
- if(table.search(keyToSearchFor) != null){
- if(table.search(keyToSearchFor).element.value.quantity >= Integer.parseInt(line2)){
- System.out.println(table.search(keyToSearchFor).element.value.toString());
- System.out.println("Napravena naracka");
- table.search(keyToSearchFor).element.value.quantity -= Integer.parseInt(line2); // print the actual status, than change it... that's why this is after printing
- }
- else{
- System.out.println(table.search(keyToSearchFor).element.value.toString());
- System.out.println("Nema dovolno lekovi");
- }
- }
- else{
- System.out.println("Nema takov lek");
- }
- }
- }
- }
- }
- class Medicine{
- String name;
- int onPositiveList;
- int price;
- int quantity;
- public Medicine(String name, int onPositiveList, int price, int quantity) {
- super();
- this.name = name;
- this.onPositiveList = onPositiveList;
- this.price = price;
- this.quantity = quantity;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(this.name.toUpperCase() + "\n");
- if(onPositiveList == 0){
- sb.append("NEG" + "\n");
- }
- else{
- sb.append("POZ" + "\n");
- }
- sb.append(this.price + "\n");
- sb.append(this.quantity);
- return sb.toString();
- }
- }
- class MedicaneKeyGenerator implements Comparable<MedicaneKeyGenerator>{
- String name;
- public MedicaneKeyGenerator(String name){
- this.name = name;
- }
- @Override
- public int compareTo(MedicaneKeyGenerator other) {
- if(name.equalsIgnoreCase(other.name))
- return 0;
- else
- return -1;
- }
- 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;
- }
- @Override
- public String toString() {
- return "Key: " + this.hashCode();
- }
- @Override
- public boolean equals(Object obj) {
- if (! obj.getClass().equals(this.getClass())){
- return false;
- }
- else{
- MedicaneKeyGenerator other = (MedicaneKeyGenerator) obj;
- return this.name.equalsIgnoreCase(other.name);
- }
- }
- }
- class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
- 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();
- }
- }
- 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;
- }
- }
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment