typescript-program

Program in TypeScript

Here’s a TypeScript program that simulates a system where a city has 5 police stations. Each station has a queue of people waiting, and the program will find the station with the shortest queue.

// Define a PoliceStation interface
interface PoliceStation {
  name: string;
  queueLength: number;
}

// Create an array of 5 police stations with initial queue lengths
let policeStations: PoliceStation[] = [
  { name: "Station A", queueLength: 12 },
  { name: "Station B", queueLength: 5 },
  { name: "Station C", queueLength: 8 },
  { name: "Station D", queueLength: 4 },
  { name: "Station E", queueLength: 10 }
];

// Function to update queue lengths for each station (simulating real-time updates)
function updateQueueLengths(): void {
  policeStations = policeStations.map((station) => ({
    ...station,
    queueLength: Math.floor(Math.random() * 20) // Random queue length between 0 and 19
  }));
}

// Function to find the station with the shortest queue
function findShortestQueue(): PoliceStation {
  return policeStations.reduce((shortest, station) => {
    return station.queueLength < shortest.queueLength ? station : shortest;
  });
}

// Simulate the program
updateQueueLengths(); // Update queues
console.log("Updated Queue Lengths:", policeStations);

const stationWithShortestQueue = findShortestQueue();
console.log(
  `The station with the shortest queue is ${stationWithShortestQueue.name}, with ${stationWithShortestQueue.queueLength} people.`
);

Figure 1: file name : (e.g. polisStationShortQueue.ts)

Explanation:

  1. PoliceStation Interface: Represents a police station with a name and queue length.
  2. policeStations Array: Contains 5 police stations with initial queue lengths.
  3. updateQueueLengths Function: Randomly updates queue lengths to simulate real-time changes.
  4. findShortestQueue Function: Finds and returns the police station with the shortest queue.
  5. Simulation: The program updates queue lengths and finds the station with the shortest queue.

More details describing of updateQueuelengths function

The updateQueuelengths function is designed to simulate dynamic updates to the queue lengths at each police station. This mimics the idea that queue lengths change over time, as people arrive or leave the station.

How It Works

  1. Iterates Through All Police Stations:
    • It uses the .map() method to loop through each police station in the policeStations array.
    • For every station, it generates a new queue length.
  2. Generates Random Queue Lengths:
    • The queue length is updated to a random number between 0 and 19 using the Math.floor(Math.random() * 20) function.
    • Math.random() produces a random decimal number between 0 and 1, and multiplying it by 20 gives a range from 0 to 19. Using Math.floor() rounds down the decimal value to the nearest whole number.
  3. Keeps Other Station Properties (name) Unchanged:

Example Output:

Updated Queue Lengths: [
  { name: 'Station A', queueLength: 10 },
  { name: 'Station B', queueLength: 6 },
  { name: 'Station C', queueLength: 12 },
  { name: 'Station D', queueLength: 3 },
  { name: 'Station E', queueLength: 8 }
]
The station with the shortest queue is Station D, with 3 people.

How to run this program?

First you should Install Node.js and npm and check the version with commands: node -v and nmp -v  and then Install the TypeScript Compiler:

npm install -g typescript
  1. Create a directory and name it (e.g. findPolisStationWithShortQueue)
  2. Open a file  and give the name polisStationShortQueue.ts)
  3.  Copy the code  from Figure 1 and paste to this file
  4. Compile TypeScript to JavaScript, using the tsc command:

    tsc polisStationShortQueue.ts

    This creates a javaScript file: polisStationShortQueue.js

  5. Run the JavaScript Code with Node.js:

    node polisStationShortQueue.js

Every time you run the program it generate stations with new queue.

 

Example Output:

Updated Queue Lengths: [
  { name: 'Station A', queueLength: 12 },
  { name: 'Station B', queueLength: 16 },
  { name: 'Station C', queueLength: 19 },
  { name: 'Station D', queueLength: 14 },
  { name: 'Station E', queueLength: 8 }
]

The output shall be: the station with the shortest queue is Station E, with 8 people.

The Code can be found in myGithup

This post is part of TypeScript-step-by-step

Back to home page