Skip to content

Fuel system had been created, test logic was added #41

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
6 changes: 5 additions & 1 deletion 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_system.TankMouth

/**
* Машина целиком
*/
Expand All @@ -19,6 +21,8 @@ interface Car : CarInput {
*/
val carOutput: CarOutput

val mouth: TankMouth

/**
* Получить оборудование
*/
Expand All @@ -28,4 +32,4 @@ interface Car : CarInput {
* Внутренний статический класс - номерой знак
*/
data class Plates(val number: String, val region: Int)
}
}
4 changes: 3 additions & 1 deletion 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
}
12 changes: 11 additions & 1 deletion 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_system.TankMouth

object Taz: Car {
/**
* Номерной знак
Expand All @@ -18,6 +20,9 @@ object Taz: Car {
override val carOutput: CarOutput
get() = throw NotImplementedError("Приборов нет")

override val mouth: TankMouth
get() = throw NotImplementedError("Горловины нет")

/**
* Получить оборудование
*/
Expand All @@ -36,4 +41,9 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}
}

override fun toString(): String {
return "Taz"
}

}
22 changes: 18 additions & 4 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ru.otus.cars

import ru.otus.cars.fuel_system.LpgMouth
import ru.otus.cars.fuel_system.Tank
import ru.otus.cars.fuel_system.TankBuilder
import ru.otus.cars.fuel_system.TankMouth
import kotlin.random.Random

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

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

/**
Expand Down Expand Up @@ -48,6 +54,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
println("Др-др-др-др....")
}

private val tank: Tank = TankBuilder(50)
private var currentSpeed: Int = 0 // Скока жмёт

/**
Expand All @@ -57,6 +64,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override lateinit var plates: Car.Plates
private set

override lateinit var mouth: TankMouth
private set

// Выводим состояние машины
override fun toString(): String {
return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)"
Expand All @@ -74,5 +84,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

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

import ru.otus.cars.fuel_system.PetrolMouth
import ru.otus.cars.fuel_system.Tank
import ru.otus.cars.fuel_system.TankBuilder
import ru.otus.cars.fuel_system.TankMouth
import kotlin.random.Random

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

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

fun alignWheels(vaz2108: Vaz2108) {
Expand Down Expand Up @@ -52,6 +58,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
return super.getEquipment() + ", музыка"
}


private val tank: Tank = TankBuilder(50)
private var currentSpeed: Int = 0 // Скока жмёт

/**
Expand All @@ -61,6 +69,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override lateinit var plates: Car.Plates
private set

override lateinit var mouth: TankMouth
private set

// Выводим состояние машины
override fun toString(): String {
return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)"
Expand All @@ -78,5 +89,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

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

interface Tank {

fun getContents(): Int

fun receiveFuel(liters: Int)

}

class TankBuilder (val capacity: Int): Tank {

private var contents = 0

override fun getContents(): Int = contents

override fun receiveFuel(liters: Int) {
contents += liters
if (contents >= capacity) println("Полный бак")
}
}
48 changes: 48 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuel_system/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ru.otus.cars.fuel_system

sealed class TankMouth(protected val tank: Tank){
private var isOpened = false
fun open(){
when(isOpened){
true -> println("Горловина бака уже открыта")
else -> {
isOpened = true
println("Горловина бака открыта")
}
}
}

fun close(){
when(isOpened){
false -> print("Горловина бака уже закрыта")
else -> {
isOpened = false
println("Горловина бака закрыта")
}
}
}

}
class PetrolMouth(tank: Tank) : TankMouth(tank){

fun fuelPetrol(liters: Int){
open()
println("Начинается заправка")
tank.receiveFuel(liters)
println("Заправка окончена")
close()
}
}


class LpgMouth(tank: Tank): TankMouth(tank) {

fun fuelLpg(liters: Int){
open()
println("Начинается заправка")
tank.receiveFuel(liters)
println("Заправка окончена")
close()

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

import ru.otus.cars.Car
import ru.otus.cars.fuel_system.LpgMouth
import ru.otus.cars.fuel_system.PetrolMouth

class GasStation {

fun getFuel(car: Car, liters: Int){
val mouth = car.mouth
when(mouth){
is PetrolMouth -> mouth.fuelPetrol(liters)
is LpgMouth -> mouth.fuelLpg(liters)
}
}

fun safeRefill(car: Car, liters: Int) {
try {
getFuel(car, liters)
}
catch (e: NotImplementedError) {
println("Бак взорвался, но никто не пострадал")
}
}
}
30 changes: 29 additions & 1 deletion src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.cars.gas_station.GasStation

fun main() {
println("\n===> drive cars...")
driveCars()
Expand All @@ -16,6 +18,8 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)
println("\n===> Fuel car collection...")
saveRefillCarCollection()
}

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

fun saveRefillCarCollection() {
val gasStation = GasStation()
val carList = listOf( Vaz2107.build(Car.Plates("123", 77)),
Vaz2108.build(Car.Plates("321", 78)),
Taz)

carList.forEach{ car ->
fun getCarOutputSafely() {
try {
println("В баке ${car.carOutput.getFuelContents()} литров")
}
catch (e: NotImplementedError){
println("Что-то пошло не так: ${e.message}")
}
}

println("Текущая машина - $car")
getCarOutputSafely()
gasStation.safeRefill(car, 50)
getCarOutputSafely()
}

}