Создал дочерний класс InRepairState, импортировал необходимые классы

related #4
parent c7055ffb
import { Driver } from "./Driver"; import { Driver } from "./Driver";
import { TruckState } from "./TruckState/TruckState";
import { InBaseState } from "./TruckState/state/InBaseState";
export class Truck { export class Truck {
id: number; id: number;
...@@ -11,7 +13,12 @@ export class Truck { ...@@ -11,7 +13,12 @@ export class Truck {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.driver = driver; this.driver = driver;
this._state = new BaseState(); this._state = new InBaseState();
this._stringState = this._state.name; this._stringState = this._state.name;
this._state.setTruck(this);
};
changeState (state: TruckState) {
this._state = state;
}; };
}; };
\ No newline at end of file
import { Truck } from "../Truck";
export abstract class TruckState { export abstract class TruckState {
protected truck!: Truck;
public setTruck (truck: Truck) {
this.truck = truck;
};
public abstract name: string; public abstract name: string;
public abstract changeDriver(): void; public abstract changeDriver(): void;
......
import { TruckState } from "../TruckState"; import { TruckState } from "../TruckState";
import { InRepairState } from "./InRepairState";
import { InRunState } from "./InRunState";
export class InBaseState extends TruckState { export class InBaseState extends TruckState {
public name: string = 'На базе'; public name: string = 'На базе';
...@@ -8,10 +10,12 @@ export class InBaseState extends TruckState { ...@@ -8,10 +10,12 @@ export class InBaseState extends TruckState {
}; };
public startRun(): void { public startRun(): void {
this.truck.changeState(new InRunState());
console.log('Грузовик успешно выдвинулся в путь'); console.log('Грузовик успешно выдвинулся в путь');
}; };
public startRepair(): void { public startRepair(): void {
this.truck.changeState(new InRepairState());
console.log('Грузовик успешно отправился на починку'); console.log('Грузовик успешно отправился на починку');
}; };
}; };
\ No newline at end of file
import { TruckState } from "../TruckState";
import { InBaseState } from "./InBaseState";
import { InRunState } from "./InRunState";
export class InRepairState extends TruckState {
public name: string = 'На починке';
public changeDriver(): void {
console.log('Ошибка. Нельзя поменять водителя в пути!');
};
public startRun(): void {
let ranNum: number = Math.random() * 2
if (ranNum = 0) {
this.truck.changeState(new InBaseState());
console.log('Грузовик успешно отправился на базу');
} else if (ranNum = 1) {
this.truck.changeState(new InRunState());
console.log('Грузовик успешно отправился в путь');
};
};
public startRepair(): void {
console.log('Ошибка. Грузовик уже находится в ремонте!');
};
};
\ No newline at end of file
import { TruckState } from "../TruckState"; import { TruckState } from "../TruckState";
import { InRepairState } from "./InRepairState";
export class InRunState extends TruckState { export class InRunState extends TruckState {
public name: string = 'В пути'; public name: string = 'В пути';
...@@ -12,6 +13,7 @@ export class InRunState extends TruckState { ...@@ -12,6 +13,7 @@ export class InRunState extends TruckState {
}; };
public startRepair(): void { public startRepair(): void {
this.truck.changeState(new InRepairState());
console.log('Грузовик успешно отправился на починку'); console.log('Грузовик успешно отправился на починку');
}; };
}; };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment