Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- enum Gender{MALE=1, FEMALE=2};
- struct Date{
- int day;
- int month;
- int year;
- Date(){
- day=1;
- month=1;
- year=1990;
- }
- Date(int day, int month, int year){
- this->day = day;
- this->month=month;
- this->year=year;
- }
- };
- struct User{
- int u_id;
- string u_name;
- string username;
- string password;
- string email;
- Date birthday;
- Gender gender;
- string phone;
- User(){}
- User(int _u_id, string _u_name, string _username, string _password, string _email, Date _birthday, Gender _gender, string _phone){
- u_id = _u_id;
- u_name = _u_name;
- username = _username;
- password = _password;
- email = _email;
- birthday = _birthday;
- gender = _gender;
- phone = _phone;
- }
- };
- void printUser(const User& u){
- cout <<"\tUsername: " <<u.username << " - " <<"Email: "<< u.email << endl;
- }
- void printAllUsers(const User us[], int size){
- for(int i=0; i<size; i++){
- printUser(us[i]);
- }
- }
- void insert(User us[], int& size, User u){
- us[size++] = u;
- }
- void deleteUser(User us[], int& size, int u_id){
- int findIndex = -1;
- for(int i=0; i<size; i++){
- if(us[i].u_id==u_id){
- findIndex = i;
- break;
- }
- }
- if(findIndex==-1){
- cout << "Couldn't find your id ";
- }else{
- for(int i=findIndex; i<size-1; i++){
- us[i] = us[i+1];
- }
- }
- size--;
- }
- User enterInfo(){
- User temp;
- cout<<"Enter your ID: ";
- cin>>temp.u_id;
- cin.ignore();
- cout<<"Enter your name: ";
- getline(cin,temp.u_name);
- cout<<"Enter username: ";
- cin>>temp.username;
- cin.ignore();
- cout<<"Enter password: ";
- cin>>temp.password;
- cin.ignore();
- cout<<"Enter email: ";
- cin>>temp.email;
- cin.ignore();
- cout<<"Enter phone number: ";
- cin>>temp.phone;
- cout<<"Enter birtday (dd/mm/yy): ";
- char c;
- cin>>temp.birthday.day>>c>>temp.birthday.month>>c>>temp.birthday.year;
- cin.ignore();
- int gender;
- cout<<"Enter gender(MALE=1/FEMALE=2): ";
- cin>>gender;
- if(gender==1)
- temp.gender=MALE;
- else
- temp.gender=FEMALE;
- return temp;
- }
- struct Book{
- int book_id;
- string name;
- int current_u;
- User user[30];
- Book(){};
- Book(int id,string name){
- current_u=0;
- this->name=name;
- book_id=id;
- }
- };
- bool checkBookValid(int id,const Book book[],int current_book){
- for(int i=0;i<current_book;i++)
- if(book[i].book_id==id){
- return 0;
- cout<<"OK";
- }
- return 1;
- }
- int main(){
- int current_book=3,id;
- Book book[100];
- book[0]=Book(182852,"Co Gai Den Tu Hom Qua");
- book[1]=Book(257874,"Toi Thay Hoa Vang Tren Co Xanh");
- book[2]=Book(125571,"Mat Biec");
- char respond;
- while(1){
- cout<<"Current book available in the library:"<<endl;
- for(int i=0;i<current_book;i++){
- cout<<"\tID: "<<book[i].book_id<<" - Name: "<<book[i].name<<endl;
- }
- cout<<endl;
- cout<<"'b' to borrow a book"<<endl;
- cout<<"'r' to return a book"<<endl;
- cout<<"'s' to show all user's info borrowed book"<<endl;
- cout<<"'i' to insert new book to library"<<endl;
- cout<<"'q' to quit"<<endl;
- cout<<"\n Enter key: ";
- cin>>respond;
- switch(respond){
- case 'i':{
- int b_id;
- string name;
- do{
- cout<<"Enter new book ID: ";
- cin>>b_id;
- }while(!checkBookValid(b_id,book,current_book));
- cout<<"Enter new book name: ";
- cin.ignore();
- getline(cin,name);
- book[current_book].book_id=b_id;
- book[current_book].name=name;
- current_book++;
- break;
- }
- case 'b': {
- cout<<"Enter book ID: ";
- cin>>id;
- int i=0;
- for(;book[i].book_id!=id && i<=current_book;i++);
- if(i==3){
- cout<<"Couldn't find book ID " <<id<<endl;
- break;
- }
- else
- cout<<"BOOK NAME: "<<"\t"<<book[i].name<<endl;
- User temp=enterInfo();
- insert(book[i].user,book[i].current_u,temp);
- break;
- }
- case 's':{
- for(int i=0;i<current_book;i++){
- cout<<"BOOK: "<<book[i].name<<endl;
- printAllUsers(book[i].user,book[i].current_u);
- cout << "---------------------" << endl;
- }
- break;
- }
- case 'r':{
- int b_id,u_id;
- cout<<"Enter book ID to return :";
- cin>>b_id;
- int i=0;
- for(;book[i].book_id!=id && i<=current_book;i++);
- if(i==3){
- cout<<"Couldn't find book ID " <<id<<endl;
- break;
- }
- else{
- cout<<"BOOK NAME: "<<"\t"<<book[i].name<<endl;
- cout<<"Enter your user ID: ";
- cin>>u_id;
- deleteUser(book[i].user,book[i].current_u,u_id);
- break;
- }
- }
- case 'q':
- return 0;
- default:
- cout<<"Invalid key"<<endl;
- }
- system("cls");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement