diff --git a/AlexSubbotin/lesson_10/.idea/compiler.xml b/AlexSubbotin/lesson_10/.idea/compiler.xml new file mode 100644 index 0000000..39231f3 --- /dev/null +++ b/AlexSubbotin/lesson_10/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_10/.idea/misc.xml b/AlexSubbotin/lesson_10/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_10/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_10/.idea/vcs.xml b/AlexSubbotin/lesson_10/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_10/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_10/.idea/workspace.xml b/AlexSubbotin/lesson_10/.idea/workspace.xml new file mode 100644 index 0000000..f250dc4 --- /dev/null +++ b/AlexSubbotin/lesson_10/.idea/workspace.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1614103939587 + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_10/lesson_10.iml b/AlexSubbotin/lesson_10/lesson_10.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_10/lesson_10.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_10/pom.xml b/AlexSubbotin/lesson_10/pom.xml new file mode 100644 index 0000000..95b8254 --- /dev/null +++ b/AlexSubbotin/lesson_10/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + org.example + lesson_10 + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + junit + junit + RELEASE + test + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_10/src/main/java/Cow.java b/AlexSubbotin/lesson_10/src/main/java/Cow.java new file mode 100644 index 0000000..9ee54f7 --- /dev/null +++ b/AlexSubbotin/lesson_10/src/main/java/Cow.java @@ -0,0 +1,45 @@ +public class Cow { + public final int DAYS_IN_YEAR = 365; + private float cowMilkProduction; + private float cowFoodConsumption; + private float cowFoodPrise; + private float cowMilkPrice; + + protected float calculateYearlyCowRevenue() { + float revenue = (this.cowMilkProduction * this.cowMilkPrice + - this.cowFoodConsumption * this.cowFoodPrise) * DAYS_IN_YEAR; + return revenue; + } + + public float getCowMilkProduction() { + return cowMilkProduction; + } + + protected void setCowMilkProduction(float cowMilkProduction) { + this.cowMilkProduction = cowMilkProduction; + } + + public float getCowFoodConsumption() { + return cowFoodConsumption; + } + + protected void setCowFoodConsumption(float cowFoodConsumption) { + this.cowFoodConsumption = cowFoodConsumption; + } + + public float getCowFoodPrise() { + return cowFoodPrise; + } + + protected void setCowFoodPrise(float cowFoodPrise) { + this.cowFoodPrise = cowFoodPrise; + } + + public float getCowMilkPrice() { + return cowMilkPrice; + } + + protected void setCowMilkPrice(float cowMilkPrice) { + this.cowMilkPrice = cowMilkPrice; + } +} diff --git a/AlexSubbotin/lesson_10/src/main/java/Farm.java b/AlexSubbotin/lesson_10/src/main/java/Farm.java new file mode 100644 index 0000000..3d35e63 --- /dev/null +++ b/AlexSubbotin/lesson_10/src/main/java/Farm.java @@ -0,0 +1,109 @@ +public class Farm { + private float farmPrice; + private float percentIncomeTax; + private float percentSalaryToMilkmaids; + private int numberOfCows; + Cow cows = new Cow(); + + public String calculateFarmProfit() { + float revenueOfAllCows = calculateYearlyAllCowsRevenue(); + float revenueAfterTax = calculateYearlyRevenueAfterTax(revenueOfAllCows); + float revenueAfterSalary = calculateYearlyRevenueAfterSalary(revenueAfterTax); + String farmProfit = calculateChanceBuyNewFarms(revenueAfterSalary); + return farmProfit; + } + + public String calculateChanceBuyNewFarms(float revenue) { + String farmsNumber; + int farmsNumb = (int) (revenue / farmPrice); + if (farmsNumb > 3) { + System.out.println("You can buy " + farmsNumb + " farms."); + farmsNumber = "SUPER PROFITABLE"; + } else if (farmsNumb >= 1) { + System.out.println("You can buy " + farmsNumb + " farms."); + farmsNumber = "PROFITABLE"; + } else { + System.out.println("You can not buy farms."); + farmsNumber = "NOT PROFITABLE"; + } + System.out.println(farmsNumber); + return farmsNumber; + } + + public float calculateYearlyRevenueAfterSalary(float revenue) { + revenue -= revenue * percentSalaryToMilkmaids / 100; + return revenue; + } + + public float calculateYearlyRevenueAfterTax(float revenue) { + revenue -= revenue * percentIncomeTax / 100; + return revenue; + } + + public float calculateYearlyAllCowsRevenue() { + float revenue = cows.calculateYearlyCowRevenue() * numberOfCows; + return revenue; + } + + + protected boolean paramsLimitsChecking(int lowerLimit, int upperLimit, float parameter) { + if (parameter < lowerLimit || parameter > upperLimit) { + return false; + } else { + return true; + } + } + + public String setFarmParams(float farmPrice, float percentIncomeTax, float percentSalaryToMilkmaids) { + String result; + if (paramsLimitsChecking(500, 1000, farmPrice) + && paramsLimitsChecking(10, 20, percentIncomeTax) + && paramsLimitsChecking(5, 10, percentSalaryToMilkmaids)) { + this.farmPrice = farmPrice; + this.percentIncomeTax = percentIncomeTax; + this.percentSalaryToMilkmaids = percentSalaryToMilkmaids; + result = ""; + } else { + result = "INPUT PARAMS ARE INCORRECT."; + System.out.println(result); + } + return result; + } + + public String setCowParams(int numberOfCows, float cowFoodConsumption, float cowFoodPrise, + float cowMilkProduction, float cowMilkPrice) { + String result; + if (paramsLimitsChecking(50, 100, numberOfCows) + && paramsLimitsChecking(5, 10, cowFoodConsumption) + && paramsLimitsChecking(1, 3, cowFoodPrise) + && paramsLimitsChecking(1, 5, cowMilkProduction) + && paramsLimitsChecking(5, 10, cowMilkPrice)) { + this.cows.setCowFoodPrise(cowFoodPrise); + this.cows.setCowMilkPrice(cowMilkPrice); + this.cows.setCowMilkProduction(cowMilkProduction); + this.cows.setCowFoodConsumption(cowFoodConsumption); + this.numberOfCows = numberOfCows; + result = ""; + } else { + result = "INPUT PARAMS ARE INCORRECT."; + System.out.println(result); + } + return result; + } + + public float getFarmPrice() { + return farmPrice; + } + + public float getPercentIncomeTax() { + return percentIncomeTax; + } + + public float getPercentSalaryToMilkmaids() { + return percentSalaryToMilkmaids; + } + + public int getNumberOfCows() { + return numberOfCows; + } +} diff --git a/AlexSubbotin/lesson_10/src/test/java/CowTest.java b/AlexSubbotin/lesson_10/src/test/java/CowTest.java new file mode 100644 index 0000000..3f1a4c4 --- /dev/null +++ b/AlexSubbotin/lesson_10/src/test/java/CowTest.java @@ -0,0 +1,15 @@ +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class CowTest { + + Farm farm = new Farm(); + + @Test + public void testCalculateYearlyCowRevenue() { + farm.setCowParams(50, 8f, 3f, 3f, 10f); + assertThat(farm.cows.calculateYearlyCowRevenue(), is(equalTo((3*10-8*3f)*365))); + } +} diff --git a/AlexSubbotin/lesson_10/src/test/java/FarmTest.java b/AlexSubbotin/lesson_10/src/test/java/FarmTest.java new file mode 100644 index 0000000..cfef3af --- /dev/null +++ b/AlexSubbotin/lesson_10/src/test/java/FarmTest.java @@ -0,0 +1,172 @@ +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class FarmTest { + Farm farm = new Farm(); + + @Before + public void initFarmAndCowParameters() { + farm.setFarmParams(500, 10, 10); + farm.setCowParams(50, 8, 3, 3, 10); + } + + @Test + public void testOfSetMiddleValidFarmParams() { + farm.setFarmParams(850f, 15.5f, 8.5f); + assertThat(farm.getFarmPrice(), equalTo(850f)); + assertThat(farm.getPercentIncomeTax(), equalTo(15.5f)); + assertThat(farm.getPercentSalaryToMilkmaids(), equalTo(8.5f)); + } + + @Test + public void testOfSetMiddleValidCowParams() { + farm.setCowParams(80, 8f, 2f, 3f, 8f); + assertThat(farm.getNumberOfCows(), equalTo(80)); + assertThat(farm.cows.getCowFoodConsumption(), equalTo(8f)); + assertThat(farm.cows.getCowFoodPrise(), equalTo(2f)); + assertThat(farm.cows.getCowMilkProduction(), equalTo(3f)); + assertThat(farm.cows.getCowMilkPrice(), equalTo(8f)); + } + + @Test + public void testOfSetMinimalValidFarmParams() { + farm.setFarmParams(500f, 10f, 5f); + assertThat(farm.getFarmPrice(), equalTo(500f)); + assertThat(farm.getPercentIncomeTax(), equalTo(10f)); + assertThat(farm.getPercentSalaryToMilkmaids(), equalTo(5f)); + } + + @Test + public void testOfSetMinimalValidCowParams() { + farm.setCowParams(50, 5f, 1f, 1f, 5f); + assertThat(farm.getNumberOfCows(), equalTo(50)); + assertThat(farm.cows.getCowFoodConsumption(), equalTo(5f)); + assertThat(farm.cows.getCowFoodPrise(), equalTo(1f)); + assertThat(farm.cows.getCowMilkProduction(), equalTo(1f)); + assertThat(farm.cows.getCowMilkPrice(), equalTo(5f)); + } + + @Test + public void testOfSetMaximalValidFarmParams() { + farm.setFarmParams(1000f, 20f, 10f); + assertThat(farm.getFarmPrice(), equalTo(1000f)); + assertThat(farm.getPercentIncomeTax(), equalTo(20f)); + assertThat(farm.getPercentSalaryToMilkmaids(), equalTo(10f)); + } + + @Test + public void testOfSetMaximalValidCowParams() { + farm.setCowParams(100, 10f, 3f, 5f, 10f); + assertThat(farm.getNumberOfCows(), equalTo(100)); + assertThat(farm.cows.getCowFoodConsumption(), equalTo(10f)); + assertThat(farm.cows.getCowFoodPrise(), equalTo(3f)); + assertThat(farm.cows.getCowMilkProduction(), equalTo(5f)); + assertThat(farm.cows.getCowMilkPrice(), equalTo(10f)); + } + + @Test + public void ifFarmParamsLessThenLowerLimitsPrintInputParamsAreIncorrect() { + assertThat(farm.setFarmParams(499f, 15f, 8f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setFarmParams(500f, 9f, 8f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setFarmParams(500f, 15f, 4f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + } + + @Test + public void ifCowParamsLessThenLowerLimitsPrintInputParamsAreIncorrect() { + assertThat(farm.setCowParams(49, 7f, 3f, 5f, 6f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setCowParams(60, 4f, 3f, 5f, 6f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setCowParams(60, 7f, 0f, 5f, 6f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setCowParams(60, 7f, 3f, 0f, 6f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setCowParams(60, 7f, 3f, 5f, 4f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + } + + @Test + public void ifFarmParamsGreaterThenUpperLimitsPrintInputParamsAreIncorrect() { + assertThat(farm.setFarmParams(1001f, 15f, 8f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setFarmParams(700f, 21f, 8f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setFarmParams(1000f, 15f, 11f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + } + + @Test + public void ifCowParamsGreaterThenUpperLimitsPrintInputParamsAreIncorrect() { + assertThat(farm.setCowParams(101, 7f, 3f, 5f, 6f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setCowParams(60, 11f, 3f, 5f, 6f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setCowParams(60, 7f, 4f, 5f, 6f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setCowParams(60, 7f, 3f, 6f, 6f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + assertThat(farm.setCowParams(60, 7f, 3f, 5f, 11f), + equalTo("INPUT PARAMS ARE INCORRECT.")); + } + + @Test + public void testCalculateYearlyAllCowYearlyRevenue() { + assertThat(farm.calculateYearlyAllCowsRevenue(), is(equalTo((3*10-8*3f)*365*50f))); + } + + @Test + public void testCalculateYearlyRevenueAfter10PercentTax() { + assertThat(farm.calculateYearlyRevenueAfterTax(1000f), is(equalTo(900f))); + } + + @Test + public void testCalculateYearlyRevenueAfter10PercentSalary() { + assertThat(farm.calculateYearlyRevenueAfterSalary(2000f), is(equalTo(1800f))); + } + + @Test + public void ifRevenue2000NumberOfFarmsIs4chanceBuyNewFarmsIsSuperProfitable() { + assertThat(farm.calculateChanceBuyNewFarms(2000f), equalTo("SUPER PROFITABLE")); + } + + @Test + public void ifRevenue1500NumberOfFarmsIs3chanceBuyNewFarmsIsProfitable() { + assertThat(farm.calculateChanceBuyNewFarms(1500f), equalTo("PROFITABLE")); + } + + @Test + public void ifRevenue1000NumberOfFarmsIs2chanceBuyNewFarmsIsSuperProfitable() { + assertThat(farm.calculateChanceBuyNewFarms(1000f), equalTo("PROFITABLE")); + } + + + @Test + public void ifRevenue0chanceBuyNewFarmsIsNotProfitable() { + assertThat(farm.calculateChanceBuyNewFarms(0f), equalTo("NOT PROFITABLE")); + } + + @Test + public void testCalculateFarmProfitSuperProfitableResult() { + assertThat(farm.calculateFarmProfit(), equalTo("SUPER PROFITABLE")); + } + + @Test + public void testCalculateFarmProfitProfitableResult() { + farm.setFarmParams(500f, 20f, 10f); + farm.setCowParams(50, 6.65f, 3f, 2f, 10f); + assertThat(farm.calculateFarmProfit(), equalTo("PROFITABLE")); + } + + @Test + public void testCalculateFarmProfitNotProfitableResult() { + farm.setFarmParams(500f, 20f, 10f); + farm.setCowParams(50, 8f, 3f, 2f, 10f); + assertThat(farm.calculateFarmProfit(), equalTo("NOT PROFITABLE")); + } +} diff --git a/AlexSubbotin/lesson_11/.idea/.gitignore b/AlexSubbotin/lesson_11/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_11/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_11/.idea/compiler.xml b/AlexSubbotin/lesson_11/.idea/compiler.xml new file mode 100644 index 0000000..5acb896 --- /dev/null +++ b/AlexSubbotin/lesson_11/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_11/.idea/misc.xml b/AlexSubbotin/lesson_11/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_11/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_11/.idea/uiDesigner.xml b/AlexSubbotin/lesson_11/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/AlexSubbotin/lesson_11/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_11/.idea/vcs.xml b/AlexSubbotin/lesson_11/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_11/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_11/lesson_11.iml b/AlexSubbotin/lesson_11/lesson_11.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_11/lesson_11.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_11/pom.xml b/AlexSubbotin/lesson_11/pom.xml new file mode 100644 index 0000000..e6552bd --- /dev/null +++ b/AlexSubbotin/lesson_11/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + org.example + lesson_11 + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + + junit + junit + 4.12 + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + junit + junit + 4.13.2 + test + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_11/src/main/java/Main.java b/AlexSubbotin/lesson_11/src/main/java/Main.java new file mode 100644 index 0000000..22b47b6 --- /dev/null +++ b/AlexSubbotin/lesson_11/src/main/java/Main.java @@ -0,0 +1,65 @@ +public class Main { + + public boolean printPairSums(Integer[] arraySorted, int sumValue) { + int firstIndexNumber = 0; + int lastIndexNumber = arraySorted.length - 1; + boolean result = false; + while (firstIndexNumber < lastIndexNumber) { + int pairSum = arraySorted[firstIndexNumber] + arraySorted[lastIndexNumber]; + if (pairSum == sumValue) { + result = true; + firstIndexNumber++; + lastIndexNumber--; + } else { + if (pairSum < sumValue) firstIndexNumber++; + else lastIndexNumber--; + } + } + return result; + } + + + public Integer[] selectionSort(Integer[] array) { + int counter = 0; + for (int i = 0; i < array.length; i++) { + int minValue = array[i]; + int minIndex = i; + for (int j = i + 1; j < array.length; j++) { + if (array[j] < minValue) { + minValue = array[j]; + minIndex = j; + counter++; + } + } + int temp = array[i]; + array[i] = minValue; + array[minIndex] = temp; + } + System.out.println(counter); + return array; + } + + + public int printFibonacciSequenceMemberByRecursion(int memberNumber) { + if (memberNumber == 0) { + return 0; + } + if (memberNumber == 1) { + return 1; + } else { + return printFibonacciSequenceMemberByRecursion(memberNumber - 1) + + printFibonacciSequenceMemberByRecursion(memberNumber - 2); + } + } + + public int printFibonacciSequenceMemberWithOutRecursion(int memberNumber) { + int firstIndex = 0; + int secondIndex = 1; + for (int i = 2; i <= memberNumber; ++i) { + int nextNumber = firstIndex + secondIndex; + firstIndex = secondIndex; + secondIndex = nextNumber; + } + return secondIndex; + } +} diff --git a/AlexSubbotin/lesson_11/src/main/java/usersContainer/Container.java b/AlexSubbotin/lesson_11/src/main/java/usersContainer/Container.java new file mode 100644 index 0000000..7048411 --- /dev/null +++ b/AlexSubbotin/lesson_11/src/main/java/usersContainer/Container.java @@ -0,0 +1,22 @@ +package usersContainer; + +public class Container { + private User user; + private Container nextContainer; + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Container getNextContainer() { + return nextContainer; + } + + public void setNextContainer(Container nextContainer) { + this.nextContainer = nextContainer; + } +} diff --git a/AlexSubbotin/lesson_11/src/main/java/usersContainer/User.java b/AlexSubbotin/lesson_11/src/main/java/usersContainer/User.java new file mode 100644 index 0000000..e6332d0 --- /dev/null +++ b/AlexSubbotin/lesson_11/src/main/java/usersContainer/User.java @@ -0,0 +1,46 @@ +package usersContainer; + +import java.util.Objects; + +public class User { + private String name; + private int age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + User user = (User) o; + return age == user.age && + Objects.equals(name, user.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + ", age=" + age + + '}'; + } +} diff --git a/AlexSubbotin/lesson_11/src/main/java/usersContainer/UsersManager.java b/AlexSubbotin/lesson_11/src/main/java/usersContainer/UsersManager.java new file mode 100644 index 0000000..9aa7bc6 --- /dev/null +++ b/AlexSubbotin/lesson_11/src/main/java/usersContainer/UsersManager.java @@ -0,0 +1,186 @@ +package usersContainer; + +public class UsersManager { + private int counter; + private Container head; + + public void addUser(User user) { + if (this.head == null) { + this.head = new Container(); + this.head.setUser(user); + } + Container temp = new Container(); + temp.setUser(user); + Container current = this.head; + if (current != null) { + while (current.getNextContainer() != null) { + current = current.getNextContainer(); + } + current.setNextContainer(temp); + } + incrementCounter(); + } + + public void addArrayOfUsers(User[] users) { + for (User user : users) { + addUser(user); + } + } + + public void removeUser(User user) { + Container current = this.head; + for (int i = 0; i < this.counter; i++) { + if (current.getNextContainer().getUser().equals(user)) { + current.setNextContainer(current.getNextContainer().getNextContainer()); + decrementCounter(); + removeUser(user); + } + current = current.getNextContainer(); + } + } + + public void insertUserByIndex(User user, int index) { + Container current = this.head; + Container temp = new Container(); + temp.setUser(user); + for (int i = 0; i < index; i++) { + current = current.getNextContainer(); + } + temp.setNextContainer(current.getNextContainer()); + current.setNextContainer(temp); + incrementCounter(); + } + + public void updateUserByIndex(User user, int index) { + Container temp = new Container(); + temp.setUser(user); + Container current = this.head; + for (int i = 0; i < index; i++) { + current = current.getNextContainer(); + } + temp.setNextContainer(current.getNextContainer().getNextContainer()); + current.setNextContainer(temp); + } + + public User findUserByName(String name) { + Container current = this.head; + User userTemp = new User(); + String userName; + for (int i = 0; i < this.counter; i++) { + current = current.getNextContainer(); + userName = current.getUser().getName(); + if (userName.equals(name)) { + userTemp = current.getUser(); + } + } + return userTemp; + } + + public User[] getUsersOrderedByName() { + boolean isSorted = false; + User temp1; + User temp2; + User userWithLowestName; + while (!isSorted) { + isSorted = true; + for (int i = 0; i < counter - 1; i++) { + temp1 = getUserByIndex(i); + temp2 = getUserByIndex(i + 1); + userWithLowestName = getUserWithLowerName(temp1, temp2); + if (userWithLowestName.equals(temp2)) { + isSorted = false; + updateUserByIndex(temp2, i); + updateUserByIndex(temp1, i + 1); + } + } + } + return getArrayUsers(); + } + + public User[] getUsersOrderedByAge() { + boolean isSorted = false; + User temp1; + User temp2; + User userWithLowestAge; + while (!isSorted) { + isSorted = true; + for (int i = 0; i < this.counter - 1; i++) { + temp1 = getUserByIndex(i); + temp2 = getUserByIndex(i + 1); + userWithLowestAge = getUserWithLowerAge(temp1, temp2); + if (userWithLowestAge.equals(temp2)) { + isSorted = false; + updateUserByIndex(temp2, i); + updateUserByIndex(temp1, i + 1); + } + } + } + return getArrayUsers(); + } + + public User getUserByIndex(int index) { + Container current = this.head; + int position = 0; + while (position <= index) { + current = current.getNextContainer(); + position++; + } + User user = current.getUser(); + return user; + } + + + private User getUserWithLowerName(User firstUser, User secondUser) { + String firstUserName = firstUser.getName(); + String secondUserName = secondUser.getName(); + int firstLength = firstUserName.length(); + int secondLength = secondUserName.length(); + int minLength = firstLength <= secondLength ? firstLength : secondLength; + for (int i = 0; i < minLength; i++) { + char first = firstUserName.charAt(i); + char second = secondUserName.charAt(i); + if (first < second) { + return firstUser; + } + if (first > second) { + return secondUser; + } + } + return firstUser; + } + + private User getUserWithLowerAge(User firstUser, User secondUser) { + int firstUserAge = firstUser.getAge(); + int secondUserAge = secondUser.getAge(); + + if (firstUserAge <= secondUserAge) { + return firstUser; + } else { + return secondUser; + } + } + + private User[] getArrayUsers() { + Container current; + current = head; + User[] sortedUsers = new User[counter]; + for (int i = 0; i < counter; i++) { + sortedUsers[i] = current.getNextContainer().getUser(); + current = current.getNextContainer(); + } + return sortedUsers; + } + + private void incrementCounter() { + counter++; + } + + private void decrementCounter() { + counter--; + } + + public int getSizeOfContainer() { + return this.counter; + } + +} diff --git a/AlexSubbotin/lesson_11/src/test/java/MainTest.java b/AlexSubbotin/lesson_11/src/test/java/MainTest.java new file mode 100644 index 0000000..3ca7f78 --- /dev/null +++ b/AlexSubbotin/lesson_11/src/test/java/MainTest.java @@ -0,0 +1,99 @@ +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.arrayContaining; +import static org.hamcrest.Matchers.equalTo; + +public class MainTest { + Main main = new Main(); + + @Test + public void testPrintPairSumsIfNegativeNumbersResultTrue() { + Integer[] sortedArray = {-7, -5, -3, -2, -1}; + int sum = -10; + assertThat(main.printPairSums(sortedArray, sum), equalTo(true)); + } + + @Test + public void testPrintPairSumsIfPositiveNumbersResultTrue() { + Integer[] sortedArray = {1, 2, 3, 5, 7}; + int sum = 10; + assertThat(main.printPairSums(sortedArray, sum), equalTo(true)); + } + + @Test + public void testPrintPairSumsForResultFalse() { + Integer[] sortedArray = {1, 2, 3, 5, 7}; + int sum = 11; + assertThat(main.printPairSums(sortedArray, sum), equalTo(false)); + } + + @Test + public void testPrintPairSumsIfSameNumbersResultTrue() { + Integer[] sortedArray = {1, 1, 3, 3, 7, 7}; + int sum = 4; + assertThat(main.printPairSums(sortedArray, sum), equalTo(true)); + } + + @Test + public void testPrintPairSumsIfSameAllNumbersResultTrue() { + Integer[] sortedArray = {1, 1, 1, 1, 1, 1}; + int sum = 2; + assertThat(main.printPairSums(sortedArray, sum), equalTo(true)); + } + + @Test + public void testSelectionSortIfPositiveNumbers() { + Integer[] array = {10, 3, 2, 5, 7, 9, 8, 1, 4, 6}; + assertThat(main.selectionSort(array), arrayContaining(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + } + + @Test + public void testSelectionSortIfPositiveNumbersArrayDoubleSize() { + Integer[] array = {10, 3, 2, 5, 7, 9, 8, 1, 4, 6, 0, 3, 2, 5, 7, 9, 8, 1, 4, 6}; + assertThat(main.selectionSort(array), + arrayContaining(0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10)); + } + + @Test + public void testSelectionSortIfNegativeNumbers() { + Integer[] array = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10}; + assertThat(main.selectionSort(array), arrayContaining(-10, -9, -8, -7, -6, -5, -4, -3, -2, -1)); + } + + @Test + public void testSelectionSortIfSameNumbers() { + Integer[] array = {-1, -1, -3, 4, 5, 6, 7, 8, 8, -10}; + assertThat(main.selectionSort(array), arrayContaining(-10, -3, -1, -1, 4, 5, 6, 7, 8, 8)); + } + + @Test + public void testPrintFibonacciSequenceMemberWithOutRecursion() { + assertThat(main.printFibonacciSequenceMemberWithOutRecursion(10), equalTo(55)); + } + + @Test + public void testPrintFibonacciFirstMemberWithOutRecursionMustBe1() { + assertThat(main.printFibonacciSequenceMemberWithOutRecursion(1), equalTo(1)); + } + + @Test + public void testPrintFibonacciSecondMemberWithOutRecursionMustBe1() { + assertThat(main.printFibonacciSequenceMemberWithOutRecursion(2), equalTo(1)); + } + + @Test + public void testPrintFibonacciSequenceMemberWithRecursion() { + assertThat(main.printFibonacciSequenceMemberByRecursion(3), equalTo(2)); + } + + @Test + public void testPrintFibonacciFirstMemberByRecursionMustBe1() { + assertThat(main.printFibonacciSequenceMemberByRecursion(1), equalTo(1)); + } + + @Test + public void testPrintFibonacciSecondMemberByRecursionMustBe1() { + assertThat(main.printFibonacciSequenceMemberByRecursion(2), equalTo(1)); + } +} diff --git a/AlexSubbotin/lesson_11/src/test/java/usersContainer/ContainerTest.java b/AlexSubbotin/lesson_11/src/test/java/usersContainer/ContainerTest.java new file mode 100644 index 0000000..03dd3de --- /dev/null +++ b/AlexSubbotin/lesson_11/src/test/java/usersContainer/ContainerTest.java @@ -0,0 +1,20 @@ +package usersContainer; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; + +public class ContainerTest { + Container container = new Container(); + + @Test + public void testContainerHasFieldUser() { + assertThat(container, Matchers.hasProperty("user")); + } + + @Test + public void testContainerHasFieldNextContainer() { + assertThat(container, Matchers.hasProperty("nextContainer")); + } +} diff --git a/AlexSubbotin/lesson_11/src/test/java/usersContainer/UserTest.java b/AlexSubbotin/lesson_11/src/test/java/usersContainer/UserTest.java new file mode 100644 index 0000000..bc7278e --- /dev/null +++ b/AlexSubbotin/lesson_11/src/test/java/usersContainer/UserTest.java @@ -0,0 +1,20 @@ +package usersContainer; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; + +public class UserTest { + User user = new User(); + + @Test + public void testUserHasFieldName() { + assertThat(user, Matchers.hasProperty("name")); + } + + @Test + public void testUserHasFieldAge() { + assertThat(user, Matchers.hasProperty("age")); + } +} diff --git a/AlexSubbotin/lesson_11/src/test/java/usersContainer/UsersManagerTest.java b/AlexSubbotin/lesson_11/src/test/java/usersContainer/UsersManagerTest.java new file mode 100644 index 0000000..30a4cb2 --- /dev/null +++ b/AlexSubbotin/lesson_11/src/test/java/usersContainer/UsersManagerTest.java @@ -0,0 +1,153 @@ +package usersContainer; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class UsersManagerTest { + UsersManager usersManager = new UsersManager(); + User user1 = new User(); + User user2 = new User(); + User user3 = new User(); + User user4 = new User(); + + @Before + public void initUsers() { + user1.setName("FIRST USER"); + user1.setAge(10); + user2.setName("SECOND USER"); + user2.setAge(20); + user3.setName("THIRD USER"); + user3.setAge(30); + user4.setName("FOURTH USER"); + user4.setAge(40); + + } + + @Test + public void testAddUser() { + usersManager.addUser(user1); + usersManager.addUser(user2); + usersManager.addUser(user3); + usersManager.addUser(user4); + assertThat(usersManager.getUserByIndex(0).toString(), equalTo("User{name='FIRST USER', age=10}")); + assertThat(usersManager.getUserByIndex(1).toString(), equalTo("User{name='SECOND USER', age=20}")); + assertThat(usersManager.getUserByIndex(2).toString(), equalTo("User{name='THIRD USER', age=30}")); + assertThat(usersManager.getUserByIndex(3).toString(), equalTo("User{name='FOURTH USER', age=40}")); + } + + @Test + public void testAddArrayOfUsers() { + User[] users = new User[4]; + users[0] = user4; + users[1] = user3; + users[2] = user2; + users[3] = user1; + usersManager.addArrayOfUsers(users); + assertThat(usersManager.getUserByIndex(0).toString(), equalTo("User{name='FOURTH USER', age=40}")); + assertThat(usersManager.getUserByIndex(1).toString(), equalTo("User{name='THIRD USER', age=30}")); + assertThat(usersManager.getUserByIndex(2).toString(), equalTo("User{name='SECOND USER', age=20}")); + assertThat(usersManager.getUserByIndex(3).toString(), equalTo("User{name='FIRST USER', age=10}")); + } + + @Test + public void removeUserSizeOfContainerShouldBeLessBy1() { + usersManager.addUser(user1); + usersManager.addUser(user2); + usersManager.addUser(user3); + usersManager.addUser(user4); + usersManager.removeUser(user2); + assertThat(usersManager.getUserByIndex(0).toString(), equalTo("User{name='FIRST USER', age=10}")); + assertThat(usersManager.getUserByIndex(1).toString(), equalTo("User{name='THIRD USER', age=30}")); + assertThat(usersManager.getUserByIndex(2).toString(), equalTo("User{name='FOURTH USER', age=40}")); + assertThat(usersManager.getSizeOfContainer(), equalTo(3)); + + } + + @Test + public void removeUserSizeOfContainerShouldBeLessBy2IfSameUsers() { + usersManager.addUser(user2); + usersManager.addUser(user2); + usersManager.addUser(user2); + usersManager.addUser(user4); + usersManager.removeUser(user2); + assertThat(usersManager.getUserByIndex(0).toString(), equalTo("User{name='FOURTH USER', age=40}")); + assertThat(usersManager.getSizeOfContainer(), equalTo(1)); + + } + + @Test + public void insertUserByIndexSizeOfContainerShouldBeGreaterBy1AndUserInfoAdded() { + usersManager.addUser(user1); + usersManager.addUser(user2); + usersManager.addUser(user3); + usersManager.addUser(user4); + usersManager.insertUserByIndex(user1, 2); + assertThat(usersManager.getUserByIndex(0).toString(), equalTo("User{name='FIRST USER', age=10}")); + assertThat(usersManager.getUserByIndex(1).toString(), equalTo("User{name='SECOND USER', age=20}")); + assertThat(usersManager.getUserByIndex(2).toString(), equalTo("User{name='FIRST USER', age=10}")); + assertThat(usersManager.getUserByIndex(3).toString(), equalTo("User{name='THIRD USER', age=30}")); + assertThat(usersManager.getUserByIndex(4).toString(), equalTo("User{name='FOURTH USER', age=40}")); + assertThat(usersManager.getSizeOfContainer(), equalTo(5)); + } + + @Test + public void updateUserByIndexSizeOfContainerShouldBeSameAndUserInfoUpdate() { + usersManager.addUser(user1); + usersManager.addUser(user2); + usersManager.addUser(user3); + usersManager.addUser(user4); + usersManager.updateUserByIndex(user1, 2); + assertThat(usersManager.getUserByIndex(0).toString(), equalTo("User{name='FIRST USER', age=10}")); + assertThat(usersManager.getUserByIndex(1).toString(), equalTo("User{name='SECOND USER', age=20}")); + assertThat(usersManager.getUserByIndex(2).toString(), equalTo("User{name='FIRST USER', age=10}")); + assertThat(usersManager.getUserByIndex(3).toString(), equalTo("User{name='FOURTH USER', age=40}")); + assertThat(usersManager.getSizeOfContainer(), equalTo(4)); + } + + @Test + public void findUserByName() { + usersManager.addUser(user1); + usersManager.addUser(user2); + usersManager.addUser(user3); + usersManager.addUser(user4); + assertThat(usersManager.findUserByName("SECOND USER").toString(), + equalTo("User{name='SECOND USER', age=20}")); + } + + @Test + public void getUsersOrderedByAgeReturnSortedByAgeArrayOfUsers() { + user4.setAge(85); + usersManager.addUser(user4); + user3.setAge(55); + usersManager.addUser(user3); + user2.setAge(45); + usersManager.addUser(user2); + user1.setAge(15); + usersManager.addUser(user1); + User[] sortedUsersArray = usersManager.getUsersOrderedByAge(); + assertThat(sortedUsersArray[0], equalTo(user1)); + assertThat(sortedUsersArray[1], equalTo(user2)); + assertThat(sortedUsersArray[2], equalTo(user3)); + assertThat(sortedUsersArray[3], equalTo(user4)); + } + + @Test + public void getUsersOrderedByNameReturnSortedByNameArrayOfUsers() { + user4.setName("aaa"); + usersManager.addUser(user4); + user3.setName("aab"); + usersManager.addUser(user3); + user2.setName("ccc"); + usersManager.addUser(user2); + user1.setName("ab"); + usersManager.addUser(user1); + User[] sortedUsersArray = usersManager.getUsersOrderedByName(); + assertThat(sortedUsersArray[0], equalTo(user4)); + assertThat(sortedUsersArray[1], equalTo(user3)); + assertThat(sortedUsersArray[2], equalTo(user1)); + assertThat(sortedUsersArray[3], equalTo(user2)); + } +} diff --git a/AlexSubbotin/lesson_12/.idea/.gitignore b/AlexSubbotin/lesson_12/.idea/.gitignore new file mode 100644 index 0000000..e7e9d11 --- /dev/null +++ b/AlexSubbotin/lesson_12/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml diff --git a/AlexSubbotin/lesson_12/.idea/compiler.xml b/AlexSubbotin/lesson_12/.idea/compiler.xml new file mode 100644 index 0000000..159cfc1 --- /dev/null +++ b/AlexSubbotin/lesson_12/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_12/.idea/misc.xml b/AlexSubbotin/lesson_12/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_12/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_12/.idea/vcs.xml b/AlexSubbotin/lesson_12/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_12/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_12/lesson_12.iml b/AlexSubbotin/lesson_12/lesson_12.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_12/lesson_12.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_12/pom.xml b/AlexSubbotin/lesson_12/pom.xml new file mode 100644 index 0000000..dc65d34 --- /dev/null +++ b/AlexSubbotin/lesson_12/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + org.example + lesson_12 + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + junit + junit + 4.13.2 + test + + + junit + junit + 4.13.2 + test + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_12/src/main/java/Main.java b/AlexSubbotin/lesson_12/src/main/java/Main.java new file mode 100644 index 0000000..ddb386e --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/Main.java @@ -0,0 +1,52 @@ +import animalInfo.Animal; +import animalInfo.Persian; +import animalInfo.Siam; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + Persian firstPersian = new Persian("Barsik"); + Persian secondPersian = new Persian("Murzik"); + Siam firstSiam = new Siam("Murka"); + Animal[] animals = {firstPersian, firstSiam, secondPersian}; + + String animalName = typeAnimalName(); + String animalInfo = getInfoByName(animals, animalName); + printInfo(animalInfo); + String hierarchy = getHierarchySubspeciesOfAnimal(animals, animalName); + printInfo(hierarchy); + } + + public static String getInfoByName(Animal[] animals, String name) { + String info = null; + for (Animal animal : animals) { + if (animal.getName().equals(name)) { + info = animal.getInfo(); + } + } + return info; + } + + public static String getHierarchySubspeciesOfAnimal(Animal[] animals, String name) { + String info = null; + for (Animal animal : animals) { + if (animal.getName().equalsIgnoreCase(name)) { + info = animal.getGroupName(); + } + } + return info; + } + + public static String typeAnimalName() { + System.out.println("Enter animal Name:"); + Scanner scanner = new Scanner(System.in); + String animalName = scanner.nextLine(); + return animalName.trim(); + } + + public static void printInfo(String info) { + System.out.println("Result:\n" + info); + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Animal.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Animal.java new file mode 100644 index 0000000..e89c18e --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Animal.java @@ -0,0 +1,25 @@ +package animalInfo; + +public class Animal extends AnimalInfo { + + protected String name; + private String groupName; + + public String getName() { + return name; + } + + protected Animal() { + this.groupName = "Kingdom - Animal."; + } + + public String getGroupName() { + String groupInfo = this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = "Are able to move.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/AnimalInfo.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/AnimalInfo.java new file mode 100644 index 0000000..8071226 --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/AnimalInfo.java @@ -0,0 +1,6 @@ +package animalInfo; + +public abstract class AnimalInfo { + + public abstract String getInfo(); +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/BigCats.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/BigCats.java new file mode 100644 index 0000000..6e1134e --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/BigCats.java @@ -0,0 +1,23 @@ +package animalInfo; + +public class BigCats extends Cats { + private String groupName; + + + protected BigCats() { + this.groupName = "Subfamily - Big Cats."; + this.canGrowl = true; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = super.getInfo() + + "Include the largest representatives. Can growl.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Caracals.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Caracals.java new file mode 100644 index 0000000..59d462c --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Caracals.java @@ -0,0 +1,23 @@ +package animalInfo; + +public class Caracals extends SmallCats { + + private String groupName; + + protected Caracals() { + this.groupName = "Genus - Caracals."; + this.wildCat = true; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = super.getInfo() + + "Is a medium-sized wild cat.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Carnivora.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Carnivora.java new file mode 100644 index 0000000..b9977ef --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Carnivora.java @@ -0,0 +1,24 @@ +package animalInfo; + +public class Carnivora extends Mammals { + + private String groupName; + protected boolean haveNightVision; + + protected Carnivora() { + this.groupName = "Order - Carnivora."; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + + String mainInfo = super.getInfo() + + "Eat other animals.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Cats.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Cats.java new file mode 100644 index 0000000..4b79fae --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Cats.java @@ -0,0 +1,28 @@ +package animalInfo; + +public class Cats extends Carnivora { + + private String groupName; + protected boolean canGrowl; + + protected Cats() { + this.groupName = "Family - Cats."; + this.haveNightVision = true; + } + + protected String howLegs() { + return "Have 4 legs."; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = super.getInfo() + + "Have retractile claws, slender muscular bodies and strong flexible forelimbs.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/DomesticCats.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/DomesticCats.java new file mode 100644 index 0000000..33f2a6d --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/DomesticCats.java @@ -0,0 +1,27 @@ +package animalInfo; + +public class DomesticCats extends ForestCats { + + private String groupName; + + protected DomesticCats() { + this.groupName = "Species - Domestic Cats."; + this.wildCat = false; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = super.getInfo() + + howLegs() + "\n" + + "Lives near people.\n" + + "This animal have night vision - " + this.haveNightVision + "\n" + + "This animal can growl - " + this.canGrowl + "\n" + + "This animal wild - " + this.wildCat + "\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/ForestCats.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/ForestCats.java new file mode 100644 index 0000000..c161c07 --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/ForestCats.java @@ -0,0 +1,24 @@ +package animalInfo; + +public class ForestCats extends SmallCats { + + private String groupName; + + + protected ForestCats() { + this.groupName = "Genus - Forest Cats."; + this.wildCat = true; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = super.getInfo() + + "Can hunt small rodents and other animals of a similar size.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Herbivorous.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Herbivorous.java new file mode 100644 index 0000000..9bdb959 --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Herbivorous.java @@ -0,0 +1,23 @@ +package animalInfo; + +public class Herbivorous extends Mammals { + + private String groupName; + + protected Herbivorous() { + this.groupName = "Order - Herbivorous."; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + + String mainInfo = super.getInfo() + + "Eats plant foods.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Lion.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Lion.java new file mode 100644 index 0000000..cb1a703 --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Lion.java @@ -0,0 +1,22 @@ +package animalInfo; + +public class Lion extends BigCats { + + private String groupName; + + protected Lion() { + this.groupName = "Genus - Lion."; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = super.getInfo() + +"The largest member of the cats family.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Mammals.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Mammals.java new file mode 100644 index 0000000..8695759 --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Mammals.java @@ -0,0 +1,23 @@ +package animalInfo; + +public class Mammals extends Animal { + + private String groupName; + + protected Mammals() { + this.groupName = "Class - Mammals."; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + + String mainInfo = super.getInfo() + + "Produce milk for feeding their young\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Persian.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Persian.java new file mode 100644 index 0000000..eebb07d --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Persian.java @@ -0,0 +1,30 @@ +package animalInfo; + +public class Persian extends DomesticCats { + + private String groupName; + + public Persian(String catName) { + this.name = catName; + this.groupName = "Breed - Persian."; + } + + public String getName() { + return name; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = getName() + "\n" + + this.groupName + "\n" + + super.getInfo() + + "Has an extremely long and thick coat, short legs, " + + "a wide head with the ears set far apart, large eyes."; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Siam.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Siam.java new file mode 100644 index 0000000..5289623 --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Siam.java @@ -0,0 +1,35 @@ +package animalInfo; + +public class Siam extends DomesticCats { + + private String name; + private String groupName; + + + public Siam(String catName) { + this.name = catName; + this.groupName = "Breed - Siam."; + } + + public String getName() { + return name; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = getName() + "\n" + + this.groupName + "\n" + + super.getInfo() + + "Is characterized by blue almond-shaped eyes; " + + "a triangular head shape; " + + "large ears; " + + "an elongated, slender, and muscular body; " + + "and various forms of point colouration."; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/SmallCats.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/SmallCats.java new file mode 100644 index 0000000..27f1db7 --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/SmallCats.java @@ -0,0 +1,24 @@ +package animalInfo; + +public class SmallCats extends Cats { + + private String groupName; + protected boolean wildCat; + + protected SmallCats() { + this.groupName = "Subfamily - Small Cats."; + this.canGrowl = false; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = super.getInfo() + + "Are able to purr but not roar.\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/main/java/animalInfo/Tigers.java b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Tigers.java new file mode 100644 index 0000000..9cb162b --- /dev/null +++ b/AlexSubbotin/lesson_12/src/main/java/animalInfo/Tigers.java @@ -0,0 +1,22 @@ +package animalInfo; + +public class Tigers extends BigCats { + + private String groupName; + + protected Tigers () { + this.groupName = "Genus - Tiger."; + } + + public String getGroupName() { + String groupInfo = super.getGroupName() + + this.groupName; + return groupInfo + "\n"; + } + + public String getInfo() { + String mainInfo = super.getInfo() + + "Have dark vertical stripes on orange-brown fur with a lighter underside\n"; + return mainInfo; + } +} diff --git a/AlexSubbotin/lesson_12/src/test/java/TestMain.java b/AlexSubbotin/lesson_12/src/test/java/TestMain.java new file mode 100644 index 0000000..8d3a87c --- /dev/null +++ b/AlexSubbotin/lesson_12/src/test/java/TestMain.java @@ -0,0 +1,86 @@ +import animalInfo.Animal; +import animalInfo.Persian; +import animalInfo.Siam; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class TestMain { + + Main main = new Main(); + Persian firstPersian = new Persian("Barsik"); + Persian secondPersian = new Persian("Murzik"); + Siam firstSiam = new Siam("Murka"); + Animal[] animals = {firstPersian, firstSiam, secondPersian}; + + @Test + public void ifNameMurzikShouldPrintPersianInfo() { + String animalInfo = main.getInfoByName(animals, "Murzik"); + main.printInfo(animalInfo); + assertThat(animalInfo, equalTo("Murzik\n" + + "Breed - Persian.\n" + + "Are able to move.\n" + + "Produce milk for feeding their young\n" + + "Eat other animals.\n" + + "Have retractile claws, slender muscular bodies and strong flexible forelimbs.\n" + + "Are able to purr but not roar.\n" + + "Can hunt small rodents and other animals of a similar size.\n" + + "Have 4 legs.\n" + + "Lives near people.\n" + + "This animal have night vision - true\n" + + "This animal can growl - false\n" + + "This animal wild - false\n" + + "Has an extremely long and thick coat, short legs, a wide head with the ears set far apart, large eyes.")); + } + + @Test + public void testGetMurzikHierarchySubspeciesOfAnimal() { + String animalInfo = main.getHierarchySubspeciesOfAnimal(animals, "Murzik"); +// main.printInfo(animalInfo); + assertThat(animalInfo, equalTo("Kingdom - Animal.\n" + + "Class - Mammals.\n" + + "Order - Carnivora.\n" + + "Family - Cats.\n" + + "Subfamily - Small Cats.\n" + + "Genus - Forest Cats.\n" + + "Species - Domestic Cats.\n" + + "Breed - Persian.\n")); + } + + @Test + public void ifNameMurkaShouldPrintSiamInfo() { + String animalInfo = main.getInfoByName(animals, "Murka"); + main.printInfo(animalInfo); + assertThat(animalInfo, equalTo("Murka\n" + + "Breed - Siam.\n" + + "Are able to move.\n" + + "Produce milk for feeding their young\n" + + "Eat other animals.\n" + + "Have retractile claws, slender muscular bodies and strong flexible forelimbs.\n" + + "Are able to purr but not roar.\n" + + "Can hunt small rodents and other animals of a similar size.\n" + + "Have 4 legs.\n" + + "Lives near people.\n" + + "This animal have night vision - true\n" + + "This animal can growl - false\n" + + "This animal wild - false\n" + + "Is characterized by blue almond-shaped eyes; " + + "a triangular head shape; " + + "large ears; " + + "an elongated, slender, and muscular body; and various forms of point colouration.")); + } + + @Test + public void testGetMurkaHierarchySubspeciesOfAnimal() { + String animalInfo = main.getHierarchySubspeciesOfAnimal(animals, "Murka"); + assertThat(animalInfo, equalTo("Kingdom - Animal.\n" + + "Class - Mammals.\n" + + "Order - Carnivora.\n" + + "Family - Cats.\n" + + "Subfamily - Small Cats.\n" + + "Genus - Forest Cats.\n" + + "Species - Domestic Cats.\n" + + "Breed - Siam.\n")); + } +} diff --git a/AlexSubbotin/lesson_13/.idea/.gitignore b/AlexSubbotin/lesson_13/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_13/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_13/.idea/compiler.xml b/AlexSubbotin/lesson_13/.idea/compiler.xml new file mode 100644 index 0000000..4653d66 --- /dev/null +++ b/AlexSubbotin/lesson_13/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_13/.idea/misc.xml b/AlexSubbotin/lesson_13/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_13/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_13/.idea/vcs.xml b/AlexSubbotin/lesson_13/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_13/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_13/lesson_13.iml b/AlexSubbotin/lesson_13/lesson_13.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_13/lesson_13.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_13/pom.xml b/AlexSubbotin/lesson_13/pom.xml new file mode 100644 index 0000000..8f4ea4c --- /dev/null +++ b/AlexSubbotin/lesson_13/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + org.example + lesson_13 + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + junit + junit + 4.13.2 + test + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_13/src/main/java/User.java b/AlexSubbotin/lesson_13/src/main/java/User.java new file mode 100644 index 0000000..ea3ca3c --- /dev/null +++ b/AlexSubbotin/lesson_13/src/main/java/User.java @@ -0,0 +1,54 @@ +import store.Product; + +public class User { + + private String login; + private String password; + private Basket userBasket; + + protected class Basket { + + private Product[] boughtProducts; + + public Basket(Product... boughtProducts) { + this.boughtProducts = boughtProducts; + } + + public Product[] getBoughtProducts() { + return this.boughtProducts; + } + + public void setBoughtProducts(Product... boughtProducts) { + this.boughtProducts = boughtProducts; + } + } + + public User(String login, String password) { + this.login = login; + this.password = password; + } + + public String getLogin() { + return this.login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Basket getUserBasket() { + return this.userBasket; + } + + public void setUserBasket(Basket userBasket) { + this.userBasket = userBasket; + } +} diff --git a/AlexSubbotin/lesson_13/src/main/java/store/Category.java b/AlexSubbotin/lesson_13/src/main/java/store/Category.java new file mode 100644 index 0000000..67103e0 --- /dev/null +++ b/AlexSubbotin/lesson_13/src/main/java/store/Category.java @@ -0,0 +1,27 @@ +package store; + +public class Category { + + private String categoryName; + private Product[] products; + + protected Category(String categoryName) { + this.categoryName = categoryName; + } + + public String getCategoryName() { + return this.categoryName; + } + + public Product[] getProducts() { + return this.products; + } + + protected void setCategoryName(String categoryName) { + this.categoryName = categoryName; + } + + protected void setProducts(Product... products) { + this.products = products; + } +} diff --git a/AlexSubbotin/lesson_13/src/main/java/store/Product.java b/AlexSubbotin/lesson_13/src/main/java/store/Product.java new file mode 100644 index 0000000..8bc8245 --- /dev/null +++ b/AlexSubbotin/lesson_13/src/main/java/store/Product.java @@ -0,0 +1,38 @@ +package store; + +public class Product { + + private String name; + private int price; + private byte rate; + + protected Product(String name, int price, byte rate) { + this.name = name; + this.price = price; + this.rate = rate; + } + + public String getName() { + return this.name; + } + + public int getPrice() { + return this.price; + } + + public byte getRate() { + return this.rate; + } + + protected void setPrice(int price) { + this.price = price; + } + + protected void setName(String name) { + this.name = name; + } + + protected void setRate(byte rate) { + this.rate = rate; + } +} diff --git a/AlexSubbotin/lesson_13/src/test/java/UserTest.java b/AlexSubbotin/lesson_13/src/test/java/UserTest.java new file mode 100644 index 0000000..7153a0d --- /dev/null +++ b/AlexSubbotin/lesson_13/src/test/java/UserTest.java @@ -0,0 +1,46 @@ +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import store.CategoryTest; +import store.Product; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class UserTest { + + private CategoryTest categoryTest = new CategoryTest(); + private User firstUser = new User("aaa", "BBB"); + + @Before + public void initAssortment() { + categoryTest.setProductsInTelephonesCategory(); + categoryTest.setProductsInTVCategory(); + categoryTest.setProductsInComputersCategory(); + } + + @Test + public void testUserFields() { + User user = new User("", ""); + assertThat(user, Matchers.hasProperty("login")); + assertThat(user, Matchers.hasProperty("password")); + assertThat(user, Matchers.hasProperty("userBasket")); + } + + @Test + public void testUserAddToBasketIBMIphone10Samsung75() { + Product[] storeAssortmentTelephones = categoryTest.telephones.getProducts(); + Product[] storeAssortmentTv = categoryTest.tvsets.getProducts(); + Product[] storeAssortmentComputers = categoryTest.computers.getProducts(); + + User.Basket userBasket = firstUser.new Basket(); + userBasket.setBoughtProducts( + storeAssortmentComputers[0], + storeAssortmentTelephones[1], + storeAssortmentTv[2] + ); + assertThat(userBasket.getBoughtProducts()[0].getName(), (equalTo("IBM"))); + assertThat(userBasket.getBoughtProducts()[1].getName(), (equalTo("Iphone 10"))); + assertThat(userBasket.getBoughtProducts()[2].getName(), (equalTo("Samsung 75``"))); + } +} diff --git a/AlexSubbotin/lesson_13/src/test/java/store/CategoryTest.java b/AlexSubbotin/lesson_13/src/test/java/store/CategoryTest.java new file mode 100644 index 0000000..cf72186 --- /dev/null +++ b/AlexSubbotin/lesson_13/src/test/java/store/CategoryTest.java @@ -0,0 +1,56 @@ +package store; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItemInArray; + +public class CategoryTest { + + public Category telephones = new Category("TELEPHONES"); + public Category tvsets = new Category("TV"); + public Category computers = new Category("Computers"); + + private Product iphone11 = new Product("Iphone 11", 1000, (byte) 10); + private Product iphone10 = new Product("Iphone 10", 900, (byte) 9); + private Product iphone8 = new Product("Iphone 8", 800, (byte) 8); + + private Product samsung75 = new Product("Samsung 75``", 1000, (byte) 10); + private Product samsung65 = new Product("Samsung 65``", 900, (byte) 9); + private Product samsung55 = new Product("Samsung 55``", 800, (byte) 8); + + private Product ibm = new Product("IBM", 360, (byte) 4); + private Product hp = new Product("HP", 600, (byte) 7); + private Product xiaomi = new Product("Xiaomi", 800, (byte) 9); + + @Test + public void testCategoryFields() { + assertThat(telephones, Matchers.hasProperty("categoryName")); + assertThat(telephones, Matchers.hasProperty("products")); + } + + @Test + public void setProductsInTelephonesCategory() { + telephones.setProducts(iphone8, iphone10, iphone11); + assertThat(telephones.getProducts(), hasItemInArray(iphone8)); + assertThat(telephones.getProducts(), hasItemInArray(iphone10)); + assertThat(telephones.getProducts(), hasItemInArray(iphone11)); + } + + @Test + public void setProductsInTVCategory() { + tvsets.setProducts(samsung55, samsung65, samsung75); + assertThat(tvsets.getProducts(), hasItemInArray(samsung55)); + assertThat(tvsets.getProducts(), hasItemInArray(samsung65)); + assertThat(tvsets.getProducts(), hasItemInArray(samsung75)); + } + + @Test + public void setProductsInComputersCategory() { + computers.setProducts(ibm, hp, xiaomi); + assertThat(computers.getProducts(), hasItemInArray(ibm)); + assertThat(computers.getProducts(), hasItemInArray(hp)); + assertThat(computers.getProducts(), hasItemInArray(xiaomi)); + } +} diff --git a/AlexSubbotin/lesson_13/src/test/java/store/ProductTest.java b/AlexSubbotin/lesson_13/src/test/java/store/ProductTest.java new file mode 100644 index 0000000..d9acc1b --- /dev/null +++ b/AlexSubbotin/lesson_13/src/test/java/store/ProductTest.java @@ -0,0 +1,18 @@ +package store; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; + +public class ProductTest { + + private Product product = new Product("a", 10, (byte) 2); + + @Test + public void testProductFields() { + assertThat(product, Matchers.hasProperty("name")); + assertThat(product, Matchers.hasProperty("price")); + assertThat(product, Matchers.hasProperty("rate")); + } +} diff --git a/AlexSubbotin/lesson_21/.idea/.gitignore b/AlexSubbotin/lesson_21/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_21/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_21/.idea/compiler.xml b/AlexSubbotin/lesson_21/.idea/compiler.xml new file mode 100644 index 0000000..904a94c --- /dev/null +++ b/AlexSubbotin/lesson_21/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_21/.idea/misc.xml b/AlexSubbotin/lesson_21/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_21/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_21/.idea/vcs.xml b/AlexSubbotin/lesson_21/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_21/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_21/lesson_21.iml b/AlexSubbotin/lesson_21/lesson_21.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_21/lesson_21.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_21/pom.xml b/AlexSubbotin/lesson_21/pom.xml new file mode 100644 index 0000000..9ad8089 --- /dev/null +++ b/AlexSubbotin/lesson_21/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + org.example + lesson_21 + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + test + + + junit + junit + 4.13.2 + test + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/Checker.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/Checker.java new file mode 100644 index 0000000..6161f72 --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/Checker.java @@ -0,0 +1,8 @@ +package shopsolid; + +import java.util.List; + +public interface Checker { + + int checkProduct(List productsList, Product product); +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/ConfirmationSender.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/ConfirmationSender.java new file mode 100644 index 0000000..76d3f7e --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/ConfirmationSender.java @@ -0,0 +1,6 @@ +package shopsolid; + +public interface ConfirmationSender { + + void sendConfirmationSMS(); +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollector.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollector.java new file mode 100644 index 0000000..1cf93b8 --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollector.java @@ -0,0 +1,8 @@ +package shopsolid; + +import java.util.List; + +public interface OrderCollector { + + Product collectOrder(List productList, Product product); +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollectorSendControler.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollectorSendControler.java new file mode 100644 index 0000000..b6e5e99 --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollectorSendControler.java @@ -0,0 +1,23 @@ +package shopsolid; + +import java.util.List; + +public class OrderCollectorSendControler implements OrderCollector, OrderSender { + + private ProductChecker pc; + private ProductPreparer pp; + + public OrderCollectorSendControler() { + this.pc = new ProductChecker(); + this.pp = new ProductPreparer(); + } + + public Product collectOrder(List productList, Product product) { + int productIndex = pc.checkProduct(productList, product); + return pp.prepareProduct(productList, productIndex); + } + + public void sendOrder(Product product) { + System.out.println("Order: " + product.toString() + " is send."); + } +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollectorSendControllerWithConfirmation.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollectorSendControllerWithConfirmation.java new file mode 100644 index 0000000..c25c9e1 --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderCollectorSendControllerWithConfirmation.java @@ -0,0 +1,24 @@ +package shopsolid; + +import java.util.List; + +public class OrderCollectorSendControllerWithConfirmation extends OrderCollectorSendControler implements ConfirmationSender { + + @Override + public Product collectOrder(List productList, Product product) { + printConfirmation(); //This method break Liskov’s Substitution Principle.(Prints an extra line) + sendConfirmationSMS(); + return super.collectOrder(productList, product); + } + + //This method break Liskov’s Substitution Principle.(Prints an extra line) + private void printConfirmation() { + System.out.println("Your order complete"); + } + + public void sendConfirmationSMS() { + String sms = "SMS: Your order on the way."; + //sms sender body... + System.out.println(sms); + } +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderSender.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderSender.java new file mode 100644 index 0000000..f03adac --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/OrderSender.java @@ -0,0 +1,6 @@ +package shopsolid; + +public interface OrderSender { + + void sendOrder(Product product); +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/Preparer.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/Preparer.java new file mode 100644 index 0000000..9bf22c4 --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/Preparer.java @@ -0,0 +1,8 @@ +package shopsolid; + +import java.util.List; + +public interface Preparer { + + Product prepareProduct(List productsList, int productIndex); +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/Product.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/Product.java new file mode 100644 index 0000000..720333c --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/Product.java @@ -0,0 +1,28 @@ +package shopsolid; + +public class Product { + + private String productName; + private int productPrice; + + public Product(String productName, int productPrice) { + this.productName = productName; + this.productPrice = productPrice; + } + + public String getProductName() { + return productName; + } + + public int getProductPrice() { + return productPrice; + } + + @Override + public String toString() { + return + "productName = " + productName + + ", productPrice = " + productPrice + + "грн."; + } +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/ProductChecker.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/ProductChecker.java new file mode 100644 index 0000000..f719d11 --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/ProductChecker.java @@ -0,0 +1,14 @@ +package shopsolid; + +import java.util.List; + +public class ProductChecker implements Checker { + + public int checkProduct(List productsList, Product product) { + int productIndex = productsList.size() + 1; + if (productsList.contains(product)) { + productIndex = productsList.indexOf(product); + } + return productIndex; + } +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/ProductPreparer.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/ProductPreparer.java new file mode 100644 index 0000000..41695c4 --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/ProductPreparer.java @@ -0,0 +1,10 @@ +package shopsolid; + +import java.util.List; + +public class ProductPreparer implements Preparer { + + public Product prepareProduct(List productsList, int productIndex) { + return productsList.get(productIndex); + } +} diff --git a/AlexSubbotin/lesson_21/src/main/java/shopsolid/Stock.java b/AlexSubbotin/lesson_21/src/main/java/shopsolid/Stock.java new file mode 100644 index 0000000..ddcdd46 --- /dev/null +++ b/AlexSubbotin/lesson_21/src/main/java/shopsolid/Stock.java @@ -0,0 +1,20 @@ +package shopsolid; + +import java.util.List; + +public class Stock { + + private List productList; + + public Stock(List productList) { + this.productList = productList; + } + + public List getProductList() { + return productList; + } + + public void addProduct(Product product) { + this.productList.add(product); + } +} diff --git a/AlexSubbotin/lesson_21/src/test/java/OrderSendControllerTest.java b/AlexSubbotin/lesson_21/src/test/java/OrderSendControllerTest.java new file mode 100644 index 0000000..e95f6fc --- /dev/null +++ b/AlexSubbotin/lesson_21/src/test/java/OrderSendControllerTest.java @@ -0,0 +1,37 @@ +import org.junit.Before; +import org.junit.Test; + + +import java.util.ArrayList; + +public class OrderSendControllerTest { + + private ArrayList shopProductsList = new ArrayList(); + private Stock stockShop = new Stock(shopProductsList); + private Product bread = new Product("White bread", 15); + private Product meat = new Product("Pork meat", 100); + private Product fish = new Product("Dorado fish", 150); + private Product milk = new Product("Cow milk", 25); + + @Before + public void initProducts() { + stockShop.addProduct(bread); + stockShop.addProduct(meat); + stockShop.addProduct(fish); + stockShop.addProduct(milk); + } + + @Test + public void sendOrderTest() { + OrderSendController orderController = new OrderSendController( + new ProductChecker(), new ProductPreparer()); + orderController.sendOrder(orderController.collectOrder(shopProductsList, meat)); + } + + @Test + public void sendOrderWithConfirmationTest() { + OrderSendControllerWithConfirmation ocwc = new OrderSendControllerWithConfirmation( + new ProductChecker(), new ProductPreparer()); + ocwc.sendOrder(ocwc.collectOrder(shopProductsList, milk)); + } +} diff --git a/AlexSubbotin/lesson_23/.idea/.gitignore b/AlexSubbotin/lesson_23/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_23/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_23/.idea/compiler.xml b/AlexSubbotin/lesson_23/.idea/compiler.xml new file mode 100644 index 0000000..3cc0c45 --- /dev/null +++ b/AlexSubbotin/lesson_23/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_23/.idea/misc.xml b/AlexSubbotin/lesson_23/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_23/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_23/.idea/vcs.xml b/AlexSubbotin/lesson_23/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_23/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_23/lesson_23.iml b/AlexSubbotin/lesson_23/lesson_23.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_23/lesson_23.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_23/pom.xml b/AlexSubbotin/lesson_23/pom.xml new file mode 100644 index 0000000..be5caf3 --- /dev/null +++ b/AlexSubbotin/lesson_23/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + org.example + lesson_23 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/AbstractCarFactory.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/AbstractCarFactory.java new file mode 100644 index 0000000..76ce7a3 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/AbstractCarFactory.java @@ -0,0 +1,22 @@ +package abstractf; + +public class AbstractCarFactory implements Factory { + + private Factory factory; + + public AbstractCarFactory(Factory factory) { + this.factory = factory; + } + + public void setFactory(Factory factory) { + this.factory = factory; + } + + public Sedan createSedan() { + return this.factory.createSedan(); + } + + public Pickup createPickup() { + return this.factory.createPickup(); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/AbstractFactoryClient.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/AbstractFactoryClient.java new file mode 100644 index 0000000..81d67c8 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/AbstractFactoryClient.java @@ -0,0 +1,19 @@ +package abstractf; + +public class AbstractFactoryClient { + + public static void main(String[] args) { + DodgeFactory dodge = new DodgeFactory(); + VAGFactory volkswagen = new VAGFactory(); + + AbstractCarFactory acf = new AbstractCarFactory(dodge); + Pickup pickup = acf.createPickup(); + System.out.println(pickup); + + acf.setFactory(volkswagen); + pickup = acf.createPickup(); + Sedan sedan = acf.createSedan(); + System.out.println(sedan); + System.out.println(pickup); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgeFactory.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgeFactory.java new file mode 100644 index 0000000..d6c241c --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgeFactory.java @@ -0,0 +1,12 @@ +package abstractf; + +public class DodgeFactory implements Factory { + + public Sedan createSedan() { + return new DodgeSedan(); + } + + public Pickup createPickup() { + return new DodgePickup(); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgePickup.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgePickup.java new file mode 100644 index 0000000..fe6f2c0 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgePickup.java @@ -0,0 +1,23 @@ +package abstractf; + +public class DodgePickup implements Pickup { + + private final String TRADEMARK = "Dodge"; + private String type; + + public DodgePickup() { + setTypePickup(); + } + + public void setTypePickup() { + this.type = "Pickup"; + } + + @Override + public String toString() { + return "DodgePickup{" + + "TRADEMARK='" + TRADEMARK + '\'' + + ", type='" + type + '\'' + + '}'; + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgeSedan.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgeSedan.java new file mode 100644 index 0000000..c65e835 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/DodgeSedan.java @@ -0,0 +1,23 @@ +package abstractf; + +public class DodgeSedan implements Sedan { + + private final String TRADEMARK = "Dodge"; + private String type; + + public DodgeSedan() { + setTypeSedan(); + } + + public void setTypeSedan() { + this.type = "Sedan"; + } + + @Override + public String toString() { + return "DodgeSedan{" + + "TRADEMARK='" + TRADEMARK + '\'' + + ", type='" + type + '\'' + + '}'; + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/Factory.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/Factory.java new file mode 100644 index 0000000..812b1db --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/Factory.java @@ -0,0 +1,7 @@ +package abstractf; + +public interface Factory { + + Sedan createSedan(); + Pickup createPickup(); +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/Pickup.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/Pickup.java new file mode 100644 index 0000000..832bf22 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/Pickup.java @@ -0,0 +1,6 @@ +package abstractf; + +public interface Pickup { + + void setTypePickup(); +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/Sedan.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/Sedan.java new file mode 100644 index 0000000..1ff5487 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/Sedan.java @@ -0,0 +1,6 @@ +package abstractf; + +public interface Sedan { + + void setTypeSedan(); +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGFactory.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGFactory.java new file mode 100644 index 0000000..0e1d2ca --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGFactory.java @@ -0,0 +1,12 @@ +package abstractf; + +public class VAGFactory implements Factory { + + public Sedan createSedan() { + return new VAGSedan(); + } + + public Pickup createPickup() { + return new VAGPickup(); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGPickup.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGPickup.java new file mode 100644 index 0000000..1975827 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGPickup.java @@ -0,0 +1,23 @@ +package abstractf; + +public class VAGPickup implements Pickup { + + private final String TRADEMARK = "Volkswagen"; + private String type; + + public VAGPickup() { + setTypePickup(); + } + + public void setTypePickup() { + this.type = "Pickup"; + } + + @Override + public String toString() { + return "VolkswagenPickup{" + + "TRADEMARK='" + TRADEMARK + '\'' + + ", type='" + type + '\'' + + '}'; + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGSedan.java b/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGSedan.java new file mode 100644 index 0000000..9904b01 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/abstractf/VAGSedan.java @@ -0,0 +1,23 @@ +package abstractf; + +public class VAGSedan implements Sedan { + + private final String TRADEMARK = "Volkswagen"; + private String type; + + public VAGSedan() { + setTypeSedan(); + } + + public void setTypeSedan() { + this.type = "Sedan"; + } + + @Override + public String toString() { + return "VolkswagenSedan{" + + "TRADEMARK='" + TRADEMARK + '\'' + + ", type='" + type + '\'' + + '}'; + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/builder/BuilderClient.java b/AlexSubbotin/lesson_23/src/main/java/builder/BuilderClient.java new file mode 100644 index 0000000..128510c --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/builder/BuilderClient.java @@ -0,0 +1,21 @@ +package builder; + +public class BuilderClient { + public static void main(String[] args) { + User newUser = User.newUser() + .setAge((byte) 37) + .setBornCity("Kyiv") + .setBornCountry("Ukraine") + .setName("Petya").build(); + + System.out.println(newUser.toString()); + + User second = User.newUser() + .setAge((byte) 20) + .setName("Vasya") + .setFootSize((byte) 45) + .setWeight((byte) 80) + .build(); + System.out.println(second); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/builder/User.java b/AlexSubbotin/lesson_23/src/main/java/builder/User.java new file mode 100644 index 0000000..e761dfd --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/builder/User.java @@ -0,0 +1,95 @@ +package builder; + +public class User { + + private String name; + private String surname; + private String bornCity; + private String bornCountry; + private String education; + private String email; + private byte age; + private byte weight; + private byte height; + private byte footSize; + + private User() { + } + + public static UserBuilder newUser() { + return new User().new UserBuilder(); + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + ", surname='" + surname + '\'' + + ", bornCity='" + bornCity + '\'' + + ", bornCountry='" + bornCountry + '\'' + + ", education='" + education + '\'' + + ", email='" + email + '\'' + + ", age=" + age + + ", weight=" + weight + + ", height=" + height + + ", footSize=" + footSize + + '}'; + } + + public class UserBuilder { + + public UserBuilder setName(String name) { + User.this.name = name; + return this; + } + + public UserBuilder setSurname(String surname) { + User.this.surname = surname; + return this; + } + + public UserBuilder setBornCity(String bornCity) { + User.this.bornCity = bornCity; + return this; + } + + public UserBuilder setBornCountry(String bornCountry) { + User.this.bornCountry = bornCountry; + return this; + } + + public UserBuilder setEducation(String education) { + User.this.education = education; + return this; + } + + public UserBuilder setEmail(String email) { + User.this.email = email; + return this; + } + + public UserBuilder setAge(byte age) { + User.this.age = age; + return this; + } + + public UserBuilder setWeight(byte weight) { + User.this.weight = weight; + return this; + } + + public UserBuilder setHeight(byte height) { + User.this.height = height; + return this; + } + + public UserBuilder setFootSize(byte footSize) { + User.this.footSize = footSize; + return this; + } + + public User build() { + return User.this; + } + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/factorym/ExcelReport.java b/AlexSubbotin/lesson_23/src/main/java/factorym/ExcelReport.java new file mode 100644 index 0000000..05c0bdb --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/factorym/ExcelReport.java @@ -0,0 +1,25 @@ +package factorym; + +public class ExcelReport implements ReportCreator { + + private Report report; + + public ExcelReport(String type) { + this.report = new Report(type); + } + + public Report createReport() { + return this.report; + } + + public void getType() { + System.out.println(this.report.getType()); + } + + @Override + public String toString() { + return "ExcelReport{" + + "report=" + report + + '}'; + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/factorym/FactoryMethodClient.java b/AlexSubbotin/lesson_23/src/main/java/factorym/FactoryMethodClient.java new file mode 100644 index 0000000..4c9d724 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/factorym/FactoryMethodClient.java @@ -0,0 +1,11 @@ +package factorym; + +public class FactoryMethodClient { + + public static void main(String[] args) { + ReportCreator rc = new PDFReport("PDF"); + rc.getType(); + rc = new ExcelReport("EXCEL"); + rc.getType(); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/factorym/PDFReport.java b/AlexSubbotin/lesson_23/src/main/java/factorym/PDFReport.java new file mode 100644 index 0000000..ca3c238 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/factorym/PDFReport.java @@ -0,0 +1,25 @@ +package factorym; + +public class PDFReport implements ReportCreator { + + private Report report; + + public PDFReport(String type) { + this.report = new Report(type); + } + + public Report createReport() { + return this.report; + } + + public void getType() { + System.out.println(this.report.getType()); + } + + @Override + public String toString() { + return "PDFReport{" + + "report=" + report + + '}'; + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/factorym/Report.java b/AlexSubbotin/lesson_23/src/main/java/factorym/Report.java new file mode 100644 index 0000000..1905b31 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/factorym/Report.java @@ -0,0 +1,21 @@ +package factorym; + +public class Report { + + private String type; + + public Report(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + @Override + public String toString() { + return "Report{" + + "type='" + type + '\'' + + '}'; + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/factorym/ReportCreator.java b/AlexSubbotin/lesson_23/src/main/java/factorym/ReportCreator.java new file mode 100644 index 0000000..69dfaec --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/factorym/ReportCreator.java @@ -0,0 +1,7 @@ +package factorym; + +public interface ReportCreator { + + Report createReport(); + void getType(); +} diff --git a/AlexSubbotin/lesson_23/src/main/java/prototype/ClientPrototype.java b/AlexSubbotin/lesson_23/src/main/java/prototype/ClientPrototype.java new file mode 100644 index 0000000..609aecd --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/prototype/ClientPrototype.java @@ -0,0 +1,17 @@ +package prototype; + +public class ClientPrototype { + + public static void main(String[] args) { + RectangularWindow first = new RectangularWindow(); + first.setHeight(10); + first.setWidth(20); + RectangularWindow second = (RectangularWindow) first.clone(); + Window window = first.clone(); + window.setColour("Black"); + + System.out.println(first); + System.out.println(second); + System.out.println(window); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/prototype/RectangularWindow.java b/AlexSubbotin/lesson_23/src/main/java/prototype/RectangularWindow.java new file mode 100644 index 0000000..d06fea5 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/prototype/RectangularWindow.java @@ -0,0 +1,39 @@ +package prototype; + +public class RectangularWindow extends Window { + + private int width; + private int height; + + public RectangularWindow() { + } + + public void setWidth(int width) { + this.width = width; + } + + public void setHeight(int height) { + this.height = height; + } + + public RectangularWindow(RectangularWindow rectangularWindow) { + super(rectangularWindow); + if (rectangularWindow != null) { + this.width = rectangularWindow.width; + this.height = rectangularWindow.height; + } + } + + @Override + public Window clone() { + return new RectangularWindow(this); + } + + @Override + public String toString() { + return "RectangularWindow{" + + "width=" + width + + ", height=" + height + + '}'; + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/prototype/RoundWindow.java b/AlexSubbotin/lesson_23/src/main/java/prototype/RoundWindow.java new file mode 100644 index 0000000..7181152 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/prototype/RoundWindow.java @@ -0,0 +1,25 @@ +package prototype; + +public class RoundWindow extends Window { + + private int radius; + + public RoundWindow() { + } + + public RoundWindow(RoundWindow roundWindow) { + super(roundWindow); + if (roundWindow != null) { + this.radius = roundWindow.radius; + } + } + + public void setRadius(int radius) { + this.radius = radius; + } + + @Override + public Window clone() { + return new RoundWindow(this); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/prototype/Window.java b/AlexSubbotin/lesson_23/src/main/java/prototype/Window.java new file mode 100644 index 0000000..57b6ae7 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/prototype/Window.java @@ -0,0 +1,21 @@ +package prototype; + +public abstract class Window { + + private String colour; + + public Window() { + } + + public Window(Window window) { + if (window != null) { + this.colour = window.colour; + } + } + + public void setColour(String colour) { + this.colour = colour; + } + + public abstract Window clone(); +} diff --git a/AlexSubbotin/lesson_23/src/main/java/singleton/ClientSingleton.java b/AlexSubbotin/lesson_23/src/main/java/singleton/ClientSingleton.java new file mode 100644 index 0000000..44d2bcc --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/singleton/ClientSingleton.java @@ -0,0 +1,11 @@ +package singleton; + +public class ClientSingleton { + + public static void main(String[] args) { + Singleton firstSingleton = Singleton.getInstance(); + System.out.println(firstSingleton.hashCode()); + Singleton secondSingleton = Singleton.getInstance(); + System.out.println(secondSingleton.hashCode()); + } +} diff --git a/AlexSubbotin/lesson_23/src/main/java/singleton/Singleton.java b/AlexSubbotin/lesson_23/src/main/java/singleton/Singleton.java new file mode 100644 index 0000000..30e8ff6 --- /dev/null +++ b/AlexSubbotin/lesson_23/src/main/java/singleton/Singleton.java @@ -0,0 +1,16 @@ +package singleton; + +public class Singleton { + + private Singleton() { + System.out.println("Singleton created."); + } + + private static class SingletonHolder { + public static final Singleton INSTANCE_HOLDER = new Singleton(); + } + + public static Singleton getInstance() { + return SingletonHolder.INSTANCE_HOLDER; + } +} diff --git a/AlexSubbotin/lesson_24/.idea/.gitignore b/AlexSubbotin/lesson_24/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_24/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_24/.idea/compiler.xml b/AlexSubbotin/lesson_24/.idea/compiler.xml new file mode 100644 index 0000000..9ac200e --- /dev/null +++ b/AlexSubbotin/lesson_24/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_24/.idea/misc.xml b/AlexSubbotin/lesson_24/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_24/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_24/.idea/uiDesigner.xml b/AlexSubbotin/lesson_24/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/AlexSubbotin/lesson_24/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_24/.idea/vcs.xml b/AlexSubbotin/lesson_24/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_24/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_24/lesson_24.iml b/AlexSubbotin/lesson_24/lesson_24.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_24/lesson_24.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_24/pom.xml b/AlexSubbotin/lesson_24/pom.xml new file mode 100644 index 0000000..e8c7ad6 --- /dev/null +++ b/AlexSubbotin/lesson_24/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + org.example + lesson_24 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_24/src/main/java/adapter/AdapterClient.java b/AlexSubbotin/lesson_24/src/main/java/adapter/AdapterClient.java new file mode 100644 index 0000000..3bc1c80 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/adapter/AdapterClient.java @@ -0,0 +1,12 @@ +package adapter; + +public class AdapterClient { + + public static void main(String[] args) { + PlugWithThreeLegs plugWithThreeLegs = + new PlugWithThreeLegs( + new PlugToSocketAdapter( + new TwoLeggedSocket())); + plugWithThreeLegs.insertPlug(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/adapter/PlugAdapter.java b/AlexSubbotin/lesson_24/src/main/java/adapter/PlugAdapter.java new file mode 100644 index 0000000..0c3fd72 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/adapter/PlugAdapter.java @@ -0,0 +1,6 @@ +package adapter; + +public interface PlugAdapter { + + void plugIntoSocket(); +} diff --git a/AlexSubbotin/lesson_24/src/main/java/adapter/PlugToSocketAdapter.java b/AlexSubbotin/lesson_24/src/main/java/adapter/PlugToSocketAdapter.java new file mode 100644 index 0000000..8af963b --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/adapter/PlugToSocketAdapter.java @@ -0,0 +1,15 @@ +package adapter; + +public class PlugToSocketAdapter implements PlugAdapter{ + + private TwoLeggedSocket twoLeggedSocket; + + public PlugToSocketAdapter(TwoLeggedSocket twoLeggedSocket) { + this.twoLeggedSocket = twoLeggedSocket; + } + + public void plugIntoSocket() { + System.out.println("Isolate one leg for changed three legged plug into two legged plug"); + this.twoLeggedSocket.acceptTwoLeggedPlug(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/adapter/PlugWithThreeLegs.java b/AlexSubbotin/lesson_24/src/main/java/adapter/PlugWithThreeLegs.java new file mode 100644 index 0000000..9d674f2 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/adapter/PlugWithThreeLegs.java @@ -0,0 +1,14 @@ +package adapter; + +public class PlugWithThreeLegs { + + private PlugAdapter plugAdapter; + + public PlugWithThreeLegs(PlugAdapter plugAdapter) { + this.plugAdapter = plugAdapter; + } + + public void insertPlug() { + this.plugAdapter.plugIntoSocket(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/adapter/TwoLeggedSocket.java b/AlexSubbotin/lesson_24/src/main/java/adapter/TwoLeggedSocket.java new file mode 100644 index 0000000..d92a728 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/adapter/TwoLeggedSocket.java @@ -0,0 +1,8 @@ +package adapter; + +public class TwoLeggedSocket { + + public void acceptTwoLeggedPlug() { + System.out.println("Two legged plug accepted."); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/bridge/BridgeClient.java b/AlexSubbotin/lesson_24/src/main/java/bridge/BridgeClient.java new file mode 100644 index 0000000..42dc6fa --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/bridge/BridgeClient.java @@ -0,0 +1,18 @@ +package bridge; + +public class BridgeClient { + public static void main(String[] args) { + + Press[] press = {new SportMagazine(new MagazineWriter()), + new Newspaper(new NewspaperWriter())}; + for (Press pr : press) { + pr.createArticles(); + } + System.out.println("===================================="); + Press[] press2 = {new SportMagazine(new NewspaperWriter()), + new Newspaper(new MagazineWriter())}; + for (Press pr : press2) { + pr.createArticles(); + } +} +} diff --git a/AlexSubbotin/lesson_24/src/main/java/bridge/MagazineWriter.java b/AlexSubbotin/lesson_24/src/main/java/bridge/MagazineWriter.java new file mode 100644 index 0000000..baf016f --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/bridge/MagazineWriter.java @@ -0,0 +1,8 @@ +package bridge; + +public class MagazineWriter implements Writer { + + public void writeArticle() { + System.out.println("Magazine writer writes an article..."); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/bridge/Newspaper.java b/AlexSubbotin/lesson_24/src/main/java/bridge/Newspaper.java new file mode 100644 index 0000000..eda364c --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/bridge/Newspaper.java @@ -0,0 +1,13 @@ +package bridge; + +public class Newspaper extends Press { + + public Newspaper(Writer writer) { + super(writer); + } + + public void createArticles() { + System.out.println("Newspaper in progress..."); + writer.writeArticle(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/bridge/NewspaperWriter.java b/AlexSubbotin/lesson_24/src/main/java/bridge/NewspaperWriter.java new file mode 100644 index 0000000..379841e --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/bridge/NewspaperWriter.java @@ -0,0 +1,8 @@ +package bridge; + +public class NewspaperWriter implements Writer { + + public void writeArticle() { + System.out.println("Newspaper writer writes an article..."); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/bridge/Press.java b/AlexSubbotin/lesson_24/src/main/java/bridge/Press.java new file mode 100644 index 0000000..7f4c9ab --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/bridge/Press.java @@ -0,0 +1,12 @@ +package bridge; + +public abstract class Press { + + protected Writer writer; + + public Press(Writer writer) { + this.writer = writer; + } + + public abstract void createArticles(); +} diff --git a/AlexSubbotin/lesson_24/src/main/java/bridge/SportMagazine.java b/AlexSubbotin/lesson_24/src/main/java/bridge/SportMagazine.java new file mode 100644 index 0000000..921322f --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/bridge/SportMagazine.java @@ -0,0 +1,13 @@ +package bridge; + +public class SportMagazine extends Press { + + public SportMagazine(Writer writer) { + super(writer); + } + + public void createArticles() { + System.out.println("Sport magazine in progress..."); + writer.writeArticle(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/bridge/Writer.java b/AlexSubbotin/lesson_24/src/main/java/bridge/Writer.java new file mode 100644 index 0000000..f744f4b --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/bridge/Writer.java @@ -0,0 +1,6 @@ +package bridge; + +public interface Writer { + + void writeArticle(); +} diff --git a/AlexSubbotin/lesson_24/src/main/java/composite/CompositeClient.java b/AlexSubbotin/lesson_24/src/main/java/composite/CompositeClient.java new file mode 100644 index 0000000..34246de --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/composite/CompositeClient.java @@ -0,0 +1,18 @@ +package composite; + +public class CompositeClient { + public static void main(String[] args) { + + NewspaperFactory newspaperFactory = new NewspaperFactory(); + Writer firstNewsWriter = new NewsWriter(); + Writer secondNewsWriter = new NewsWriter(); + Writer sportNewsWriter = new SportNewsWriter(); + + newspaperFactory.addWriter(firstNewsWriter); + newspaperFactory.addWriter(secondNewsWriter); + newspaperFactory.addWriter(sportNewsWriter); + newspaperFactory.removeWriter(secondNewsWriter); + + newspaperFactory.createNewspaper(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/composite/NewsWriter.java b/AlexSubbotin/lesson_24/src/main/java/composite/NewsWriter.java new file mode 100644 index 0000000..cec5e92 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/composite/NewsWriter.java @@ -0,0 +1,8 @@ +package composite; + +public class NewsWriter implements Writer { + + public void writeArticle() { + System.out.println("News writer write an news article..."); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/composite/NewspaperFactory.java b/AlexSubbotin/lesson_24/src/main/java/composite/NewspaperFactory.java new file mode 100644 index 0000000..8eb917d --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/composite/NewspaperFactory.java @@ -0,0 +1,25 @@ +package composite; + +import java.util.ArrayList; +import java.util.List; + +public class NewspaperFactory { + + private List writers = new ArrayList(); + + public void addWriter(Writer writer) { + writers.add(writer); + } + + public void removeWriter(Writer writer) { + writers.remove(writer); + } + + public void createNewspaper() { + System.out.println("Factory creates newspaper...\n "); + + for (Writer writers : writers) { + writers.writeArticle(); + } + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/composite/SportNewsWriter.java b/AlexSubbotin/lesson_24/src/main/java/composite/SportNewsWriter.java new file mode 100644 index 0000000..42f6df2 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/composite/SportNewsWriter.java @@ -0,0 +1,8 @@ +package composite; + +public class SportNewsWriter implements Writer { + + public void writeArticle() { + System.out.println("Sport news writer writes an sport article... "); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/composite/Writer.java b/AlexSubbotin/lesson_24/src/main/java/composite/Writer.java new file mode 100644 index 0000000..7abd9c2 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/composite/Writer.java @@ -0,0 +1,6 @@ +package composite; + +public interface Writer { + + void writeArticle(); +} diff --git a/AlexSubbotin/lesson_24/src/main/java/decorator/DecoratorClient.java b/AlexSubbotin/lesson_24/src/main/java/decorator/DecoratorClient.java new file mode 100644 index 0000000..511ab85 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/decorator/DecoratorClient.java @@ -0,0 +1,11 @@ +package decorator; + +public class DecoratorClient { + public static void main(String[] args) { + ReportSender reportSender = new SmsConfirmSender( + new EmailReportDuplicator( + new PaperReport())); + + System.out.println(reportSender.sendReport()); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/decorator/EmailReportDuplicator.java b/AlexSubbotin/lesson_24/src/main/java/decorator/EmailReportDuplicator.java new file mode 100644 index 0000000..b3cb112 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/decorator/EmailReportDuplicator.java @@ -0,0 +1,17 @@ +package decorator; + +public class EmailReportDuplicator extends ReportDecorator { + + public EmailReportDuplicator(ReportSender reportSender) { + super(reportSender); + } + + public String sendEmail() { + return "Duplicate of report sent on email... "; + } + + @Override + public String sendReport() { + return super.sendReport() + sendEmail(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/decorator/PaperReport.java b/AlexSubbotin/lesson_24/src/main/java/decorator/PaperReport.java new file mode 100644 index 0000000..cee0b46 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/decorator/PaperReport.java @@ -0,0 +1,8 @@ +package decorator; + +public class PaperReport implements ReportSender { + + public String sendReport() { + return "Report on paper sent... "; + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/decorator/ReportDecorator.java b/AlexSubbotin/lesson_24/src/main/java/decorator/ReportDecorator.java new file mode 100644 index 0000000..aea4bcf --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/decorator/ReportDecorator.java @@ -0,0 +1,14 @@ +package decorator; + +public class ReportDecorator implements ReportSender { + + private ReportSender reportSender; + + public ReportDecorator(ReportSender reportSender) { + this.reportSender = reportSender; + } + + public String sendReport() { + return this.reportSender.sendReport(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/decorator/ReportSender.java b/AlexSubbotin/lesson_24/src/main/java/decorator/ReportSender.java new file mode 100644 index 0000000..3f6d79e --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/decorator/ReportSender.java @@ -0,0 +1,5 @@ +package decorator; + +public interface ReportSender { + String sendReport (); +} diff --git a/AlexSubbotin/lesson_24/src/main/java/decorator/SmsConfirmSender.java b/AlexSubbotin/lesson_24/src/main/java/decorator/SmsConfirmSender.java new file mode 100644 index 0000000..bf55185 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/decorator/SmsConfirmSender.java @@ -0,0 +1,17 @@ +package decorator; + +public class SmsConfirmSender extends ReportDecorator { + + public SmsConfirmSender(ReportSender reportSender) { + super(reportSender); + } + + public String sendSmsConfirmation() { + return "Confirmation from the sent report has been sent.."; + } + + @Override + public String sendReport() { + return super.sendReport() + sendSmsConfirmation(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/facade/CarAssembler.java b/AlexSubbotin/lesson_24/src/main/java/facade/CarAssembler.java new file mode 100644 index 0000000..781000a --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/facade/CarAssembler.java @@ -0,0 +1,20 @@ +package facade; + +public class CarAssembler { + + private boolean assemblyComplete; + + public boolean isAssemblyComplete() { + return assemblyComplete; + } + + public void doAssemble(CarBodyPainter carBodyPainter) { + if (carBodyPainter.isFinishedPainting()) { + System.out.println("Assembly in progress..."); + this.assemblyComplete = true; + } else { + System.out.println("Assembly is not possible, finish painting..."); + this.assemblyComplete = false; + } + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/facade/CarBodyPainter.java b/AlexSubbotin/lesson_24/src/main/java/facade/CarBodyPainter.java new file mode 100644 index 0000000..974d674 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/facade/CarBodyPainter.java @@ -0,0 +1,20 @@ +package facade; + +public class CarBodyPainter { + + private boolean finishedPainting; + + public boolean isFinishedPainting() { + return finishedPainting; + } + + public void startPaint() { + System.out.println("Painting in progress..."); + this.finishedPainting = false; + } + + public void finishPaint() { + System.out.println("Painting is finished."); + this.finishedPainting = true; + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/facade/CarWasher.java b/AlexSubbotin/lesson_24/src/main/java/facade/CarWasher.java new file mode 100644 index 0000000..8d35ea4 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/facade/CarWasher.java @@ -0,0 +1,13 @@ +package facade; + +public class CarWasher { + + public void washCar(CarAssembler carAssembler) { + if (carAssembler.isAssemblyComplete()) { + System.out.println("The car is ready for sale!"); + + } else { + System.out.println("The car is not ready for sale, finish all process..."); + } + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/facade/FacadeClient.java b/AlexSubbotin/lesson_24/src/main/java/facade/FacadeClient.java new file mode 100644 index 0000000..8dbd465 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/facade/FacadeClient.java @@ -0,0 +1,10 @@ +package facade; + +public class FacadeClient { + + public static void main(String[] args) { + Workflow workflow = new Workflow(); + workflow.prepareCar(); + } + +} diff --git a/AlexSubbotin/lesson_24/src/main/java/facade/Workflow.java b/AlexSubbotin/lesson_24/src/main/java/facade/Workflow.java new file mode 100644 index 0000000..e0171b7 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/facade/Workflow.java @@ -0,0 +1,15 @@ +package facade; + +public class Workflow { + + private CarBodyPainter cbp = new CarBodyPainter(); + private CarAssembler ca = new CarAssembler(); + private CarWasher cw = new CarWasher(); + + public void prepareCar() { + cbp.startPaint(); + cbp.finishPaint(); + ca.doAssemble(cbp); + cw.washCar(ca); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/flyweight/Archer.java b/AlexSubbotin/lesson_24/src/main/java/flyweight/Archer.java new file mode 100644 index 0000000..21507f6 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/flyweight/Archer.java @@ -0,0 +1,8 @@ +package flyweight; + +public class Archer implements Hero { + + public void move() { + System.out.println("Archer move ..."); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/flyweight/FlyweightClient.java b/AlexSubbotin/lesson_24/src/main/java/flyweight/FlyweightClient.java new file mode 100644 index 0000000..e42d156 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/flyweight/FlyweightClient.java @@ -0,0 +1,20 @@ +package flyweight; + +import java.util.ArrayList; +import java.util.List; + +public class FlyweightClient { + + public static void main(String[] args) { + HeroFactory heroFactory = new HeroFactory(); + List heroes = new ArrayList(); + for (int i = 0; i < 10; i++) { + heroes.add(heroFactory.takeHeroByClassName("Archer")); + heroes.add(heroFactory.takeHeroByClassName("Knight")); + } + + for (Hero hero : heroes) { + hero.move(); + } + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/flyweight/Hero.java b/AlexSubbotin/lesson_24/src/main/java/flyweight/Hero.java new file mode 100644 index 0000000..102a8b6 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/flyweight/Hero.java @@ -0,0 +1,6 @@ +package flyweight; + +public interface Hero { + + void move (); +} diff --git a/AlexSubbotin/lesson_24/src/main/java/flyweight/HeroFactory.java b/AlexSubbotin/lesson_24/src/main/java/flyweight/HeroFactory.java new file mode 100644 index 0000000..82bc609 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/flyweight/HeroFactory.java @@ -0,0 +1,24 @@ +package flyweight; + +import java.util.HashMap; +import java.util.Map; + +public class HeroFactory { + + private static final Map heroes = new HashMap(); + + public Hero takeHeroByClassName(String warriorName) { + Hero hero = heroes.get(warriorName); + if (hero == null) { + if ("Knight".equals(warriorName)) { + System.out.println("Take Knight..."); + hero = new Knight(); + } else if ("Archer".equals(warriorName)) { + System.out.println("Take Archer..."); + hero = new Archer(); + } + heroes.put(warriorName, hero); + } + return hero; + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/flyweight/Knight.java b/AlexSubbotin/lesson_24/src/main/java/flyweight/Knight.java new file mode 100644 index 0000000..8364e58 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/flyweight/Knight.java @@ -0,0 +1,8 @@ +package flyweight; + +public class Knight implements Hero { + + public void move() { + System.out.println("Knight move ..."); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/proxy/Car.java b/AlexSubbotin/lesson_24/src/main/java/proxy/Car.java new file mode 100644 index 0000000..9fdd116 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/proxy/Car.java @@ -0,0 +1,6 @@ +package proxy; + +public interface Car { + + void move(); +} diff --git a/AlexSubbotin/lesson_24/src/main/java/proxy/ProxyClient.java b/AlexSubbotin/lesson_24/src/main/java/proxy/ProxyClient.java new file mode 100644 index 0000000..a212b11 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/proxy/ProxyClient.java @@ -0,0 +1,9 @@ +package proxy; + +public class ProxyClient { + + public static void main(String[] args) { + Car car = new ProxyVAG(true); + car.move(); + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/proxy/ProxyVAG.java b/AlexSubbotin/lesson_24/src/main/java/proxy/ProxyVAG.java new file mode 100644 index 0000000..8e42764 --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/proxy/ProxyVAG.java @@ -0,0 +1,18 @@ +package proxy; + +public class ProxyVAG implements Car { + + private boolean turnOffAlarm = false; + private VAG volvo; + + public ProxyVAG(boolean turnOffAlarm) { + this.turnOffAlarm = turnOffAlarm; + } + + public void move() { + if (volvo == null) { + volvo = new VAG(turnOffAlarm); + volvo.move(); + } + } +} diff --git a/AlexSubbotin/lesson_24/src/main/java/proxy/VAG.java b/AlexSubbotin/lesson_24/src/main/java/proxy/VAG.java new file mode 100644 index 0000000..e4c3fdd --- /dev/null +++ b/AlexSubbotin/lesson_24/src/main/java/proxy/VAG.java @@ -0,0 +1,21 @@ +package proxy; + +public class VAG implements Car { + + private boolean turnOffAlarm = false; + + public VAG (boolean turnOffAlarm) { + this.turnOffAlarm = turnOffAlarm; + startEngine(); + } + + public void startEngine() { + if (turnOffAlarm) { + System.out.println("Engine started..."); + } + } + + public void move() { + System.out.println("Car is moving..."); + } +} diff --git a/AlexSubbotin/lesson_25/.idea/.gitignore b/AlexSubbotin/lesson_25/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_25/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_25/.idea/compiler.xml b/AlexSubbotin/lesson_25/.idea/compiler.xml new file mode 100644 index 0000000..db19eb9 --- /dev/null +++ b/AlexSubbotin/lesson_25/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_25/.idea/misc.xml b/AlexSubbotin/lesson_25/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_25/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_25/.idea/uiDesigner.xml b/AlexSubbotin/lesson_25/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/AlexSubbotin/lesson_25/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_25/.idea/vcs.xml b/AlexSubbotin/lesson_25/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_25/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_25/lesson_25.iml b/AlexSubbotin/lesson_25/lesson_25.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_25/lesson_25.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_25/pom.xml b/AlexSubbotin/lesson_25/pom.xml new file mode 100644 index 0000000..c1b3e5f --- /dev/null +++ b/AlexSubbotin/lesson_25/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + org.example + lesson_25 + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Client.java b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Client.java new file mode 100644 index 0000000..4cf1361 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Client.java @@ -0,0 +1,17 @@ +package chainOfResposibility; + +public class Client { + + public static void main(String[] args) { + Notifier reportNotifier = new SimpleReportNotifier(Priority.ROUTINE); + Notifier emailNotifier = new EmailNotifier(Priority.IMPORTANT); + Notifier smsNotifier = new SMSNotifier(Priority.ASAP); + + reportNotifier.setNextNotifier(emailNotifier); + emailNotifier.setNextNotifier(smsNotifier); + + reportNotifier.notifyManager("Everything is OK", Priority.ROUTINE); + reportNotifier.notifyManager("Something went wrong!", Priority.IMPORTANT); + reportNotifier.notifyManager("We had a problem here!!!", Priority.ASAP); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/EmailNotifier.java b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/EmailNotifier.java new file mode 100644 index 0000000..d9e9c0e --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/EmailNotifier.java @@ -0,0 +1,11 @@ +package chainOfResposibility; + +public class EmailNotifier extends Notifier{ + public EmailNotifier(int priority) { + super(priority); + } + + public void write(String massage) { + System.out.println("Sending email: " + massage); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Notifier.java b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Notifier.java new file mode 100644 index 0000000..dbc4abf --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Notifier.java @@ -0,0 +1,25 @@ +package chainOfResposibility; + +public abstract class Notifier { + private int priority; + private Notifier nextNotifier; + + public Notifier(int priority) { + this.priority = priority; + } + + public void setNextNotifier(Notifier nextNotifier) { + this.nextNotifier = nextNotifier; + } + + public void notifyManager(String massage, int level) { + if (level >= priority) { + write(massage); + } + if (nextNotifier != null) { + nextNotifier.notifyManager(massage, level); + } + } + + public abstract void write(String massage); +} diff --git a/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Priority.java b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Priority.java new file mode 100644 index 0000000..7a347ac --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/Priority.java @@ -0,0 +1,8 @@ +package chainOfResposibility; + +public class Priority { + + public static final int ROUTINE = 1; + public static final int IMPORTANT = 2; + public static final int ASAP = 3; +} diff --git a/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/SMSNotifier.java b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/SMSNotifier.java new file mode 100644 index 0000000..094f8e9 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/SMSNotifier.java @@ -0,0 +1,12 @@ +package chainOfResposibility; + +public class SMSNotifier extends Notifier { + + public SMSNotifier(int priority) { + super(priority); + } + + public void write(String massage) { + System.out.println("Sending SMS to manager: " + massage); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/SimpleReportNotifier.java b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/SimpleReportNotifier.java new file mode 100644 index 0000000..3632cda --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/chainOfResposibility/SimpleReportNotifier.java @@ -0,0 +1,11 @@ +package chainOfResposibility; + +public class SimpleReportNotifier extends Notifier { + public SimpleReportNotifier(int priority) { + super(priority); + } + + public void write(String massage) { + System.out.println("Notifying using simple report: " + massage); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/command/Client.java b/AlexSubbotin/lesson_25/src/main/java/command/Client.java new file mode 100644 index 0000000..de4b9fa --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/command/Client.java @@ -0,0 +1,16 @@ +package command; + +public class Client { + + public static void main(String[] args) { + Database database = new Database(); + + Invoker developer = new Invoker(new InsertCommand(database), (Command) new UpdateCommand (database), + new SelectCommand(database), new DeleteCommand(database)); + + developer.insertRecord(); + developer.updateRecord(); + developer.selectRecord(); + developer.deleteRecord(); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/command/Command.java b/AlexSubbotin/lesson_25/src/main/java/command/Command.java new file mode 100644 index 0000000..2d2f1c5 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/command/Command.java @@ -0,0 +1,6 @@ +package command; + +public interface Command { + + void execute(); +} diff --git a/AlexSubbotin/lesson_25/src/main/java/command/Database.java b/AlexSubbotin/lesson_25/src/main/java/command/Database.java new file mode 100644 index 0000000..ed7e558 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/command/Database.java @@ -0,0 +1,20 @@ +package command; + +public class Database { + + public void insert() { + System.out.println("inserting record..."); + } + + public void update() { + System.out.println("Updating record..."); + } + + public void select() { + System.out.println("Reading record..."); + } + + public void delete() { + System.out.println("Deleting record..."); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/command/DeleteCommand.java b/AlexSubbotin/lesson_25/src/main/java/command/DeleteCommand.java new file mode 100644 index 0000000..1e9d443 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/command/DeleteCommand.java @@ -0,0 +1,14 @@ +package command; + +public class DeleteCommand implements Command { + + private Database database; + + public DeleteCommand(Database database) { + this.database = database; + } + + public void execute() { + this.database.delete(); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/command/InsertCommand.java b/AlexSubbotin/lesson_25/src/main/java/command/InsertCommand.java new file mode 100644 index 0000000..ce96e93 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/command/InsertCommand.java @@ -0,0 +1,13 @@ +package command; + +public class InsertCommand implements Command { + + private Database database; + + public InsertCommand(Database database) { + this.database = database; + } + public void execute() { + this.database.insert(); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/command/Invoker.java b/AlexSubbotin/lesson_25/src/main/java/command/Invoker.java new file mode 100644 index 0000000..2b68b04 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/command/Invoker.java @@ -0,0 +1,31 @@ +package command; + +public class Invoker { + private Command insert; + private Command update; + private Command select; + private Command delete; + + public Invoker(Command insert, Command update, Command select, Command delete) { + this.insert = insert; + this.update = update; + this.select = select; + this.delete = delete; + } + + public void insertRecord() { + this.insert.execute(); + } + + public void updateRecord() { + this.update.execute(); + } + + public void selectRecord() { + this.select.execute(); + } + + public void deleteRecord() { + this.delete.execute(); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/command/SelectCommand.java b/AlexSubbotin/lesson_25/src/main/java/command/SelectCommand.java new file mode 100644 index 0000000..098e2a1 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/command/SelectCommand.java @@ -0,0 +1,14 @@ +package command; + +public class SelectCommand implements Command { + + private Database database; + + public SelectCommand(Database database) { + this.database = database; + } + + public void execute() { + this.database.select(); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/command/UpdateCommand.java b/AlexSubbotin/lesson_25/src/main/java/command/UpdateCommand.java new file mode 100644 index 0000000..7c4f258 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/command/UpdateCommand.java @@ -0,0 +1,14 @@ +package command; + +public class UpdateCommand { + + private Database database; + + public UpdateCommand( Database database) { + this.database = database; + } + + public void execute() { + this.database.update(); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/interpreter/BinaryExpression.java b/AlexSubbotin/lesson_25/src/main/java/interpreter/BinaryExpression.java new file mode 100644 index 0000000..d097b1e --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/interpreter/BinaryExpression.java @@ -0,0 +1,15 @@ +package interpreter; + +public class BinaryExpression implements Expression { + + private int number; + + public BinaryExpression(int number) { + this.number = number; + } + + @Override + public String interpret(Context context) { + return context.getBinaryExpression(number); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/interpreter/Client.java b/AlexSubbotin/lesson_25/src/main/java/interpreter/Client.java new file mode 100644 index 0000000..acfb679 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/interpreter/Client.java @@ -0,0 +1,39 @@ +package interpreter; + +public class Client { + + private Context context; + + public Client(Context context) { + this.context = context; + } + + public static void main(String[] args) { + String binary = "256 in Binary: "; + String octal = "256 in Octal: "; + String hexadecimal = "256 in Hexadecimal: "; + + Client expression = new Client(new Context()); + System.out.println(binary + expression.interpret(binary)); + System.out.println(octal + expression.interpret(octal)); + System.out.println(hexadecimal + expression.interpret(hexadecimal)); + } + + private String interpret(String text) { + Expression expression; + + int number = Integer.parseInt(text.substring(0, text.indexOf(" "))); + + if (text.contains("Binary")) { + expression = new BinaryExpression(number); + } else if (text.contains("Octal")) { + expression = new OctalExpression(number); + } else if (text.contains("Hexadecimal")) { + expression = new HexadecimalExpression(number); + } else { + return text; + } + + return expression.interpret(context); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/interpreter/Context.java b/AlexSubbotin/lesson_25/src/main/java/interpreter/Context.java new file mode 100644 index 0000000..adc18ce --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/interpreter/Context.java @@ -0,0 +1,16 @@ +package interpreter; + +public class Context { + + public String getBinaryExpression(int number) { + return Integer.toBinaryString(number); + } + + public String getOctalExpression(int number) { + return Integer.toOctalString(number); + } + + public String getHexadecimalExpression(int number) { + return Integer.toHexString(number); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/interpreter/Expression.java b/AlexSubbotin/lesson_25/src/main/java/interpreter/Expression.java new file mode 100644 index 0000000..93ebbe7 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/interpreter/Expression.java @@ -0,0 +1,5 @@ +package interpreter; + +public interface Expression { + String interpret(Context context); +} \ No newline at end of file diff --git a/AlexSubbotin/lesson_25/src/main/java/interpreter/HexadecimalExpression.java b/AlexSubbotin/lesson_25/src/main/java/interpreter/HexadecimalExpression.java new file mode 100644 index 0000000..6289f97 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/interpreter/HexadecimalExpression.java @@ -0,0 +1,15 @@ +package interpreter; + +public class HexadecimalExpression implements Expression { + + private int number; + + public HexadecimalExpression(int number) { + this.number = number; + } + + @Override + public String interpret(Context context) { + return context.getHexadecimalExpression(number); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/interpreter/OctalExpression.java b/AlexSubbotin/lesson_25/src/main/java/interpreter/OctalExpression.java new file mode 100644 index 0000000..c257755 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/interpreter/OctalExpression.java @@ -0,0 +1,15 @@ +package interpreter; + +public class OctalExpression implements Expression { + + private int number; + + public OctalExpression(int number) { + this.number = number; + } + + @Override + public String interpret(Context context) { + return context.getOctalExpression(number); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/iterator/Client.java b/AlexSubbotin/lesson_25/src/main/java/iterator/Client.java new file mode 100644 index 0000000..765bdd0 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/iterator/Client.java @@ -0,0 +1,17 @@ +package iterator; + +public class Client { + + public static void main(String[] args) { + String[] skills = {"Java Core", "OOP", "Maven", "SQL", "Algorithms", "Patterns"}; + JavaDeveloper javaDeveloper = new JavaDeveloper("Alex", skills); + + Iterator iterator = javaDeveloper.getIterator(); + System.out.println("Developer: " + javaDeveloper.getName()); + System.out.println("Skills: "); + + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/iterator/Collection.java b/AlexSubbotin/lesson_25/src/main/java/iterator/Collection.java new file mode 100644 index 0000000..c405b0f --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/iterator/Collection.java @@ -0,0 +1,6 @@ +package iterator; + +public interface Collection { + + Iterator getIterator(); +} diff --git a/AlexSubbotin/lesson_25/src/main/java/iterator/Iterator.java b/AlexSubbotin/lesson_25/src/main/java/iterator/Iterator.java new file mode 100644 index 0000000..987f9c2 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/iterator/Iterator.java @@ -0,0 +1,7 @@ +package iterator; + +public interface Iterator { + + boolean hasNext(); + Object next(); +} diff --git a/AlexSubbotin/lesson_25/src/main/java/iterator/JavaDeveloper.java b/AlexSubbotin/lesson_25/src/main/java/iterator/JavaDeveloper.java new file mode 100644 index 0000000..e58ad8e --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/iterator/JavaDeveloper.java @@ -0,0 +1,50 @@ +package iterator; + +public class JavaDeveloper implements Collection { + + private String name; + private String[] skills; + + public JavaDeveloper(String name, String[] skills) { + this.name = name; + this.skills = skills; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String[] getSkills() { + return skills; + } + + public void setSkills(String[] skills) { + this.skills = skills; + } + + @Override + public Iterator getIterator() { + return new SkillIterator(); + } + + private class SkillIterator implements Iterator { + private int index; + + @Override + public boolean hasNext() { + if (index < skills.length) { + return true; + } + return false; + } + + @Override + public Object next() { + return skills[index++]; + } + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/memento/Client.java b/AlexSubbotin/lesson_25/src/main/java/memento/Client.java new file mode 100644 index 0000000..b2624c2 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/memento/Client.java @@ -0,0 +1,27 @@ +package memento; + +public class Client { + + public static void main(String[] args) throws InterruptedException { + Project project = new Project(); + GitHubRepo gitHubRepo = new GitHubRepo(); + + System.out.println("Creating project. Version 1.0"); + project.setVersionAndDate("Version 1.0"); + System.out.println(project); + + System.out.println("Saving current version..."); + gitHubRepo.setSave(project.save()); + + System.out.println("Updating project to version 1.1"); + Thread.sleep(5000); + project.setVersionAndDate("Version 1.1"); + System.out.println(project); + + System.out.println("Rolling back to Version 1.0"); + project.load(gitHubRepo.getSave()); + + System.out.println("Project after roll back"); + System.out.println(project); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/memento/GitHubRepo.java b/AlexSubbotin/lesson_25/src/main/java/memento/GitHubRepo.java new file mode 100644 index 0000000..d568e9f --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/memento/GitHubRepo.java @@ -0,0 +1,14 @@ +package memento; + +public class GitHubRepo { + + private Save save; + + public Save getSave() { + return save; + } + + public void setSave(Save save) { + this.save = save; + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/memento/Project.java b/AlexSubbotin/lesson_25/src/main/java/memento/Project.java new file mode 100644 index 0000000..52ab8d8 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/memento/Project.java @@ -0,0 +1,30 @@ +package memento; + +import java.util.Date; + +public class Project { + + private String version; + private Date date; + + public void setVersionAndDate(String version) { + this.version = version; + this.date = new Date(); + } + + public Save save() { + return new Save(version); + } + + public void load(Save save) { + this.version = save.getVersion(); + this.date = save.getDate(); + } + + @Override + public String toString() { + return "Project: " + + "\nVersion: " + version + + "\nDate: " + date + "\n"; + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/memento/Save.java b/AlexSubbotin/lesson_25/src/main/java/memento/Save.java new file mode 100644 index 0000000..abb1eaa --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/memento/Save.java @@ -0,0 +1,22 @@ +package memento; + +import java.util.Date; + +public class Save { + + private final String version; + private final Date date; + + public Save(String version) { + this.version = version; + this.date = new Date(); + } + + public String getVersion() { + return version; + } + + public Date getDate() { + return date; + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/templateMethod/Client.java b/AlexSubbotin/lesson_25/src/main/java/templateMethod/Client.java new file mode 100644 index 0000000..894bc78 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/templateMethod/Client.java @@ -0,0 +1,12 @@ +package templateMethod; + +public class Client { + + public static void main(String[] args) { + HouseTemplate houseType = new WoodenHouse(); + houseType.buildHouse(); + + houseType = new GlassHouse(); + houseType.buildHouse(); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/templateMethod/GlassHouse.java b/AlexSubbotin/lesson_25/src/main/java/templateMethod/GlassHouse.java new file mode 100644 index 0000000..bbc8e5a --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/templateMethod/GlassHouse.java @@ -0,0 +1,14 @@ +package templateMethod; + +public class GlassHouse extends HouseTemplate { + + @Override + public void buildWalls() { + System.out.println("Building Glass Walls"); + } + + @Override + public void buildPillars() { + System.out.println("Building Pillars with glass coating"); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/templateMethod/HouseTemplate.java b/AlexSubbotin/lesson_25/src/main/java/templateMethod/HouseTemplate.java new file mode 100644 index 0000000..8758df6 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/templateMethod/HouseTemplate.java @@ -0,0 +1,25 @@ +package templateMethod; + +public abstract class HouseTemplate { + + public final void buildHouse() { + buildFoundation(); + buildPillars(); + buildWalls(); + buildWindows(); + System.out.println("House is built."); + } + + private void buildWindows() { + System.out.println("Building Glass Windows"); + } + + public abstract void buildWalls(); + + public abstract void buildPillars(); + + private void buildFoundation() { + System.out.println("Build foundation with cement," + + "iron rods and sand"); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/templateMethod/WoodenHouse.java b/AlexSubbotin/lesson_25/src/main/java/templateMethod/WoodenHouse.java new file mode 100644 index 0000000..b59e0d1 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/templateMethod/WoodenHouse.java @@ -0,0 +1,14 @@ +package templateMethod; + +public class WoodenHouse extends HouseTemplate { + + @Override + public void buildWalls() { + System.out.println("Building Wooden Walls"); + } + + @Override + public void buildPillars() { + System.out.println("Building Pillars with Wood coating"); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/Client.java b/AlexSubbotin/lesson_25/src/main/java/visitor/Client.java new file mode 100644 index 0000000..73876e0 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/Client.java @@ -0,0 +1,18 @@ +package visitor; + +public class Client { + + public static void main(String[] args) { + Project project = new Project(); + + Developer junior = new JuniorDeveloper(); + Developer senior = new SeniorDeveloper(); + + System.out.println("Junior in action..."); + project.beWritten(junior); + + System.out.println("Senior in action...."); + project.beWritten(senior); + + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/Database.java b/AlexSubbotin/lesson_25/src/main/java/visitor/Database.java new file mode 100644 index 0000000..cc683d3 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/Database.java @@ -0,0 +1,9 @@ +package visitor; + +public class Database implements ProjectElement { + + @Override + public void beWritten(Developer developer) { + developer.create(this); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/Developer.java b/AlexSubbotin/lesson_25/src/main/java/visitor/Developer.java new file mode 100644 index 0000000..68af30d --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/Developer.java @@ -0,0 +1,10 @@ +package visitor; + +public interface Developer { + + void create(ProjectClass projectClass); + + void create(Database database); + + void create(Test test); +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/JuniorDeveloper.java b/AlexSubbotin/lesson_25/src/main/java/visitor/JuniorDeveloper.java new file mode 100644 index 0000000..1d70086 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/JuniorDeveloper.java @@ -0,0 +1,19 @@ +package visitor; + +public class JuniorDeveloper implements Developer { + + @Override + public void create(ProjectClass projectClass) { + System.out.println("Writing poor code..."); + } + + @Override + public void create(Database database) { + System.out.println("Drop database..."); + } + + @Override + public void create(Test test) { + System.out.println("Creating not reliable test..."); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/Project.java b/AlexSubbotin/lesson_25/src/main/java/visitor/Project.java new file mode 100644 index 0000000..57a2044 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/Project.java @@ -0,0 +1,21 @@ +package visitor; + +public class Project implements ProjectElement { + + ProjectElement[] projectElements; + + public Project() { + this.projectElements = new ProjectElement[] { + new ProjectClass(), + new Database(), + new Test() + }; + } + + @Override + public void beWritten(Developer developer) { + for (ProjectElement element : projectElements) { + element.beWritten(developer); + } + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/ProjectClass.java b/AlexSubbotin/lesson_25/src/main/java/visitor/ProjectClass.java new file mode 100644 index 0000000..7babdde --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/ProjectClass.java @@ -0,0 +1,9 @@ +package visitor; + +public class ProjectClass implements ProjectElement { + + @Override + public void beWritten(Developer developer) { + developer.create(this); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/ProjectElement.java b/AlexSubbotin/lesson_25/src/main/java/visitor/ProjectElement.java new file mode 100644 index 0000000..e6fe350 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/ProjectElement.java @@ -0,0 +1,6 @@ +package visitor; + +public interface ProjectElement { + + void beWritten(Developer developer); +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/SeniorDeveloper.java b/AlexSubbotin/lesson_25/src/main/java/visitor/SeniorDeveloper.java new file mode 100644 index 0000000..d5f2407 --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/SeniorDeveloper.java @@ -0,0 +1,19 @@ +package visitor; + +public class SeniorDeveloper implements Developer { + + @Override + public void create(ProjectClass projectClass) { + System.out.println("Rewriting code after junior..."); + } + + @Override + public void create(Database database) { + System.out.println("Fixing database..."); + } + + @Override + public void create(Test test) { + System.out.println("Creating reliable test..."); + } +} diff --git a/AlexSubbotin/lesson_25/src/main/java/visitor/Test.java b/AlexSubbotin/lesson_25/src/main/java/visitor/Test.java new file mode 100644 index 0000000..8d02b5a --- /dev/null +++ b/AlexSubbotin/lesson_25/src/main/java/visitor/Test.java @@ -0,0 +1,9 @@ +package visitor; + +public class Test implements ProjectElement { + + @Override + public void beWritten(Developer developer) { + developer.create(this); + } +} diff --git a/AlexSubbotin/lesson_26/.idea/.gitignore b/AlexSubbotin/lesson_26/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_26/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_26/.idea/compiler.xml b/AlexSubbotin/lesson_26/.idea/compiler.xml new file mode 100644 index 0000000..cd79aa3 --- /dev/null +++ b/AlexSubbotin/lesson_26/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_26/.idea/misc.xml b/AlexSubbotin/lesson_26/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/AlexSubbotin/lesson_26/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_26/.idea/vcs.xml b/AlexSubbotin/lesson_26/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_26/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_26/lesson_26.iml b/AlexSubbotin/lesson_26/lesson_26.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AlexSubbotin/lesson_26/lesson_26.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_26/pom.xml b/AlexSubbotin/lesson_26/pom.xml new file mode 100644 index 0000000..93ff174 --- /dev/null +++ b/AlexSubbotin/lesson_26/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + org.example + lesson_26 + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_26/src/main/java/mediator/Action.java b/AlexSubbotin/lesson_26/src/main/java/mediator/Action.java new file mode 100644 index 0000000..31eeec5 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/mediator/Action.java @@ -0,0 +1,12 @@ +package mediator; + +public interface Action { + + void createOrder(); + + void setOrder(Order order); + + String getInfo(); + + Order getOrder(); +} diff --git a/AlexSubbotin/lesson_26/src/main/java/mediator/Chef.java b/AlexSubbotin/lesson_26/src/main/java/mediator/Chef.java new file mode 100644 index 0000000..1de293c --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/mediator/Chef.java @@ -0,0 +1,38 @@ +package mediator; + +public class Chef implements Action { + + private Mediator mediator; + + private String name; + + private Order order; + + public Chef(Mediator mediator, String name) { + this.mediator = mediator; + this.name = name; + } + + @Override + public void createOrder() { + System.out.println(this.name + " create order: "); + for (Product product : order.getProducts()) { + System.out.println(product.getName()); + } + } + + @Override + public void setOrder(Order order) { + this.order = order; + } + + @Override + public String getInfo() { + return name; + } + + @Override + public Order getOrder() { + return order; + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/mediator/Client.java b/AlexSubbotin/lesson_26/src/main/java/mediator/Client.java new file mode 100644 index 0000000..3aab576 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/mediator/Client.java @@ -0,0 +1,43 @@ +package mediator; + +public class Client { + + public static void main(String[] args) { + Product coffee = new Product("Coffee"); + Product tea = new Product("Tea"); + Product cake = new Product("Cake"); + + Mediator mediator = new Waiter(); + + Action firstCooker = new Chef(mediator, "First cooker"); + Action secondCooker = new Chef(mediator, "Second cooker"); + Action firstVisitor = new Guest(mediator, "Bob"); + Action secondVisitor = new Guest(mediator, "Ashly"); + + Order firstOrder = new Order(firstVisitor.getInfo()); + firstOrder.addProduct(coffee); + firstOrder.addProduct(cake); + + mediator.addAction(firstVisitor, firstOrder); + mediator.addAction(firstCooker, firstOrder); + + Order secondOrder = new Order(secondVisitor.getInfo()); + secondOrder.addProduct(tea); + secondOrder.addProduct(cake); + + mediator.addAction(secondVisitor, secondOrder); + mediator.addAction(secondCooker, secondOrder); + + mediator.transferFrom(firstVisitor); + System.out.println("\n=====================\n"); + + mediator.transferFrom(firstCooker); + System.out.println("\n=====================\n"); + + mediator.transferFrom(secondVisitor); + System.out.println("\n=====================\n"); + + mediator.transferFrom(secondCooker); + System.out.println("\n=====================\n"); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/mediator/Guest.java b/AlexSubbotin/lesson_26/src/main/java/mediator/Guest.java new file mode 100644 index 0000000..7925f4b --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/mediator/Guest.java @@ -0,0 +1,38 @@ +package mediator; + +public class Guest implements Action { + + private Mediator mediator; + + private String name; + + private Order order; + + public Guest(Mediator mediator, String name) { + this.mediator = mediator; + this.name = name; + } + + @Override + public void createOrder() { + System.out.println(this.name + " making order: "); + for (Product product : this.order.getProducts()) { + System.out.println(product.getName()); + } + } + + @Override + public void setOrder(Order order) { + this.order = order; + } + + @Override + public String getInfo() { + return name; + } + + @Override + public Order getOrder() { + return order; + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/mediator/Mediator.java b/AlexSubbotin/lesson_26/src/main/java/mediator/Mediator.java new file mode 100644 index 0000000..6d1fa0c --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/mediator/Mediator.java @@ -0,0 +1,8 @@ +package mediator; + +public interface Mediator { + + void transferFrom(Action sender); + + void addAction(Action action, Order order); +} diff --git a/AlexSubbotin/lesson_26/src/main/java/mediator/Order.java b/AlexSubbotin/lesson_26/src/main/java/mediator/Order.java new file mode 100644 index 0000000..29b9db1 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/mediator/Order.java @@ -0,0 +1,36 @@ +package mediator; + + import java.util.ArrayList; + import java.util.List; + +public class Order { + + private String id; + + private List products; + + public Order(String id) { + this.id = id; + this.products = new ArrayList<>(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public List getProducts() { + return products; + } + + public void addProduct(Product product) { + this.products.add(product); + } + + public void removeProduct(Product product) { + this.products.remove(product); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/mediator/Product.java b/AlexSubbotin/lesson_26/src/main/java/mediator/Product.java new file mode 100644 index 0000000..90b4d25 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/mediator/Product.java @@ -0,0 +1,14 @@ +package mediator; + +public class Product { + + private String name; + + public Product(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/mediator/Waiter.java b/AlexSubbotin/lesson_26/src/main/java/mediator/Waiter.java new file mode 100644 index 0000000..ce5d115 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/mediator/Waiter.java @@ -0,0 +1,25 @@ +package mediator; + +import java.util.ArrayList; +import java.util.List; + +public class Waiter implements Mediator { + + private List actions = new ArrayList<>(); + + @Override + public void transferFrom(Action sender) { + for (Action action : actions) { + if (action.equals(sender)) { + sender.createOrder(); + System.out.println("Order from: " + sender.getOrder().getId()); + } + } + } + + @Override + public void addAction(Action action, Order order) { + action.setOrder(order); + this.actions.add(action); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/observer/Client.java b/AlexSubbotin/lesson_26/src/main/java/observer/Client.java new file mode 100644 index 0000000..373812e --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/observer/Client.java @@ -0,0 +1,21 @@ +package observer; + +public class Client { + + public static void main(String[] args) { + DeveloperJobSite jobSite = new DeveloperJobSite(); + + jobSite.addVacancy("First java position"); + jobSite.addVacancy("Second java position"); + + Observer firstSubscriber = new Subscriber("Bob"); + Observer secondSubscriber = new Subscriber("Tom"); + + jobSite.addObserver(firstSubscriber); + jobSite.addObserver(secondSubscriber); + + jobSite.addVacancy("Third java position"); + + jobSite.removeVacancy("First java position"); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/observer/DeveloperJobSite.java b/AlexSubbotin/lesson_26/src/main/java/observer/DeveloperJobSite.java new file mode 100644 index 0000000..c333241 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/observer/DeveloperJobSite.java @@ -0,0 +1,35 @@ +package observer; + +import java.util.ArrayList; +import java.util.List; + +public class DeveloperJobSite implements Observed { + + private List vacancies = new ArrayList<>(); + + private List subscribers = new ArrayList<>(); + + public void addVacancy(String vacancy) { + this.vacancies.add(vacancy); + notifyObservers(); + } + + public void removeVacancy(String vacancy) { + this.vacancies.remove(vacancy); + notifyObservers(); + } + + public void addObserver(Observer observer) { + this.subscribers.add(observer); + } + + public void removeObserver(Observer observer) { + this.subscribers.remove(observer); + } + + public void notifyObservers() { + for (Observer observer : subscribers) { + observer.handleEvent(this.vacancies); + } + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/observer/Observed.java b/AlexSubbotin/lesson_26/src/main/java/observer/Observed.java new file mode 100644 index 0000000..f2c9e96 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/observer/Observed.java @@ -0,0 +1,10 @@ +package observer; + +public interface Observed { + void addObserver(Observer observer); + + void removeObserver(Observer observer); + + void notifyObservers(); + +} diff --git a/AlexSubbotin/lesson_26/src/main/java/observer/Observer.java b/AlexSubbotin/lesson_26/src/main/java/observer/Observer.java new file mode 100644 index 0000000..d491019 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/observer/Observer.java @@ -0,0 +1,7 @@ +package observer; + +import java.util.List; + +public interface Observer { + void handleEvent(List vacancies); +} diff --git a/AlexSubbotin/lesson_26/src/main/java/observer/Subscriber.java b/AlexSubbotin/lesson_26/src/main/java/observer/Subscriber.java new file mode 100644 index 0000000..15cccaa --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/observer/Subscriber.java @@ -0,0 +1,17 @@ +package observer; + +import java.util.List; + +public class Subscriber implements Observer { + + private String name; + + public Subscriber(String name) { + this.name = name; + } + + public void handleEvent(List vacancies) { + System.out.println("Dear " + name + "\nWe have some changes in vacancies:\n" + + vacancies + "\n============================================\n"); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/state/Activity.java b/AlexSubbotin/lesson_26/src/main/java/state/Activity.java new file mode 100644 index 0000000..2474a1f --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/state/Activity.java @@ -0,0 +1,5 @@ +package state; + +public interface Activity { + void doAction(); +} diff --git a/AlexSubbotin/lesson_26/src/main/java/state/Client.java b/AlexSubbotin/lesson_26/src/main/java/state/Client.java new file mode 100644 index 0000000..914b505 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/state/Client.java @@ -0,0 +1,16 @@ +package state; + +public class Client { + + public static void main(String[] args) { + Activity activity = new Sleeping(); + Developer developer = new Developer(); + + developer.setActivity(activity); + + for (int i = 0; i < 10; i++) { + developer.doAction(); + developer.changeActivity(); + } + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/state/Coding.java b/AlexSubbotin/lesson_26/src/main/java/state/Coding.java new file mode 100644 index 0000000..3955da0 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/state/Coding.java @@ -0,0 +1,9 @@ +package state; + +public class Coding implements Activity { + + @Override + public void doAction() { + System.out.println("Writing code..."); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/state/Developer.java b/AlexSubbotin/lesson_26/src/main/java/state/Developer.java new file mode 100644 index 0000000..bdea21a --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/state/Developer.java @@ -0,0 +1,26 @@ +package state; + +public class Developer { + + Activity activity; + + public void setActivity(Activity activity) { + this.activity = activity; + } + + public void changeActivity() { + if (this.activity instanceof Sleeping) { + setActivity(new Training()); + } else if (this.activity instanceof Training) { + setActivity(new Coding()); + } else if (this.activity instanceof Coding) { + setActivity(new Reading()); + } else if (this.activity instanceof Reading) { + setActivity(new Sleeping()); + } + } + + public void doAction() { + this.activity.doAction(); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/state/Reading.java b/AlexSubbotin/lesson_26/src/main/java/state/Reading.java new file mode 100644 index 0000000..d54b261 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/state/Reading.java @@ -0,0 +1,9 @@ +package state; + +public class Reading implements Activity { + + @Override + public void doAction() { + System.out.println("Reading book..."); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/state/Sleeping.java b/AlexSubbotin/lesson_26/src/main/java/state/Sleeping.java new file mode 100644 index 0000000..474368b --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/state/Sleeping.java @@ -0,0 +1,9 @@ +package state; + +public class Sleeping implements Activity { + + @Override + public void doAction() { + System.out.println("Sleeping"); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/state/Training.java b/AlexSubbotin/lesson_26/src/main/java/state/Training.java new file mode 100644 index 0000000..ad7133a --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/state/Training.java @@ -0,0 +1,9 @@ +package state; + +public class Training implements Activity { + + @Override + public void doAction() { + System.out.println("Training..."); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/strategy/AggressiveStrategy.java b/AlexSubbotin/lesson_26/src/main/java/strategy/AggressiveStrategy.java new file mode 100644 index 0000000..9119bde --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/strategy/AggressiveStrategy.java @@ -0,0 +1,9 @@ +package strategy; + +public class AggressiveStrategy implements Strategy { + + @Override + public void actionCommand() { + System.out.println("Aggressive strategy. Find and kill opponent"); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/strategy/CounterStrike.java b/AlexSubbotin/lesson_26/src/main/java/strategy/CounterStrike.java new file mode 100644 index 0000000..905aba1 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/strategy/CounterStrike.java @@ -0,0 +1,24 @@ +package strategy; + +public class CounterStrike { + public static void main(String[] args) { + Player terrorist = new Player("Terrorist"); + Player counter_Terrorist = new Player("Counter-Terrorist"); + + System.out.println("Before bomb is planted"); + + terrorist.setStrategy(new AggressiveStrategy()); + counter_Terrorist.setStrategy(new DefensiveStrategy()); + + terrorist.action(); + counter_Terrorist.action(); + + System.out.println("After bomb is planted"); + + terrorist.setStrategy(new DefensiveStrategy()); + counter_Terrorist.setStrategy(new AggressiveStrategy()); + + terrorist.action(); + counter_Terrorist.action(); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/strategy/DefensiveStrategy.java b/AlexSubbotin/lesson_26/src/main/java/strategy/DefensiveStrategy.java new file mode 100644 index 0000000..13562e0 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/strategy/DefensiveStrategy.java @@ -0,0 +1,9 @@ +package strategy; + +public class DefensiveStrategy implements Strategy { + + @Override + public void actionCommand() { + System.out.println("Defensive strategy. Protect self and teammates"); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/strategy/Player.java b/AlexSubbotin/lesson_26/src/main/java/strategy/Player.java new file mode 100644 index 0000000..e4784b3 --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/strategy/Player.java @@ -0,0 +1,20 @@ +package strategy; + +public class Player { + + Strategy behavior; + String type; + + public Player(String type) { + this.type = type; + } + + public void setStrategy(Strategy behavior) { + this.behavior = behavior; + } + + public void action() { + System.out.println("Player: " + this.type); + this.behavior.actionCommand(); + } +} diff --git a/AlexSubbotin/lesson_26/src/main/java/strategy/Strategy.java b/AlexSubbotin/lesson_26/src/main/java/strategy/Strategy.java new file mode 100644 index 0000000..811deeb --- /dev/null +++ b/AlexSubbotin/lesson_26/src/main/java/strategy/Strategy.java @@ -0,0 +1,6 @@ +package strategy; + +public interface Strategy { + + void actionCommand(); +} diff --git a/AlexSubbotin/lesson_7/.idea/.gitignore b/AlexSubbotin/lesson_7/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_7/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_7/.idea/misc.xml b/AlexSubbotin/lesson_7/.idea/misc.xml new file mode 100644 index 0000000..e0844bc --- /dev/null +++ b/AlexSubbotin/lesson_7/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_7/.idea/modules.xml b/AlexSubbotin/lesson_7/.idea/modules.xml new file mode 100644 index 0000000..d2a0eb2 --- /dev/null +++ b/AlexSubbotin/lesson_7/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_7/.idea/vcs.xml b/AlexSubbotin/lesson_7/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_7/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_7/Flashlights.jar b/AlexSubbotin/lesson_7/Flashlights.jar new file mode 100644 index 0000000..8a38d2b Binary files /dev/null and b/AlexSubbotin/lesson_7/Flashlights.jar differ diff --git a/AlexSubbotin/lesson_7/lesson_7.iml b/AlexSubbotin/lesson_7/lesson_7.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/AlexSubbotin/lesson_7/lesson_7.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_7/out/production/lesson_7/Flashlights.class b/AlexSubbotin/lesson_7/out/production/lesson_7/Flashlights.class new file mode 100644 index 0000000..80a5d46 Binary files /dev/null and b/AlexSubbotin/lesson_7/out/production/lesson_7/Flashlights.class differ diff --git a/AlexSubbotin/lesson_7/out/production/lesson_7/FlashlightsProperties.class b/AlexSubbotin/lesson_7/out/production/lesson_7/FlashlightsProperties.class new file mode 100644 index 0000000..b29ffd3 Binary files /dev/null and b/AlexSubbotin/lesson_7/out/production/lesson_7/FlashlightsProperties.class differ diff --git a/AlexSubbotin/lesson_7/out/production/lesson_7/Gearboxes.class b/AlexSubbotin/lesson_7/out/production/lesson_7/Gearboxes.class new file mode 100644 index 0000000..e51851c Binary files /dev/null and b/AlexSubbotin/lesson_7/out/production/lesson_7/Gearboxes.class differ diff --git a/AlexSubbotin/lesson_7/out/production/lesson_7/GearboxesOperation.class b/AlexSubbotin/lesson_7/out/production/lesson_7/GearboxesOperation.class new file mode 100644 index 0000000..9be6fcc Binary files /dev/null and b/AlexSubbotin/lesson_7/out/production/lesson_7/GearboxesOperation.class differ diff --git a/AlexSubbotin/lesson_7/out/production/lesson_7/GearboxesProperties.class b/AlexSubbotin/lesson_7/out/production/lesson_7/GearboxesProperties.class new file mode 100644 index 0000000..86262d0 Binary files /dev/null and b/AlexSubbotin/lesson_7/out/production/lesson_7/GearboxesProperties.class differ diff --git a/AlexSubbotin/lesson_7/out/production/lesson_7/Phones.class b/AlexSubbotin/lesson_7/out/production/lesson_7/Phones.class new file mode 100644 index 0000000..5165401 Binary files /dev/null and b/AlexSubbotin/lesson_7/out/production/lesson_7/Phones.class differ diff --git a/AlexSubbotin/lesson_7/out/production/lesson_7/PhonesProperties.class b/AlexSubbotin/lesson_7/out/production/lesson_7/PhonesProperties.class new file mode 100644 index 0000000..d39098a Binary files /dev/null and b/AlexSubbotin/lesson_7/out/production/lesson_7/PhonesProperties.class differ diff --git a/AlexSubbotin/lesson_7/src/Flashlights.java b/AlexSubbotin/lesson_7/src/Flashlights.java new file mode 100644 index 0000000..8444dd4 --- /dev/null +++ b/AlexSubbotin/lesson_7/src/Flashlights.java @@ -0,0 +1,23 @@ +public class Flashlights { + public FlashlightsProperties properties = new FlashlightsProperties(); + + public static void main(String[] args) { + Flashlights firstFlashlight = new Flashlights(); + firstFlashlight.properties.setName("BIG"); + firstFlashlight.properties.setColour("Silver"); + firstFlashlight.properties.setManufacturer("Big Corp."); + firstFlashlight.properties.setPrice(15.5f); + firstFlashlight.properties.putOnBatteries(Integer.parseInt(args[0])); + + printFlashlight(firstFlashlight); + } + + private static void printFlashlight(Flashlights flashlight) { + System.out.println("Flashlight name: " + flashlight.properties.getName()); + System.out.println("Flashlight manufacturer: " + flashlight.properties.getManufacturer()); + System.out.println("Flashlight colour: " + flashlight.properties.getColour()); + System.out.println("Flashlight price: " + flashlight.properties.getPrice() + "$"); + flashlight.properties.whichGlow(); + } + +} diff --git a/AlexSubbotin/lesson_7/src/FlashlightsProperties.java b/AlexSubbotin/lesson_7/src/FlashlightsProperties.java new file mode 100644 index 0000000..19a0fdc --- /dev/null +++ b/AlexSubbotin/lesson_7/src/FlashlightsProperties.java @@ -0,0 +1,65 @@ +public class FlashlightsProperties { + private final String RED = "RED LIGHT"; + private final String WHITE = "WHITE LIGHT"; + private final String FLASHING = "FLASHING LIGHT"; + + private String name; + private int batteries; + private String glow; + private String colour; + private float price; + private String manufacturer; + + public void putOnBatteries(int numberOfBatteries) { + batteries = numberOfBatteries; + } + + public void whichGlow() { + switch (batteries) { + case 1: + glow = WHITE; + break; + case 2: + glow = RED; + break; + case 3: + glow = FLASHING; + break; + default: + glow = "Not working. For start, put on 1,2 or 3 batteries, please."; + } + System.out.println("Flashlight glow: " + glow); + } + + public void setName(String flashlightName) { + name = flashlightName; + } + + public String getName() { + return name; + } + + public void setManufacturer(String flashlightManufacturer) { + manufacturer = flashlightManufacturer; + } + + public String getManufacturer() { + return manufacturer; + } + + public void setColour(String flashlightColour) { + colour = flashlightColour; + } + + public String getColour() { + return colour; + } + + public void setPrice(float flashlightPrice) { + price = flashlightPrice; + } + + public float getPrice() { + return price; + } +} diff --git a/AlexSubbotin/lesson_7/src/Gearboxes.java b/AlexSubbotin/lesson_7/src/Gearboxes.java new file mode 100644 index 0000000..99164f7 --- /dev/null +++ b/AlexSubbotin/lesson_7/src/Gearboxes.java @@ -0,0 +1,42 @@ +import java.util.Scanner; + +public class Gearboxes { + GearboxesProperties properties = new GearboxesProperties(); + GearboxesOperation operation = new GearboxesOperation(); + + public static void main(String[] args) { + Gearboxes firstGearbox = new Gearboxes(); + firstGearbox.properties.setGearboxesCountry("Japan"); + firstGearbox.properties.setGearboxesManufacturer("Jatco"); + firstGearbox.properties.setGearboxesName("J1039I"); + firstGearbox.properties.setTypeAutomatic(false); + firstGearbox.operation.setNumberOfGears((byte) 4); + firstGearbox.operation.setSpeedStep((byte) 26); + + gearboxResult(firstGearbox); + + Gearboxes secondGearbox = new Gearboxes(); + secondGearbox.properties.setGearboxesCountry("Germany"); + secondGearbox.properties.setGearboxesManufacturer("ZF Friedrichshafen AG"); + secondGearbox.properties.setGearboxesName("MS Tronic"); + secondGearbox.properties.setTypeAutomatic(true); + secondGearbox.operation.setNumberOfGears((byte) 4); + secondGearbox.operation.setSpeedStep((byte) 35); + + gearboxResult(secondGearbox); + + } + + private static void gearboxResult(Gearboxes gearbox) { + System.out.println("Gearbox name: " + gearbox.properties.getGearboxName()); + System.out.println("Gearbox manufacturer: " + gearbox.properties.getGearboxManufacturer()); + System.out.println("Gearbox country: " + gearbox.properties.getGearboxesCountry()); + gearbox.properties.type(gearbox.properties.isGearboxType()); //Print gearbox type. + System.out.println("To determine the maximum speed, enter the gear number: "); + Scanner scanner = new Scanner(System.in); + byte gearNumber = scanner.nextByte(); + gearbox.operation.setGearNumber(gearNumber); + byte speedStep = gearbox.operation.getSpeedStep(); + gearbox.operation.maxSpeedOnGear(gearNumber, speedStep, gearbox.properties.isGearboxType()); + } +} diff --git a/AlexSubbotin/lesson_7/src/GearboxesOperation.java b/AlexSubbotin/lesson_7/src/GearboxesOperation.java new file mode 100644 index 0000000..8fb8418 --- /dev/null +++ b/AlexSubbotin/lesson_7/src/GearboxesOperation.java @@ -0,0 +1,51 @@ +public class GearboxesOperation { + GearboxesProperties properties = new GearboxesProperties(); + + private byte numberOfGears; + private byte speedStep; + private byte gearNumber; + + public void maxSpeedOnGear(byte gearNumber, byte speedStep, boolean gearboxType) { + if (gearboxType) { + speedStep -= 5; + } + int speed; + switch (gearNumber) { + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + speed = gearNumber * speedStep; + System.out.println("Maximum speed in " + gearNumber + " gear " + speed + " km/h."); + break; + default: + System.out.println("Enter a correct gear number."); + } + } + + public void setNumberOfGears(byte numberGears) { + numberOfGears = numberGears; + } + + public byte getNumberOfGears() { + return numberOfGears; + } + + public void setSpeedStep(byte gearboxSpeedStep) { + speedStep = gearboxSpeedStep; + } + + public byte getSpeedStep() { + return speedStep; + } + + public void setGearNumber(byte setGearNumber) { + gearNumber = setGearNumber; + } + + public byte getGearNumber() { + return gearNumber; + } +} diff --git a/AlexSubbotin/lesson_7/src/GearboxesProperties.java b/AlexSubbotin/lesson_7/src/GearboxesProperties.java new file mode 100644 index 0000000..f113c4e --- /dev/null +++ b/AlexSubbotin/lesson_7/src/GearboxesProperties.java @@ -0,0 +1,46 @@ +public class GearboxesProperties { + private String name; + private String manufacturer; + private String country; + private boolean typeAutomatic; + + public void type(boolean typeAutomatic) { + if (typeAutomatic) { + System.out.println("Automatic gearbox."); + } else { + System.out.println("Mechanic gearbox."); + } + } + + public void setGearboxesName(String gearboxName) { + name = gearboxName; + } + + public String getGearboxName() { + return name; + } + + public void setGearboxesManufacturer(String gearboxManufacturer) { + manufacturer = gearboxManufacturer; + } + + public String getGearboxManufacturer() { + return manufacturer; + } + + public void setGearboxesCountry(String gearboxCountry) { + country = gearboxCountry; + } + + public String getGearboxesCountry() { + return country; + } + + public void setTypeAutomatic(boolean gearboxType) { + typeAutomatic = gearboxType; + } + + public boolean isGearboxType() { + return typeAutomatic; + } +} diff --git a/AlexSubbotin/lesson_7/src/Phones.java b/AlexSubbotin/lesson_7/src/Phones.java new file mode 100644 index 0000000..b8418b4 --- /dev/null +++ b/AlexSubbotin/lesson_7/src/Phones.java @@ -0,0 +1,16 @@ +public class Phones { + public static void main(String[] args) { + PhonesProperties phone = new PhonesProperties(); + phone.setModel("Iphone 12 Pro"); + phone.setPrice(1000); + phone.showInfo(); + phone.checkPrice(); + phone.checkCategory(); + phone.makePhoto(); + phone.callSomeone(); + phone.goToInternet(); + phone.playGame(); + phone.showBattery(); + phone.setOff(); + } +} diff --git a/AlexSubbotin/lesson_7/src/PhonesProperties.java b/AlexSubbotin/lesson_7/src/PhonesProperties.java new file mode 100644 index 0000000..f29e6bb --- /dev/null +++ b/AlexSubbotin/lesson_7/src/PhonesProperties.java @@ -0,0 +1,147 @@ +public class PhonesProperties { + private String model; + private double price; + private String category; + private int batteryCharge; + private boolean state; + + public PhonesProperties() { + model = "New Phone"; + batteryCharge = 100; + state = true; + price = 0; + } + + private boolean checkState(boolean state) { + if (batteryCharge > 0) { + System.out.println("Phone is on. Available " + batteryCharge + " %"); + return true; + } else { + System.out.println("Phone is off. Need charge!"); + return false; + } + } + + public void setOff() { + state = false; + System.out.println("Phone is off"); + } + + public void makePhoto() { + if (checkState(state)) { + System.out.println("Nice shot"); + spendCharge(10); + System.out.println("Left " + batteryCharge + " %"); + } + else { + System.out.println("Phone is off. Need to charge"); + } + } + + public void goToInternet() { + if (checkState(state)) { + System.out.println("What site are we going to?"); + spendCharge(20); + System.out.println("Left " + batteryCharge + " %"); + } + else { + System.out.println("Phone is off. Need to charge"); + } + } + + public void playGame() { + if (checkState(state)) { + System.out.println("Time to games"); + spendCharge(30); + System.out.println("Left " + batteryCharge + " %"); + } + else { + System.out.println("Phone is off. Need to charge"); + } + } + + public void callSomeone() { + if (checkState(state)) { + System.out.println("Who are we calling?"); + spendCharge(5); + System.out.println("Left " + batteryCharge + " %"); + } + else { + System.out.println("Phone is off. Need to charge"); + } + } + + public int getBatteryCharge() { + return batteryCharge; + } + + public void spendCharge(int batteryCharge) { + if (batteryCharge > 0) { + this.batteryCharge -= batteryCharge; + } else { + System.out.println("Nothing to spend. Need charge!"); + } + } + + public void chargeBattery(int batteryCharge) { + if (batteryCharge < 100) { + this.batteryCharge += batteryCharge; + } else { + System.out.println("Battery is full. Don't need to charge"); + } + } + + public void showBattery() { + System.out.println(model + " have " + batteryCharge + " % battery"); + } + + public void setModel(String model) { + this.model = model; + } + + public String getModel() { + return model; + } + + public void setPrice(double price) { + this.price = price; + } + + public double getPrice() { + return price; + } + + public void checkPrice() { + if (price < 100 && price > 0) { + this.category = "Cheap"; + System.out.println("Your phone is simple"); + } + else if (price < 500 && price > 100) { + this.category = "Normal"; + System.out.println("Your phone is good"); + } + else if (price < 1000 && price > 500 ) { + this.category = "Premium"; + System.out.println("Your phone is amazing. Can you give me a call?"); + } + else { + System.out.println("We don’t have such expensive phones yet"); + } + } + + public void checkCategory() { + if (category.equals("Cheap")) { + System.out.println("Your " + model + " from " + category + " segment"); + } + if (category.equals("Normal")) { + System.out.println("Your " + model + " from " + category + " segment"); + } + if (category.equals("Premium")) { + System.out.println("Your " + model + " from " + category + " segment"); + } + } + + public void showInfo() { + System.out.println("Model is " + model + ", price is " + price + " $"); + } +} diff --git a/AlexSubbotin/lesson_8/.idea/.gitignore b/AlexSubbotin/lesson_8/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_8/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_8/.idea/misc.xml b/AlexSubbotin/lesson_8/.idea/misc.xml new file mode 100644 index 0000000..e0844bc --- /dev/null +++ b/AlexSubbotin/lesson_8/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_8/.idea/modules.xml b/AlexSubbotin/lesson_8/.idea/modules.xml new file mode 100644 index 0000000..348c044 --- /dev/null +++ b/AlexSubbotin/lesson_8/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_8/.idea/vcs.xml b/AlexSubbotin/lesson_8/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_8/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_8/lesson_8.iml b/AlexSubbotin/lesson_8/lesson_8.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/AlexSubbotin/lesson_8/lesson_8.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_8/src/HomeworkEight.java b/AlexSubbotin/lesson_8/src/HomeworkEight.java new file mode 100644 index 0000000..1074c7c --- /dev/null +++ b/AlexSubbotin/lesson_8/src/HomeworkEight.java @@ -0,0 +1,186 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class HomeworkEight { + public static void main(String[] args) { + // -------TASK 1------- + + System.out.println("HOW MANY ROWS OF PASCAL`S TRIANGLE?"); + Scanner scanner = new Scanner(System.in); + int rowsNumber = scanner.nextInt(); + int[][] pascalTriangle = new int[rowsNumber][rowsNumber]; + pascalTriangle(pascalTriangle, rowsNumber); + + //-------TASK 2------- + + Scanner scann = new Scanner(System.in); + System.out.println("Enter number of rows:"); + int rows = scann.nextInt(); + System.out.println("Enter number of columns:"); + int columns = scanner.nextInt(); + + arraysSpiralFill(rows, columns); + + //-------TASK 3------- + + System.out.println("To check for a palindrome, enter words separated by ',': "); + Scanner wordsScan = new Scanner(System.in); + String words = wordsScan.nextLine(); + + palindromeChecker(words); + + palindromeCheckerByChars(words); + + //-------TASK 4------- + + System.out.println("Sorted array: " + Arrays.toString(arraySorter())); + + //-------TASK 5------- + + int[] firstArray = arraySorter(); + int[] secondArray = arraySorter(); + + arraysMerger(firstArray, secondArray); + } + + public static void pascalTriangle(int[][] pascalTriangle, int rowsNumber) { + for (int row = 0; row < rowsNumber; row++) { + for (int column = 0; column <= row; column++) { + if (column == 0 || column == row) + pascalTriangle[row][column] = 1; + else + pascalTriangle[row][column] = pascalTriangle[row - 1][column - 1] + + pascalTriangle[row - 1][column]; + } + System.out.println(Arrays.toString(pascalTriangle[row]).replace(",", "\t")); + } + } + + static void arraysSpiralFill(int rows, int columns) { + int value = 1; + int[][] array = new int[rows][columns]; + int startRowIndex = 0; + int endColumnIndex = rows; + int startColumnIndex = 0; + int endRowIndex = columns; + + while (startRowIndex < endColumnIndex && startColumnIndex < endRowIndex) { + for (int i = startColumnIndex; i < endRowIndex; ++i) { + array[startRowIndex][i] = value++; + } + startRowIndex++; + for (int i = startRowIndex; i < endColumnIndex; ++i) { + array[i][endRowIndex - 1] = value++; + } + endRowIndex--; + if (startRowIndex < endColumnIndex) { + for (int i = endRowIndex - 1; i >= startColumnIndex; --i) { + array[endColumnIndex - 1][i] = value++; + } + endColumnIndex--; + } + if (startColumnIndex < endRowIndex) { + for (int i = endColumnIndex - 1; i >= startRowIndex; --i) { + array[i][startColumnIndex] = value++; + } + startColumnIndex++; + } + } + for (int i = 0; i < rows; i++) { + System.out.println(Arrays.toString(array[i]).replace(",", "\t")); + } + } + + public static void palindromeChecker(String words) { + String[] arrayWords = words.split(","); + System.out.println(Arrays.toString(arrayWords)); + boolean palindrome; + int counter = 0; + for (String word : arrayWords) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(word).reverse(); + palindrome = word.equalsIgnoreCase(String.valueOf(stringBuilder)); + if (palindrome) { + counter++; + } + } + System.out.println("There are " + counter + " palindromes in the text."); + } + + public static int[] arraySorter() { + System.out.println("Enter the size of the array: "); + Scanner scan = new Scanner(System.in); + int arraySize = scan.nextInt(); + System.out.println("Enter number generations lower limit: "); + int lowerLimit = scan.nextInt(); + System.out.println("Enter number generations upper limit: "); + int upperLimit = scan.nextInt(); + int[] array = new int[arraySize]; + + for (int i = 0; i < array.length; i++) { + array[i] = (int) (Math.random() * (upperLimit - lowerLimit + 1) + lowerLimit); + } + System.out.println("Unsorted arrey: " + Arrays.toString(array)); + boolean sorted = false; + int counter = 0; + int temp; + while (!sorted) { + sorted = true; + for (int i = 0; i < array.length - 1; i++) { + if (array[i] > array[i + 1]) { + sorted = false; + temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + counter++; + } + } + } + System.out.println("The number of cycles: " + counter); + return array; + } + + public static void arraysMerger(int[] firstArray, int[] secondArray) { + int[] resultArray = new int[firstArray.length + secondArray.length]; + int firstArrIndex = 0; + int secondArrIndex = 0; + + for (int i = 0; i < resultArray.length; i++) { + if (firstArrIndex == firstArray.length) { + resultArray[i] = secondArray[secondArrIndex]; + secondArrIndex++; + } else if (secondArrIndex == secondArray.length) { + resultArray[i] = firstArray[firstArrIndex]; + firstArrIndex++; + } else if (firstArray[firstArrIndex] < secondArray[secondArrIndex]) { + resultArray[i] = firstArray[firstArrIndex]; + firstArrIndex++; + } else { + resultArray[i] = secondArray[secondArrIndex]; + secondArrIndex++; + } + } + System.out.println("First array: " + Arrays.toString(firstArray)); + System.out.println("Second array: " + Arrays.toString(secondArray)); + System.out.println("Merged array: " + Arrays.toString(resultArray)); + } + + public static void palindromeCheckerByChars(String words) { + String[] arrayWords = words.split(","); + System.out.println(Arrays.toString(arrayWords)); + int counter = 0; + for (String word : arrayWords) { + String lowCaseWord = word.toLowerCase(); + boolean palindrome = true; + for (int i = 0; i < lowCaseWord.length() / 2; i++) { + if (lowCaseWord.charAt(i) != lowCaseWord.charAt(lowCaseWord.length() - 1 - i)) { + palindrome = false; + } + } + if (palindrome) { + counter++; + } + } + System.out.println("There are " + counter + " palindromes in the text."); + } +} diff --git a/AlexSubbotin/lesson_9/.idea/.gitignore b/AlexSubbotin/lesson_9/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/AlexSubbotin/lesson_9/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/AlexSubbotin/lesson_9/.idea/misc.xml b/AlexSubbotin/lesson_9/.idea/misc.xml new file mode 100644 index 0000000..e0844bc --- /dev/null +++ b/AlexSubbotin/lesson_9/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_9/.idea/modules.xml b/AlexSubbotin/lesson_9/.idea/modules.xml new file mode 100644 index 0000000..95f2885 --- /dev/null +++ b/AlexSubbotin/lesson_9/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_9/.idea/vcs.xml b/AlexSubbotin/lesson_9/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/AlexSubbotin/lesson_9/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_9/lesson_9.iml b/AlexSubbotin/lesson_9/lesson_9.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/AlexSubbotin/lesson_9/lesson_9.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/AlexSubbotin/lesson_9/src/Circle.java b/AlexSubbotin/lesson_9/src/Circle.java new file mode 100644 index 0000000..c5db939 --- /dev/null +++ b/AlexSubbotin/lesson_9/src/Circle.java @@ -0,0 +1,17 @@ +public class Circle extends Figures { + private double radius; + + public void setRadius (double radius) { + this.radius = radius; + } + + public double getPerimeter() { + double perimeter = this.radius * 2 * Math.PI; + return perimeter; + } + + public double getArea() { + double area = Math.pow(this.radius, 2) * Math.PI; + return area; + } +} diff --git a/AlexSubbotin/lesson_9/src/Figures.java b/AlexSubbotin/lesson_9/src/Figures.java new file mode 100644 index 0000000..6a17290 --- /dev/null +++ b/AlexSubbotin/lesson_9/src/Figures.java @@ -0,0 +1,9 @@ +public abstract class Figures { + public double getPerimeter () { + return 0; + } + + public double getArea () { + return 0; + } +} diff --git a/AlexSubbotin/lesson_9/src/Homework_9.java b/AlexSubbotin/lesson_9/src/Homework_9.java new file mode 100644 index 0000000..4ecca70 --- /dev/null +++ b/AlexSubbotin/lesson_9/src/Homework_9.java @@ -0,0 +1,88 @@ +import java.util.Scanner; + +public class Homework_9 { + public static void main(String[] args) { +// 1) Напишите программу, которая заменяет символы в почтовом адресе пользователя: +// @ заменяется на [ at ] +// . заменяется на [ dot ] +// Пример ввода: +// person@gmail.com +// Результат: +// person[ at ]gmail[ dot ].com + + System.out.println("Please, enter email:"); + Scanner scanner = new Scanner(System.in); + String email = scanner.nextLine(); + + charChanger(email); + +// 2) i18n используется для обозначения слова internationalization, где 18 означает количество +// букв между первой и последней в этом слове, такое можно встретить в среде разработчиков. +// Например, для слова localization используется сокращение l10n. +// Напишите функцию, которая преобразует слова введенные из консоли через пробел в сокращенную форму. +// Слова длиной меньше 4х оставить без изменений. +// Пример ввода: +// internationalization localization cat elephant monitor +// Результат: +// i18n l10n cat e6t m5r +// PS: напишите функцию, которая сокращает слово, в основной функции используйте метод сплит, который поместит +// слова в массив и для каждого из них вызовет вашу функцию + + String enteredWords = "internationalization localization cat elephant monitor"; + String[] wordsArray = enteredWords.split(" "); + for (String fullWord : wordsArray) { + if (fullWord.length() >= 4) { + String resultWord = wordChanger(fullWord); + System.out.println(fullWord + " : " + resultWord); + } else { + System.out.println(fullWord); + } + } + +// 3) Создайте несколько классов геометрических фигур с инстанс методами: +// - getArea (расчет площади) +// - getPerimeter (расчет периметра) + + Circle firstCircle = new Circle(); + firstCircle.setRadius(10); + + Rhombus firstRhombus = new Rhombus(); + firstRhombus.setFirstSide(10); + firstRhombus.setHeight(8.66); + + Square firstSquare = new Square(); + firstSquare.setFirstSide(5); + + +// 4) Занесите все фигуры в массив и пощитайте в нем сумму площадей и периметров для всех фигур + + Figures[] figures = new Figures[3]; + figures[0] = firstCircle; + figures[1] = firstRhombus; + figures[2] = firstSquare; + + double sumAllPerimeters = 0; + double sumAllAreas = 0; + + for (int i = 0; i < figures.length; i++) { + sumAllAreas += figures[i].getArea(); + sumAllPerimeters += figures[i].getPerimeter(); + } + System.out.println("Sum of all areas: " + sumAllAreas); + System.out.println("Sum of all perimeters: " + sumAllPerimeters); + } + + private static String wordChanger(String fullWord) { + StringBuilder stringBuilder = new StringBuilder(fullWord); + String middleCharLength = String.valueOf(stringBuilder.length() - 2); + String resultWord = String.valueOf(stringBuilder.replace(1, stringBuilder.length() - 1, middleCharLength)); + return resultWord; + } + + private static void charChanger(String email) { + email = email + .replace("@", "[ at ]") + .replace(".", "[ dot ]"); + System.out.println(email); + } +} diff --git a/AlexSubbotin/lesson_9/src/Rhombus.java b/AlexSubbotin/lesson_9/src/Rhombus.java new file mode 100644 index 0000000..1a7ab7f --- /dev/null +++ b/AlexSubbotin/lesson_9/src/Rhombus.java @@ -0,0 +1,24 @@ +public class Rhombus extends Figures { + private double firstSide; + + private double height; + + + public void setFirstSide (double firstSide) { + this.firstSide = firstSide; + } + public void setHeight (double height) { + this.height = height; + } + + + public double getPerimeter () { + double perimeter = this.firstSide * 4; + return perimeter; + } + + public double getArea () { + double area = this.firstSide * this.height; + return area; + } +} diff --git a/AlexSubbotin/lesson_9/src/Square.java b/AlexSubbotin/lesson_9/src/Square.java new file mode 100644 index 0000000..5684d0b --- /dev/null +++ b/AlexSubbotin/lesson_9/src/Square.java @@ -0,0 +1,19 @@ +public class Square extends Figures { + private double firstSide; + + + public void setFirstSide (double firstSide) { + this.firstSide = firstSide; + } + + public double getPerimeter () { + double perimeter = this.firstSide * 4; + return perimeter; + } + + public double getArea () { + double area = Math.pow(this.firstSide, 2); + return area; + } +} +