Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson7 #7

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lesson2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.gb.savilin</groupId>
<artifactId>lesson2</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
22 changes: 22 additions & 0 deletions lesson2/src/main/java/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main.java;

public abstract class Animal {

RunBehavior runBehavior;
SwimBehavior swimBehavior;
int appetite;
String name;
boolean satiety;

public void performSwim(){
swimBehavior.swim();
}

public void performRun(int distance){
runBehavior.run(distance);
}

public void setSatiety(boolean satiety){
this.satiety = satiety;
}
}
19 changes: 19 additions & 0 deletions lesson2/src/main/java/AnimalManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main.java;

import main.java.Cat;
import main.java.Dog;

public class AnimalManager {

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());
}
}
35 changes: 35 additions & 0 deletions lesson2/src/main/java/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main.java;

public class Cat extends Animal{
private static int counter = 0;
private int appetite;
private boolean satiety;

public Cat(String name, int appetite){
swimBehavior = new SwimNoWay();
runBehavior = new CatRunning();
this.name = name;
this.appetite = appetite;
this.satiety = false;
counter ++;
this.appetite = appetite;
satiety = false;
}

public void eat(Plate p){
if (p.getFood()<appetite){
System.out.println("eat not enough");
}else {
p.decreaseFood(appetite);
satiety = true;
}
}

public boolean isSatiety(){
return satiety;
}

public static int getAllCats(){
return counter;
}
}
13 changes: 13 additions & 0 deletions lesson2/src/main/java/CatRunning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main.java;

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");
}
}
}
15 changes: 15 additions & 0 deletions lesson2/src/main/java/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main.java;

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;
}
}
12 changes: 12 additions & 0 deletions lesson2/src/main/java/DogRunning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main.java;

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");
}
}
}
16 changes: 16 additions & 0 deletions lesson2/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main.java;

public class Main {

public static void main(String[] args) {
Plate plate = new Plate(100);
Cat[] cats = new Cat[]{new Cat("Murzik",20),
new Cat("Stepa",10),
new Cat("Ezhi",500),};

for (int i = 0; i < cats.length; i++) {
cats[i].eat(plate);
System.out.println(cats[i].name + " " + cats[i].isSatiety());
}
}
}
31 changes: 31 additions & 0 deletions lesson2/src/main/java/Plate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main.java;

public class Plate {
private int food;

Plate(int food){
this.food = food;
}

public boolean decreaseFood(int n) {
if (food >= n) {
food -= n;
return true;
} else {
System.out.println("not enough food");
}
return false;
}

public int getFood() {
return food;
}

public void info(){
System.out.println("plate: " + food);
}

public void addFood(int food){
this.food += food;
}
}
5 changes: 5 additions & 0 deletions lesson2/src/main/java/RunBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main.java;

public interface RunBehavior {
public void run(int distance);
}
8 changes: 8 additions & 0 deletions lesson2/src/main/java/Swim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main.java;

public class Swim implements SwimBehavior{

public void swim(){
System.out.println("i'm swimming");
}
}
5 changes: 5 additions & 0 deletions lesson2/src/main/java/SwimBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main.java;

public interface SwimBehavior {
public void swim();
}
7 changes: 7 additions & 0 deletions lesson2/src/main/java/SwimNoWay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main.java;

public class SwimNoWay implements SwimBehavior{
public void swim(){
System.out.println("i can't swim");
}
}