diff --git a/Seminar 5/code/Project 5/.idea/.gitignore b/Seminar 5/code/Project 5/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Seminar 5/code/Project 5/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Seminar 5/code/Project 5/.idea/misc.xml b/Seminar 5/code/Project 5/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /dev/null +++ b/Seminar 5/code/Project 5/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar 5/code/Project 5/.idea/modules.xml b/Seminar 5/code/Project 5/.idea/modules.xml new file mode 100644 index 0000000..53a8812 --- /dev/null +++ b/Seminar 5/code/Project 5/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Seminar 5/code/Project 5/.idea/vcs.xml b/Seminar 5/code/Project 5/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/Seminar 5/code/Project 5/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar 5/code/Project 5/Project 5.iml b/Seminar 5/code/Project 5/Project 5.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Seminar 5/code/Project 5/Project 5.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Seminar 5/code/Project 5/file1.txt b/Seminar 5/code/Project 5/file1.txt new file mode 100644 index 0000000..6baeab4 --- /dev/null +++ b/Seminar 5/code/Project 5/file1.txt @@ -0,0 +1 @@ +Some text here \ No newline at end of file diff --git a/Seminar 5/code/Project 5/myfile.txt b/Seminar 5/code/Project 5/myfile.txt new file mode 100644 index 0000000..b8e9c4d Binary files /dev/null and b/Seminar 5/code/Project 5/myfile.txt differ diff --git a/Seminar 5/code/Project 5/out/production/Project 5/Main.class b/Seminar 5/code/Project 5/out/production/Project 5/Main.class new file mode 100644 index 0000000..243905d Binary files /dev/null and b/Seminar 5/code/Project 5/out/production/Project 5/Main.class differ diff --git a/Seminar 5/code/Project 5/out/production/Project 5/Student.class b/Seminar 5/code/Project 5/out/production/Project 5/Student.class new file mode 100644 index 0000000..25e8a46 Binary files /dev/null and b/Seminar 5/code/Project 5/out/production/Project 5/Student.class differ diff --git a/Seminar 5/code/Project 5/src/Main.java b/Seminar 5/code/Project 5/src/Main.java new file mode 100644 index 0000000..39f8646 --- /dev/null +++ b/Seminar 5/code/Project 5/src/Main.java @@ -0,0 +1,74 @@ +import java.io.*; + +public class Main { + private static final String FILE_NAME = "myfile.txt"; + + public static void main(String[] args) { +// Student s1 = new Student(); +// s1.setFacultyNumber("123124"); +// s1.setName("Bai Ivan"); +// s1.setEmail("BaiIvan@abv.bg"); + + Student s2 = deserializeStudent(); + System.out.println(s2.getEmail()); + } + + public static void externalizeTeacher(Teacher teacher) { + try (FileOutputStream fileOutput = new FileOutputStream(FILE_NAME); + ObjectOutputStream objectOut = new ObjectOutputStream(fileOutput);) { + objectOut.writeObject(teacher); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static Student deserializeStudent() { + Student result = null; + try (FileInputStream fileOutput = new FileInputStream(FILE_NAME); + ObjectInputStream objectOut = new ObjectInputStream(fileOutput);) { + result = (Student)objectOut.readObject(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + return result; + } + + public static void serializeStudent(Student student) { + try (FileOutputStream fileOutput = new FileOutputStream(FILE_NAME); + ObjectOutputStream objectOut = new ObjectOutputStream(fileOutput);) { + objectOut.writeObject(student); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void writeToAFile(String fileName) { + try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))) { + String fileContent = "Some text here"; + bufferedWriter.write(fileContent); + } catch (IOException e) { + // Exception handling​ + } + } + + public static String readStringFromAFile(String fileName) { + String result = ""; + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) { + String line = bufferedReader.readLine(); + while (line != null) { + System.out.println("I am from 25: " + line); + result += line; + line = bufferedReader.readLine(); + } + } catch (FileNotFoundException e) { + // Exception handling + } catch (IOException e) { + // Exception handling + } + + return result; + } +} \ No newline at end of file diff --git a/Seminar 5/code/Project 5/src/Student.java b/Seminar 5/code/Project 5/src/Student.java new file mode 100644 index 0000000..c6de30b --- /dev/null +++ b/Seminar 5/code/Project 5/src/Student.java @@ -0,0 +1,31 @@ +import java.io.Serializable; + +public class Student implements Serializable { + private String facultyNumber; + private String name; + private String email; + + public String getFacultyNumber() { + return facultyNumber; + } + + public void setFacultyNumber(String facultyNumber) { + this.facultyNumber = facultyNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/Seminar 5/code/Project 5/src/Teacher.java b/Seminar 5/code/Project 5/src/Teacher.java new file mode 100644 index 0000000..9bdbc0a --- /dev/null +++ b/Seminar 5/code/Project 5/src/Teacher.java @@ -0,0 +1,35 @@ +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; + +public class Teacher implements Externalizable { + private String name; + private boolean isStupid; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isStupid() { + return isStupid; + } + + public void setStupid(boolean stupid) { + isStupid = stupid; + } + + @Override + public void writeExternal(ObjectOutput out) throws IOException { + out.writeUTF(this.getName()); + } + + @Override + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + this.setName(in.readUTF()); + } +} diff --git a/Seminar 5/notes/Seminar0501.png b/Seminar 5/notes/Seminar0501.png new file mode 100644 index 0000000..f48dcb8 Binary files /dev/null and b/Seminar 5/notes/Seminar0501.png differ diff --git a/Seminar 6/code/Lambda/.idea/.gitignore b/Seminar 6/code/Lambda/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Seminar 6/code/Lambda/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Seminar 6/code/Lambda/.idea/.name b/Seminar 6/code/Lambda/.idea/.name new file mode 100644 index 0000000..88ae8ac --- /dev/null +++ b/Seminar 6/code/Lambda/.idea/.name @@ -0,0 +1 @@ +Lambda.iml \ No newline at end of file diff --git a/Seminar 6/code/Lambda/.idea/misc.xml b/Seminar 6/code/Lambda/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /dev/null +++ b/Seminar 6/code/Lambda/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar 6/code/Lambda/.idea/modules.xml b/Seminar 6/code/Lambda/.idea/modules.xml new file mode 100644 index 0000000..7daedb0 --- /dev/null +++ b/Seminar 6/code/Lambda/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Seminar 6/code/Lambda/.idea/vcs.xml b/Seminar 6/code/Lambda/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/Seminar 6/code/Lambda/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar 6/code/Lambda/Lambda.iml b/Seminar 6/code/Lambda/Lambda.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Seminar 6/code/Lambda/Lambda.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Seminar 6/code/Lambda/src/FuncInterface.java b/Seminar 6/code/Lambda/src/FuncInterface.java new file mode 100644 index 0000000..188283f --- /dev/null +++ b/Seminar 6/code/Lambda/src/FuncInterface.java @@ -0,0 +1,9 @@ +@FunctionalInterface +public interface FuncInterface { + //public abstract boolean canRun(); + void run(); + public static void runFast() {} + public default void runAgain() { + + } +} diff --git a/Seminar 6/code/Lambda/src/Main.java b/Seminar 6/code/Lambda/src/Main.java new file mode 100644 index 0000000..46ae5f9 --- /dev/null +++ b/Seminar 6/code/Lambda/src/Main.java @@ -0,0 +1,29 @@ +import java.util.*; + +public class Main { + public static void main(String[] args) { + List names = Arrays.asList("name 1", "Ivan", "Gosho", "Toshok", "Van"); + Map grades = new HashMap<>(); + grades.put("Ivan", 2); + grades.put("Yordan", 6); + + names.stream().parallel().filter(n -> n.contains("name")).sorted().forEach(System.out::println); + names.stream().forEach(n -> System.out.println(n)); + + List names2 = new ArrayList<>(Arrays.asList("Ivan", "Gosho", "Toshok", "Van")); + names2.removeIf(n -> n.contains("I")); + names2.forEach(n -> System.out.println(n)); + + grades.forEach((String key, Integer value) -> { System.out.println(key + " " + value); } ); + + //"Ivan", "Gosho" -> 4, 5 + +// doSth((e) -> { String f = ""; return "Poof"; }); + //n -> "" + //n -> {return ""; } + } + +// private static void doSth(Object a) { +// return "dasdasd"; +// } +} \ No newline at end of file diff --git a/Seminar 6/code/Problem6/.idea/.gitignore b/Seminar 6/code/Problem6/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Seminar 6/code/Problem6/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Seminar 6/code/Problem6/.idea/.name b/Seminar 6/code/Problem6/.idea/.name new file mode 100644 index 0000000..1ea59c3 --- /dev/null +++ b/Seminar 6/code/Problem6/.idea/.name @@ -0,0 +1 @@ +Problem6.iml \ No newline at end of file diff --git a/Seminar 6/code/Problem6/.idea/misc.xml b/Seminar 6/code/Problem6/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /dev/null +++ b/Seminar 6/code/Problem6/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar 6/code/Problem6/.idea/modules.xml b/Seminar 6/code/Problem6/.idea/modules.xml new file mode 100644 index 0000000..5d076b2 --- /dev/null +++ b/Seminar 6/code/Problem6/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Seminar 6/code/Problem6/.idea/vcs.xml b/Seminar 6/code/Problem6/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/Seminar 6/code/Problem6/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar 6/code/Problem6/Problem6.iml b/Seminar 6/code/Problem6/Problem6.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Seminar 6/code/Problem6/Problem6.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Seminar 6/code/Problem6/src/Main.java b/Seminar 6/code/Problem6/src/Main.java new file mode 100644 index 0000000..7f31e35 --- /dev/null +++ b/Seminar 6/code/Problem6/src/Main.java @@ -0,0 +1,14 @@ +import java.util.Arrays; +import java.util.List; + +public class Main { + public static void main(String[] args) { + List names = Arrays.asList("name 1", "Ivan", "Gosho", "Toshok", "Van"); + + //filter out names containing "name" + names.stream().filter(n -> !n.contains("name")).forEach(System.out::println); + + //sort by length + names.stream().filter(n -> !n.contains("name")).sorted((n1, n2) -> n1.length() - n2.length() ).forEach(System.out::println); + } +} \ No newline at end of file diff --git a/Seminar 6/notes/Seminar0601.png b/Seminar 6/notes/Seminar0601.png new file mode 100644 index 0000000..ae960be Binary files /dev/null and b/Seminar 6/notes/Seminar0601.png differ diff --git a/Seminar 6/notes/Seminar0602.png b/Seminar 6/notes/Seminar0602.png new file mode 100644 index 0000000..4362306 Binary files /dev/null and b/Seminar 6/notes/Seminar0602.png differ diff --git a/Seminar7/code/Server/.idea/.gitignore b/Seminar7/code/Server/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Seminar7/code/Server/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Seminar7/code/Server/.idea/misc.xml b/Seminar7/code/Server/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /dev/null +++ b/Seminar7/code/Server/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar7/code/Server/.idea/modules.xml b/Seminar7/code/Server/.idea/modules.xml new file mode 100644 index 0000000..7ec00e1 --- /dev/null +++ b/Seminar7/code/Server/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Seminar7/code/Server/.idea/vcs.xml b/Seminar7/code/Server/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/Seminar7/code/Server/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Seminar7/code/Server/out/production/Server/ClientResolver.class b/Seminar7/code/Server/out/production/Server/ClientResolver.class new file mode 100644 index 0000000..c5a5a53 Binary files /dev/null and b/Seminar7/code/Server/out/production/Server/ClientResolver.class differ diff --git a/Seminar7/code/Server/out/production/Server/Computer.class b/Seminar7/code/Server/out/production/Server/Computer.class new file mode 100644 index 0000000..31fc3ed Binary files /dev/null and b/Seminar7/code/Server/out/production/Server/Computer.class differ diff --git a/Seminar7/code/Server/out/production/Server/ComputerBuilder.class b/Seminar7/code/Server/out/production/Server/ComputerBuilder.class new file mode 100644 index 0000000..bbb31ea Binary files /dev/null and b/Seminar7/code/Server/out/production/Server/ComputerBuilder.class differ diff --git a/Seminar7/code/Server/out/production/Server/Main.class b/Seminar7/code/Server/out/production/Server/Main.class new file mode 100644 index 0000000..8072f8b Binary files /dev/null and b/Seminar7/code/Server/out/production/Server/Main.class differ diff --git a/Seminar7/code/Server/out/production/Server/ServerHandler.class b/Seminar7/code/Server/out/production/Server/ServerHandler.class new file mode 100644 index 0000000..776302f Binary files /dev/null and b/Seminar7/code/Server/out/production/Server/ServerHandler.class differ diff --git a/Seminar7/code/Server/out/production/Server/Store.class b/Seminar7/code/Server/out/production/Server/Store.class new file mode 100644 index 0000000..d80240d Binary files /dev/null and b/Seminar7/code/Server/out/production/Server/Store.class differ diff --git a/Seminar7/code/Server/out/production/Server/StoreSeeder.class b/Seminar7/code/Server/out/production/Server/StoreSeeder.class new file mode 100644 index 0000000..4f86982 Binary files /dev/null and b/Seminar7/code/Server/out/production/Server/StoreSeeder.class differ diff --git a/Seminar7/code/Server/out/production/Server/StreamCommunicator.class b/Seminar7/code/Server/out/production/Server/StreamCommunicator.class new file mode 100644 index 0000000..0b1e980 Binary files /dev/null and b/Seminar7/code/Server/out/production/Server/StreamCommunicator.class differ diff --git a/Seminar7/code/Server/src/ClientResolver.java b/Seminar7/code/Server/src/ClientResolver.java new file mode 100644 index 0000000..57eda9f --- /dev/null +++ b/Seminar7/code/Server/src/ClientResolver.java @@ -0,0 +1,34 @@ +import java.io.PrintStream; +import java.net.Socket; +import java.util.Scanner; + +public class ClientResolver implements Runnable { + private Socket clientSocket; + private Store store; + + public ClientResolver(Socket socket, Store store) + { + this.clientSocket = socket; + this.store = store; + } + + public void run() + { + Scanner in = null; + PrintStream out = null; + try { + in = new Scanner(clientSocket.getInputStream()); + out = new PrintStream(clientSocket.getOutputStream()); + StreamCommunicator communicator = new StreamCommunicator(in, out); + + var server = new ServerHandler(communicator, this.store); + server.startCommunication(); + } catch (Exception e) { + System.out.println(e.getMessage()); + System.out.println("Connection closed."); + } finally { + in.close(); + out.close(); + } + } +} diff --git a/Seminar7/code/Server/src/Computer.java b/Seminar7/code/Server/src/Computer.java new file mode 100644 index 0000000..c592ebc --- /dev/null +++ b/Seminar7/code/Server/src/Computer.java @@ -0,0 +1,76 @@ +import java.time.LocalDate; + +public class Computer { + private int id; + private String brand; + private String model; + private boolean isUsed; + private LocalDate manufacturedDate; + private int quantity; + + public Computer() { } + + public Computer(String brand, String model, boolean isUsed, LocalDate manufacturedDate, int quantity) { + this.setBrand(brand); + this.setModel(model); + this.setUsed(isUsed); + this.setManufacturedDate(manufacturedDate); + this.setQuantity(quantity); + } + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + public String getBrand() { + return brand; + } + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + public void setModel(String model) { + this.model = model; + } + + public boolean isUsed() { + return isUsed; + } + public void setUsed(boolean used) { + isUsed = used; + } + + public LocalDate getManufacturedDate() { + return manufacturedDate; + } + public void setManufacturedDate(LocalDate manufacturedDate) { + this.manufacturedDate = manufacturedDate; + } + + public int getQuantity() { + return quantity; + } + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Computer: #").append(this.getId()).append("\t") + .append(this.getBrand()).append(" ").append(this.getModel()).append("\t") + .append("Year of manufacturing: ").append(this.getManufacturedDate().toString()).append("\t") + .append("Quantity: ").append(this.getQuantity()).append("\t"); + if(this.isUsed()) { + sb.append("Computer is used").append("\t"); + } + + return sb.toString(); + } +} diff --git a/Seminar7/code/Server/src/ComputerBuilder.java b/Seminar7/code/Server/src/ComputerBuilder.java new file mode 100644 index 0000000..2c161ad --- /dev/null +++ b/Seminar7/code/Server/src/ComputerBuilder.java @@ -0,0 +1,14 @@ +import java.time.LocalDate; + +public class ComputerBuilder { + public static Computer build(StreamCommunicator communicator) { + Computer computer = new Computer(); + computer.setBrand(communicator.communicateMessage("Enter brand:")); + computer.setModel(communicator.communicateMessage("Enter model:")); + computer.setManufacturedDate(LocalDate.parse( + communicator.communicateMessage("Enter date (in format: yyyy-mm-dd):"))); + computer.setUsed(Boolean.parseBoolean(communicator.communicateMessage("Is used:"))); + computer.setQuantity(Integer.parseInt(communicator.communicateMessage("Quantity:"))); + return computer; + } +} diff --git a/Seminar7/code/Server/src/Main.java b/Seminar7/code/Server/src/Main.java new file mode 100644 index 0000000..99c652c --- /dev/null +++ b/Seminar7/code/Server/src/Main.java @@ -0,0 +1,29 @@ +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; + +public class Main { + private static final int SOCKET_PORT = 5038; //Any random free port + + public static void main(String[] args) { + ServerSocket server = null; + Store store = StoreSeeder.getSeededStore(); + + try { + server = new ServerSocket(SOCKET_PORT); + + while (true) { + Socket client = server.accept(); + System.out.println("Connection established: " + client.getInetAddress().getHostAddress()); + + ClientResolver resolver = new ClientResolver(client, store); + new Thread(resolver).start(); + } + } catch (SocketException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/Seminar7/code/Server/src/ServerHandler.java b/Seminar7/code/Server/src/ServerHandler.java new file mode 100644 index 0000000..dee0e11 --- /dev/null +++ b/Seminar7/code/Server/src/ServerHandler.java @@ -0,0 +1,45 @@ +import java.io.PrintStream; +import java.util.Scanner; + +public class ServerHandler { + private StreamCommunicator communicator; + private Store store; + + public ServerHandler(StreamCommunicator communicator, Store store) { + this.communicator = communicator; + this.store = store; + } + + public void startCommunication() { + int choice = Integer.parseInt(communicator.communicateMessage("Write 1 for administrator and 2 for client")); + switch(choice) { + case 1: + this.communicateAdmin(); + break; + case 2: + this.communicateCustomer(); + break; + default: + communicator.communicateMessage("Not an option"); + } + } + + private void communicateAdmin() { + communicator.addMessage("Entering a new computer details."); + Computer computer = ComputerBuilder.build(communicator); + this.store.addComputer(computer); + } + + private void communicateCustomer() { + communicator.communicateMessage("Buying a computer.", + this.store.getAvailableComputers(), + "Which computer would you like to buy"); + int computerId = Integer.parseInt(communicator.communicateMessage()); + int quantity = Integer.parseInt(communicator.communicateMessage("How many?")); + try { + store.sellComputers(computerId, quantity); + } catch (Exception ex) { + communicator.communicateMessage(ex.getMessage()); + } + } +} diff --git a/Seminar7/code/Server/src/Store.java b/Seminar7/code/Server/src/Store.java new file mode 100644 index 0000000..c04f25d --- /dev/null +++ b/Seminar7/code/Server/src/Store.java @@ -0,0 +1,43 @@ +import java.util.*; + +public class Store { + private List availableComputers = Collections.synchronizedList(new ArrayList<>()); + + public void addComputer(Computer computer) { + computer.setId(this.availableComputers.size()); + this.availableComputers.add(computer); + } + + public String getAvailableComputers() { + StringBuffer sb = new StringBuffer(); + for (var computer : this.availableComputers) { + sb.append(computer); + } + + return sb.toString(); + } + + public void sellComputers(int computerId, int desiredQuantity) { + synchronized (this.availableComputers) { + var computerInStock = this.availableComputers.stream() + .filter(c -> c.getId() == computerId).findFirst(); + + if (computerInStock.isEmpty()) { + throw new IllegalArgumentException("Computer with id " + computerId + " doesn't exist."); + } + + Computer computer = computerInStock.get(); + int newQuantity = computer.getQuantity() - desiredQuantity; + if (newQuantity < 0) { + throw new IllegalArgumentException("There are only " + computer.getQuantity() + " computers left." + + " We cannot sell you " + desiredQuantity); + } + + if(newQuantity == 0) { + this.availableComputers.remove(computer); + } else { + computer.setQuantity(newQuantity); + } + } + } +} diff --git a/Seminar7/code/Server/src/StoreSeeder.java b/Seminar7/code/Server/src/StoreSeeder.java new file mode 100644 index 0000000..dc8f300 --- /dev/null +++ b/Seminar7/code/Server/src/StoreSeeder.java @@ -0,0 +1,17 @@ +import java.time.LocalDate; + +public class StoreSeeder { + public static Store getSeededStore() { + Store store = new Store(); + populate(store); + return store; + } + + private static void populate(Store store) { + store.addComputer(new Computer("Lenovo", "T15", false, LocalDate.now(), 5)); + store.addComputer(new Computer("Asus", "Zenbook", + true, LocalDate.of(2020, 10, 1), 2)); + store.addComputer(new Computer("Apple", "Macbook Pro", + false, LocalDate.of(2021, 10, 31), 30)); + } +} diff --git a/Seminar7/code/Server/src/StreamCommunicator.java b/Seminar7/code/Server/src/StreamCommunicator.java new file mode 100644 index 0000000..c773bba --- /dev/null +++ b/Seminar7/code/Server/src/StreamCommunicator.java @@ -0,0 +1,32 @@ +import java.io.PrintStream; +import java.lang.invoke.StringConcatException; +import java.util.*; + +public class StreamCommunicator { + private Scanner in; + private PrintStream out; + private List messages; + + public StreamCommunicator(Scanner in, PrintStream out) { + this.in = in; + this.out = out; + this.messages = new ArrayList<>(); + } + + public void addMessage(String message) { + this.messages.add(message); + } + + public String communicateMessage() { + var result = this.communicateMessage(this.messages.toArray(String[]::new)); + this.messages.clear(); + return result; + } + + public String communicateMessage(String ...messsages) { + String messageToSend = Arrays.stream(messsages).reduce("", + (String total, String current) -> total + current); + out.println(messageToSend); + return in.nextLine(); + } +} diff --git a/Seminar7/notes/Seminar0701.png b/Seminar7/notes/Seminar0701.png new file mode 100644 index 0000000..7eb3b85 Binary files /dev/null and b/Seminar7/notes/Seminar0701.png differ diff --git a/Seminar7/notes/Seminar0702.png b/Seminar7/notes/Seminar0702.png new file mode 100644 index 0000000..c301ff3 Binary files /dev/null and b/Seminar7/notes/Seminar0702.png differ diff --git a/Seminar7/notes/Seminar0703.png b/Seminar7/notes/Seminar0703.png new file mode 100644 index 0000000..2d00f0c Binary files /dev/null and b/Seminar7/notes/Seminar0703.png differ