Skip to content

Commit

Permalink
a little more code (a lil less embarrassed)
Browse files Browse the repository at this point in the history
  • Loading branch information
INAHIDC committed Sep 28, 2024
0 parents commit b547fa8
Show file tree
Hide file tree
Showing 16 changed files with 1,761 additions and 0 deletions.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

# Library Management System in C


## whatiss it ?

- **Search & Sort**: search for books by title, author, or year, and sort results by title or year. - i used bubble sort
- **Librarian Actions**: Librarians can add and delete books, with 'maybe' plans for modifying book records.
- **Retro ui**: I like the retro look a lot so why not?

---

## Tech Stack

### Backend:
- **C**: Core logic for searching, sorting, and managing books.
- **Node.js**: talks with ui and the C file.
- **Express.js**: RESTful routes.

### Frontend:
- **HTML/CSS**
- **NES.css**
- **JavaScript**

### Database (Future Plan):
- **CSV (Current)**: Csv for now haha
- **SQL (Future)**: probably **SQLite** or **MySQL**

---

## you can try it !

1. Clone the repository:
```bash
git clone https://github.com/INAHIDC/DR.BOOKMAN.git
cd Book Keeper
```

2. Compile the **C files**


3. Run the librarian program

4. Set up the front-end server (You need to have Node.js)
```bash
node server.js
```

### Members:
- `/member` lets you search and sort books.
- type a title, author, or year, and click "Search." and then sort results by title or year after searching.

### Librarians:
- `/librarian`, log in with the password `knowledge`, and manage the library.

## Files Treeee

```
/dr-bookman
├── book_manager.c # main logic
├── librarian.c # Librarian functions
├── books.csv # Book storage
├── server.js # Node.js for front end
├── public
│ ├── index.html # Ni stands for Intuitive Introverts (a carl yungs theory)
│ ├── member.html # search page
│ ├── librarian.html # Librarian page
└── README.md
```
## Maybe Plans (maybe ill maybe not)
1. **Move to SQL**: maybe move from the CSV file to a relational db like **SQL**
2. **More Librarian Functions**: features like modifying book details, borrowing/returning functionality
3. **Gamification**: because im a gamer i think will add grammar challenges or quizzes, turning it into a small game with points and leaderboards?
---
165 changes: 165 additions & 0 deletions book_manager.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_TITLE 100
#define MAX_AUTHOR 50
#define CSV_FILE "books.csv"

typedef struct Book
{
char title[MAX_TITLE];
char author[MAX_AUTHOR];
int year;
} Book;

void add_book(const char *title, const char *author, int year);
void delete_book(const char *title);
void list_books();
void save_book_to_csv(const Book *book);
void load_books_from_csv();
void rewrite_csv_without(const char *title);

int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: ./book_manager <command> [arguments]\n");
return 1;
}

char *command = argv[1];

if (strcmp(command, "add") == 0)
{
if (argc < 5)
{
printf("Usage: ./book_manager add <title> <author> <year>\n");
return 1;
}
char *title = argv[2];
char *author = argv[3];
int year = atoi(argv[4]);
add_book(title, author, year);
printf("Book added: %s by %s (%d)\n", title, author, year);
}
else if (strcmp(command, "list") == 0)
{
list_books();
}
else if (strcmp(command, "delete") == 0)
{
if (argc < 3)
{
printf("Usage: ./book_manager delete <title>\n");
return 1;
}
delete_book(argv[2]);
printf("Book deleted: %s\n", argv[2]);
}
else
{
printf("Unknown command.\n");
}

return 0;
}

// CSV
void add_book(const char *title, const char *author, int year)
{
Book book;
strncpy(book.title, title, MAX_TITLE);
strncpy(book.author, author, MAX_AUTHOR);
book.year = year;

save_book_to_csv(&book);
}

void delete_book(const char *title)
{
rewrite_csv_without(title);
}

