Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Key {
- private String name;
- public Key (String name) {
- this.name = name;
- }
- public boolean equals(Object obj) {
- if(obj==null||this.getClass()!=obj.getClass())
- return false;
- Key key=(Key)obj;
- return name.equals(key.name);
- }
- public int hashCode() {
- int sum = 0;
- for (int i = 0; i < name.length(); i++)
- sum += (int) name.charAt(i);
- return ((29 * (29 * (29 + sum))) % 102780);
- }
- }
- class Lek {
- private String name;
- private String type;
- private int price;
- private int n;
- public Lek (String name, int type, int price, int n) {
- this.name = name;
- if(type == 0)
- this.type = "NEG";
- else
- this.type = "POZ";
- this.price = price;
- this.n = n;
- }
- public int shop (int amount) {
- if(amount <= n) {
- n -= amount;
- return n + amount;
- }
- else return -1;
- }
- public String toStringHave () {
- return String.format("%s\n%s\n%d",name,type,price);
- }
- public String toString () {
- return String.format("%s\n%s\n%d\n%d",name,type,price,n);
- }
- }
- public class Apteka {
- public static void main (String [] args) {
- Scanner sc = new Scanner (System.in);
- HashMap <Key,Lek> map = new HashMap <> ();
- int N = Integer.parseInt(sc.nextLine());
- for(int i=0; i<N; i++) {
- String [] parts = sc.nextLine().split("\\s++");
- Lek l = new Lek (parts[0].toUpperCase(),Integer.parseInt(parts[1]),
- Integer.parseInt(parts[2]),Integer.parseInt(parts[3]));
- map.put(new Key(parts[0].toUpperCase()),l);
- }
- String line = sc.nextLine();
- while(!line.equals("KRAJ")) {
- int amount = Integer.parseInt(sc.nextLine());
- if(!map.containsKey(new Key(line.toUpperCase())))
- System.out.println("Nema takov lek");
- else {
- Lek l = map.get(new Key(line.toUpperCase()));
- int tester = l.shop(amount);
- if(tester == -1)
- System.out.println(l.toString() + "\n" + "Nema dovolno lekovi");
- else
- System.out.println(l.toStringHave() + "\n" + tester + "\n" + "Napravena naracka");
- }
- line = sc.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment