Вы находитесь на странице: 1из 4

Homework

Assignment 1

Write a program that maintains a bookstore database. The program keeps records of books available in
a local bookstore. Each record contains a given book’s information such as ISBN, book title, author,
edition, number of copies available, and price. The program also performs typical bookstore
functionalities. These include adding book records to the bookstore, showing book records, modifying
book records, deleting book records, sorting book records, and checking availability of a particular
book to buy.

You need to create a structure, Book, to hold individual book details and a vector of Book structs,
BookDatabase, to hold all books records. The Book struct contains the following fields:

struct Book
{
string ISBN;
string title;
string author;
int edition;
int quantity;
double price;
}

The program should read and process a transaction file “bookstoreTrans.txt”. The transaction file
“bookstoreTrans.txt” contains several commands such as “display”, “add”, “searchBook”,
“searchAuthor”, “delete”, “updatePrice”, “updateQuantity”, “sort”... Each command is written on a
new line along with some other information:

 Display

This command displays the details of all books in the bookstore in a neat table format. For
example:

ISBN Title Author Edition Quantity Price


-----------------------------------------------------------------------------
0321563842 TheC++ProgrammingLanguage BjarneStroustrup 4 10 80.00
0134448237 C++HowToProgram PaulDeitel 10 3 175.00
0132165419 C++HowtoProgram PaulDeitel 7 8 176.00

Note: Decimal numbers should be printed with 2 digits of precision.


There are no spaces in the title and author name. Each new word in title and author name starts
with capital letter. You may print these as is (no spaces). If you print them separated by
spaces, you will get extra credit.

 Add bookISBN bookTitle bookAuthor bookEdition bookQuantity bookPrice

This command adds a new book record to the database. The program should read the book
details from the transaction file (bookISBN, bookTitle, bookAuthor, bookEdition,
bookQuantity, bookPrice). The program should check for duplicate book entries (same isbn).
If the book does not exist in the database, the program should add an entry for that book and
its details at the end of the database. You can simply use the push_back function to add it to
the end of the vector. The program should also display a message that the book was
successfully added to the database. For example:

0321563842 successfully added to the database…


If the book already exists in the database, then it should not be added again. The program
should display a message that the book already exists and simply update the quantity of that
book. For example:

0134448237 already exists in the database. The number of copies is now 5…

 SearchBook bookTitle

This command searches and returns all books whose titles match bookTitle. bookTitle could be
the exact title or a keyword found in the title. So any entries whose title fully or partially
matches bookTitle should be displayed. For example, if bookTitle is “C++”, the program
should display the following books from the list:

Searching for C++ book…


0321563842 TheC++ProgrammingLanguage BjarneStroustrup 4
0134448237 C++HowToProgram PaulDeitel 10
0132165419 C++HowToProgram PaulDeitel 7

If no matching entry is found, the program should display a message that this book does not
exist in the database. For example:

Searching for History book…


Sorry, no matching book exists

 SearchAuthor bookAuthor

This command searches and returns all books written by a certain author, bookAuthor. Any
entries whose author name fully or partially matches bookAuthor should be displayed. For
example, if bookAuthor is “Gaddis”, the program should display the following books from the
list:

Searching for author Gaddis…


0133985078 StartingOutWwithProgrammingLogicandDesign TonyGaddis 4

If no matching entry is found, the program should display a message that this author does not
have any books in this library. For example:

Searching for author James…


Sorry, no matching book exists

 Delete bookISBN

This command deletes an existing book entry specified by its bookISBN. The entry whose
ISBN matches bookISBN should be deleted. Note that deleting could be from any entry in the
database and not necessarily the last record. The program should also display a message that
the book was successfully deleted from the database. For example:

0128017333 successfully deleted

If no matching entry is found, the program should display a message that this book does not
exist in the directory. For example:

0128017332 does not exist and can not be deleted


 UpdatePrice bookISBN newPrice

This command updates a given book’s price. The command specifies bookISBN, the ISBN of
the book whose price should be changed and newPrice, the new price. The program searches
for the book whose ISBN matches bookISBN. If a match is found, the matching entry’s price
should be updated to newPrice. The program should also display a message that the book
price was successfully updated. For example:

0128017333 price successfully updated to 72

Otherwise, the program should display a message that this book was not found. For example:

0128017332 does not exist and its price can not be updated

 UpdateQuantity bookISBN newQuantity

This command updates a given book’s quantity. The command specifies bookISBN, the ISBN
of the book whose quantity should be changed and newQuantity, the new number of copies of
this book. The program searches for the book whose ISBN matches bookISBN. If a match is
found, the matching entry’s quantity should be updated to newQuantity. The program should
also display a message that the book quantity was successfully updated. For example:

0128017333 quantity successfully updated to 72

Otherwise, the program should display a message that this book was not found. For example:

0128017332 does not exist and its quantity can not be updated

 Sort

This command sorts the books in the database alphabetically by book title. If two records have
the same book title, they are sorted by edition. Once done, a message must be displayed that
the sort operation is complete. For example:

Bookstore database successfully sorted…

 Save

This command saves the vector data in an output file “bookstoreOut.txt”. Once done, the
program should display a message that the database is successfully written to file. For
example:

Bookstore database successfully saved…

Note: Each command must be implemented in a separate function.


To test your code, you may load and process the following transaction file:

“bookstoreTrans.txt”
add 0321563842 TheC++ProgrammingLanguage BjarneStroustrup 4 10 80.00
add 0134448237 C++HowToProgram PaulDeitel 10 3 175.00
add 0132165419 C++HowToProgram PaulDeitel 7 8 176.00
add 0073383095 DiscreteMathematicsAndItsApplications KennethRosen 7 8 223.39
add 0133985078 StartingOutWwithProgrammingLogicandDesign TonyGaddis 4 7 127.40
add 0134448237 C++HowToProgram PaulDeitel 10 2 175.00
add 1118407813 BeginningProgrammingWithJavaForDummies BarryBurd 4 1 29.99
add 0128122757 ComputerOrganizationAndDesignRISCVEdition DavidPatterson 1 15 95
add 0124077263 ComputerOrganizationAndDesignMIPSEdition DavidPatterson 5 5 89.95
add 0128017333 ComputerOrganizationAndDesignARMEdition DavidPatterson 1 2 94.95
add 0124077263 ComputerOrganizationAndDesignMIPSEdition DavidPatterson 5 2 89.95
display
delete 0128017332
delete 0073383095
searchBook C++
searchBook Science
searchAuthor PaulDeitel
searchAuthor Gaddis
searchAuthor James
updateQuantity 0128017333 4
updateQuantity 0128127333 4
updatePrice 0321563842 72.00
updatePrice 0128017332 135.00
sort
display
save

Once done, you need to submit the following:

1. A hard copy (printed copy) of the source code (.cpp), output (a.out), and output file
(“bookstoreOut.txt”) placed in a folder. You should hand in the folder during the first 5
minutes of the class when the homework is due. You lose 10% of the grade if you do not
provide a complete folder.
2. An electronic copy of your source code and executable file. You should place all source codes
in a zipped folder and name it based on your first/last name and the assignment number. For
example, JoeSmith-A1.zip. You should submit the zipped folder online through cougar
website.

Вам также может понравиться