void list_books()
{
load_books_from_csv();
}

void save_book_to_csv(const Book *book)
{
FILE *file = fopen(CSV_FILE, "a");
if (!file)
{
printf("Error opening file for writing.\n");
return;
}

fprintf(file, "%s,%s,%d\n", book->title, book->author, book->year);
fclose(file);
}

void load_books_from_csv()
{
FILE *file = fopen(CSV_FILE, "r");
if (!file)
{
printf("No books available.\n");
return;
}

char line[256];
while (fgets(line, sizeof(line), file))
{
char *title = strtok(line, ",");
char *author = strtok(NULL, ",");
int year = atoi(strtok(NULL, ","));

printf("Title: %s\nAuthor: %s\nYear: %d\n\n", title, author, year);
}
fclose(file);
}

void rewrite_csv_without(const char *title)
{
FILE *file = fopen(CSV_FILE, "r");
if (!file)
{
printf("No books to delete.\n");
return;
}

FILE *temp_file = fopen("temp.csv", "w");
if (!temp_file)
{
printf("Error creating temporary file.\n");
fclose(file);
return;
}

char line[256];
int found = 0;
while (fgets(line, sizeof(line), file))
{
char *book_title = strtok(line, ",");
if (strcmp(book_title, title) != 0)
{
fputs(line, temp_file);
}
else
{
found = 1;
}
}

fclose(file);
fclose(temp_file);

remove(CSV_FILE);
rename("temp.csv", CSV_FILE);

if (!found)
{
printf("Book not found.\n");
}
}
Binary file added book_manager.exe
Binary file not shown.
38 changes: 38 additions & 0 deletions books.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Pride and Prejudice, Jane Austen, 1813
1984, George Orwell, 1949
Pather Panchali, Bibhutibhushan Bandopadhyay, 1929
Gitanjali, Rabindranath Tagore, 1910
Gora, Rabindranath Tagore, 1910
The Great Gatsby, F. Scott Fitzgerald, 1925
Slaughterhouse-Five, Kurt Vonnegut, 1969
The Hobbit, J.R.R. Tolkien, 1937
Fahrenheit 451, Ray Bradbury, 1953
The Handmaid's Tale, Margaret Atwood, 1985
Crime and Punishment, Fyodor Dostoevsky, 1866
The Brothers Karamazov, Fyodor Dostoevsky, 1880
War and Peace, Leo Tolstoy, 1869
Anna Karenina, Leo Tolstoy, 1877
The Count of Monte Cristo, Alexandre Dumas, 1844
Don Quixote, Miguel de Cervantes, 1605/1615
Ulysses, James Joyce, 1922
A Tale of Two Cities, Charles Dickens, 1859
The Picture of Dorian Gray, Oscar Wilde, 1890
Lolita, Vladimir Nabokov, 1955
Catch-22, Joseph Heller, 1961
The Master and Margarita, Mikhail Bulgakov, 1967
Invisible Man, Ralph Ellison, 1952
The Sound and the Fury, William Faulkner, 1929
Beloved, Toni Morrison, 1987
One Hundred Years of Solitude, Gabriel Garcia Marquez, 1967
Midnight's Children, Salman Rushdie, 1981
Heart of Darkness, Joseph Conrad, 1899
Things Fall Apart, Chinua Achebe, 1958
The Sun Also Rises, Ernest Hemingway, 1926
The Brief Wondrous Life of Oscar Wao, Junot Díaz, 2007
The Amazing Adventures of Kavalier & Clay, Michael Chabon, 2000
The Goldfinch, Donna Tartt, 2013
The Kite Runner, Khaled Hosseini, 2003
Never Let Me Go, Kazuo Ishiguro, 2005
Persepolis, Marjane Satrapi, 2000
The Hunger Games, Suzanne Collins, 2008
Shantaram, Gregory David Roberts, 2003
Loading

0 comments on commit b547fa8

Please sign in to comment.