-
Notifications
You must be signed in to change notification settings - Fork 99
Homework #4 #67
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
base: master
Are you sure you want to change the base?
Homework #4 #67
Changes from 2 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 |
|---|---|---|
|
|
@@ -8,4 +8,5 @@ interface CarOutput { | |
| * Скажи текущую скорость | ||
| */ | ||
| fun getCurrentSpeed(): Int | ||
| fun getFuelLevel(): Int | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package ru.otus.cars | ||
|
|
||
| import java.lang.Exception | ||
|
|
||
| class FuelStation { | ||
| companion object { | ||
|
|
||
| private lateinit var currentCar: Car | ||
| fun refuelCar(car: Car, litres: Int) { | ||
| println("Заправка авто: ${car.toString()} на ${litres.toString()} литров") | ||
| currentCar = car | ||
|
|
||
| val isOk = when(car.fuelSystem.tankMouth.type){ | ||
| Type.GAS, Type.FUEL -> { | ||
| addFuel(litres) | ||
| true | ||
| } | ||
| else -> { | ||
| println("Не обслуживаентся") | ||
| false | ||
| } | ||
| } | ||
| if(isOk) | ||
| println("Заправлено. Уровень топлива в авто: ${car.carOutput.getFuelLevel()} литров") | ||
| else | ||
| println("Не заправлено. Уровень топлива в авто: ${car.carOutput.getFuelLevel()} литров") | ||
|
|
||
| println("") | ||
|
|
||
| } | ||
|
|
||
| private fun addFuel(litres: Int): Boolean { | ||
| return try { | ||
| currentCar.fuelSystem.tankMouth.addFuel(litres) | ||
| } catch (e:Exception){ | ||
| false | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package ru.otus.cars | ||
|
|
||
| import java.lang.RuntimeException | ||
|
|
||
| /** | ||
| * Тип топлива: Бензин или Газ | ||
| */ | ||
| enum class Type{ | ||
| FUEL, GAS | ||
| } | ||
| interface Tank{ | ||
| var level: Int | ||
| } | ||
|
|
||
| /** | ||
| * Горловина к топливному баку | ||
| */ | ||
| data class TankMouth(val type:Type, val tank:Tank, val car:Car){ | ||
| /** | ||
| * Заправка бака топливом через горловину | ||
| * @param level - сколько залить в бак | ||
| * @return сколько в баке после заправки | ||
| */ | ||
| fun addFuel(level: Int): Boolean { | ||
| if(car is Taz){ | ||
|
||
| throw RuntimeException("ТАЗ НЕ УМЕЕТ ЗАПРАВЛЯТЬСЯ") | ||
| } | ||
| val fuelLevelBefore = this.tank.level | ||
| this.tank.level += level | ||
| return (this.tank.level - fuelLevelBefore > 0) | ||
| } | ||
| } | ||
|
|
||
| class FuelSystem { | ||
|
Collaborator
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. @mahonin-ms идея объединить бак и горловину единой внешней сущностью - это хорошее решение! Но ссылка к машине, опять же, кажется лишней. В реальной жизни топливную систему в сборе ставят в машину. Горловина торчит наружу - это и есть интерфейс для заправки |
||
| private val car:Car | ||
| private var tank:Tank // Топливный бак | ||
| var tankMouth: TankMouth // Горловина бака | ||
| constructor(fuelType:Type, car:Car = Taz){ | ||
| this.car = car | ||
| when(fuelType){ | ||
| Type.FUEL -> { | ||
| this.tank = FuelTank() | ||
| } | ||
| Type.GAS -> { | ||
| this.tank = GasTank() | ||
| } | ||
| } | ||
| this.tankMouth = TankMouth(fuelType, this.tank, this.car) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Реализация бака с бензином | ||
| * @version = 1.0 | ||
| */ | ||
| class FuelTank: Tank { | ||
| override var level = 0 | ||
| var type = "Бак с бензином" | ||
| } | ||
|
|
||
| /** | ||
| * Реализация бака с газом | ||
| * @version = 1.0 | ||
| */ | ||
| class GasTank: Tank{ | ||
| override var level = 0 | ||
| var type = "Бак с газом" | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { | |
| override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply { | ||
| this.engine = getRandomEngine() | ||
| this.plates = plates | ||
| this.fuelSystem = FuelSystem(Type.GAS, this) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -39,7 +40,6 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { | |
| // Переопределяем свойство родителя | ||
| override lateinit var engine: VazEngine | ||
| private set | ||
|
|
||
| /** | ||
| * Семерка едет так | ||
| */ | ||
|
|
@@ -59,20 +59,32 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { | |
|
|
||
| // Выводим состояние машины | ||
| override fun toString(): String { | ||
| return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" | ||
| return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, " + | ||
| "fuelType=${fuelSystem.tankMouth.type}, fuelLevel=${fuelSystem.tankMouth.tank.level})" | ||
| } | ||
|
|
||
| /** | ||
| * Делегируем приборы внутреннему классу | ||
| */ | ||
| override val carOutput: CarOutput = VazOutput() | ||
|
|
||
| /** | ||
| * Топливная система ВАЗ 2107 | ||
| */ | ||
| override lateinit var fuelSystem: FuelSystem | ||
| private set | ||
|
|
||
| /** | ||
| * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2107! | ||
| */ | ||
| inner class VazOutput : CarOutput { | ||
| override fun getCurrentSpeed(): Int { | ||
| return [email protected] | ||
| } | ||
|
|
||
| override fun getFuelLevel(): Int { | ||
| return [email protected] | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { | |
| override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply { | ||
| this.engine = getRandomEngine() | ||
| this.plates = plates | ||
| this.fuelSystem = FuelSystem(Type.FUEL, this) | ||
| } | ||
|
|
||
| fun alignWheels(vaz2108: Vaz2108) { | ||
|
|
@@ -63,13 +64,16 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { | |
|
|
||
| // Выводим состояние машины | ||
| override fun toString(): String { | ||
| return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" | ||
| return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, " + | ||
| "fuelType=${fuelSystem.tankMouth.type}, fuelLevel=${fuelSystem.tankMouth.tank.level}" | ||
| } | ||
|
|
||
| /** | ||
| * Делегируем приборы внутреннему классу | ||
| */ | ||
| override val carOutput: CarOutput = VazOutput() | ||
| override lateinit var fuelSystem: FuelSystem | ||
| private set | ||
|
|
||
| /** | ||
| * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2108! | ||
|
|
@@ -78,5 +82,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { | |
| override fun getCurrentSpeed(): Int { | ||
| return [email protected] | ||
| } | ||
|
|
||
| override fun getFuelLevel(): Int { | ||
| return [email protected] | ||
| } | ||
| } | ||
| } | ||
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.
@mahonin-ms, давайте отвлечемся от программирования, вспомним реальные конструкции, и какая между ними связь...
В реальной жизни: бак установлен в автомобиль, он связан горловиной с внешним миром.
В вашем варианте: горловина, в которую установлены бак и машина