TypeScript: A fleet of vehicles

Vehicles share the rules for driving and refuelling, and differ in how much fuel they use. Put the shared rules in an abstract class.

What to build

A program that drives and refuels a fleet and reports on it.

Requirements

  • Write an abstract class Vehicle with a name, protected fuel and capacity, an abstract litresPerKm(): number, drive(km: number): string, refuel(litres: number): string and status(): string.
  • Subclasses: Car (0.08 litres per km), Truck (0.25 litres per km, plus 0.05 for each tonne of load) and Bike (0 litres, capacity 0).
  • drive NAME KM: if there is enough fuel, use it and return NAME drove KM km; otherwise return NAME needs F more litres, with F the missing fuel to one decimal place, and do not move.
  • refuel NAME L: add up to the capacity and return NAME refuelled to F litres, or NAME has no tank for a bike. status: every vehicle in the order added, NAME: F litres, D km, fuel to one decimal place.

Input

Line 1: n. Then n lines: car NAME CAPACITY, truck NAME CAPACITY LOAD, bike NAME, drive NAME KM, refuel NAME L or status. Vehicles start with a full tank and 0 km.

Output

One line per drive and refuel, and the status lines.

Constraints

1 ≤ n ≤ 100; names are distinct; numbers are not negative.

Example

Input:

6
car mini 40
truck big 200 4
drive mini 100
drive big 500
refuel mini 50
status

Output:

mini drove 100 km
big needs 25.0 more litres
mini refuelled to 40.0 litres
mini: 40.0 litres, 100 km
big: 200.0 litres, 0 km

Why it works

drive() is written once in the parent and asks each object for its own consumption, so every vehicle follows the same rules about fuel while using its own figure.

Examples

Example 1 · Input
6
car mini 40
truck big 200 4
drive mini 100
drive big 500
refuel mini 50
status
Expected output
mini drove 100 km
big needs 25.0 more litres
mini refuelled to 40.0 litres
mini: 40.0 litres, 100 km
big: 200.0 litres, 0 km
Example 2 · Input
4
bike bmx
drive bmx 1000
refuel bmx 5
status
Expected output
bmx drove 1000 km
bmx has no tank
bmx: 0.0 litres, 1000 km

Hints

Hint 1

abstract class Vehicle keeps protected fuel and capacity, and drive() uses the abstract litresPerKm().

Hint 2

A bike's litresPerKm() is 0, so it can always drive.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";

abstract class Vehicle {
  protected km = 0;
  constructor(readonly name: string, protected capacity: number, protected fuel: number = capacity) {}

  abstract litresPerKm(): number;

  drive(km: number): string {
    const needed = km * this.litresPerKm();
    if (needed > this.fuel + 1e-9) return this.name + " needs " + (needed - this.fuel).toFixed(1) + " more litres";
    this.fuel -= needed;
    this.km += km;
    return this.name + " drove " + km + " km";
  }

  refuel(litres: number): string {
    if (this.capacity === 0) return this.name + " has no tank";
    this.fuel = Math.min(this.capacity, this.fuel + litres);
    return this.name + " refuelled to " + this.fuel.toFixed(1) + " litres";
  }

  status(): string {
    return this.name + ": " + this.fuel.toFixed(1) + " litres, " + this.km + " km";
  }
}

class Car extends Vehicle {
  litresPerKm(): number {
    return 0.08;
  }
}

class Truck extends Vehicle {
  constructor(name: string, capacity: number, private load: number) {
    super(name, capacity);
  }
  litresPerKm(): number {
    return 0.25 + 0.05 * this.load;
  }
}

class Bike extends Vehicle {
  constructor(name: string) {
    super(name, 0);
  }
  litresPerKm(): number {
    return 0;
  }
}

const fleet = new Map<string, Vehicle>();
const n = Number(read());
for (let i = 0; i < n; i++) {
  const [command, name, a, b] = read().split(" ");
  if (command === "car") fleet.set(name, new Car(name, Number(a)));
  else if (command === "truck") fleet.set(name, new Truck(name, Number(a), Number(b)));
  else if (command === "bike") fleet.set(name, new Bike(name));
  else if (command === "status") for (const v of fleet.values()) console.log(v.status());
  else {
    const vehicle = fleet.get(name);
    if (!vehicle) continue;
    console.log(command === "drive" ? vehicle.drive(Number(a)) : vehicle.refuel(Number(a)));
  }
}

Lesson: Inheritance, abstract classes and implements →