You are on page 1of 9

TASK – 25-07-2023 Nithiyaraj_vadivel

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

<script>

class Bus {

constructor(

Id,

source,

destination,

noOfSeats,

noOfBeds,

fare,

startTime,

endTime,

boardingPoints,

droppingPoints

) {

this.Id = Id;

this.source = source;

this.destination = destination;

this.noOfSeats = noOfSeats;
this.noOfBeds = noOfBeds;

this.fare = fare;

this.startTime = startTime;

this.endTime = endTime;

this.boardingPoints = boardingPoints;

this.droppingPoints = droppingPoints;

const buses = [];

let rowsFilled = 0;

function addBus() {

if (rowsFilled >= 4) {

alert('You have already filled 4 rows in the table.');

return;

const Id = parseInt(prompt('Enter Bus ID:'));

const source = prompt('Enter Source:');

const destination = prompt('Enter Destination:');

const noOfSeats = parseInt(prompt('Enter Number of Seats:'));

const noOfBeds = parseInt(prompt('Enter Number of Beds:'));

const fare = parseFloat(prompt('Enter Fare:'));


const startTime = parseInt(prompt('Enter Start Time (in 24-hour
format):'));

const endTime = parseInt(prompt('Enter End Time (in 24-hour format):'));

const boardingPoints = prompt('Enter Boarding Points (comma-


separated):').split(',');

const droppingPoints = prompt('Enter Dropping Points (comma-


separated):').split(',');

const newBus = new Bus(Id, source, destination, noOfSeats, noOfBeds,


fare, startTime, endTime, boardingPoints, droppingPoints);

buses.push(newBus);

rowsFilled++;

if (rowsFilled === 4) {

alert('You have filled 4 rows. The table is complete!');

function modifyBusById() {

const Id = parseInt(prompt('Enter Bus ID to modify:'));

const index = buses.findIndex(bus => bus.Id === Id);

if (index !== -1) {

const modifiedBus = {};

modifiedBus.source = prompt('Enter new Source (leave empty to skip


modification):');
modifiedBus.destination = prompt('Enter new Destination (leave empty
to skip modification):');

modifiedBus.noOfSeats = parseInt(prompt('Enter new Number of Seats


(leave empty to skip modification):'));

modifiedBus.noOfBeds = parseInt(prompt('Enter new Number of Beds


(leave empty to skip modification):'));

modifiedBus.fare = parseFloat(prompt('Enter new Fare (leave empty to


skip modification):'));

modifiedBus.startTime = parseInt(prompt('Enter new Start Time (in 24-


hour format) (leave empty to skip modification):'));

modifiedBus.endTime = parseInt(prompt('Enter new End Time (in 24-hour


format) (leave empty to skip modification):'));

modifiedBus.boardingPoints = prompt('Enter new Boarding Points (comma-


separated) (leave empty to skip modification):');

modifiedBus.droppingPoints = prompt('Enter new Dropping Points (comma-


separated) (leave empty to skip modification):');

Object.keys(modifiedBus).forEach(key => {

if (modifiedBus[key] === '') {

delete modifiedBus[key];

});

buses[index] = { ...buses[index], ...modifiedBus };

} else {

alert('Bus not found.');

}
function displayAllBuses() {

let tableHtml = '<table border="1"><tr><th>Bus


ID</th><th>Source</th><th>Destination</th><th>No. of Seats</th><th>No. of
Beds</th><th>Fare</th><th>Start Time</th><th>End Time</th><th>Boarding
Points</th><th>Dropping Points</th></tr>';

buses.forEach(bus => {

tableHtml += `

<tr>

<td>${bus.Id}</td>

<td>${bus.source}</td>

<td>${bus.destination}</td>

<td>${bus.noOfSeats}</td>

<td>${bus.noOfBeds}</td>

<td>${bus.fare}</td>

<td>${bus.startTime}</td>

<td>${bus.endTime}</td>

<td>${bus.boardingPoints.join(', ')}</td>

<td>${bus.droppingPoints.join(', ')}</td>

</tr>

`;

});

tableHtml += '</table>';
document.getElementById('busTable').innerHTML = tableHtml;

function displayBusById() {

const Id = parseInt(prompt('Enter Bus ID to display:'));

const bus = buses.find(bus => bus.Id === Id);

if (bus) {

const message = `

Bus ID: ${bus.Id}

Source: ${bus.source}

Destination: ${bus.destination}

Number of Seats: ${bus.noOfSeats}

Number of Beds: ${bus.noOfBeds}

Fare: ${bus.fare}

Start Time: ${bus.startTime}

End Time: ${bus.endTime}

Boarding Points: ${bus.boardingPoints.join(', ')}

Dropping Points: ${bus.droppingPoints.join(', ')}

`;

alert(message);

} else {

alert('Bus not found.');

}
}

</script>

<button onclick="addBus()">Add Bus</button>

<button onclick="modifyBusById()">Modify Bus</button>

<button onclick="displayAllBuses()">Display All Buses</button>

<button onclick="displayBusById()">Display Bus by ID</button>

<div id="busTable"></div>
</body>
</html>

You might also like