Skip to content

Implemented homework #50

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

Open
wants to merge 1 commit into
base: master
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
4 changes: 4 additions & 0 deletions src/main/kotlin/ru/otus/cars/Car.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.cars.fuel.TankMouth

/**
* Машина целиком
*/
Expand Down Expand Up @@ -28,4 +30,6 @@ interface Car : CarInput {
* Внутренний статический класс - номерой знак
*/
data class Plates(val number: String, val region: Int)

val tankMouth: TankMouth
}
4 changes: 2 additions & 2 deletions src/main/kotlin/ru/otus/cars/CarFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object Togliatti : CarFactory {

override fun buildCar(builder: CarBuilder, plates: Car.Plates): Car {
return when (builder) {
is Vaz2107.Companion -> return buildVaz2107(plates)
is Vaz2108.Companion -> return buildVaz2108(plates)
is Vaz2107.Companion -> buildVaz2107(plates)
is Vaz2108.Companion -> buildVaz2108(plates)
}
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/ru/otus/cars/CarOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

fun getFuelContents(): Int
}
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.cars.fuel.TankMouth

object Taz: Car {
/**
* Номерной знак
Expand Down Expand Up @@ -36,4 +38,7 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}

override val tankMouth: TankMouth
get() = throw NotImplementedError("Взрыв")
}
22 changes: 22 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.otus.cars

import ru.otus.cars.fuel.Tank
import ru.otus.cars.fuel.TankMouth
import ru.otus.cars.fuel.VazTank
import kotlin.random.Random

/**
Expand All @@ -17,9 +20,18 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
}
}

private fun getTank() : Tank{
val tank = VazTank()
val tankMouth = TankMouth.LpgTankMouth(tank)
tank.mouth = tankMouth
return tank
}

override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply {
this.engine = getRandomEngine()
this.plates = plates
this.tank = getTank()
this.tankMouth = this.tank.mouth
}

/**
Expand All @@ -40,6 +52,12 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override lateinit var engine: VazEngine
private set

override lateinit var tank: Tank
private set

override lateinit var tankMouth: TankMouth
private set

/**
* Семерка едет так
*/
Expand Down Expand Up @@ -74,5 +92,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelContents(): Int {
return [email protected]()
}
}
}
22 changes: 22 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.otus.cars

import ru.otus.cars.fuel.Tank
import ru.otus.cars.fuel.TankMouth
import ru.otus.cars.fuel.VazTank
import kotlin.random.Random

/**
Expand All @@ -18,9 +21,18 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
}
}

private fun getTank() : Tank {
val tank = VazTank()
val tankMouth = TankMouth.PetrolTankMouth(tank)
tank.mouth = tankMouth
return tank
}

override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply {
this.engine = getRandomEngine()
this.plates = plates
this.tank = getTank()
this.tankMouth = this.tank.mouth
}

fun alignWheels(vaz2108: Vaz2108) {
Expand All @@ -38,6 +50,12 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override lateinit var engine: VazEngine
private set

override lateinit var tank: Tank
private set

override lateinit var tankMouth: TankMouth
private set

/**
* Восьмерка едет так
*/
Expand Down Expand Up @@ -78,5 +96,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelContents(): Int {
return [email protected]()
}
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/ru/otus/cars/VazPlatform.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.cars.fuel.Tank

abstract class VazPlatform(override val color: String) : Car {
// Положение руля. Доступно только внутри класса и наследникам
protected var wheelAngle: Int = 0 // Положение руля
Expand All @@ -14,6 +16,8 @@ abstract class VazPlatform(override val color: String) : Car {

// Абстрактное свойство двигателя
abstract val engine: VazEngine

abstract val tank: Tank
}

// Перечисление двигателей ВАЗ
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuel/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.otus.cars.fuel

interface Tank {
val mouth: TankMouth
fun getContents(): Int
fun receiveFuel(liters: Int)
}

19 changes: 19 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuel/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.otus.cars.fuel

sealed class TankMouth(private val tank: Tank) {
fun open() {
println("Горловина открыта")
}

fun close() {
println("Горловина закрыта")
}

fun fill(liters: Int) {
tank.receiveFuel(liters)
}

class LpgTankMouth(tank: Tank) : TankMouth(tank)
class PetrolTankMouth(tank: Tank) : TankMouth(tank)
}

14 changes: 14 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuel/VazTank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.otus.cars.fuel

class VazTank : Tank {
override lateinit var mouth: TankMouth

private var fuelAmount: Int = 0

override fun getContents(): Int = fuelAmount

override fun receiveFuel(liters: Int) {
fuelAmount += liters
}

}
31 changes: 31 additions & 0 deletions src/main/kotlin/ru/otus/cars/gasstation/GasStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.otus.cars.gasstation

import ru.otus.cars.Car
import ru.otus.cars.fuel.TankMouth

class GasStation {
fun fillUpCar(car: Car, liters: Int){
try {
when(car.tankMouth){
is TankMouth.LpgTankMouth -> fillWithLpg(car, liters)
is TankMouth.PetrolTankMouth -> fillWithPetrol(car, liters)
}
} catch (e: Error) {
println("Что-то пошло не там при заправке: ${e.message}")
}
}

private fun fillWithLpg(car: Car, liters: Int) {
car.tankMouth.open()
car.tankMouth.fill(liters)
car.tankMouth.close()
println("Заправил $liters газа")
}

private fun fillWithPetrol(car: Car, liters: Int) {
car.tankMouth.open()
car.tankMouth.fill(liters)
car.tankMouth.close()
println("Заправил $liters бензина")
}
}
31 changes: 31 additions & 0 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ru.otus.cars

import ru.otus.cars.gasstation.GasStation

fun main() {
println("\n===> refueling")
refueling()
println("\n===> drive cars...")
driveCars()
println("\n===> inner test...")
Expand Down Expand Up @@ -90,4 +94,31 @@ fun repairEngine(car: VazPlatform) {
is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car")
is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car")
}
}

fun refueling() {
val vaz2107 = Vaz2107.build(Car.Plates("123", 77))
val vaz2108 = Vaz2108.build(Car.Plates("321", 78))
val taz = Taz

println("Fuel level of VAZ2107: ${vaz2107.VazOutput().getFuelContents()}")
println("Fuel level of VAZ2108: ${vaz2108.VazOutput().getFuelContents()}")
try {
println("Fuel level of TAZ: ${taz.carOutput.getFuelContents()}")
} catch (e: Error) {
println("Can't get TAZ's fuel level: ${e.message}")
}

val gasStation = GasStation()
gasStation.fillUpCar(vaz2107, 60)
gasStation.fillUpCar(vaz2108, 40)
gasStation.fillUpCar(taz, 10)

println("Fuel level of VAZ2107: ${vaz2107.VazOutput().getFuelContents()}")
println("Fuel level of VAZ2108: ${vaz2108.VazOutput().getFuelContents()}")
try {
println("Fuel level of TAZ: ${taz.carOutput.getFuelContents()}")
} catch (e: Error) {
println("Can't get TAZ's fuel level: ${e.message}")
}
}