From b7bbf7179266a147635645935c0072602995e1e7 Mon Sep 17 00:00:00 2001 From: valkonsky Date: Thu, 3 Jun 2021 17:41:48 +0300 Subject: [PATCH] lesson 6 homework --- lesson2/src/main/java/Animal.java | 13 +++++++++++++ lesson2/src/main/java/AnimalFactory.java | 14 ++++++++++++++ lesson2/src/main/java/Cat.java | 13 +++++++++++++ lesson2/src/main/java/CatRunning.java | 11 +++++++++++ lesson2/src/main/java/Dog.java | 13 +++++++++++++ lesson2/src/main/java/DogRunning.java | 10 ++++++++++ lesson2/src/main/java/Main.java | 13 +++++++++++++ lesson2/src/main/java/RunBehavior.java | 3 +++ lesson2/src/main/java/Swim.java | 6 ++++++ lesson2/src/main/java/SwimBehavior.java | 3 +++ lesson2/src/main/java/SwimNoWay.java | 5 +++++ 11 files changed, 104 insertions(+) create mode 100644 lesson2/src/main/java/Animal.java create mode 100644 lesson2/src/main/java/AnimalFactory.java create mode 100644 lesson2/src/main/java/Cat.java create mode 100644 lesson2/src/main/java/CatRunning.java create mode 100644 lesson2/src/main/java/Dog.java create mode 100644 lesson2/src/main/java/DogRunning.java create mode 100644 lesson2/src/main/java/Main.java create mode 100644 lesson2/src/main/java/RunBehavior.java create mode 100644 lesson2/src/main/java/Swim.java create mode 100644 lesson2/src/main/java/SwimBehavior.java create mode 100644 lesson2/src/main/java/SwimNoWay.java diff --git a/lesson2/src/main/java/Animal.java b/lesson2/src/main/java/Animal.java new file mode 100644 index 0000000..a4b4d30 --- /dev/null +++ b/lesson2/src/main/java/Animal.java @@ -0,0 +1,13 @@ +public abstract class Animal { + + RunBehavior runBehavior; + SwimBehavior swimBehavior; + + public void performSwim(){ + swimBehavior.swim(); + } + + public void performRun(int distance){ + runBehavior.run(distance); + } +} diff --git a/lesson2/src/main/java/AnimalFactory.java b/lesson2/src/main/java/AnimalFactory.java new file mode 100644 index 0000000..c7de5aa --- /dev/null +++ b/lesson2/src/main/java/AnimalFactory.java @@ -0,0 +1,14 @@ +public class AnimalFactory { + + public static void getAllDogs(){ + System.out.println(Dog.getAllDogs()); + } + + public static void getAllCats(){ + System.out.println(Cat.getAllCats()); + } + + public static void getAllAnimals(){ + System.out.println(Dog.getAllDogs() + Cat.getAllCats()); + } +} diff --git a/lesson2/src/main/java/Cat.java b/lesson2/src/main/java/Cat.java new file mode 100644 index 0000000..d82151d --- /dev/null +++ b/lesson2/src/main/java/Cat.java @@ -0,0 +1,13 @@ +public class Cat extends Animal{ + private static int counter = 0; + + public Cat(){ + swimBehavior = new SwimNoWay(); + runBehavior = new CatRunning(); + counter ++; + } + + public static int getAllCats(){ + return counter; + } +} diff --git a/lesson2/src/main/java/CatRunning.java b/lesson2/src/main/java/CatRunning.java new file mode 100644 index 0000000..399e658 --- /dev/null +++ b/lesson2/src/main/java/CatRunning.java @@ -0,0 +1,11 @@ +class CatRunning implements RunBehavior{ + + @Override + public void run(int distance) { + if (distance<=200){ + System.out.println("i'm running on " + distance + " meters"); + }else{ + System.out.println("i can't running on this distance"); + } + } +} diff --git a/lesson2/src/main/java/Dog.java b/lesson2/src/main/java/Dog.java new file mode 100644 index 0000000..fddf7b9 --- /dev/null +++ b/lesson2/src/main/java/Dog.java @@ -0,0 +1,13 @@ +public class Dog extends Animal{ + private static int counter = 0; + + public Dog(){ + swimBehavior = new Swim(); + runBehavior = new DogRunning(); + counter ++; + } + + public static int getAllDogs(){ + return counter; + } +} diff --git a/lesson2/src/main/java/DogRunning.java b/lesson2/src/main/java/DogRunning.java new file mode 100644 index 0000000..261384c --- /dev/null +++ b/lesson2/src/main/java/DogRunning.java @@ -0,0 +1,10 @@ +public class DogRunning implements RunBehavior{ + @Override + public void run(int distance) { + if (distance<=500){ + System.out.println("i'm running on " + distance + " meters"); + }else{ + System.out.println("i can't eun on this distance"); + } + } +} diff --git a/lesson2/src/main/java/Main.java b/lesson2/src/main/java/Main.java new file mode 100644 index 0000000..172db54 --- /dev/null +++ b/lesson2/src/main/java/Main.java @@ -0,0 +1,13 @@ +public class Main { + + public static void main(String[] args) { + Cat stepa = new Cat(); + stepa.performRun(100); + Dog lord = new Dog(); + lord.performRun(300); + + AnimalFactory.getAllDogs(); + AnimalFactory.getAllCats(); + AnimalFactory.getAllAnimals(); + } +} diff --git a/lesson2/src/main/java/RunBehavior.java b/lesson2/src/main/java/RunBehavior.java new file mode 100644 index 0000000..062573b --- /dev/null +++ b/lesson2/src/main/java/RunBehavior.java @@ -0,0 +1,3 @@ +public interface RunBehavior { + public void run(int distance); +} diff --git a/lesson2/src/main/java/Swim.java b/lesson2/src/main/java/Swim.java new file mode 100644 index 0000000..c2cea0a --- /dev/null +++ b/lesson2/src/main/java/Swim.java @@ -0,0 +1,6 @@ +public class Swim implements SwimBehavior{ + + public void swim(){ + System.out.println("i'm swimming"); + } +} diff --git a/lesson2/src/main/java/SwimBehavior.java b/lesson2/src/main/java/SwimBehavior.java new file mode 100644 index 0000000..35d79b8 --- /dev/null +++ b/lesson2/src/main/java/SwimBehavior.java @@ -0,0 +1,3 @@ +public interface SwimBehavior { + public void swim(); +} diff --git a/lesson2/src/main/java/SwimNoWay.java b/lesson2/src/main/java/SwimNoWay.java new file mode 100644 index 0000000..45bebc7 --- /dev/null +++ b/lesson2/src/main/java/SwimNoWay.java @@ -0,0 +1,5 @@ +public class SwimNoWay implements SwimBehavior{ + public void swim(){ + System.out.println("i can't swim"); + } +}