diff --git a/lab-01/by/KseniyaGnezdilova/quizer/Main.java b/lab-01/by/KseniyaGnezdilova/quizer/Main.java new file mode 100644 index 0000000..3c61a34 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/Main.java @@ -0,0 +1,61 @@ +package by.KseniyaGnezdilova.quizer; + +import by.KseniyaGnezdilova.quizer.generators.GroupTaskGenerator; +import by.KseniyaGnezdilova.quizer.generators.PoolTaskGenerator; +import by.KseniyaGnezdilova.quizer.tasks.Task; +import by.KseniyaGnezdilova.quizer.tasks.TextTask; +import by.KseniyaGnezdilova.quizer.tasks.math_tasks.EquationTask; +import by.KseniyaGnezdilova.quizer.tasks.math_tasks.ExpressionTask; +import by.KseniyaGnezdilova.quizer.tasks.math_tasks.Operations; + +import java.util.*; + +public class Main { + + static Map getQuizMap(){ + Map quizzes = new HashMap<>(); + + Task.Generator equation = new EquationTask.Generator(0, -20, 20, + EnumSet.of(Operations.SUM, Operations.DIV, Operations.MUL, Operations.DIFF)); + Quiz quiz_equation = new Quiz(equation, 10); + quizzes.put("Equation", quiz_equation); + + Task.Generator expression = new ExpressionTask.Generator(1, -20, 20, + EnumSet.of(Operations.SUM, Operations.DIV, Operations.MUL, Operations.DIFF)); + Quiz quiz_expression = new Quiz(expression, 10); + quizzes.put("Expression", quiz_expression); + + quizzes.put("Notations", new Quiz(new GroupTaskGenerator(equation, expression), 10)); + + Vector textTasks = new Vector<>(List.of( + new TextTask("Galya has 5 apples, Petya has 12 apples. How many apples does Vanya have, " + + "if it is known that he has 2 times more apples than Galya and Petya combined?", "34"), + new TextTask("What is the median of a right triangle if its hypotenuse is C?", "C / 2"), + new TextTask("sin^2(a) + cos^2(a) = ?", "1"), + new TextTask("The programmer's first word?", "Hello, World!")) + ); + + quizzes.put("Text", new Quiz(new PoolTaskGenerator(false, textTasks), textTasks.size())); + + return quizzes; + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Map quizzes = getQuizMap(); + System.out.println("Available quizzes: "); + for (Map.Entry item: quizzes.entrySet()){ + System.out.println(item.getKey()); + } + + System.out.println("Choose a quiz: "); + String quizName = scanner.nextLine(); + Quiz quiz = quizzes.get(quizName); + while (!quiz.isFinished()){ + System.out.println(quiz.nextTask().getText()); + String ans = scanner.nextLine(); + quiz.provideAnswer(ans); + } + System.out.println("Result "); + System.out.println(quiz.getMark()); + } +} \ No newline at end of file diff --git a/lab-01/by/KseniyaGnezdilova/quizer/Quiz.java b/lab-01/by/KseniyaGnezdilova/quizer/Quiz.java new file mode 100644 index 0000000..8331322 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/Quiz.java @@ -0,0 +1,69 @@ +package by.KseniyaGnezdilova.quizer; + +import by.KseniyaGnezdilova.quizer.tasks.Task; + +class Quiz { + + private Task currentTask; + private int taskCount; + + private Task.Generator generator; + private boolean status; + private int correctAnswerNumber; + private int wrongAnswerNumber; + + private int incorrectInputNumber; + + Quiz(Task.Generator generator, int taskCount) { + this.currentTask = null; + this.taskCount = taskCount; + this.generator = generator; + this.status = true; + this.correctAnswerNumber = 0; + this.wrongAnswerNumber = 0; + this.incorrectInputNumber = 0; + } + + Task repeatTask(){ + return currentTask; + } + Task nextTask() { + currentTask = generator.generate(); + return currentTask; + } + + Result provideAnswer(String answer) { + Result result = currentTask.validate(answer); + switch (result){ + case OK -> this.correctAnswerNumber++; + case WRONG -> this.wrongAnswerNumber++; + case INCORRECT_INPUT -> { + this.incorrectInputNumber++; + repeatTask(); + } + } + return result; + } + + boolean isFinished() { + this.status = (this.correctAnswerNumber + this.wrongAnswerNumber == taskCount); + return this.status; + } + + int getTaskCount(){ return taskCount; } + int getCorrectAnswerNumber() { + return correctAnswerNumber; + } + + int getWrongAnswerNumber() { + return wrongAnswerNumber; + } + + int getIncorrectInputNumber() { + return incorrectInputNumber; + } + + double getMark() { + return (double)getCorrectAnswerNumber() / (double)getTaskCount() * 100; + } +} diff --git a/lab-01/by/KseniyaGnezdilova/quizer/Result.java b/lab-01/by/KseniyaGnezdilova/quizer/Result.java new file mode 100644 index 0000000..c2a7f38 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/Result.java @@ -0,0 +1,7 @@ +package by.KseniyaGnezdilova.quizer; + +public enum Result { + OK, + WRONG, + INCORRECT_INPUT +} diff --git a/lab-01/by/KseniyaGnezdilova/quizer/generators/GroupTaskGenerator.java b/lab-01/by/KseniyaGnezdilova/quizer/generators/GroupTaskGenerator.java new file mode 100644 index 0000000..142fbe3 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/generators/GroupTaskGenerator.java @@ -0,0 +1,33 @@ +package by.KseniyaGnezdilova.quizer.generators; + +import by.KseniyaGnezdilova.quizer.tasks.Task; + +import java.util.Collection; +import java.util.Collections; +import java.util.Random; +import java.util.Vector; + +public class GroupTaskGenerator implements Task.Generator { + Vector generators = new Vector<>(); + public GroupTaskGenerator(Task.Generator... generators) { + Collections.addAll(this.generators, generators); + } + + public GroupTaskGenerator(Collection generators) { + this.generators.addAll(generators); + } + + public Task generate() { + Random random = new Random(); + while (!generators.isEmpty()){ + int pos = random.nextInt(this.generators.size()); + try { + return generators.get(pos).generate(); + } catch (Exception e) { + generators.remove(pos); + } + } + throw new IndexOutOfBoundsException("No generators"); + } + +} diff --git a/lab-01/by/KseniyaGnezdilova/quizer/generators/PoolTaskGenerator.java b/lab-01/by/KseniyaGnezdilova/quizer/generators/PoolTaskGenerator.java new file mode 100644 index 0000000..4c86057 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/generators/PoolTaskGenerator.java @@ -0,0 +1,41 @@ +package by.KseniyaGnezdilova.quizer.generators; + +import by.KseniyaGnezdilova.quizer.tasks.Task; + +import java.util.Collection; +import java.util.Collections; +import java.util.Random; +import java.util.Vector; + +public class PoolTaskGenerator implements Task.Generator { + + private Vector tasks = new Vector<>(); + private boolean allowDuplicate; + + public PoolTaskGenerator( + boolean allowDuplicate, + Task... tasks + ) { + this.allowDuplicate = allowDuplicate; + Collections.addAll(this.tasks, tasks); + } + + public PoolTaskGenerator( + boolean allowDuplicate, + Collection tasks + ) { + this.allowDuplicate = allowDuplicate; + this.tasks.addAll(tasks); + } + + public Task generate() { + if (this.tasks.isEmpty()){ + throw new IndexOutOfBoundsException("No more tasks"); + } + Random random = new Random(); + int rnd = random.nextInt(tasks.size()); + Task task = tasks.get(rnd); + if (!allowDuplicate) this.tasks.remove(rnd); + return task; + } +} \ No newline at end of file diff --git a/lab-01/by/KseniyaGnezdilova/quizer/tasks/Task.java b/lab-01/by/KseniyaGnezdilova/quizer/tasks/Task.java new file mode 100644 index 0000000..84abd0d --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/tasks/Task.java @@ -0,0 +1,13 @@ +package by.KseniyaGnezdilova.quizer.tasks; + +import by.KseniyaGnezdilova.quizer.Result; + +public interface Task { + String getText(); + + Result validate(String answer); + + interface Generator { + Task generate(); + } +} diff --git a/lab-01/by/KseniyaGnezdilova/quizer/tasks/TextTask.java b/lab-01/by/KseniyaGnezdilova/quizer/tasks/TextTask.java new file mode 100644 index 0000000..99aabe4 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/tasks/TextTask.java @@ -0,0 +1,32 @@ +package by.KseniyaGnezdilova.quizer.tasks; + +import by.KseniyaGnezdilova.quizer.Result; + +import java.util.Objects; + +public class TextTask implements Task { + private String text; + private String answer; + public TextTask( + String text, + String answer + ) { + this.text = text; + this.answer = answer; + } + + + @Override + public String getText() { + return text; + } + + @Override + public Result validate(String answer) { + if (Objects.equals(this.answer, answer)){ + return Result.OK; + } else { + return Result.WRONG; + } + } +} \ No newline at end of file diff --git a/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/AbstractMathTask.java b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/AbstractMathTask.java new file mode 100644 index 0000000..ec74218 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/AbstractMathTask.java @@ -0,0 +1,32 @@ +package by.KseniyaGnezdilova.quizer.tasks.math_tasks; + +import java.util.EnumSet; + +public abstract class AbstractMathTask implements MathTask { + static abstract class Generator implements MathTask.Generator{ + protected int precision; + protected double minNumber; + protected double maxNumber; + protected EnumSet operations; + + public Generator( + int precision, + double minNumber, + double maxNumber, + EnumSet operations + ) { + this.precision = precision; + this.maxNumber = maxNumber; + this.minNumber = minNumber; + this.operations = operations; + } + + public double getMaxNumber(){ + return maxNumber; + } + + public double getMinNumber(){ + return minNumber; + } + } +} diff --git a/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/EquationTask.java b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/EquationTask.java new file mode 100644 index 0000000..6768f49 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/EquationTask.java @@ -0,0 +1,127 @@ +package by.KseniyaGnezdilova.quizer.tasks.math_tasks; + +import by.KseniyaGnezdilova.quizer.Result; + +import java.util.EnumSet; +import java.util.Objects; +import java.util.Random; +import java.util.Vector; + +public class EquationTask extends AbstractMathTask { + private int precision; + private boolean unknown; + private double firstNum; + private double secondNum; + + private double thirdNum; + private String operation; + private double ans; + + public EquationTask(boolean unknown, double firstNum, double secondNum, double thirdNum, String operation, int precision) { + this.firstNum = firstNum; + this.secondNum = secondNum; + this.thirdNum = thirdNum; + this.operation = operation; + this.ans = 0; + this.unknown = unknown; + this.precision = precision; + } + + public EquationTask() { + this.firstNum = 0; + this.secondNum = 0; + this.thirdNum = 0; + this.operation = null; + this.ans = 0; + this.unknown = false; + this.precision = 0; + } + + @Override + public String getText() { + return (this.unknown ? "X " + this.operation + " " + this.secondNum + " = " + this.thirdNum : + this.firstNum + " " + this.operation + " X = " + this.thirdNum ); + } + + @Override + public Result validate(String answer) { + switch (this.operation){ + case "+" -> { + if (this.unknown) { + this.ans = this.thirdNum - this.secondNum; + } else { + this.ans = this.thirdNum - this.firstNum; + } + } + case "-" -> { + if (this.unknown){ + this.ans = this.secondNum + this.thirdNum; + } else { + this.ans = this.firstNum - this.thirdNum; + } + } + case "*" -> { + if (this.unknown) { + this.ans = this.thirdNum / this.secondNum; + } else { + this.ans = this.thirdNum / this.firstNum; + } + } + + case "/" -> { + if (this.unknown) { + this.ans = this.thirdNum * this.secondNum; + } else { + this.ans = this.firstNum / this.thirdNum; + } + } + } + return (Math.abs(Double.parseDouble(answer) - this.ans) < Math.pow(0.1, 2 * precision)? Result.OK : Result.WRONG); + } + + public static class Generator extends AbstractMathTask.Generator { + public Generator( + int precision, + double minNumber, + double maxNumber, + EnumSet operations + ) { + super( precision, + minNumber, + maxNumber, + operations + ); + + } + + public EquationTask generate() { + Random random = new Random(); + double firstNum = random.nextDouble( getDiffNumber() + 1) + this.minNumber; + double secondNum = random.nextDouble(getDiffNumber() + 1) + this.minNumber; + firstNum = Math.round(firstNum * Math.pow(10, precision)) / Math.pow(10, precision); + secondNum = Math.round(secondNum * Math.pow(10, precision)) / Math.pow(10, precision); + double thirdNum = 0; + Vector operations_ = new Vector<>(); + for (var operation: operations){ + operations_.add(operation.name()); + } + int pos = random.nextInt(operations_.size()); + EquationTask equationTask = new EquationTask(); + boolean unknown = random.nextBoolean(); + String operator = operations_.get(pos); + if (!unknown && secondNum == 0 && Objects.equals(operator, "DIV")) { + generate(); + } else { + switch (operator) { + case "SUM" -> {operator = "+"; thirdNum = firstNum + secondNum;} + case "DIFF" -> {operator = "-"; thirdNum = firstNum - secondNum;} + case "MUL" -> {operator = "*"; thirdNum = firstNum * secondNum;} + case "DIV" -> {operator = "/"; thirdNum = firstNum / secondNum;} + } + } + thirdNum = Math.round(firstNum * Math.pow(10, 2 * precision)) / Math.pow(10, 2 * precision); + equationTask = new EquationTask(unknown, firstNum, secondNum, thirdNum, operator, precision); + return equationTask; + } + } +} \ No newline at end of file diff --git a/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/ExpressionTask.java b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/ExpressionTask.java new file mode 100644 index 0000000..778dc37 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/ExpressionTask.java @@ -0,0 +1,90 @@ +package by.KseniyaGnezdilova.quizer.tasks.math_tasks; + +import by.KseniyaGnezdilova.quizer.Result; + +import javax.swing.*; +import java.util.EnumSet; +import java.util.Objects; +import java.util.Random; +import java.util.Vector; + +public class ExpressionTask extends AbstractMathTask { + + private double firstNum; + private double secondNum; + private double ans; + private String operation; + + private int precision; + public ExpressionTask(){ + this.ans = 0; + this.firstNum = 0; + this.secondNum = 0; + this.operation = null; + this.precision = 1; + } + public ExpressionTask(double firstNum, double secondNum, String operation, int precision) { + this.ans = 0; + this.firstNum = firstNum; + this.secondNum = secondNum; + this.operation = operation; + this.precision = precision; + } + + @Override + public String getText() { + return Double.toString(firstNum) + " " + operation + " " + Double.toString(secondNum) + " = ?"; + } + + @Override + public Result validate(String answer) { + switch (this.operation) { + case "+" -> this.ans = this.firstNum + this.secondNum; + case "-" -> this.ans = this.firstNum - this.secondNum; + case "*" -> this.ans = this.firstNum * this.secondNum; + case "/" -> this.ans = this.firstNum / this.secondNum; + } + return (Math.abs(Double.parseDouble(answer) - Math.round(this.ans * Math.pow(10, 2* this.precision)) / Math.pow(10, 2 * this.precision)) < Math.pow(0.1, 2 * this.precision) ? Result.OK : Result.WRONG); + } + + public static class Generator extends AbstractMathTask.Generator { + public Generator( + int precision, + double minNumber, + double maxNumber, + EnumSet operations + ) { + super( precision, + minNumber, + maxNumber, + operations + ); + } + public ExpressionTask generate() { + Random random = new Random(); + double firstNum = random.nextDouble(getDiffNumber()+ 1) + this.minNumber; + double secondNum = random.nextDouble(getDiffNumber() + 1) + this.minNumber; + firstNum = Math.round(firstNum * Math.pow(10, precision)) / Math.pow(10, precision); + secondNum = Math.round(secondNum * Math.pow(10, precision)) / Math.pow(10, precision); + Vector operations_ = new Vector<>(); + for (var operation: operations){ + operations_.add(operation.name()); + } + int pos = random.nextInt(operations_.size()); + ExpressionTask expressionTask = new ExpressionTask(); + String operator = operations_.get(pos); + if (secondNum == 0 && Objects.equals(operator, "DIV")) { + generate(); + } else { + switch (operator) { + case "SUM" -> operator = "+"; + case "DIFF" -> operator = "-"; + case "MUL" -> operator = "*"; + case "DIV" -> operator = "/"; + } + expressionTask = new ExpressionTask(firstNum, secondNum, operator, precision); + } + return expressionTask; + } + } +} \ No newline at end of file diff --git a/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/MathTask.java b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/MathTask.java new file mode 100644 index 0000000..8084192 --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/MathTask.java @@ -0,0 +1,17 @@ +package by.KseniyaGnezdilova.quizer.tasks.math_tasks; + +import by.KseniyaGnezdilova.quizer.tasks.Task; + +public interface MathTask extends Task { + + interface Generator extends Task.Generator { + + double getMaxNumber(); + + double getMinNumber(); + default double getDiffNumber(){ + return getMaxNumber() - getMinNumber(); + } + + } +} diff --git a/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/Operations.java b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/Operations.java new file mode 100644 index 0000000..ba79c6c --- /dev/null +++ b/lab-01/by/KseniyaGnezdilova/quizer/tasks/math_tasks/Operations.java @@ -0,0 +1,8 @@ +package by.KseniyaGnezdilova.quizer.tasks.math_tasks; + +public enum Operations { + SUM, + DIFF, + MUL, + DIV +} diff --git a/lab-02/config.json b/lab-02/config.json new file mode 100644 index 0000000..d229cfa --- /dev/null +++ b/lab-02/config.json @@ -0,0 +1,41 @@ +{ + "generating_time" : 3, + "ship_capacity_min": 1, + "ship_capacity_max": 10, + "cargo_types" : [ + "bread", + "meat", + "cheese" + ], + "max_ships": 5, + "unloading_speed": 2, + "dock_capacity": [ + { + "key" : "bread", + "value" : 30 + }, { + "key" : "meat", + "value" : 30 + }, + { + "key": "cheese", + "value": 40 + } + ], + "hobos": 3, + "ingridients_count" : [ + { + "key" : "bread", + "value" : 5 + }, { + "key" : "meat", + "value" : 2 + }, + { + "key": "cheese", + "value": 5 + } + ], + "stealing_time": 2, + "eating_time": 2 +} \ No newline at end of file diff --git a/lab-02/pom.xml b/lab-02/pom.xml new file mode 100644 index 0000000..7f90d35 --- /dev/null +++ b/lab-02/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + by.KseniyaGnezdilova.docks_and_hobos + lab-02 + 1.0-SNAPSHOT + + + 20 + 20 + UTF-8 + + + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + com.vaadin.external.google + android-json + 0.0.20131108.vaadin1 + + + \ No newline at end of file diff --git a/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/Main.java b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/Main.java new file mode 100644 index 0000000..1bf58af --- /dev/null +++ b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/Main.java @@ -0,0 +1,86 @@ +package by.KseniyaGnezdilova.docks_and_hobos; + +import by.KseniyaGnezdilova.docks_and_hobos.threads.DocksThread; +import by.KseniyaGnezdilova.docks_and_hobos.threads.HobosThread; +import by.KseniyaGnezdilova.docks_and_hobos.threads.ShipThread; +import by.KseniyaGnezdilova.docks_and_hobos.threads.StatusThread; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.simple.parser.*; + +import java.io.FileReader; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.Semaphore; + +public class Main { + public static LinkedList tunnel = new LinkedList<>();; + public static int max_ships; + public static HashMap dock = new HashMap(); + public static HashMap ingridients = new HashMap(); + + public static Semaphore sem = new Semaphore(1); + + public static void main(String[] args) throws IOException, ParseException, JSONException { + + Object obj = new JSONParser().parse(new FileReader(args[0])); + JSONObject jsonObject = new JSONObject(obj.toString()); + + max_ships = jsonObject.getInt("max_ships"); + + + StatusThread statusThread = new StatusThread(); + + Vector cargo_types = new Vector<>(); + JSONArray jsonArray = jsonObject.getJSONArray("cargo_types"); + for (int i = 0; i < jsonArray.length(); i++){ + cargo_types.add((String) jsonArray.get(i)); + } + + + HashMap dock_capacity = new HashMap<>( ); + jsonArray = jsonObject.getJSONArray("dock_capacity"); + for (int i = 0; i < jsonArray.length(); i++){ + Object object = jsonArray.get(i); + JSONObject cargo = new JSONObject(object.toString()); + dock_capacity.put(cargo.getString("key"), cargo.getInt("value")); + } + + for (var key: dock_capacity.keySet()){ + dock.put(key, 0); + } + + int generating_time = (int) jsonObject.get("generating_time"); + int ship_capacity_min = (int) jsonObject.get("ship_capacity_min"); + int ship_capacity_max = (int) jsonObject.get("ship_capacity_max"); + + ShipThread shipThread = new ShipThread(generating_time , ship_capacity_min, ship_capacity_max, cargo_types); + + int unloading_speed = (int) jsonObject.get("unloading_speed"); + DocksThread docksThread = new DocksThread(unloading_speed, dock_capacity); + + HashMap ingridients_count = new HashMap<>(); + jsonArray = jsonObject.getJSONArray("ingridients_count"); + for (int i = 0; i < jsonArray.length(); i++){ + Object object = jsonArray.get(i); + JSONObject cargo = new JSONObject(object.toString()); + ingridients_count.put(cargo.getString("key"), cargo.getInt("value")); + } + + for (var key: ingridients_count.keySet()){ + ingridients.put(key, 0); + } + + int hobos = (int) jsonObject.get("hobos"); + int stealing_time = (int) jsonObject.get("stealing_time"); + int eating_time = (int) jsonObject.get("eating_time"); + + HobosThread hobosThread = new HobosThread(hobos, stealing_time, eating_time, ingridients_count); + + statusThread.start(); + shipThread.start(); + docksThread.start(); + hobosThread.start(); + } +} \ No newline at end of file diff --git a/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/Ship.java b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/Ship.java new file mode 100644 index 0000000..d63d2f5 --- /dev/null +++ b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/Ship.java @@ -0,0 +1,32 @@ +package by.KseniyaGnezdilova.docks_and_hobos; + +public class Ship { + private int ship_capacity; + private String cargo_type; + public Ship(int ship_capacity, String cargo_type){ + this.ship_capacity = ship_capacity; + this.cargo_type = cargo_type; + } + + public void setShip_capacity(int ship_capacity){ + this.ship_capacity = ship_capacity; + } + public int getShip_capacity(){ + return ship_capacity; + } + + public String getCargo_type(){ + return cargo_type; + } + @Override + public String toString() { + return "Ship{" + + "ship_capacity=" + ship_capacity + + ", cargo_type=" + cargo_type + + '}'; + } + + public void writeShip(){ + System.out.println(toString()); + } +} diff --git a/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/DocksThread.java b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/DocksThread.java new file mode 100644 index 0000000..da092f5 --- /dev/null +++ b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/DocksThread.java @@ -0,0 +1,65 @@ +package by.KseniyaGnezdilova.docks_and_hobos.threads; + +import by.KseniyaGnezdilova.docks_and_hobos.Main; +import by.KseniyaGnezdilova.docks_and_hobos.Ship; + +import java.util.HashMap; +import java.util.Vector; +import java.util.concurrent.Semaphore; + +public class DocksThread extends Thread{ + private final int unloading_speed; + private final HashMap dock_capacity; + private Ship cur_ship; + public DocksThread(int unloading_speed, HashMap dock_capacity){ + this.unloading_speed = unloading_speed; + this.dock_capacity = dock_capacity; + this.cur_ship = null; + } + + @Override + public void run(){ + while (true){ + try { + Main.sem.acquire(Main.sem.availablePermits()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + if (Main.tunnel.isEmpty()) { + Main.sem.release(1); + try { + sleep(1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } else if (cur_ship == null) { + System.out.println('\n'); + this.cur_ship = Main.tunnel.get(0); + + try { + Main.sem.release(1); + sleep(1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } else if (cur_ship.getShip_capacity() > 0 && cur_ship != null) { + cur_ship.writeShip(); + cur_ship.setShip_capacity(cur_ship.getShip_capacity() - unloading_speed); + String type = cur_ship.getCargo_type(); + Main.dock.replace(type, Math.min(Main.dock.get(type) + unloading_speed, dock_capacity.get(type))); + if (cur_ship.getShip_capacity() <= 0) { + Main.tunnel.removeFirst(); + cur_ship = null; + } + Main.sem.release(1); + try { + sleep(1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + + } + +} diff --git a/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/HobosThread.java b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/HobosThread.java new file mode 100644 index 0000000..e710fd9 --- /dev/null +++ b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/HobosThread.java @@ -0,0 +1,80 @@ +package by.KseniyaGnezdilova.docks_and_hobos.threads; + +import by.KseniyaGnezdilova.docks_and_hobos.Main; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Vector; +import java.util.concurrent.Semaphore; + +public class HobosThread extends Thread{ + private final int hobos_num; + private final int stealing_time; + private final int eating_time; + private final int ingridients_size; + private final HashMap ingridients_count; + + public HobosThread(int hobos_num, int stealing_time, int eating_time, HashMap ingridients_count){ + this.eating_time = eating_time; + this.hobos_num = hobos_num; + this.stealing_time = stealing_time; + this.ingridients_size = ingridients_count.size(); + this.ingridients_count = ingridients_count; + + } + + @Override + public void run(){ + while (true){ + try { + Main.sem.acquire(Main.sem.availablePermits()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + int ingridient = 0; + for (var key: ingridients_count.keySet()){ + if (ingridients_count.get(key) <= Main.ingridients.get(key)){ + ingridient++; + } + } + if (ingridient != ingridients_size){ + int hobos_free = hobos_num - 2; + int i = 0; + var names = Main.dock.keySet().toArray(); + while (hobos_free > 0 && i < names.length){ + String key = (String) names[i]; + if (Main.dock.get(key) > 0 && Main.ingridients.get(key) < ingridients_count.get(key)){ + Main.dock.replace(key, Main.dock.get(key) - 1); + hobos_free--; + Main.ingridients.replace(key, Main.ingridients.get(key) + 1); + } + i++; + } + + try { + Main.sem.release(); + sleep(stealing_time * 1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } else{ + for (var key: Main.ingridients.keySet()){ + Main.ingridients.replace(key, Main.ingridients.get(key) - ingridients_count.get(key)); + } + try { + Main.sem.release(); + sleep(eating_time * 1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + try { + Main.sem.release(); + sleep(1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/ShipThread.java b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/ShipThread.java new file mode 100644 index 0000000..cbb2022 --- /dev/null +++ b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/ShipThread.java @@ -0,0 +1,48 @@ +package by.KseniyaGnezdilova.docks_and_hobos.threads; + +import by.KseniyaGnezdilova.docks_and_hobos.Main; +import by.KseniyaGnezdilova.docks_and_hobos.Ship; + +import java.util.Random; +import java.util.Vector; +import java.util.concurrent.Semaphore; + +public class ShipThread extends Thread{ + private final int generating_time; + private final int ship_capacity_min; + private final int ship_capacity_max; + private final Vector cargo_types; + + public ShipThread(int generating_time, int ship_capacity_min, int ship_capacity_max, Vector cargo_types){ + this.ship_capacity_min = ship_capacity_min; + this.generating_time = generating_time; + this.ship_capacity_max = ship_capacity_max; + this.cargo_types = cargo_types; + } + public void addShip(Ship ship){ + if (Main.tunnel.size() < Main.max_ships){ + Main.tunnel.add(ship); + } + } + @Override + public void run(){ + while (true) { + try { + Main.sem.acquire(Main.sem.availablePermits()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + Random random = new Random(); + int capacity = random.nextInt(ship_capacity_max - ship_capacity_min + 1) + ship_capacity_min; + int cargo = random.nextInt(cargo_types.size()); + Ship ship = new Ship(capacity, cargo_types.get(cargo)); + addShip(ship); + try { + Main.sem.release(); + sleep(generating_time * 1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/StatusThread.java b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/StatusThread.java new file mode 100644 index 0000000..0792ad2 --- /dev/null +++ b/lab-02/src/main/java/by/KseniyaGnezdilova/docks_and_hobos/threads/StatusThread.java @@ -0,0 +1,38 @@ +package by.KseniyaGnezdilova.docks_and_hobos.threads; + +import by.KseniyaGnezdilova.docks_and_hobos.Main; +import by.KseniyaGnezdilova.docks_and_hobos.Ship; + +public class StatusThread extends Thread{ + public StatusThread(){ + } + + @Override + public void run(){ + while (true) { + try { + Main.sem.acquire(Main.sem.availablePermits()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + System.out.print("Docks now >>> "); + System.out.println(Main.dock.toString()); + + System.out.print("Ingridients now >>> "); + System.out.println(Main.ingridients.toString()); + + System.out.println("Tunnel now >>> "); + for (Ship obj : Main.tunnel) { + obj.writeShip(); + } + + Main.sem.release(); + try { + Thread.sleep(1000L); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +}