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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
// Description: This program simulates the checking out and checking in of books from a library
#include <iostream>
#include <fstream>
#include "card.h"
#include "book.hpp"
#include <iomanip>
using namespace std;
void ShowMenu();
int findLibraryCard(int card, class card cards[]);
int findbook(int book, class book books[]);
int main()
{
// declare an array of 20 cards
card cards[20];
int numCards = 0;
int i = 0, j = 0;
int command = 0;
// declare an array of 20 books
// declare a file pointer and open the cards file
ifstream cardInput("cards.rtf");
if (!cardInput)
{
cout << "ERROR: Cannot open file cards.rtf " << endl;
system("pause");
exit(1);
}
cout << "File openned. Reading cards.rtf... " << endl;
// read the next line from the file
char name[30];
char phone[15];
int cardID;
char junk[5];
cardInput.getline(name, 30, ',');
while (cardInput)
{
cardInput.getline(junk, 5, ' ');
cardInput.getline(phone, 15, ',');
cardInput.getline(junk, 5, ' ');
cardInput >> cardID;
cardInput.getline(junk, 5, '\n');
//: Update the next empty card
cards[i].setName(name);
cards[i].setPhone(phone);
cards[i].setCardID(cardID);
numCards++;
i++;
cardInput.getline(name, 30, ',');
}
for (i = 0; i < numCards; i++)
{
cards[i].print();
}
// declare a file pointer and open the books file
// read the next line from the file
// display main menu
ShowMenu();
cout << "Enter a command: ";
cin >> command;
while (command != 0)
{
switch (command)
{
case 1: // Print all cards
for (i = 0; i < numCards; i++)
{
cards[i].printcard();
}
break;
case 2: // print all books
for (i = 0; i < numBooks; i++)
{
books[i].printBook();
}
break;
case 3: // check out a book
// Ask for CardID
cout << "Enter a Card Id: ";
cin >> cardID;
// Search cards[] for ID
i = 0;
while (i < numCards && cardID != cards[i].getCardID())
{
i++;
}
// If NOT found, Error and stop
if (i == numCards)
{
cout << "ERROR: Card Not Found. Go to create card ID to make one. " << endl;
}
else
{
// if have a book, error and stop
if (cards[i].getBookID() != 0)
{
cout << "ERROR: Already have a book checked out." << endl;
}
else
{
// Ask for bookID
cout << "Enter the BookID: ";
cin >> bookID;
// Search books[] for ID
j = 0;
while (j < numBooks && bookID != books[j].getBookID())
{
j++;
}
// if NOT found. error and stop
// If NOT found, Error and stop
if (j == numBooks)
{
cout << "ERROR: Book Not Found." << endl;
}
else
{
// if have a book, error and stop
if (books[j].isCheckedOut())
{
cout << "ERROR: Book checked out." << endl;
}
else
{
// check out the book
cards[i].setBookID(bookID);
books[j].setCardID(cardID);
}
}
}
}
break;
case 4: // check in a book
// Ask for CardID
cout << "Enter a Card Id: ";
cin >> cardID;
// Search cards[] for ID
i = findLibraryCard(cardID);
// If NOT found, Error and stop
if (i >= 0)
{
// Ask for bookID
cout << "Enter the BookID: ";
cin >> bookID;
j = findBook(bookID);
if (j >= 0 && !books[j].isCheckedOut())
{
// check out the book
cards[i].setBookID(0);
books[j].setCardID(0);
}
break;
case 5: // create a new card
cout << "Enter your name: ";
cin >> name; // joe_smith (no spaces in name)
cout << "Enter your phone number: ";
cin >> phone; // Use 999-999-1234 format (no Spaces)
cout << "Enter a Card ID: ";
cin >> cardID;
cards[numCards].setName(name);
cards[numCards].setPhone(phone);
cards[numCards].setCardID(cardID);
numCards++;
break;
case 0:
default:
cout << " ERROR: Menu options must be between 0 and 5. " << endl;
}
ShowMenu();
cin >> command;
}
}
}
void ShowMenu()
{
cout << endl << endl;
cout << " [ M a i n M e n u] " << endl;
cout << "============================ " << endl;
cout << " 1. show all library cards " << endl;
cout << " 2. show all books " << endl;
cout << " 3. check out a book " << endl;
cout << " 4. check in a book " << endl;
cout << " 5. Create a new Library Card " << endl;
cout << " 0. Exit the system " << endl;
}
int findLibraryCard(int card, card cards[])
{
// Search cards[] for ID
int i = 0;
while (i < numCards && cardID != cards[i].getCardID())
{
i++;
}
// If NOT found, Error and stop
if (i == numCards)
{
cout << "ERROR: Card Not Found. Go to create card ID to make one. " << endl;
return -1;
}
else
{
return i;
}
}
int findbook(int book, class book books[])
{
int j = 0;
// Search books[] for ID
while (j < numBooks && bookID != books[j].getBookID())
{
j++;
}
// if NOT found. error and stop
// If NOT found, Error and stop
if (j == numBooks)
{
cout << "ERROR: Book Not Found." << endl;
return -1;
}
else
{
return j;
}
}
| |