Skip to content

Instantly share code, notes, and snippets.

@itsyashuvo
Created May 14, 2024 13:55
Show Gist options
  • Save itsyashuvo/9277d0a06e96936fb97ffcb6ba6a3861 to your computer and use it in GitHub Desktop.
Save itsyashuvo/9277d0a06e96936fb97ffcb6ba6a3861 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Book
{
private:
int id;
string title;
string author;
int number;
public:
Book(int id, string title, string author, int number)
{
this->id = id;
this->title = title;
this->author = author;
this->number = number;
}
int getID()
{
return id;
}
string getTitle()
{
return title;
}
string getAuthor()
{
return author;
}
int getNumber()
{
return number;
}
};
class Member
{
private:
string username;
string password;
string name;
string phone;
public:
Member(string uname, string pass, string name, string phone)
{
username = uname;
password = pass;
this->name = name;
this->phone = phone;
}
string getUsername()
{
return username;
}
string getPassword()
{
return password;
}
string getName()
{
return name;
}
string getPhone()
{
return phone;
}
void login(vector<Book> &books)
{
// TODO
}
};
class Admin
{
private:
string username;
string password;
string name;
public:
Admin(string uname, string pwd, string n)
{
username = uname;
password = pwd;
name = n;
}
string getUsername()
{
return username;
}
string getPassword()
{
return password;
}
string getName()
{
return name;
}
void login(vector<Book> books, vector<Member> members)
{
// TODO
}
};
class Library
{
private:
vector<Book> books;
vector<Member> members;
vector<Admin> admins;
public:
Library()
{
fstream booksfile("books.txt");
fstream adminsfile("admins.txt");
fstream membersfile("members.txt");
if (booksfile.is_open())
{
int id;
string title;
string author;
int number;
while (booksfile >> id >> title >> author >> number)
{
Book book(id, title, author, number);
books.push_back(book);
}
booksfile.close();
}
if (adminsfile.is_open())
{
string username;
string password;
string name;
while (adminsfile >> username >> password >> name)
{
Admin admin(username, password, name);
admins.push_back(admin);
}
adminsfile.close();
}
if (membersfile.is_open())
{
string username;
string password;
string name;
string phone;
while (membersfile >> username >> password >> name >> phone)
{
Member member(username, password, name, phone);
members.push_back(member);
}
membersfile.close();
}
}
void run()
{
while (true)
{
int choice;
string username, password;
cout << "1. Member" << endl;
cout << "2. Admin" << endl;
cin >> choice;
cout << "Username: ";
cin >> username;
cout << "Password: ";
cin >> password;
if (choice == 1)
{
bool success = false;
for (auto &member : members)
{
if (member.getUsername() == username && member.getPassword() == password)
{
member.login(books);
success = true;
}
}
if (!success)
{
cout << "Invalid username or password, please try again..." << endl;
}
}
else if (choice == 2)
{
bool success = false;
for (auto &admin : admins)
{
if (admin.getUsername() == username && admin.getPassword() == password)
{
admin.login(books, members);
success = true;
}
}
if (!success)
{
cout << "Invalid username or password, please try again..." << endl;
}
}
}
}
~Library()
{
fstream booksfile("books.txt");
fstream adminsfile("admins.txt");
fstream membersfile("members.txt");
if (booksfile.is_open())
{
for (auto &book : books)
{
booksfile << book.getID() << endl;
booksfile << book.getTitle() << endl;
booksfile << book.getAuthor() << endl;
booksfile << book.getNumber() << endl;
}
booksfile.close();
}
if (adminsfile.is_open())
{
for (auto &admin : admins)
{
adminsfile << admin.getUsername() << endl;
adminsfile << admin.getPassword() << endl;
adminsfile << admin.getName() << endl;
}
adminsfile.close();
}
if (membersfile.is_open())
{
for (auto &member : members)
{
membersfile << member.getUsername() << endl;
membersfile << member.getPassword() << endl;
membersfile << member.getName() << endl;
membersfile << member.getPhone() << endl;
}
membersfile.close();
}
}
};
int main()
{
Library library;
library.run();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment