0% found this document useful (0 votes)
26 views9 pages

Code Pairing - Solutions For Asked Problems

The document contains three separate programming solutions for different problems: a library management system, a parking lot management system, and a ticket booking system for shows. Each solution includes classes and methods for managing books, vehicles, and show tickets, respectively, with functionalities such as borrowing, returning, parking, un-parking, booking, and canceling tickets. The code is implemented in C++ and C, demonstrating object-oriented programming principles and basic data structures.

Uploaded by

cse21216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Code Pairing - Solutions For Asked Problems

The document contains three separate programming solutions for different problems: a library management system, a parking lot management system, and a ticket booking system for shows. Each solution includes classes and methods for managing books, vehicles, and show tickets, respectively, with functionalities such as borrowing, returning, parking, un-parking, booking, and canceling tickets. The code is implemented in C++ and C, demonstrating object-oriented programming principles and basic data structures.

Uploaded by

cse21216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Problem statement 1:​

Solution 1:
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Book {
public:
string title, author, isbn;
bool available;

Book(string t, string a, string i) : title(t), author(a), isbn(i), available(true) {}


};

class Member {
public:
string name;
int memberID;
vector<string> borrowedBooks;

Member(string n, int id) : name(n), memberID(id) {}


};

class Library {
private:
vector<Book> books;
vector<Member> members;

public:
void addBook(string title, string author, string isbn) {
books.push_back(Book(title, author, isbn));
}

void addMember(string name, int id) {


members.push_back(Member(name, id));
}

void borrowBook(int memberID, string isbn) {


for (auto& book : books) {
if (book.isbn == isbn && book.available) {
for (auto& member : members) {
if (member.memberID == memberID) {
member.borrowedBooks.push_back(isbn);
book.available = false;
cout << "Book borrowed successfully!\n";
return;
}
}
}
}
cout << "Book not available or invalid member ID.\n";
}

void returnBook(int memberID, string isbn) {


for (auto& member : members) {
if (member.memberID == memberID) {
for (size_t i = 0; i < member.borrowedBooks.size(); i++) {
if (member.borrowedBooks[i] == isbn) {
member.borrowedBooks.erase(member.borrowedBooks.begin() + i);
for (auto& book : books) {
if (book.isbn == isbn) {
book.available = true;
cout << "Book returned successfully!\n";
return;
}
}
}
}
}
}
cout << "Invalid return attempt!\n";
}

void viewAvailableBooks() {
cout << "Available Books: " << books.size() << "\n";
for (const auto& book : books) {
if (book.available) {
cout << "Title: " << book.title << ", Author: " << book.author << ", ISBN: "
<< book.isbn << "\n";
}
}
}

void searchBook(string query) {


cout << "Search Results:\n";
for (const auto& book : books) {
if (book.title.find(query) != string::npos || book.author.find(query) !=
string::npos) {
cout << "Title: " << book.title << ", Author: " << book.author << ", ISBN: "
<< book.isbn << ", Available: " << (book.available ? "Yes" : "No") << "\n";
}
}
}
};

int main() {
Library lib;
lib.addBook("The Great Gatsby", "F. Scott Fitzgerald", "1234567890");
lib.addBook("1984", "George Orwell", "0987654321");
lib.addMember("Alice", 1);
lib.addMember("Bob", 2);

lib.borrowBook(1, "1234567890");
lib.viewAvailableBooks();
lib.returnBook(1, "1234567890");
lib.viewAvailableBooks();
lib.searchBook("1984");

return 0;
}
Problem statement 2:

Solution :
#include <iostream>
#include <vector>
#include <unordered_map>
#include <ctime>
using namespace std;

enum VehicleSize { SMALL, MEDIUM, LARGE };

class Vehicle {
public:
string licensePlate;
VehicleSize size;
time_t entryTime;

Vehicle(string plate, VehicleSize sz) : licensePlate(plate), size(sz) {


entryTime = time(nullptr);
}
};

