-
Notifications
You must be signed in to change notification settings - Fork 0
init commit homework - lesson7 #9
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package ru.homchenko.java.basic.homeworks.homework7; | ||
|
|
||
| public class AllTerrainVehicle implements Transport { | ||
| private int benzine; | ||
| protected String name; | ||
|
|
||
| public AllTerrainVehicle() { | ||
| benzine = 50; | ||
| name = "AllTerrainVehicle"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean move(int distance, TerrainType terrainType) { | ||
| if (benzine > benzine - (int) (distance * 0.2f)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не понятна проверка, зачем расстояние умножать именно на 0.2? Вообще такие числа называются "магическими", их нужно выносить в константы с понятным названием, что они означают. Я так понимаю, это расход топлива на 1 километр? |
||
| System.out.println("Поехали"); | ||
| benzine = benzine - (int) (distance * 0.2f); | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getNameTransport() { | ||
| return this.name; | ||
| } | ||
|
|
||
| @Override | ||
| public void printInfo() { | ||
| System.out.println("Осталось бензина: " + this.benzine); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package ru.homchenko.java.basic.homeworks.homework7; | ||
|
|
||
| public class Bicycle implements Transport { | ||
| protected String name; | ||
| protected TerrainType restrictionOfMovement; | ||
|
|
||
| public Bicycle() { | ||
| this.restrictionOfMovement = TerrainType.SWAMP; | ||
| this.name = "Bicycle"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean move(int distance, TerrainType terrainType) { | ||
| if (!this.restrictionOfMovement.equals(terrainType)) { | ||
| System.out.println("Поехали"); | ||
| return true; | ||
| } else { | ||
| System.out.println("Смените вид транспорта"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getNameTransport() { | ||
| return this.name; | ||
| } | ||
|
|
||
| @Override | ||
| public void printInfo() { | ||
| System.out.println("Не расходую ничего"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package ru.homchenko.java.basic.homeworks.homework7; | ||
|
|
||
| public class Car implements Transport { | ||
| private int benzine; | ||
| protected TerrainType[] restrictionOfMovement; | ||
| protected String name; | ||
|
|
||
| public Car() { | ||
| benzine = 50; | ||
| restrictionOfMovement = new TerrainType[]{TerrainType.DENSE_FOREST, TerrainType.SWAMP}; | ||
| name = "Car"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean move(int distance, TerrainType terrainType) { | ||
| for (int i = 0; i < restrictionOfMovement.length; i++) { | ||
| if (!restrictionOfMovement[i].equals(terrainType) && benzine > benzine - (int) (distance * 0.1f)) { | ||
| benzine = benzine - (int) (distance * 0.1f); | ||
| return true; | ||
| } | ||
| } | ||
| System.out.println(this.getNameTransport() + " не может перемещаться по " + terrainType); | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String getNameTransport() { | ||
| return this.name; | ||
| } | ||
|
|
||
| @Override | ||
| public void printInfo() { | ||
| System.out.println("Осталось бензина: " + this.benzine); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package ru.homchenko.java.basic.homeworks.homework7; | ||
|
|
||
| public class Horse implements Transport { | ||
| private int energy; | ||
| protected TerrainType restrictionOfMovement; | ||
| protected String name; | ||
|
|
||
| public Horse() { | ||
| energy = 50; | ||
| restrictionOfMovement = TerrainType.SWAMP; | ||
| name = "Horse"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean move(int distance, TerrainType terrainType) { | ||
| if (!this.restrictionOfMovement.equals(terrainType) && energy > (int) (distance * 0.5f)) { | ||
| energy = energy - (int) (distance * 0.5f); | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getNameTransport() { | ||
| return this.name; | ||
| } | ||
| @Override | ||
| public void printInfo() { | ||
| System.out.println("Осталось энергии: " + this.energy); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package ru.homchenko.java.basic.homeworks.homework7; | ||
|
|
||
| public class Human { | ||
| private String name; | ||
| private int energy; | ||
| private Transport currentTransport; | ||
|
|
||
| public void setEnergy(int energy) { | ||
| this.energy = energy; | ||
| } | ||
|
|
||
| public Transport getCurrentTransport() { | ||
| return currentTransport; | ||
| } | ||
|
|
||
| public void setCurrentTransport(Transport currentTransport) { | ||
| this.currentTransport = currentTransport; | ||
| } | ||
|
|
||
| public Human(String name) { | ||
| this.name = name; | ||
| energy = 10; | ||
| } | ||
|
|
||
| public void getOffTransport() { | ||
| System.out.println("Вы без транспорта."); | ||
| this.currentTransport = null; | ||
| } | ||
|
|
||
| public void walk(int distance, TerrainType terrainType) { | ||
| if (energy > energy - (int) (distance * 0.5f)) { | ||
| energy = energy - (int) (distance * 0.5f); | ||
| System.out.println("Транспорт не выбран. Иду пешком по: " + terrainType); | ||
| } | ||
| } | ||
|
|
||
| public void wantToMove(int distance, TerrainType terrainType) { | ||
| if (this.getCurrentTransport() != null) { | ||
| this.getCurrentTransport().move(distance, terrainType); | ||
| } else { | ||
| this.walk(distance, terrainType); | ||
| } | ||
| } | ||
|
|
||
| public void printInfo() { | ||
| String transportName; | ||
| if (currentTransport != null) { | ||
| transportName = currentTransport.getNameTransport(); | ||
| } else { | ||
| transportName = "не выбран"; | ||
| } | ||
| System.out.println("Имя: " + this.name + " Энергия: " + this.energy + " Транспорт: " + transportName); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package ru.homchenko.java.basic.homeworks.homework7; | ||
|
|
||
| public class MainApp { | ||
| public static void main(String[] args) { | ||
| AllTerrainVehicle allTerrainVehicle = new AllTerrainVehicle(); | ||
| Bicycle bicycle = new Bicycle(); | ||
| Car car = new Car(); | ||
| Horse horse = new Horse(); | ||
|
|
||
| Human human = new Human("Oleg"); | ||
| human.setCurrentTransport(allTerrainVehicle); | ||
| human.printInfo(); | ||
| allTerrainVehicle.printInfo(); | ||
| human.wantToMove(10, TerrainType.PLAIN); | ||
| allTerrainVehicle.printInfo(); | ||
|
|
||
| human.getOffTransport(); | ||
| human.printInfo(); | ||
| human.wantToMove(10, TerrainType.DENSE_FOREST); | ||
| human.printInfo(); | ||
|
|
||
| human.setCurrentTransport(car); | ||
| human.wantToMove(10, TerrainType.SWAMP); | ||
| human.printInfo(); | ||
| car.printInfo(); | ||
|
|
||
| human.setCurrentTransport(bicycle); | ||
| human.wantToMove(10, TerrainType.DENSE_FOREST); | ||
| human.printInfo(); | ||
|
|
||
| human.setCurrentTransport(horse); | ||
| human.wantToMove(15, TerrainType.PLAIN); | ||
| human.printInfo(); | ||
| horse.printInfo(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.homchenko.java.basic.homeworks.homework7; | ||
|
|
||
| public enum TerrainType { | ||
| DENSE_FOREST, | ||
| PLAIN, | ||
| SWAMP | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package ru.homchenko.java.basic.homeworks.homework7; | ||
|
|
||
| public interface Transport { | ||
| //возможность перемещаться на определенное расстояние с указанием типа местности | ||
| public boolean move(int distance, TerrainType terrainType); | ||
|
|
||
| public String getNameTransport(); | ||
|
|
||
| public void printInfo(); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше не писать русские слова английскими буквами, если уже пишите на английском, то везде.
Также рекомендую использовать double для расчета бензина.
В остальных классах также поправьте пожалуйста