1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/* exercise - library software, which takes book names,ISBN in form n-n-n-x where n is integer and x is integer or letter and other details.
*/
enum class Genre
{
fiction=1,nonfiction,periodical,biography,children
};
class Book
{
public:
void take_ISBN_details(Book& b);
void show_details();
void rent();
vector<Book> ret_rack(){return rack;}
string ret_ISBN() {return ISBN;}
private:
string ISBN,author,title,cpydate;
Genre gen;
bool checkedout{false};
vector<Book>rack;
};
bool verify_ISBN(Book& b,string ISBN)
{
int a{0};
//check ISBN for format error n-n-n-x
for(int i=0; i<ISBN.size(); ++i)
{
if(ISBN[i]=='-') ++a;
if(a==0 || a==1 || a==2)
{
if(isalpha(ISBN[i]))
{
cout<<"\nInvalid ISBN"<<endl;
return false;
break;
}
}
else if(a>3)
{
cout<<"\nInvalid ISBN"<<endl;
return false;
break;
}
}
if(a==0)
{
cout<<"\nInvalid ISBN"<<endl;
return false;
}
// check ISBN if duplicate
for(auto& a:b.ret_rack())
{
if(a.ret_ISBN() == ISBN)
{
cout<<"\nDuplicate ISBN";
cout<<"Press any key to continue";
return false;
getch();
system("CLS");
}
}
return true;
}
void Book::take_ISBN_details(Book& b)
{
char c;
do
{
bool flag{true};
do
{
cout<<"\nEnter ISBN: ";
getline(cin,ISBN);
flag=verify_ISBN(b,ISBN);
}
while(!flag);
if(flag)
{
string str;
cout<<"\nISBN valid "<<endl;
cout<<"Enter author name: ";
getline(cin,author);
cout<<"Enter Title: ";
getline(cin,title);
cout<<"Enter copyright date: (dd/mm/yy)";
getline(cin,cpydate);
cout<<"Enter Genre(fiction,nonfiction,periodical,biography,children): ";
cin>>str;
if(str=="fiction") gen=Genre::fiction;
else if(str=="nonfiction") gen=Genre::nonfiction;
else if(str=="periodical") gen=Genre::periodical;
else if(str=="biography") gen=Genre::biography;
else if(str=="children") gen=Genre::children;
rack.emplace_back(b);
}
cout<<"\nRecord updated. Enter another record?(y/n): ";
cin>>c;
cin.ignore();
}
while(c=='y');
cout<<"Press any key to continue";
getch();
system("CLS");
}
void Book::show_details()
{
for(const auto& a:rack)
{
cout<<"\n"<<"ISBN: "<<a.ISBN<<" "<<"Author: "<<a.author<<" "<<"Title: "<<a.title<<" "<<"Copyright date: "<<a.cpydate<<endl;
}
cout<<"Press any key to continue";
getch();
system("CLS");
}
void Book::rent()
{
string name; bool flag{false};
cout<<"\nEnter the book name: "; getline(cin,name);
for(auto& a:rack)
{
if(name==a.title && a.checkedout==true)
{
cout<<"Book already rented out "<<endl; flag=true;
break;
}
if(name==a.title && a.checkedout==false )
{
a.checkedout=true;
cout<<"\nBook rented"<<endl; flag=true;
break;
}
}
if(!flag) cout<<"\nBook not found"<<endl;
cout<<"Press any key to continue"; getch();
system("CLS");
}
int main()
{
Book b; int choice;
while(true)
{
cout<<"Welcome to the library. "<<"\n"<<"Enter your choice(1,2,3)"<<"\n1. Enter a new record"<<"\n2. Show all records"<<"\n3. Rent a book"<<endl;
cin>>choice;
cin.ignore();
switch(choice)
{
case 1:
b.take_ISBN_details(b);
break;
case 2:
b.show_details();
break;
case 3:
b.rent();
break;
}
}
}
|