├── README.md ├── Seminar 1 ├── code │ ├── FirstApp │ │ ├── FirstApp.class │ │ ├── FirstApp.java │ │ └── README.md │ └── FirstIntelliJApp │ │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ └── modules.xml │ │ ├── FirstIntelliJApp.iml │ │ ├── README.md │ │ ├── out │ │ └── production │ │ │ └── FirstIntelliJApp │ │ │ └── Main.class │ │ └── src │ │ └── Main.java └── notes │ ├── Seminar0101.png │ ├── Seminar0102.png │ └── Seminar0103.png ├── Seminar 2 ├── code │ └── SecondApp │ │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ └── modules.xml │ │ ├── SecondApp.iml │ │ ├── out │ │ └── production │ │ │ └── SecondApp │ │ │ └── Main.class │ │ └── src │ │ └── Main.java └── notes │ ├── Seminar0201.png │ ├── Seminar0202.png │ └── Seminar0203.png ├── Seminar 3 ├── Homework │ ├── Problem1.md │ ├── Problem2.md │ ├── Problem3.md │ ├── Problem4.md │ ├── Problem5.md │ └── Problem6.md ├── code │ └── ThirdSeminar │ │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── vcs.xml │ │ ├── ThirdSeminar.iml │ │ ├── out │ │ └── production │ │ │ └── ThirdSeminar │ │ │ └── Main.class │ │ └── src │ │ └── Main.java └── notes │ ├── Seminar0301.png │ ├── Seminar0302.png │ ├── Seminar0303.png │ └── Seminar0304.png ├── Seminar 4 ├── assignment │ └── from-class.txt ├── code │ └── Project 4 │ │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ ├── modules.xml │ │ ├── uiDesigner.xml │ │ └── vcs.xml │ │ ├── Project 4.iml │ │ ├── out │ │ └── production │ │ │ └── Project 4 │ │ │ ├── Animal.class │ │ │ ├── Human.class │ │ │ ├── Interfaces │ │ │ └── Walkable.class │ │ │ ├── Main.class │ │ │ └── Mammel.class │ │ └── src │ │ ├── Animal.java │ │ ├── Human.java │ │ ├── Interfaces │ │ └── Walkable.java │ │ ├── Main.java │ │ └── Mammal.java └── notes │ ├── Seminar0401.png │ └── Seminar0402.png ├── Seminar 5 ├── code │ └── Project 5 │ │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── vcs.xml │ │ ├── Project 5.iml │ │ ├── file1.txt │ │ ├── myfile.txt │ │ ├── out │ │ └── production │ │ │ └── Project 5 │ │ │ ├── Main.class │ │ │ └── Student.class │ │ └── src │ │ ├── Main.java │ │ ├── Student.java │ │ └── Teacher.java └── notes │ └── Seminar0501.png ├── Seminar 6 ├── code │ ├── Lambda │ │ ├── .idea │ │ │ ├── .gitignore │ │ │ ├── .name │ │ │ ├── misc.xml │ │ │ ├── modules.xml │ │ │ └── vcs.xml │ │ ├── Lambda.iml │ │ └── src │ │ │ ├── FuncInterface.java │ │ │ └── Main.java │ └── Problem6 │ │ ├── .idea │ │ ├── .gitignore │ │ ├── .name │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── vcs.xml │ │ ├── Problem6.iml │ │ └── src │ │ └── Main.java └── notes │ ├── Seminar0601.png │ └── Seminar0602.png └── Seminar7 ├── code └── Server │ ├── .idea │ ├── .gitignore │ ├── misc.xml │ ├── modules.xml │ └── vcs.xml │ ├── out │ └── production │ │ └── Server │ │ ├── ClientResolver.class │ │ ├── Computer.class │ │ ├── ComputerBuilder.class │ │ ├── Main.class │ │ ├── ServerHandler.class │ │ ├── Store.class │ │ ├── StoreSeeder.class │ │ └── StreamCommunicator.class │ └── src │ ├── ClientResolver.java │ ├── Computer.java │ ├── ComputerBuilder.java │ ├── Main.java │ ├── ServerHandler.java │ ├── Store.java │ ├── StoreSeeder.java │ └── StreamCommunicator.java └── notes ├── Seminar0701.png ├── Seminar0702.png └── Seminar0703.png /README.md: -------------------------------------------------------------------------------- 1 | # java-tutorial 2 | Technical University Sofia Java Seminars 3 | -------------------------------------------------------------------------------- /Seminar 1/code/FirstApp/FirstApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 1/code/FirstApp/FirstApp.class -------------------------------------------------------------------------------- /Seminar 1/code/FirstApp/FirstApp.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class FirstApp { 4 | public static void main(String[] args) { 5 | System.out.println("Hello World"); 6 | } 7 | } -------------------------------------------------------------------------------- /Seminar 1/code/FirstApp/README.md: -------------------------------------------------------------------------------- 1 | # Run first project with terminal only 2 | In order to execute that next lines, you need only JDK installed. To install JDK you can run the following commands: 3 | 4 | Mac: 5 | ``` 6 | brew install openjdk 7 | ``` 8 | Windows 9 | ``` 10 | choco install openjdk 11 | ``` 12 | The next command will compile the code: 13 | ``` 14 | javac FirstApp.java 15 | ``` 16 | This command will will generate the second file: FirstApp.class: 17 | ``` 18 | java FirstApp 19 | ``` 20 | The compilation is the process conversing the .java to bytecode. The next command will execute the code. On the terminal you should see: "Hello world" get printed. -------------------------------------------------------------------------------- /Seminar 1/code/FirstIntelliJApp/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Seminar 1/code/FirstIntelliJApp/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 1/code/FirstIntelliJApp/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Seminar 1/code/FirstIntelliJApp/FirstIntelliJApp.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Seminar 1/code/FirstIntelliJApp/README.md: -------------------------------------------------------------------------------- 1 | # Run project using the IntelliJ IDEA IDE 2 | This project is entirely created by IntelliJ IDEA. 3 | 4 | To generate a new project, open IntelliJ and navigate to: 5 | * File 6 | * New Project 7 | * Name you project 8 | 9 | Additionally: 10 | * Set the language to Java (default) 11 | * Set the build system to IntelliJ (default). Later that will be changed. 12 | * Make sure "Add simple code" checkbox is checked. 13 | 14 | As a result a new project is opened and the folder strucure and things like the main class, the main method and a sample "Hello world" code are generated. 15 | 16 | In this example we explore the console input and output operations. -------------------------------------------------------------------------------- /Seminar 1/code/FirstIntelliJApp/out/production/FirstIntelliJApp/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 1/code/FirstIntelliJApp/out/production/FirstIntelliJApp/Main.class -------------------------------------------------------------------------------- /Seminar 1/code/FirstIntelliJApp/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner scaner = new Scanner(System.in); 6 | 7 | System.out.print("Enter vehicle type: "); 8 | String vehicle = scaner.next(); 9 | System.out.print("Enter vehicle year: "); 10 | int year = scaner.nextInt(); 11 | System.out.print("no new line."); 12 | System.out.println(String.format("Hello Gorublqne! Az sym %s! Ot %d", vehicle, year)); 13 | System.out.println("with new line."); 14 | } 15 | } -------------------------------------------------------------------------------- /Seminar 1/notes/Seminar0101.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 1/notes/Seminar0101.png -------------------------------------------------------------------------------- /Seminar 1/notes/Seminar0102.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 1/notes/Seminar0102.png -------------------------------------------------------------------------------- /Seminar 1/notes/Seminar0103.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 1/notes/Seminar0103.png -------------------------------------------------------------------------------- /Seminar 2/code/SecondApp/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Seminar 2/code/SecondApp/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 2/code/SecondApp/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Seminar 2/code/SecondApp/SecondApp.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Seminar 2/code/SecondApp/out/production/SecondApp/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 2/code/SecondApp/out/production/SecondApp/Main.class -------------------------------------------------------------------------------- /Seminar 2/code/SecondApp/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | printSemiPyramid(); 4 | } 5 | 6 | public static void triggerTernaryOperation() { 7 | int a = 11; 8 | System.out.println(a > 10 ? "bigger" : "smaller"); 9 | } 10 | 11 | public static void increment() { 12 | int a = 10; 13 | // a++ 14 | // a += 1; 15 | // a = a + 1; 16 | 17 | // a+=5 18 | // a = a + 5 19 | 20 | int b = a++ * ++a; 21 | System.out.println(); 22 | } 23 | 24 | public static void bitwise() { 25 | int a = 5; 26 | int b = 2; 27 | 28 | int binaryOr = b | a; 29 | System.out.println(Integer.toBinaryString(binaryOr) + "or that is: " + binaryOr); 30 | } 31 | 32 | public static void demoConditons() { 33 | int i = 11; 34 | if(i == 10) { 35 | System.out.println("Number is 10"); 36 | } else if (i > 10) { 37 | System.out.println("Number is bigger than 10"); 38 | } else { 39 | System.out.println("Number is smaller than 10"); 40 | } 41 | } 42 | 43 | public static void demoForLoops() { 44 | for(int i = 0; i <= 10; i+=2) { 45 | System.out.println("Hello - " + i); 46 | } 47 | } 48 | 49 | public static void printSemiPyramid() { 50 | int a = 4; 51 | String charToRepeat = "*"; 52 | for(int i = 0; i < a; i++) { 53 | //System.out.println(" ".repeat(a - i - 1) + charToRepeat.repeat(i + 1)); 54 | System.out.println(customRepeat(" ", a - i - 1) 55 | + customRepeat("*", (i * 2 + 1))); 56 | } 57 | } 58 | 59 | public static String customRepeat(String stringToRepeat, int count) { 60 | String result = ""; 61 | for(int i = 0; i < count; i++) { 62 | result += stringToRepeat; 63 | } 64 | 65 | return result; 66 | } 67 | } -------------------------------------------------------------------------------- /Seminar 2/notes/Seminar0201.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 2/notes/Seminar0201.png -------------------------------------------------------------------------------- /Seminar 2/notes/Seminar0202.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 2/notes/Seminar0202.png -------------------------------------------------------------------------------- /Seminar 2/notes/Seminar0203.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 2/notes/Seminar0203.png -------------------------------------------------------------------------------- /Seminar 3/Homework/Problem1.md: -------------------------------------------------------------------------------- 1 | # Find min/max number in array 2 | 3 | ## Find max 4 | Given an array of integers find the biggest integer. 5 | 6 | --- 7 | Sample input: 8 | ``` java 9 | [1,2,1,51,2] 10 | ``` 11 | Sample output: 12 | ``` java 13 | 51 14 | ``` 15 | --- 16 | Sample input: 17 | ``` java 18 | [1000,2,200, -5124] 19 | ``` 20 | Sample output: 21 | ``` java 22 | 1000 23 | ``` 24 | --- 25 | 26 | ## Find min 27 | Given an array of integers find the smallest integer. 28 | 29 | --- 30 | Sample input: 31 | ``` java 32 | [1,2,1,51,2] 33 | ``` 34 | Sample output: 35 | ``` java 36 | 1 37 | ``` 38 | --- 39 | Sample input: 40 | ``` java 41 | [1000,2,200, -5124] 42 | ``` 43 | Sample output: 44 | ``` java 45 | -5124 46 | ``` 47 | 48 | -------------------------------------------------------------------------------- /Seminar 3/Homework/Problem2.md: -------------------------------------------------------------------------------- 1 | # Longest string in array 2 | Given array of strings, find the string with the most characters. 3 | 4 | There can be more than one string with the most characters. In such case, print all strings with the max length. 5 | 6 | --- 7 | Sample input: 8 | ``` java 9 | ["gosho", "PeSho", "StaMAT", "Roki 5", "Baba Dobrinka", "Devata ot Gladalupe"] 10 | ``` 11 | Sample output: 12 | ``` 13 | Devata ot Gladalupe 14 | ``` 15 | --- 16 | Sample input: 17 | ``` java 18 | ["gosho", "PeSho", "StaMAT", "Roki 5"] 19 | ``` 20 | Sample output: 21 | ``` 22 | StaMAT, Roki 5 23 | ``` -------------------------------------------------------------------------------- /Seminar 3/Homework/Problem3.md: -------------------------------------------------------------------------------- 1 | # Reverse words in a string 2 | Given a string, return a new string that has all the words in reversed order.t all strings with the max length. 3 | 4 | --- 5 | Sample input: 6 | ``` java 7 | "This is a string sentense" 8 | ``` 9 | Sample output: 10 | ``` 11 | sihT si a gnirts esnetnes 12 | ``` 13 | --- 14 | Sample input: 15 | ``` java 16 | "My second sentence to reverse" 17 | ``` 18 | Sample output: 19 | ``` 20 | yM dnoces ecnetnes ot esrever 21 | ``` 22 | 23 |
Hint 24 | Use the string method .split() to make an array of strings and then assemble them back. 25 |
-------------------------------------------------------------------------------- /Seminar 3/Homework/Problem4.md: -------------------------------------------------------------------------------- 1 | # Find longest sequesnce of repeating numbers 2 | Given an array of integers, return the integers that repeats sequentially the most times. 3 | 4 | In case, there is more than one numbers with the same number of reapeating times, return the first. 5 | 6 | --- 7 | Sample input: 8 | ``` java 9 | [6, 5, 2, 2, 5, 6, 7, 5, 5] 10 | ``` 11 | Sample output: 12 | ``` 13 | 2 14 | ``` 15 | --- 16 | Sample input: 17 | ``` java 18 | [-12, -4, 5, 5, 5, 6, 7, 12, 12, 12, 12, 5, 5, 5] 19 | ``` 20 | Sample output: 21 | ``` 22 | 12 23 | ``` -------------------------------------------------------------------------------- /Seminar 3/Homework/Problem5.md: -------------------------------------------------------------------------------- 1 | # Find longest sequesnce of progressing sequential numbers 2 | Given an array of integers, return the longest progresing sequence. 3 | 4 | In case there are more than one progressing sequence repeating same max number of times, return the first. 5 | 6 | --- 7 | Sample input: 8 | ``` java 9 | [500, 5, 6, 7, 1, 2] 10 | ``` 11 | Sample output: 12 | ``` 13 | 5,6,7 14 | ``` 15 | --- 16 | Sample input: 17 | ``` java 18 | [-4, -5, 0, 2, 100, 0, 1, 2, 3] 19 | ``` 20 | Sample output: 21 | ``` 22 | -5, 0, 2, 100 23 | ``` -------------------------------------------------------------------------------- /Seminar 3/Homework/Problem6.md: -------------------------------------------------------------------------------- 1 | # Find longest sequesnce of progressing non-sequential numbers 2 | Given an array of integers, return the longest non-sequential progressing sequence. 3 | 4 | In case there are more than one progressing sequence repeating same max number of times, return the first. 5 | 6 | --- 7 | Sample input: 8 | ``` java 9 | [1, 0, 500, 5, 6, 7, 1, 2] 10 | ``` 11 | Sample output: 12 | ``` 13 | 1, 5,6,7 14 | ``` 15 | --- 16 | Sample input: 17 | ``` java 18 | [-4, -5, 0, 2, 100, -2, 0, 1, 2, 3] 19 | ``` 20 | Sample output: 21 | ``` 22 | -4, 0, 0, 1, 2, 3 23 | ``` -------------------------------------------------------------------------------- /Seminar 3/code/ThirdSeminar/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Seminar 3/code/ThirdSeminar/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 3/code/ThirdSeminar/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Seminar 3/code/ThirdSeminar/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 3/code/ThirdSeminar/ThirdSeminar.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Seminar 3/code/ThirdSeminar/out/production/ThirdSeminar/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 3/code/ThirdSeminar/out/production/ThirdSeminar/Main.class -------------------------------------------------------------------------------- /Seminar 3/code/ThirdSeminar/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | // int[] array = generateArray(10); 6 | // int[] array2 = array; 7 | // 8 | // System.out.println(array); 9 | // System.out.println(array2); 10 | // 11 | // array2[0] = 10; 12 | // 13 | // printArray(array); 14 | // System.out.println(); 15 | // printArray(array2); 16 | 17 | int[] array = new int[]{0, 1, 1, 2, 0, 0, 510, 12, 5}; 18 | 19 | System.out.println(sumArray(array)); 20 | // Arrays.sort(array); 21 | // printArray(array); 22 | // int index = Arrays.binarySearch(array, 400); //always call sort before binarySearch 23 | // System.out.println(index); 24 | } 25 | 26 | public static int sumArray(int[] array) { 27 | // int sum = 0; 28 | // for (int element : array) { 29 | // sum += element; 30 | // } 31 | 32 | // return sum; 33 | return Arrays.stream(array).sum(); 34 | } 35 | 36 | public static int[] generateArray(int size) { 37 | int[] array = new int[size]; 38 | for (int i = 0; i < size; i++) { 39 | array[i] = i; 40 | } 41 | return array; 42 | } 43 | 44 | public static void printArray(int[] array) { 45 | // for (int element : array) { 46 | // System.out.print(element + " "); //array[i] 47 | // } 48 | 49 | // for(int i = 0; i < array.length; i++) { 50 | // System.out.print(array[i] + " "); 51 | // } 52 | 53 | System.out.println(Arrays.toString(array)); 54 | } 55 | } -------------------------------------------------------------------------------- /Seminar 3/notes/Seminar0301.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 3/notes/Seminar0301.png -------------------------------------------------------------------------------- /Seminar 3/notes/Seminar0302.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 3/notes/Seminar0302.png -------------------------------------------------------------------------------- /Seminar 3/notes/Seminar0303.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 3/notes/Seminar0303.png -------------------------------------------------------------------------------- /Seminar 3/notes/Seminar0304.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 3/notes/Seminar0304.png -------------------------------------------------------------------------------- /Seminar 4/assignment/from-class.txt: -------------------------------------------------------------------------------- 1 | Create an electric store that manages computers. 2 | Computer: 3 | size 4 | peripheral: string[] 5 | brand 6 | canPLayGames? 7 | 8 | Laptop 9 | Desktop 10 | Tablet 11 | 12 | 13 | Presentable 14 | Computable 15 | Interactable 16 | Movable -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/Project 4.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/out/production/Project 4/Animal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 4/code/Project 4/out/production/Project 4/Animal.class -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/out/production/Project 4/Human.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 4/code/Project 4/out/production/Project 4/Human.class -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/out/production/Project 4/Interfaces/Walkable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 4/code/Project 4/out/production/Project 4/Interfaces/Walkable.class -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/out/production/Project 4/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 4/code/Project 4/out/production/Project 4/Main.class -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/out/production/Project 4/Mammel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 4/code/Project 4/out/production/Project 4/Mammel.class -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/src/Animal.java: -------------------------------------------------------------------------------- 1 | public abstract class Animal { 2 | private int weigth; 3 | 4 | public Animal() {} 5 | 6 | public Animal(int weigth) { 7 | setWeigth(weigth); 8 | } 9 | 10 | public int getWeigth() { 11 | return weigth; 12 | } 13 | 14 | protected void setWeigth(int weigth) { 15 | this.weigth = weigth; 16 | } 17 | 18 | public abstract void move(); 19 | 20 | public abstract void eat(); 21 | } 22 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/src/Human.java: -------------------------------------------------------------------------------- 1 | import Interfaces.Walkable; 2 | 3 | public class Human extends Mammal implements Walkable { 4 | public void move() { 5 | walk(); 6 | } 7 | 8 | public void walk() { 9 | System.out.println("I am walking"); 10 | } 11 | 12 | public void eat() { 13 | System.out.println("I am eating"); 14 | } 15 | 16 | public void giveBirth() { 17 | System.out.println("I am giving birth"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/src/Interfaces/Walkable.java: -------------------------------------------------------------------------------- 1 | package Interfaces; 2 | 3 | public interface Walkable { 4 | void walk(); 5 | } 6 | -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/src/Main.java: -------------------------------------------------------------------------------- 1 | import Interfaces.Walkable; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Walkable walker = new Human(); 6 | walker.walk(); 7 | 8 | Human human1 = new Human(); 9 | human1.giveBirth(); 10 | human1.eat(); 11 | 12 | Mammal mammal = new Human(); 13 | mammal.move(); 14 | } 15 | } -------------------------------------------------------------------------------- /Seminar 4/code/Project 4/src/Mammal.java: -------------------------------------------------------------------------------- 1 | public abstract class Mammal extends Animal { 2 | private int temperature; 3 | public Mammal() {} 4 | 5 | public Mammal(int weight) { 6 | super(weight); 7 | } 8 | 9 | public int getTemperature() { 10 | return temperature; 11 | } 12 | 13 | public void setTemperature(int temperature) { 14 | this.temperature = temperature; 15 | } 16 | 17 | public abstract void giveBirth(); 18 | } 19 | -------------------------------------------------------------------------------- /Seminar 4/notes/Seminar0401.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 4/notes/Seminar0401.png -------------------------------------------------------------------------------- /Seminar 4/notes/Seminar0402.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 4/notes/Seminar0402.png -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/Project 5.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/file1.txt: -------------------------------------------------------------------------------- 1 | Some text here -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/myfile.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 5/code/Project 5/myfile.txt -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/out/production/Project 5/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 5/code/Project 5/out/production/Project 5/Main.class -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/out/production/Project 5/Student.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 5/code/Project 5/out/production/Project 5/Student.class -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Main { 4 | private static final String FILE_NAME = "myfile.txt"; 5 | 6 | public static void main(String[] args) { 7 | // Student s1 = new Student(); 8 | // s1.setFacultyNumber("123124"); 9 | // s1.setName("Bai Ivan"); 10 | // s1.setEmail("BaiIvan@abv.bg"); 11 | 12 | Student s2 = deserializeStudent(); 13 | System.out.println(s2.getEmail()); 14 | } 15 | 16 | public static void externalizeTeacher(Teacher teacher) { 17 | try (FileOutputStream fileOutput = new FileOutputStream(FILE_NAME); 18 | ObjectOutputStream objectOut = new ObjectOutputStream(fileOutput);) { 19 | objectOut.writeObject(teacher); 20 | } catch (IOException e) { 21 | e.printStackTrace(); 22 | } 23 | } 24 | 25 | public static Student deserializeStudent() { 26 | Student result = null; 27 | try (FileInputStream fileOutput = new FileInputStream(FILE_NAME); 28 | ObjectInputStream objectOut = new ObjectInputStream(fileOutput);) { 29 | result = (Student)objectOut.readObject(); 30 | } catch (IOException e) { 31 | e.printStackTrace(); 32 | } catch (ClassNotFoundException e) { 33 | e.printStackTrace(); 34 | } 35 | 36 | return result; 37 | } 38 | 39 | public static void serializeStudent(Student student) { 40 | try (FileOutputStream fileOutput = new FileOutputStream(FILE_NAME); 41 | ObjectOutputStream objectOut = new ObjectOutputStream(fileOutput);) { 42 | objectOut.writeObject(student); 43 | } catch (IOException e) { 44 | e.printStackTrace(); 45 | } 46 | } 47 | 48 | public static void writeToAFile(String fileName) { 49 | try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))) { 50 | String fileContent = "Some text here"; 51 | bufferedWriter.write(fileContent); 52 | } catch (IOException e) { 53 | // Exception handling​ 54 | } 55 | } 56 | 57 | public static String readStringFromAFile(String fileName) { 58 | String result = ""; 59 | try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) { 60 | String line = bufferedReader.readLine(); 61 | while (line != null) { 62 | System.out.println("I am from 25: " + line); 63 | result += line; 64 | line = bufferedReader.readLine(); 65 | } 66 | } catch (FileNotFoundException e) { 67 | // Exception handling 68 | } catch (IOException e) { 69 | // Exception handling 70 | } 71 | 72 | return result; 73 | } 74 | } -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/src/Student.java: -------------------------------------------------------------------------------- 1 | import java.io.Serializable; 2 | 3 | public class Student implements Serializable { 4 | private String facultyNumber; 5 | private String name; 6 | private String email; 7 | 8 | public String getFacultyNumber() { 9 | return facultyNumber; 10 | } 11 | 12 | public void setFacultyNumber(String facultyNumber) { 13 | this.facultyNumber = facultyNumber; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | public String getEmail() { 25 | return email; 26 | } 27 | 28 | public void setEmail(String email) { 29 | this.email = email; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Seminar 5/code/Project 5/src/Teacher.java: -------------------------------------------------------------------------------- 1 | import java.io.Externalizable; 2 | import java.io.IOException; 3 | import java.io.ObjectInput; 4 | import java.io.ObjectOutput; 5 | 6 | public class Teacher implements Externalizable { 7 | private String name; 8 | private boolean isStupid; 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | 18 | public boolean isStupid() { 19 | return isStupid; 20 | } 21 | 22 | public void setStupid(boolean stupid) { 23 | isStupid = stupid; 24 | } 25 | 26 | @Override 27 | public void writeExternal(ObjectOutput out) throws IOException { 28 | out.writeUTF(this.getName()); 29 | } 30 | 31 | @Override 32 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 33 | this.setName(in.readUTF()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Seminar 5/notes/Seminar0501.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 5/notes/Seminar0501.png -------------------------------------------------------------------------------- /Seminar 6/code/Lambda/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Seminar 6/code/Lambda/.idea/.name: -------------------------------------------------------------------------------- 1 | Lambda.iml -------------------------------------------------------------------------------- /Seminar 6/code/Lambda/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 6/code/Lambda/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Seminar 6/code/Lambda/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 6/code/Lambda/Lambda.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Seminar 6/code/Lambda/src/FuncInterface.java: -------------------------------------------------------------------------------- 1 | @FunctionalInterface 2 | public interface FuncInterface { 3 | //public abstract boolean canRun(); 4 | void run(); 5 | public static void runFast() {} 6 | public default void runAgain() { 7 | 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Seminar 6/code/Lambda/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | List names = Arrays.asList("name 1", "Ivan", "Gosho", "Toshok", "Van"); 6 | Map grades = new HashMap<>(); 7 | grades.put("Ivan", 2); 8 | grades.put("Yordan", 6); 9 | 10 | names.stream().parallel().filter(n -> n.contains("name")).sorted().forEach(System.out::println); 11 | names.stream().forEach(n -> System.out.println(n)); 12 | 13 | List names2 = new ArrayList<>(Arrays.asList("Ivan", "Gosho", "Toshok", "Van")); 14 | names2.removeIf(n -> n.contains("I")); 15 | names2.forEach(n -> System.out.println(n)); 16 | 17 | grades.forEach((String key, Integer value) -> { System.out.println(key + " " + value); } ); 18 | 19 | //"Ivan", "Gosho" -> 4, 5 20 | 21 | // doSth((e) -> { String f = ""; return "Poof"; }); 22 | //n -> "" 23 | //n -> {return ""; } 24 | } 25 | 26 | // private static void doSth(Object a) { 27 | // return "dasdasd"; 28 | // } 29 | } -------------------------------------------------------------------------------- /Seminar 6/code/Problem6/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Seminar 6/code/Problem6/.idea/.name: -------------------------------------------------------------------------------- 1 | Problem6.iml -------------------------------------------------------------------------------- /Seminar 6/code/Problem6/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 6/code/Problem6/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Seminar 6/code/Problem6/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar 6/code/Problem6/Problem6.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Seminar 6/code/Problem6/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.List; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | List names = Arrays.asList("name 1", "Ivan", "Gosho", "Toshok", "Van"); 7 | 8 | //filter out names containing "name" 9 | names.stream().filter(n -> !n.contains("name")).forEach(System.out::println); 10 | 11 | //sort by length 12 | names.stream().filter(n -> !n.contains("name")).sorted((n1, n2) -> n1.length() - n2.length() ).forEach(System.out::println); 13 | } 14 | } -------------------------------------------------------------------------------- /Seminar 6/notes/Seminar0601.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 6/notes/Seminar0601.png -------------------------------------------------------------------------------- /Seminar 6/notes/Seminar0602.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar 6/notes/Seminar0602.png -------------------------------------------------------------------------------- /Seminar7/code/Server/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /Seminar7/code/Server/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar7/code/Server/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Seminar7/code/Server/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Seminar7/code/Server/out/production/Server/ClientResolver.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/code/Server/out/production/Server/ClientResolver.class -------------------------------------------------------------------------------- /Seminar7/code/Server/out/production/Server/Computer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/code/Server/out/production/Server/Computer.class -------------------------------------------------------------------------------- /Seminar7/code/Server/out/production/Server/ComputerBuilder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/code/Server/out/production/Server/ComputerBuilder.class -------------------------------------------------------------------------------- /Seminar7/code/Server/out/production/Server/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/code/Server/out/production/Server/Main.class -------------------------------------------------------------------------------- /Seminar7/code/Server/out/production/Server/ServerHandler.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/code/Server/out/production/Server/ServerHandler.class -------------------------------------------------------------------------------- /Seminar7/code/Server/out/production/Server/Store.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/code/Server/out/production/Server/Store.class -------------------------------------------------------------------------------- /Seminar7/code/Server/out/production/Server/StoreSeeder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/code/Server/out/production/Server/StoreSeeder.class -------------------------------------------------------------------------------- /Seminar7/code/Server/out/production/Server/StreamCommunicator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/code/Server/out/production/Server/StreamCommunicator.class -------------------------------------------------------------------------------- /Seminar7/code/Server/src/ClientResolver.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.net.Socket; 3 | import java.util.Scanner; 4 | 5 | public class ClientResolver implements Runnable { 6 | private Socket clientSocket; 7 | private Store store; 8 | 9 | public ClientResolver(Socket socket, Store store) 10 | { 11 | this.clientSocket = socket; 12 | this.store = store; 13 | } 14 | 15 | public void run() 16 | { 17 | Scanner in = null; 18 | PrintStream out = null; 19 | try { 20 | in = new Scanner(clientSocket.getInputStream()); 21 | out = new PrintStream(clientSocket.getOutputStream()); 22 | StreamCommunicator communicator = new StreamCommunicator(in, out); 23 | 24 | var server = new ServerHandler(communicator, this.store); 25 | server.startCommunication(); 26 | } catch (Exception e) { 27 | System.out.println(e.getMessage()); 28 | System.out.println("Connection closed."); 29 | } finally { 30 | in.close(); 31 | out.close(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Seminar7/code/Server/src/Computer.java: -------------------------------------------------------------------------------- 1 | import java.time.LocalDate; 2 | 3 | public class Computer { 4 | private int id; 5 | private String brand; 6 | private String model; 7 | private boolean isUsed; 8 | private LocalDate manufacturedDate; 9 | private int quantity; 10 | 11 | public Computer() { } 12 | 13 | public Computer(String brand, String model, boolean isUsed, LocalDate manufacturedDate, int quantity) { 14 | this.setBrand(brand); 15 | this.setModel(model); 16 | this.setUsed(isUsed); 17 | this.setManufacturedDate(manufacturedDate); 18 | this.setQuantity(quantity); 19 | } 20 | 21 | public int getId() { 22 | return id; 23 | } 24 | public void setId(int id) { 25 | this.id = id; 26 | } 27 | 28 | public String getBrand() { 29 | return brand; 30 | } 31 | public void setBrand(String brand) { 32 | this.brand = brand; 33 | } 34 | 35 | public String getModel() { 36 | return model; 37 | } 38 | public void setModel(String model) { 39 | this.model = model; 40 | } 41 | 42 | public boolean isUsed() { 43 | return isUsed; 44 | } 45 | public void setUsed(boolean used) { 46 | isUsed = used; 47 | } 48 | 49 | public LocalDate getManufacturedDate() { 50 | return manufacturedDate; 51 | } 52 | public void setManufacturedDate(LocalDate manufacturedDate) { 53 | this.manufacturedDate = manufacturedDate; 54 | } 55 | 56 | public int getQuantity() { 57 | return quantity; 58 | } 59 | public void setQuantity(int quantity) { 60 | this.quantity = quantity; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | StringBuilder sb = new StringBuilder(); 66 | sb.append("Computer: #").append(this.getId()).append("\t") 67 | .append(this.getBrand()).append(" ").append(this.getModel()).append("\t") 68 | .append("Year of manufacturing: ").append(this.getManufacturedDate().toString()).append("\t") 69 | .append("Quantity: ").append(this.getQuantity()).append("\t"); 70 | if(this.isUsed()) { 71 | sb.append("Computer is used").append("\t"); 72 | } 73 | 74 | return sb.toString(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Seminar7/code/Server/src/ComputerBuilder.java: -------------------------------------------------------------------------------- 1 | import java.time.LocalDate; 2 | 3 | public class ComputerBuilder { 4 | public static Computer build(StreamCommunicator communicator) { 5 | Computer computer = new Computer(); 6 | computer.setBrand(communicator.communicateMessage("Enter brand:")); 7 | computer.setModel(communicator.communicateMessage("Enter model:")); 8 | computer.setManufacturedDate(LocalDate.parse( 9 | communicator.communicateMessage("Enter date (in format: yyyy-mm-dd):"))); 10 | computer.setUsed(Boolean.parseBoolean(communicator.communicateMessage("Is used:"))); 11 | computer.setQuantity(Integer.parseInt(communicator.communicateMessage("Quantity:"))); 12 | return computer; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Seminar7/code/Server/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.net.ServerSocket; 3 | import java.net.Socket; 4 | import java.net.SocketException; 5 | 6 | public class Main { 7 | private static final int SOCKET_PORT = 5038; //Any random free port 8 | 9 | public static void main(String[] args) { 10 | ServerSocket server = null; 11 | Store store = StoreSeeder.getSeededStore(); 12 | 13 | try { 14 | server = new ServerSocket(SOCKET_PORT); 15 | 16 | while (true) { 17 | Socket client = server.accept(); 18 | System.out.println("Connection established: " + client.getInetAddress().getHostAddress()); 19 | 20 | ClientResolver resolver = new ClientResolver(client, store); 21 | new Thread(resolver).start(); 22 | } 23 | } catch (SocketException e) { 24 | throw new RuntimeException(e); 25 | } catch (IOException e) { 26 | throw new RuntimeException(e); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Seminar7/code/Server/src/ServerHandler.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.Scanner; 3 | 4 | public class ServerHandler { 5 | private StreamCommunicator communicator; 6 | private Store store; 7 | 8 | public ServerHandler(StreamCommunicator communicator, Store store) { 9 | this.communicator = communicator; 10 | this.store = store; 11 | } 12 | 13 | public void startCommunication() { 14 | int choice = Integer.parseInt(communicator.communicateMessage("Write 1 for administrator and 2 for client")); 15 | switch(choice) { 16 | case 1: 17 | this.communicateAdmin(); 18 | break; 19 | case 2: 20 | this.communicateCustomer(); 21 | break; 22 | default: 23 | communicator.communicateMessage("Not an option"); 24 | } 25 | } 26 | 27 | private void communicateAdmin() { 28 | communicator.addMessage("Entering a new computer details."); 29 | Computer computer = ComputerBuilder.build(communicator); 30 | this.store.addComputer(computer); 31 | } 32 | 33 | private void communicateCustomer() { 34 | communicator.communicateMessage("Buying a computer.", 35 | this.store.getAvailableComputers(), 36 | "Which computer would you like to buy"); 37 | int computerId = Integer.parseInt(communicator.communicateMessage()); 38 | int quantity = Integer.parseInt(communicator.communicateMessage("How many?")); 39 | try { 40 | store.sellComputers(computerId, quantity); 41 | } catch (Exception ex) { 42 | communicator.communicateMessage(ex.getMessage()); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Seminar7/code/Server/src/Store.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Store { 4 | private List availableComputers = Collections.synchronizedList(new ArrayList<>()); 5 | 6 | public void addComputer(Computer computer) { 7 | computer.setId(this.availableComputers.size()); 8 | this.availableComputers.add(computer); 9 | } 10 | 11 | public String getAvailableComputers() { 12 | StringBuffer sb = new StringBuffer(); 13 | for (var computer : this.availableComputers) { 14 | sb.append(computer); 15 | } 16 | 17 | return sb.toString(); 18 | } 19 | 20 | public void sellComputers(int computerId, int desiredQuantity) { 21 | synchronized (this.availableComputers) { 22 | var computerInStock = this.availableComputers.stream() 23 | .filter(c -> c.getId() == computerId).findFirst(); 24 | 25 | if (computerInStock.isEmpty()) { 26 | throw new IllegalArgumentException("Computer with id " + computerId + " doesn't exist."); 27 | } 28 | 29 | Computer computer = computerInStock.get(); 30 | int newQuantity = computer.getQuantity() - desiredQuantity; 31 | if (newQuantity < 0) { 32 | throw new IllegalArgumentException("There are only " + computer.getQuantity() + " computers left." 33 | + " We cannot sell you " + desiredQuantity); 34 | } 35 | 36 | if(newQuantity == 0) { 37 | this.availableComputers.remove(computer); 38 | } else { 39 | computer.setQuantity(newQuantity); 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Seminar7/code/Server/src/StoreSeeder.java: -------------------------------------------------------------------------------- 1 | import java.time.LocalDate; 2 | 3 | public class StoreSeeder { 4 | public static Store getSeededStore() { 5 | Store store = new Store(); 6 | populate(store); 7 | return store; 8 | } 9 | 10 | private static void populate(Store store) { 11 | store.addComputer(new Computer("Lenovo", "T15", false, LocalDate.now(), 5)); 12 | store.addComputer(new Computer("Asus", "Zenbook", 13 | true, LocalDate.of(2020, 10, 1), 2)); 14 | store.addComputer(new Computer("Apple", "Macbook Pro", 15 | false, LocalDate.of(2021, 10, 31), 30)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Seminar7/code/Server/src/StreamCommunicator.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.lang.invoke.StringConcatException; 3 | import java.util.*; 4 | 5 | public class StreamCommunicator { 6 | private Scanner in; 7 | private PrintStream out; 8 | private List messages; 9 | 10 | public StreamCommunicator(Scanner in, PrintStream out) { 11 | this.in = in; 12 | this.out = out; 13 | this.messages = new ArrayList<>(); 14 | } 15 | 16 | public void addMessage(String message) { 17 | this.messages.add(message); 18 | } 19 | 20 | public String communicateMessage() { 21 | var result = this.communicateMessage(this.messages.toArray(String[]::new)); 22 | this.messages.clear(); 23 | return result; 24 | } 25 | 26 | public String communicateMessage(String ...messsages) { 27 | String messageToSend = Arrays.stream(messsages).reduce("", 28 | (String total, String current) -> total + current); 29 | out.println(messageToSend); 30 | return in.nextLine(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Seminar7/notes/Seminar0701.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/notes/Seminar0701.png -------------------------------------------------------------------------------- /Seminar7/notes/Seminar0702.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/notes/Seminar0702.png -------------------------------------------------------------------------------- /Seminar7/notes/Seminar0703.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deyanpeev/java-tutorial/e043a3de77ad38e124598c9554cac2c1e66ce8aa/Seminar7/notes/Seminar0703.png --------------------------------------------------------------------------------