Task 1: Design a C++ class to hold information of a Book. The information includ
ID: 3621283 • Letter: T
Question
Task 1:Design a C++ class to hold information of a Book. The information includes Book Code, Book Name and Unit Price. Book Code contains exactly 6 characters, which the first character is the upper-case letter (A-Z) and the five next characters are digits (0-9). Book Name contains no more than 30 characters. Unit Price is double type, and the price must be positive and less than $1000 (<1000).
Tasks 2:
Write a C++ program to do the below tasks:
1. Create default 4 books with their information
2. Add new books
3. Search book by code
4. List all books
5. List all books after sorting by book code
Tasks 2.1:
When user choose this task, the program create 4 books by default. Then the list of books currently have 4 books.
Task 2.2:
Allow users to add new information of as many books as they want. After check validation of all data (including that the code could not be duplicated), save those information into the program.
Task 2.3:
Search book by code from all books that was created in task 2.1 and books which are added in task 2.2. Then display the result.
Task 2.4:
Display all information of books in file that was created in task 2.1 and books which are added in task 2.2.
Task 2.5:
Sort by book code and display all information of books in file that was created in task 2.1 and books which are added in task 2.2.
Explanation / Answer
Hope this helps. Please rate. :) #include #include #include #include using namespace std; class book { public: string code; string name; double price; book() { code = ""; name = ""; price = -1; // signifies nonexistent book when searching } book(string c, string n, double p) { code = c; name = n; price = p; } }; // returns a vector of books vector create4() { vector vec; book b1("A12345", "Huckleberry Finn", 24.99); book b2("F55845", "Great Expectations", 12.49); book b3("B87754", "Jane Eyre", 20.99); book b4("B55445", "The Velveteen Rabbit", 105.99); vec.push_back(b1); vec.push_back(b2); vec.push_back(b3); vec.push_back(b4); return vec; } bool dataIsValid(string code, string name, double price) { // verify code if (code.length() != 6) return false; if (!(code[0] >= 'A' && code[0] = '0' && code[i] 30) return false; // verify price if (price 1000) return false; // if we get here, then all data passed the tests return true; } void addBook(vector &v) { string code, name; double price; cout > code; cout price; if (dataIsValid(code, name, price)) { book b(code, name, price); v.push_back(b); } else { cout code; b = search(v, code); if (b.price != -1) print(b); else printf("Book not found with code %s", code.c_str()); break; case 4: print(v); break; case 5: sort(v); print(v); break; default: break; } printf(" 1. Create default 4 books with their information "); printf("2. Add new book "); printf("3. Search for book by code "); printf("4. List all books "); printf("5. Sort books by code and then list all books "); printf(" Please enter selection (-1 to exit): "); cin >> choice; } return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.