0% found this document useful (0 votes)
7 views6 pages

c micro project

Uploaded by

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

c micro project

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct Book
{
int id;
char title[100];
int price;
int qty;
char category[100];
int isBorrowed;
char borrower[100];
struct Book* next;
};
struct Book* start = NULL;
int createOrInsert(int id, char title[], int price, int qty, char category[])

{
struct Book* temp = (struct Book*)malloc(sizeof(struct Book));
temp->id = id;
strcpy(temp->title, title);
temp->price = price;
temp->qty = qty;
strcpy(temp->category, category);

if (start == NULL)
{
temp->next = temp;
start = temp;
} else {
struct Book* q = start;
while (q->next != start)
{
q = q->next;
}
q->next = temp;
temp->next = start;
}
return 1;
}
void countBooks()
{
int count = 0;
struct Book* q = start;

if (q == NULL) {
printf("The list is empty.\n");
return;
}

do {
count++;
q = q->next;
} while (q != start);

printf("Total number of books: %d\n", count);


}
void displayBooks() {
struct Book* q = start;
if (q == NULL) {
printf("The list is empty.\n");
return;
}

printf("List of Books:\n");
printf("------------------------------------------------------------\n");
printf("| ID | Title | Price | Qty | Category |\n");
printf("------------------------------------------------------------\n");

do {
printf("| %d | %s | %d | %d | %s |\n", q->id, q->title, q-
>price, q->qty, q->category);
q = q->next;
} while (q != start);

printf("------------------------------------------------------------\n");
}

void searchBook(int bookID)


{
struct Book* q = start;

if (q == NULL)
{
printf("The library is empty. No books to search.\n");
return;
}

while (1) {
if (q->id == bookID)
{
printf("Book found:\n");
printf("------------------------------------------------------------\
n");
printf("| %-4d | %-3s | %-6d | %-4d | %-15s |\n", q->id, q->title, q-
>price, q->qty, q->category);
printf("------------------------------------------------------------\
n");

return;
}

q = q->next;
if (q == start)
{
printf("Book with ID %d not found in the library.\n", bookID);
return;
}
}
}

void borrowBook(int bookID, const char *personName) {


struct Book *q = start;
int found = 0;
while (q->next != start) {
if (q->id == bookID) {
found = 1;
if (q->isBorrowed == 0) {
q->isBorrowed = 1;
strcpy(q->borrower, personName);
printf("Book with ID %d has been successfully borrowed by %s.\n",
bookID, personName);
break;
} else {
printf("Book with ID %d is currently unavailable.\n", bookID);
break;
}
}
q = q->next;
}

if (!found) {
printf("Book with ID %d not found in the library.\n", bookID);
}
}
float calculateBorrowedBookFee(int bookID, int daysLate) {

float lateFeePerDay = 30.0;

struct Book* q = start;


int found = 0;
int bookPrice = 0;

while (q->next != start) {


if (q->id == bookID) {
found = 1;
if (q->isBorrowed == 1)
{
float lateFee = lateFeePerDay * daysLate;
float totalFee = q->price + lateFee;
printf("Total fee for the borrowed book (Book ID %d): %.2f\n",
bookID, totalFee);
break;
} else
{
printf("Book with ID %d is not borrowed or is not late.\n",
bookID);
break;
}
}
bookPrice = q->price;
q = q->next;
}

if (!found) {
printf("Book with ID %d not found in the library.\n", bookID);
}
}
void sortBooksByID() {
struct Book *q= start;
struct Book *index = NULL;
int temp;
if (start == NULL) {
return;
} else {
do {
index = q->next;

while (index != start) {


if (q->id > index->id) {
temp = q->id;
q->id = index->id;
index->id = temp;
}
index = index->next;
}
q =q->next;
} while (q != start);
}
}

int main()
{
int choice;
int id, price, qty;
char title[100], category[100];
int bookID1, bookID2;
printf("*********Library Management System*********\n");
while (1) {
printf("MENU:\n");
printf("1. Add books\n");
printf("2. Display all books\n");
printf("3. Count added books\n");
printf("4. Search for a book\n");
printf("5. Borrow a book\n");
printf("6. Calculate borrowed book fee\n");
printf("7. Sort books by ID\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
{
printf("____________________________________\n");
int numBooks, i;
printf("Enter the number of books to add: ");
scanf("%d", &numBooks);

for (i = 0; i < numBooks; i++) {

printf("Enter book ID: ");


scanf("%d",&id);
printf("\nEnter book title: ");
scanf(" %s",title);
printf("\nEnter book price: ");
scanf("%d",&price);
printf("\nEnter book quantity: ");
scanf("%d",&qty);
printf("\nEnter book category: ");
scanf(" %s",&category);
int result = createOrInsert(id, title, price, qty, category);

if (result)
{
printf("BOOK ADDED SUCESSFULLY.\n");
printf("____________________________________\n");
} else {
printf("Failed to add the book.\n");
}
}
break;
}
case 2:
printf("____________________________________\n");
displayBooks();
break;
case 3:
printf("____________________________________\n");

countBooks();
printf("____________________________________\n");

break;
case 4:
printf("____________________________________\n");
printf("Enter the ID of the book you want to search: ");
scanf("%d", &id);
searchBook(id);
break;

case 5:
char personName[100];
printf("____________________________________\n");
printf("Enter the ID of the book you want to borrow: ");
scanf("%d", &id);
printf("Enter your name: ");
scanf("%s", &personName);
borrowBook(id, personName);
break;
case 6:
{
int bookID;
printf("Enter the ID of the borrowed book: ");
scanf("%d", &bookID);
int daysLate;
printf("Enter the number of days late: ");
scanf("%d", &daysLate);

calculateBorrowedBookFee(bookID, daysLate);
break;
}
case 7:

printf("Sorting books by ID...\n");


sortBooksByID();
printf("Books sorted by ID.\n");
break;
case 8:

printf("Exiting the program.\n");


exit(0);
break;

default:
printf("Invalid choice. Please select a valid option.\n");
break;
}
}

return 0;
}

You might also like