class ParkingSpot {
public:
int level, spotNumber;
VehicleSize size;
bool isOccupied;

ParkingSpot(int lvl, int num, VehicleSize sz) : level(lvl), spotNumber(num), size(sz),


isOccupied(false) {}
};

class ParkingLot {
private:
int levels;
int spotsPerLevel;
vector<vector<ParkingSpot>> parkingSpots;
unordered_map<string, pair<int, int>> parkedCars; // Maps license plate to (level,
spotNumber)
unordered_map<string, time_t> parkingTime; // Maps license plate to entry time
double hourlyRate = 50.0;

public:
ParkingLot(int lvls, int spots) : levels(lvls), spotsPerLevel(spots) {
for (int i = 0; i < levels; i++) {
vector<ParkingSpot> levelSpots;
for (int j = 0; j < spotsPerLevel; j++) {
VehicleSize size = (j < spots / 3) ? SMALL : (j < 2 * spots / 3) ? MEDIUM :
LARGE;
levelSpots.emplace_back(i, j, size);
}
parkingSpots.push_back(levelSpots);
}
}

bool parkVehicle(Vehicle vehicle) {


for (int i = 0; i < levels; i++) {
for (int j = 0; j < spotsPerLevel; j++) {
if (!parkingSpots[i][j].isOccupied && parkingSpots[i][j].size >= vehicle.size)
{
parkingSpots[i][j].isOccupied = true;
parkedCars[vehicle.licensePlate] = {i, j};
parkingTime[vehicle.licensePlate] = vehicle.entryTime;
cout << "Vehicle " << vehicle.licensePlate << " parked at Level " << i <<
" Spot " << j << "\n";
return true;
}
}
}
cout << "Parking full or no suitable spot for " << vehicle.licensePlate << "\n";
return false;
}

bool unparkVehicle(string licensePlate) {


if (parkedCars.find(licensePlate) != parkedCars.end()) {
int level = parkedCars[licensePlate].first;
int spot = parkedCars[licensePlate].second;
parkingSpots[level][spot].isOccupied = false;
parkedCars.erase(licensePlate);

time_t exitTime = time(nullptr);


double hoursParked = 2; //difftime(exitTime, parkingTime[licensePlate]) / 3600;
double totalCharge = hoursParked * hourlyRate;
parkingTime.erase(licensePlate);
cout << "Vehicle " << licensePlate << " has left the parking lot.\n";
cout << "Total charge: Rs " << totalCharge << " for " << hoursParked << "
hours.\n";
return true;
}
cout << "Vehicle not found in the parking lot. " << licensePlate << "\n";
return false;
}

bool isFull() {
return parkedCars.size() >= levels * spotsPerLevel;
}

void reserveSpot(int level, int spotNumber) {


if (level < levels && spotNumber < spotsPerLevel &&
!parkingSpots[level][spotNumber].isOccupied) {
parkingSpots[level][spotNumber].isOccupied = true;
cout << "Spot " << spotNumber << " at Level " << level << " has been reserved.\n";
} else {
cout << "Invalid or already occupied spot.\n";
}
}
};

int main() {
ParkingLot lot(3, 9);

Vehicle car1("UP16 AB 1234", SMALL);


Vehicle car2("DL4 SAB 2345", MEDIUM);
Vehicle car3("UP16 AZ 9876", LARGE);

lot.parkVehicle(car1);
lot.parkVehicle(car2);
lot.parkVehicle(car3);

lot.unparkVehicle("DL4 SAB 2345");

// testing for invalid vehicle number


lot.unparkVehicle("AAA AAA 1234");

if (lot.isFull()) {
cout << "Parking lot is full!\n";
} else {
cout << "Parking lot has space available.\n";
}

lot.reserveSpot(1, 2);

return 0;
}
Problem statement 3:​

Solution:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_SHOWS 5
#define MAX_SEATS 10

typedef struct {
char eventName[50];
char date[20];
int availableSeats;
int seats[MAX_SEATS]; // 0 for available, 1 for booked
} Show;

