Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Hashtable;
- import java.util.Scanner;
- enum posNeg{
- NEG,
- POZ;
- public static posNeg fromInteger(int x){
- switch (x){
- case 0:
- return NEG;
- case 1:
- return POZ;
- }
- return null;
- }
- }
- class Product {
- private final String name;
- private final posNeg status;
- private final int price;
- private final int stock;
- public Product(String name, int status, int price, int stock) {
- this.name = name;
- this.status = posNeg.fromInteger(status);
- this.price = price;
- this.stock = stock;
- }
- public String toString(){
- return String.format("%s\n%s\n%d\n%d\n", this.name, this.status, this.price, this.stock);
- }
- public int getStock(){
- return this.stock;
- }
- public String getName(){
- return this.name;
- }
- public int getStatus() {
- return this.status == posNeg.NEG ? 0:1;
- }
- public int getPrice() {
- return price;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof Product)) return false;
- Product product = (Product) o;
- if (price != product.price) return false;
- if (stock != product.stock) return false;
- if (name != null ? !name.equals(product.name) : product.name != null) return false;
- return status == product.status;
- }
- @Override
- public int hashCode() {
- int value = 0;
- for (int i = 0; i < this.name.length(); i++) {
- value += 29 * i + this.name.charAt(i);
- }
- return (29 * (29 * value)) % 102780;
- }
- }
- class DrugStore{
- public final ArrayList<Product> products;
- DrugStore() {
- this.products = new ArrayList<>();
- }
- public int getOptimalSize(int x){
- return (int)(x / 0.75);
- }
- public void handleProductsAndCustomers(InputStream inputStream){
- Scanner scanner = new Scanner(inputStream);
- int numDrugs = scanner.nextInt();
- scanner.nextLine();
- Hashtable<String, Product> drugStoreTable = new Hashtable<>(getOptimalSize(numDrugs));
- for (int i = 0; i < numDrugs; i++) {
- String line = scanner.nextLine();
- String[] parts = line.split(" ");
- products.add(new Product(parts[0].toUpperCase(), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3])));
- drugStoreTable.put(parts[0].toUpperCase(), products.get(i));
- }
- handleCustomers(scanner, drugStoreTable);
- }
- private void handleCustomers(Scanner scanner, Hashtable<String, Product> drugStoreTable) {
- String line = scanner.nextLine();
- while(!line.equals("KRAJ")) {
- int amountAskedFor = scanner.nextInt();
- scanner.nextLine(); //Consume line
- if (!drugStoreTable.containsKey(line.toUpperCase())) {
- System.out.println("Nema takov lek");
- }
- else {
- Product product = drugStoreTable.get(line.toUpperCase());
- if (amountAskedFor > product.getStock()) {
- System.out.println(product + "Nema dovolno lekovi");
- }
- //Update product. . .
- else {
- updateProduct(drugStoreTable, line, amountAskedFor, product);
- }
- }
- line = scanner.nextLine();
- }
- }
- private void updateProduct(Hashtable<String, Product> drugStoreTable, String line, int amountAskedFor, Product product) {
- Product updated = new Product(product.getName(), product.getStatus(), product.getPrice(), product.getStock() - amountAskedFor);
- drugStoreTable.remove(line.toUpperCase());
- drugStoreTable.put(product.getName().toUpperCase(), updated);
- System.out.println(product + "Napravena naracka");
- }
- }
- public class Apteka {
- //Runner
- public static void main(String[] args) {
- DrugStore drugStore = new DrugStore();
- drugStore.handleProductsAndCustomers(System.in);
- }
- }
Add Comment
Please, Sign In to add comment