Skip to content

homework Igor Alekseev #40

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
![LCE state diagram](http://www.plantuml.com/plantuml/proxy?src=https://raw.githubusercontent.com/Android-Developer-Basic/Kotlin-6/master/doc/Car.puml)

Сделайте машинам топливную систему:

- Семерка пусть ездит на газу
- Восьмерка пусть ездит на бензине
- ТАЗ может иметь любую систему, но его бак взрывается при попытке заправить (бросает исключение)
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/Car.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ package ru.otus.cars
* Машина целиком
*/
interface Car : CarInput {
/**
* Бак
*/
var tank: Tank

/**
* Номерной знак
*/
Expand Down
5 changes: 5 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,9 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

/**
* Скажи уровень топлива
*/
fun getFuelContents(): Int
}
36 changes: 36 additions & 0 deletions src/main/kotlin/ru/otus/cars/FuelStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.otus.cars

class FuelStation {
fun reFuel(cars: List<Car>){
for(car in cars){

try {
println("Заправляем машину с номером:")
println(car.plates.toString())
} catch ( e: NotImplementedError ) {
println(e.toString())
}

try {
println("Уровень топлива до заправки:")
println(car.carOutput.getFuelContents().toString())
} catch ( e: NotImplementedError ) {
println(e.toString())
}

try {
car.tank.recieveFuel(20)
} catch ( e: Exception ) {
println(e.toString())
}

try {
println("Уровень топлива после заправки:")
println(car.carOutput.getFuelContents().toString())
} catch ( e: NotImplementedError ) {
println(e.toString())
}

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

interface Tank{
//val mouth: TankMouth
//var fuelLevel : Int
fun getContents(): Int
fun recieveFuel(literes: Int)
}

abstract class TankMouth() : Tank{
private enum class State{ OPEN, CLOSE }
private var cap : State = State.CLOSE
var fuelLevel : Int = 0
protected fun open() {cap = State.OPEN}
protected fun close() {cap = State.CLOSE}
override fun getContents(): Int = fuelLevel
class PetrolMouth : TankMouth(){
override fun recieveFuel(literes: Int){
open()
fuelLevel = literes
println("Заправка бензином, принято ${literes.toString()} литров")
close()
}
}
class LpgMouth : TankMouth() {
override fun recieveFuel(literes: Int){
open()
println("Заправка газом, принято ${literes.toString()} литров")
fuelLevel = literes
close()
}
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ object Taz: Car {
override val plates: Car.Plates
get() = throw NotImplementedError("Номера сняты")

/**
* Бак
*/
override lateinit var tank: Tank

/**
* Цвет машины
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.tank = TankMouth.PetrolMouth()
}

/**
Expand All @@ -39,6 +40,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
// Переопределяем свойство родителя
override lateinit var engine: VazEngine
private set
override lateinit var tank: Tank

/**
* Семерка едет так
Expand Down Expand Up @@ -74,5 +76,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}
override fun getFuelContents(): Int {
return [email protected]()
}
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.tank = TankMouth.LpgMouth()
}

fun alignWheels(vaz2108: Vaz2108) {
Expand All @@ -37,6 +38,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
// Переопределяем свойство родителя
override lateinit var engine: VazEngine
private set
override lateinit var tank: Tank

/**
* Восьмерка едет так
Expand Down Expand Up @@ -78,5 +80,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}
override fun getFuelContents(): Int {
return [email protected]()
}
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.otus.cars

fun main() {
println("\n===> refuel")
refuelCars()
println("\n===> drive cars...")
driveCars()
println("\n===> inner test...")
Expand All @@ -18,6 +20,16 @@ fun main() {
println(Taz.color)
}

fun refuelCars(){
val cars: List<Car> = listOf(
Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)),
Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)),
Taz
)
val fuelStation = FuelStation()
fuelStation.reFuel(cars)
}

fun driveCars() {
val vaz1 = Togliatti.buildCar(Vaz2107, Car.Plates("123", 77))
val vaz2 = Togliatti.buildCar(Vaz2108, Car.Plates("321", 78))
Expand Down