Show shows[MAX_SHOWS];
int showCount = 0;

void addShow(char *name, char *date, int seats) {


if (showCount >= MAX_SHOWS) {
printf("Cannot add more shows.\n");
return;
}
strcpy(shows[showCount].eventName, name);
strcpy(shows[showCount].date, date);
shows[showCount].availableSeats = seats;
for (int i = 0; i < MAX_SEATS; i++) {
shows[showCount].seats[i] = 0;
}
showCount++;
}

void viewShows() {
printf("Available Shows:\n");
for (int i = 0; i < showCount; i++) {
printf("%d. %s on %s (%d seats available)\n", i + 1, shows[i].eventName,
shows[i].date, shows[i].availableSeats);
}
}

void bookTicket(int showIndex, int seatNumber) {


if (showIndex < 0 || showIndex >= showCount || seatNumber < 1 || seatNumber > MAX_SEATS) {
printf("Invalid selection!\n");
return;
}
if (shows[showIndex].seats[seatNumber - 1] == 0) {
shows[showIndex].seats[seatNumber - 1] = 1;
shows[showIndex].availableSeats--;
printf("Ticket booked for %s, Seat %d.\n", shows[showIndex].eventName, seatNumber);
} else {
printf("Seat already booked!\n");
}
}

void bookGroupTickets(int showIndex, int numSeats) {


if (showIndex < 0 || showIndex >= showCount || numSeats > shows[showIndex].availableSeats)
{
printf("Invalid selection or not enough seats available!\n");
return;
}
printf("Booking %d seats for %s:\n", numSeats, shows[showIndex].eventName);
int booked = 0;
for (int i = 0; i < MAX_SEATS && booked < numSeats; i++) {
if (shows[showIndex].seats[i] == 0) {
shows[showIndex].seats[i] = 1;
shows[showIndex].availableSeats--;
printf("Seat %d booked.\n", i + 1);
booked++;
}
}
}

void cancelBooking(int showIndex, int seatNumber) {


if (showIndex < 0 || showIndex >= showCount || seatNumber < 1 || seatNumber > MAX_SEATS) {
printf("Invalid selection!\n");
return;
}
if (shows[showIndex].seats[seatNumber - 1] == 1) {
shows[showIndex].seats[seatNumber - 1] = 0;
shows[showIndex].availableSeats++;
printf("Booking canceled for %s, Seat %d.\n", shows[showIndex].eventName, seatNumber);
} else {
printf("Seat was not booked!\n");
}
}
int main() {
addShow((char*)"Rock Concert", (char*)"2025-03-10", MAX_SEATS);
addShow((char*)"Movie Premiere", (char*)"2025-04-15", MAX_SEATS);

int choice, showIndex, seatNumber, numSeats;


while (1) {
printf("\n1. View Shows\n2. Book Ticket\n3. Book Group Tickets\n4. Cancel Booking\n5.
Exit\nChoose an option: ");
scanf("%d", &choice);
switch (choice) {
case 1:
viewShows();
break;
case 2:
viewShows();
printf("Enter show number: ");
scanf("%d", &showIndex);
printf("Enter seat number (1-%d): ", MAX_SEATS);
scanf("%d", &seatNumber);
bookTicket(showIndex - 1, seatNumber);
break;
case 3:
viewShows();
printf("Enter show number: ");
scanf("%d", &showIndex);
printf("Enter number of seats: ");
scanf("%d", &numSeats);
bookGroupTickets(showIndex - 1, numSeats);
break;
case 4:
viewShows();
printf("Enter show number: ");
scanf("%d", &showIndex);
printf("Enter seat number (1-%d): ", MAX_SEATS);
scanf("%d", &seatNumber);
cancelBooking(showIndex - 1, seatNumber);
break;
case 5:
printf("Exiting system.\n");
return 0;
default:
printf("Invalid option!\n");
}
}
}

You might also like