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:
PoliceStation
Interface: Represents a police station with a name and queue length.policeStations
Array: Contains 5 police stations with initial queue lengths.updateQueueLengths
Function: Randomly updates queue lengths to simulate real-time changes.findShortestQueue
Function: Finds and returns the police station with the shortest queue.- 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
- Iterates Through All Police Stations:
- It uses the
.map()
method to loop through each police station in thepoliceStations
array. - For every station, it generates a new queue length.
- It uses the
- 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. UsingMath.floor()
rounds down the decimal value to the nearest whole number.
- The queue length is updated to a random number between 0 and 19 using the
- 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
- Create a directory and name it (e.g. findPolisStationWithShortQueue)
- Open a file and give the name polisStationShortQueue.ts)
- Copy the code from Figure 1 and paste to this file
-
Compile TypeScript to JavaScript, using the tsc command:
tsc polisStationShortQueue.ts
This creates a javaScript file: polisStationShortQueue.js
-
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