From 581d9522b82f88bbff4dcda65d059031e7c612d8 Mon Sep 17 00:00:00 2001 From: samat Date: Thu, 11 May 2023 15:30:27 +0400 Subject: [PATCH 1/4] hw02 --- .idea/codeStyles/Project.xml | 10 ++ .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/kotlinc.xml | 6 ++ .idea/uiDesigner.xml | 124 +++++++++++++++++++++++++ src/main/kotlin/builder/Builder.kt | 52 +++++++++++ src/main/kotlin/command/Command.kt | 29 ++++++ src/main/kotlin/decorator/Decorator.kt | 58 ++++++++++++ src/main/kotlin/singleton/Singleton.kt | 29 ++++++ 8 files changed, 313 insertions(+) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/kotlinc.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/kotlin/builder/Builder.kt create mode 100644 src/main/kotlin/command/Command.kt create mode 100644 src/main/kotlin/decorator/Decorator.kt create mode 100644 src/main/kotlin/singleton/Singleton.kt diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..0e65cea --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/builder/Builder.kt b/src/main/kotlin/builder/Builder.kt new file mode 100644 index 0000000..8b43955 --- /dev/null +++ b/src/main/kotlin/builder/Builder.kt @@ -0,0 +1,52 @@ +package builder + + +data class Person( + val name: String, + val email: String, + val phone: Int, + val address: String +) + +class PersonBuilder { + private var name: String = "default name" + private var email: String = "default name" + private var phone: Int = 0 + private var address: String = "default address" + + fun setName(value: String): PersonBuilder { + name = value + return this + } + + fun setEmail(value: String): PersonBuilder { + email = value + return this + } + + fun setPhone(value: Int): PersonBuilder { + phone = value + return this + } + + fun setAddress(value: String): PersonBuilder { + address = value + return this + } + + fun build(): Person { + return Person(name, email, phone, address) + } + + +} + +fun main() { + val somePerson = + PersonBuilder().setName("some name").setEmail("some email").setPhone(1111111).setAddress("some address").build() + println(somePerson) + val defaultPerson = PersonBuilder().build() + println(defaultPerson) + + +} \ No newline at end of file diff --git a/src/main/kotlin/command/Command.kt b/src/main/kotlin/command/Command.kt new file mode 100644 index 0000000..629218a --- /dev/null +++ b/src/main/kotlin/command/Command.kt @@ -0,0 +1,29 @@ +package command +interface Command{ + fun execute() +} + +class ConcreteCommand(private val receiver: Receiver) : Command{ + override fun execute() { + receiver.action() + } +} + +class Receiver(){ + fun action(){ + println("получатель выполняет действия") + } +} + +class Initializer(private val command: Command){ + fun executeCommand(){ + command.execute() + } +} + +fun main() { + val receiver = Receiver() + val concreteCommand = ConcreteCommand(receiver) + val initializer = Initializer(concreteCommand) + initializer.executeCommand() +} diff --git a/src/main/kotlin/decorator/Decorator.kt b/src/main/kotlin/decorator/Decorator.kt new file mode 100644 index 0000000..a7a57cc --- /dev/null +++ b/src/main/kotlin/decorator/Decorator.kt @@ -0,0 +1,58 @@ +package decorator + +abstract class Lesson { + abstract val title: String + abstract val description: String + + abstract fun learn() + +} + +class EnglishLesson(override val title: String, override val description: String) : Lesson() { + override fun learn() { + println("изучение английского: $title - $description") + } +} + +class SpanishLesson(override val title: String, override val description: String) : Lesson() { + override fun learn() { + println("изучение испанского: $title - $description") + } +} + +class VocabularyLessonDecorator(private val lesson: Lesson) : Lesson() { + override val description: String + get() = lesson.description + override val title: String + get() = lesson.title + + override fun learn() { + lesson.learn() + println("новые слова") + } +} + +class GrammarLessonDecorator(private val lesson: Lesson) : Lesson() { + override val description: String + get() = lesson.description + override val title: String + get() = lesson.title + + override fun learn() { + lesson.learn() + println("новая грамматика") + } +} + + +fun main() { + val spanish = SpanishLesson("урок 1", "введение в испанский") + + val vocabularySpanish = VocabularyLessonDecorator(spanish) + vocabularySpanish.learn() + val vocabularyAndGrammarSpanish = GrammarLessonDecorator(vocabularySpanish) + + vocabularyAndGrammarSpanish.learn() + + +} \ No newline at end of file diff --git a/src/main/kotlin/singleton/Singleton.kt b/src/main/kotlin/singleton/Singleton.kt new file mode 100644 index 0000000..d9935b9 --- /dev/null +++ b/src/main/kotlin/singleton/Singleton.kt @@ -0,0 +1,29 @@ +package singleton + +class GameConfig private constructor(){ + private var heroPull : Int = 124; + private var latency: Int = 500; + private var waitingPlayers: Int = 0; + private var matchName: String = "default" + + companion object { + val instance : GameConfig by lazy { GameConfig() } + } + + fun addPlayers(int: Int){ + this.waitingPlayers +=int + } + fun printConfig(){ + println("$matchName $heroPull $latency $waitingPlayers") + } +} + +fun main(){ + val config = GameConfig.instance + val anotherConfig = GameConfig.instance + anotherConfig.addPlayers(23) + config.addPlayers(23) + + config.printConfig() + +} \ No newline at end of file From 3de6a3299a42d2ff8d0165bd15455d67a9121910 Mon Sep 17 00:00:00 2001 From: samat Date: Fri, 12 May 2023 18:53:48 +0400 Subject: [PATCH 2/4] =?UTF-8?q?hw02=20-=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BA=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/decorator/SecondDecorator.kt | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/kotlin/decorator/SecondDecorator.kt diff --git a/src/main/kotlin/decorator/SecondDecorator.kt b/src/main/kotlin/decorator/SecondDecorator.kt new file mode 100644 index 0000000..0e0d64d --- /dev/null +++ b/src/main/kotlin/decorator/SecondDecorator.kt @@ -0,0 +1,56 @@ +package decorator + +interface Pizza { + val price: Int + + fun getTotalPrice(): Int + fun getDescription(): String +} + +class BasicPizza : Pizza { + override val price: Int = 100 + + override fun getTotalPrice(): Int = price + override fun getDescription(): String = "Базовая пицца" +} + +abstract class PizzaDecorator(protected val pizza: Pizza) : Pizza { + abstract override val price: Int + override fun getTotalPrice(): Int = price + pizza.getTotalPrice() +} + +class Meat(pizza: Pizza) : PizzaDecorator(pizza){ + override val price: Int = 50 + override fun getDescription(): String = "${pizza.getDescription()} с мясом" +} + +class Cheese(pizza: Pizza) : PizzaDecorator(pizza){ + override val price: Int = 100 + override fun getDescription(): String = "${pizza.getDescription()} с сыром" +} + +class Vegetables(pizza: Pizza) : PizzaDecorator(pizza){ + override val price: Int = 150 + override fun getDescription(): String = "${pizza.getDescription()} с овощами" + override fun getTotalPrice(): Int = price + this.pizza.getTotalPrice() +} + +fun main(){ + val badPizza = BasicPizza() + println("${badPizza.getDescription()} ${badPizza.getTotalPrice()}") + + val normalPizza = Meat(badPizza) + println("${normalPizza.getDescription()} ${normalPizza.getTotalPrice()}") + + val goodPizza = Cheese(normalPizza) + println("${goodPizza.getDescription()} ${goodPizza.getTotalPrice()}") + + val awesomePizza = Vegetables(goodPizza) + println("${awesomePizza.getDescription()} ${awesomePizza.getTotalPrice()}") + + val allIngredients = Meat(Cheese(Vegetables(pizza = BasicPizza()))) + + println("${allIngredients.getDescription()} ${allIngredients.getTotalPrice()}") + + +} \ No newline at end of file From 4851a92ccf54543850b75754f7317938c7479940 Mon Sep 17 00:00:00 2001 From: samat Date: Fri, 12 May 2023 18:57:44 +0400 Subject: [PATCH 3/4] =?UTF-8?q?hw02=20-=20=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D1=88=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/decorator/SecondDecorator.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/decorator/SecondDecorator.kt b/src/main/kotlin/decorator/SecondDecorator.kt index 0e0d64d..625a89a 100644 --- a/src/main/kotlin/decorator/SecondDecorator.kt +++ b/src/main/kotlin/decorator/SecondDecorator.kt @@ -36,6 +36,7 @@ class Vegetables(pizza: Pizza) : PizzaDecorator(pizza){ } fun main(){ + val badPizza = BasicPizza() println("${badPizza.getDescription()} ${badPizza.getTotalPrice()}") @@ -49,8 +50,10 @@ fun main(){ println("${awesomePizza.getDescription()} ${awesomePizza.getTotalPrice()}") val allIngredients = Meat(Cheese(Vegetables(pizza = BasicPizza()))) - println("${allIngredients.getDescription()} ${allIngredients.getTotalPrice()}") + val veganPizza = Cheese(Vegetables(pizza = BasicPizza())) + println("${veganPizza.getDescription()} ${veganPizza.getTotalPrice()}") + } \ No newline at end of file From b5d5bc880bf4a78ff268df46364bd039ed87128a Mon Sep 17 00:00:00 2001 From: samat Date: Fri, 12 May 2023 19:02:49 +0400 Subject: [PATCH 4/4] fix --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7bec898..696b755 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Project exclude paths -/.gradle/ \ No newline at end of file +/.gradle/ +/.idea/