Question & Answer: 6 Tweny Thousand Leagues Under the Sea…..

The purpose of this project is to work will pointers and dynamic memory. The project will be an app for customers to buy books from a bookstores website. A customer can add books to a shopping cart and remove them too. A customer gets a discount of 905 for every 5 books they buy. The user chooses books from a catalog of books that are stored in a file When the program ends, it prints the total amount paid for the books plus how much the customer saved. What To Code The book list will be in a file called inventory.txt The format will be in this order: counts etitle1> cauthor1> <price1> cinStock1> <title2 price2> cinStock2> and so on. The first line of the file will have the number of books in the list. Create the book inventory list to be this size. Dont fix the size of the array at compile time You need to make at least 2 classes. I would suggest a class for the book information and a class for storing the inventory of books. You could also have a class for storing the shopping cart list for keeping track of what book is being bought and how many copies but you can also store this list in the same class as the inventory list. Tasks A user can do the following -print all books in the inventory -print current contents of shopping cart - also print the total amount and current discount. -add book to cart ask user for a book title. Then ask to how many copies. This should also decrease the in stock amount of the book in the inventory. Re-figure both discount and total, each time a book is added or the number of copies is changed. NOTE: If the user enters a title already in the cart, you can let them change the quantity in the

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: 6 Tweny Thousand Leagues Under the Sea…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

inventory.txt

6

Tweny Thousand Leagues Under the Sea

Jules Verne

20.99

10

A Tale of Two Cities

Charles Dickens

15.99

15

The House of the Seven Gables

‎Nathaniel Hawthorne

12.99

13

Around the World in 80 Days

Jules Verne

11.99

3

Flatland: A Romance of Many Dimensions

Edwin Abbott

8.99

18

Alice in Wonderland

Lewis Carroll

14.99

11

CODE SHOULD BE IN C++

Show transcribed image textThe purpose of this project is to work will pointers and dynamic memory. The project will be an app for customers to buy books from a bookstore’s website. A customer can add books to a shopping cart and remove them too. A customer gets a discount of 905 for every 5 books they buy. The user chooses books from a catalog of books that are stored in a file When the program ends, it prints the total amount paid for the books plus how much the customer saved. What To Code The book list will be in a file called ‘inventory.txt The format will be in this order: counts etitle1> cauthor1> cinStock1> cinStock2> and so on. The first line of the file will have the number of books in the list. Create the book inventory list to be this size. Don’t fix the size of the array at compile time You need to make at least 2 classes. I would suggest a class for the book information and a class for storing the inventory of books. You could also have a class for storing the shopping cart list for keeping track of what book is being bought and how many copies but you can also store this list in the same class as the inventory list. Tasks A user can do the following -print all books in the inventory -print current contents of shopping cart – also print the total amount and current discount. -add book to cart ask user for a book title. Then ask to how many copies. This should also decrease the in stock amount of the book in the inventory. Re-figure both discount and total, each time a book is added or the number of copies is changed. NOTE: If the user enters a title already in the cart, you can let them change the quantity in the

Expert Answer

 

Given below is the code for the question along with output.

Note:

In the sample program run shown in the question, the output is slightly wrong. The quantity and count for each book have been swapped. Probably the order in which it was read from the file is wrong. For example, the inventory.txt shows 20.99 as price and 10 as quantity for the title “Tweny Thousand Leagues Under the Sea”. But the sample run in question shows count = 20 and price =10 (interchanged). Same is the case for all the books. See the output when menu choice 1 is selected to list all books. So the when a book is added into a cart, this wrong price is being considered to calculate total price as well. So the price , count and total price are not correct according to input file.

The program given by me below reads the quantity and price in correct order and hence output is correct. So please don’t compare numeric values from the answer program to that in the sample given in question. Hope I am clear.

When entering title for book, the case is exactly same i.e. its case sensitive.

Please don’t forget to rate the answer if it helped. Thank you.

book.h

#ifndef book_h
#define book_h
class Book
{
private:

char *title;
char *author;
int quantity;
double price;

public:
Book();
~Book();
void display();
const char* getTitle();
const char* getAuthor() ;
int getQuantity();
double getPrice();
void setTitle(char* title);
void setAuthor(char* author);
void setQuantity(int qty);
void setPrice(double price);
};

#endif /* book_h */

book.cpp

#include “book.h”
#include <cstring>
#include <iostream>
using namespace std;
Book::Book()
{
title = NULL;
author = NULL;
quantity = 0;
price = 0;
}
void Book::display()
{
cout << “Title: ” << title << endl;
cout << “Author: ” << author << endl;
cout << “Count: ” << quantity << endl;
cout << “Price: ” << price << endl;
cout << “———————————” << endl;
}

const char* Book::getTitle()
{
return title;
}
const char* Book::getAuthor()
{
return author;
}
int Book::getQuantity()
{
return quantity;
}
double Book::getPrice()
{
return price;
}

void Book::setTitle(char* title)
{
if(this->title != NULL) delete this->title;
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
}
void Book::setAuthor(char* author)
{
if(this->author != NULL)
delete this->author;
this->author = new char[strlen(author) + 1];
strcpy(this->author, author);
}
void Book::setQuantity(int qty)
{
this->quantity = qty;
}
void Book::setPrice(double price)
{
this->price = price;
}

Book::~Book()
{
if(title != NULL) delete title;
if(author != NULL) delete author;
}

inventory.h

#ifndef inventory_h
#define inventory_h
#include “book.h”

class Inventory
{
private:
Book **books;
int numBooks;
Book *cartBooks[50]; //array of pointers to books
int cartQuantity[50];
int numCartItems;
int totalQuantity;
double totalPrice;
double discount;
void calculateDiscount();
void loadFromFile(char *fname);
int findBook(char *title);
int searchCart(char *title);
public:
Inventory(char* filename);
~Inventory();
int getNumCopies(char *title);
void listBooks();
void listShoppingCart();
bool addBookToCart(char *title, int qty );
bool removeBookFromCart(char *title);
void showTotalPrice();
bool isBookInCart(char *title);
void checkOut();
};

#endif /* inventory_h */

inventory.cpp

#include “inventory.h”
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
Inventory::Inventory(char *filename)
{
totalQuantity = 0;
totalPrice = 0;
numBooks = 0;
numCartItems = 0;
loadFromFile(filename);
}
void Inventory::loadFromFile(char *fname)
{
ifstream infile(fname);
char line[100];
double price;
int qty;
if(!infile.is_open())
{
cout << “Input file ” << fname << ” could not be opened ” << endl;
return;
}

infile >> numBooks;

books = new Book*[numBooks];
for(int i = 0; i < numBooks; i++)
{
infile.ignore(); //remove newline
infile.getline(line, 100);
books[i] = new Book;
books[i]->setTitle(line);

infile.getline(line, 100);
books[i]->setAuthor(line);
infile >> price;
books[i]->setPrice(price);
infile >> qty;
books[i]->setQuantity(qty);
}
infile.close();
}
Inventory::~Inventory()
{
for(int i = 0; i < numBooks;i++)
delete books[i];
delete []books;
}
int Inventory::getNumCopies(char *title)
{
int idx = findBook(title);
if(idx == -1)
return 0;
else
return books[idx]->getQuantity();
}

void Inventory::listBooks()
{
for(int i = 0; i < numBooks; i++)
{
books[i]->display();
}
}
void Inventory::listShoppingCart()
{
cout << “— Shopping Cart —” << endl;
if(numCartItems == 0)
{
cout << “**Your Shopping Cart is Empty **” << endl;
return;
}

for(int i = 0; i < numCartItems; i++)
{
cout << “=>Item: ” << cartBooks[i]->getTitle() << endl;
cout << “Price per book: ” << cartBooks[i]->getPrice() << endl;
cout << “Quantity: ” << cartQuantity[i] << endl;
cout << “—————————————-” << endl;
}
cout << endl;
showTotalPrice();
}
int Inventory::findBook(char *title)
{
for(int i = 0; i < numBooks; i++)
{
if(strcmp(books[i]->getTitle(),title) == 0)
return i;
}
return -1;
}
bool Inventory::addBookToCart(char *title, int qty )
{

int cidx = searchCart(title);
if(cidx == -1)
{
int idx = findBook(title);
if(idx == 0 || books[idx]->getQuantity() < qty)
return false;
cidx = numCartItems;
cartBooks[numCartItems] = books[idx];
cartQuantity[numCartItems] = qty;
numCartItems++;
}
else
{
cartQuantity[cidx] += qty;
}
totalQuantity += qty;
totalPrice += qty * cartBooks[cidx]->getPrice();
cartBooks[cidx]->setQuantity( cartBooks[cidx]->getQuantity() – qty);
calculateDiscount();
return true;
}
bool Inventory::removeBookFromCart(char *title)
{
int idx = searchCart(title);
if(idx == -1)
return false;
else
{
//add back to inventory
cartBooks[idx]->setQuantity(cartBooks[idx]->getQuantity() + cartQuantity[idx]);
totalPrice -= cartQuantity[idx] * cartBooks[idx]->getPrice();
totalQuantity -= cartQuantity[idx];
//move other 1 position up
for(int i = idx; i < numCartItems – 1; i++ )
cartBooks[i] = cartBooks[i+1];
numCartItems –;
return true;
}
}
void Inventory::showTotalPrice()
{
cout << “Total before discount: $” << totalPrice << endl;
cout << “Discount is: ” << discount << endl;
cout << “Total: $” << ((1-discount) * totalPrice) << endl;
cout << endl;
}
void Inventory::calculateDiscount()
{
int n = totalQuantity / 5;
discount = 5 * n / 100.0;
}
int Inventory::searchCart(char *title)
{
for(int i = 0 ; i < numCartItems; i++)
if(strcmp(cartBooks[i]->getTitle(), title) == 0)
return i;
return -1;
}
bool Inventory::isBookInCart(char *title)
{
return searchCart(title) != -1;
}
void Inventory::checkOut()
{
cout << “*** CHECKOUT *** ” << endl;
double finalTotal = totalPrice * ( 1 – discount);
cout << “Final Total is: $” << finalTotal << endl;
cout << “Final discount is: ” << discount << endl;
cout << “You saved: $” << (totalPrice – finalTotal) << endl;
cout << “**** THANK YOU FOR SHOPPING WITH US ****” << endl;
}

main.cpp

#include “inventory.h”
#include <iostream>
#include <iomanip>
using namespace std;
void addBook(Inventory &inventory);
void removeBook(Inventory &inventory);

int main()
{
int choice = 0;
char filename[100] = “/Users/raji/Documents/Chegg/c++/Test/Test/bookstore/inventory.txt”;
Inventory inventory(filename);
cout << fixed << setprecision(2);

cout << “############## WELCOME TO OUR BOOKSTORE ##############” << endl << endl;

while(choice != 5)
{
cout << “——– Menu ——–” << endl;
cout << “1. List books to buy.” << endl;
cout << “2. List Shopping cart.” << endl;
cout << “3. Add book to cart.” << endl;
cout << “4. Remove book from cart.” << endl;
cout << “5. Checkout and quit.” << endl;
cout << “Enter choice: “;
cin >> choice;
cin.ignore();//remove newline
switch(choice)
{
case 1:
inventory.listBooks();
break;
case 2:
inventory.listShoppingCart();
break;
case 3:
addBook(inventory);
break;
case 4:
removeBook(inventory);
break;
case 5:
inventory.checkOut();
break;
default:
cout << “Invalid menu choice!” << endl;
}
}
return 0;
}

void addBook(Inventory &inventory)
{
char title[250];
char ans;
int qty;
cout << “Which book would you like to add (type ‘p’ to see list of books)?” << endl;
cin.getline(title, 250);
if(strcmp(title, “p”) == 0)
{
inventory.listBooks();
cout << “Which book would you like to add? ” << endl;
cin.getline(title, 250);
}
int available = inventory.getNumCopies(title);
if(available == 0)
{
cout << “Sorry no copies of the book.” << endl;
return;
}
if(inventory.isBookInCart(title))
{
cout << “This book is already in your cart.” << endl;
cout << “Do you want to change the quantity? (y or n): ” ;
cin >> ans;
if(!(ans == ‘y’ || ans ==’Y’))
return;
}

cout << “How many copies? ” ;
cin >> qty;

while(qty < 0)
{
cout << “Can’t have zero or negative quantity. Please re-enter.” << endl;
cin >> qty;
}
while(qty > available)
{
cout << “Sorry we don’t have that many copies in stock. We have ” << available << ” available” << endl;
cout << “Please re-enter a quantity less than this.” << endl;
cin >> qty;
}

inventory.addBookToCart(title, qty);
cout << qty << ” copies of ” << title << ” added to your cart.” << endl;
inventory.showTotalPrice();
cout << endl;
}

void removeBook(Inventory &inventory)
{

char title[250];

cout << “Which book would you like to remove (type ‘p’ to see list of books)?” << endl;
cin.getline(title, 250);
if(inventory.removeBookFromCart(title))
cout << title << ” has been removed from your cart.” << endl;
else
cout << “Can’t find the title in inventory.” << endl;
cout << endl;
}

output

############## WELCOME TO OUR BOOKSTORE ##############

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 2
— Shopping Cart —
**Your Shopping Cart is Empty **
——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 1
Title: Tweny Thousand Leagues Under the Sea
Author: Jules Verne
Count: 10
Price: 20.99
———————————
Title: A Tale of Two Cities
Author: Charles Dickens
Count: 15
Price: 15.99
———————————
Title: The House of the Seven Gables
Author: ‎Nathaniel Hawthorne
Count: 13
Price: 12.99
———————————
Title: Around the World in 80 Days
Author: Jules Verne
Count: 3
Price: 11.99
———————————
Title: Flatland: A Romance of Many Dimensions
Author: Edwin Abbott
Count: 18
Price: 8.99
———————————
Title: Alice in Wonderland
Author: Lewis Carroll
Count: 11
Price: 14.99
———————————
——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 3
Which book would you like to add (type ‘p’ to see list of books)?
Alice in Wonderland
How many copies? -3
Can’t have zero or negative quantity. Please re-enter.
1000
Sorry we don’t have that many copies in stock. We have 11 available
Please re-enter a quantity less than this.
3
3 copies of Alice in Wonderland added to your cart.
Total before discount: $44.97
Discount is: 0.00
Total: $44.97

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 2
— Shopping Cart —
=>Item: Alice in Wonderland
Price per book: 14.99
Quantity: 3
—————————————-

Total before discount: $44.97
Discount is: 0.00
Total: $44.97

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 3
Which book would you like to add (type ‘p’ to see list of books)?
p
Title: Tweny Thousand Leagues Under the Sea
Author: Jules Verne
Count: 10
Price: 20.99
———————————
Title: A Tale of Two Cities
Author: Charles Dickens
Count: 15
Price: 15.99
———————————
Title: The House of the Seven Gables
Author: ‎Nathaniel Hawthorne
Count: 13
Price: 12.99
———————————
Title: Around the World in 80 Days
Author: Jules Verne
Count: 3
Price: 11.99
———————————
Title: Flatland: A Romance of Many Dimensions
Author: Edwin Abbott
Count: 18
Price: 8.99
———————————
Title: Alice in Wonderland
Author: Lewis Carroll
Count: 8
Price: 14.99
———————————
Which book would you like to add?
Around the World in 80 Days
How many copies? 9
Sorry we don’t have that many copies in stock. We have 3 available
Please re-enter a quantity less than this.
3
3 copies of Around the World in 80 Days added to your cart.
Total before discount: $80.94
Discount is: 0.05
Total: $76.89

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 2
— Shopping Cart —
=>Item: Alice in Wonderland
Price per book: 14.99
Quantity: 3
—————————————-
=>Item: Around the World in 80 Days
Price per book: 11.99
Quantity: 3
—————————————-

Total before discount: $80.94
Discount is: 0.05
Total: $76.89

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 3
Which book would you like to add (type ‘p’ to see list of books)?
Alice in Wonderland
This book is already in your cart.
Do you want to change the quantity? (y or n): y
How many copies? 6
6 copies of Alice in Wonderland added to your cart.
Total before discount: $170.88
Discount is: 0.10
Total: $153.79

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 2
— Shopping Cart —
=>Item: Alice in Wonderland
Price per book: 14.99
Quantity: 9
—————————————-
=>Item: Around the World in 80 Days
Price per book: 11.99
Quantity: 3
—————————————-

Total before discount: $170.88
Discount is: 0.10
Total: $153.79

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 1
Title: Tweny Thousand Leagues Under the Sea
Author: Jules Verne
Count: 10
Price: 20.99
———————————
Title: A Tale of Two Cities
Author: Charles Dickens
Count: 15
Price: 15.99
———————————
Title: The House of the Seven Gables
Author: ‎Nathaniel Hawthorne
Count: 13
Price: 12.99
———————————
Title: Around the World in 80 Days
Author: Jules Verne
Count: 0
Price: 11.99
———————————
Title: Flatland: A Romance of Many Dimensions
Author: Edwin Abbott
Count: 18
Price: 8.99
———————————
Title: Alice in Wonderland
Author: Lewis Carroll
Count: 2
Price: 14.99
———————————
——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 4
Which book would you like to remove (type ‘p’ to see list of books)?
addd
Can’t find the title in inventory.

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 4
Which book would you like to remove (type ‘p’ to see list of books)?
Around the World in 80 Days
Around the World in 80 Days has been removed from your cart.

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 2
— Shopping Cart —
=>Item: Alice in Wonderland
Price per book: 14.99
Quantity: 9
—————————————-

Total before discount: $134.91
Discount is: 0.10
Total: $121.42

——– Menu ——–
1. List books to buy.
2. List Shopping cart.
3. Add book to cart.
4. Remove book from cart.
5. Checkout and quit.
Enter choice: 5
*** CHECKOUT ***
Final Total is: $121.42
Final discount is: 0.10
You saved: $13.49

Grand Paper Writers
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All papers are written by the best professional writers to ensure 100% originality. We always provide plagiarism reports whenever we deliver completed papers.

Free Revisions

All papers by Grand Paper Writers are completed and submitted on time. This timely delivery of papers gives you time to go through the paper before the official deadline.

Title-page

As an additional service, we will provide a title page that precedes the contents of your paper. Here, you will provide your personal details.

Bibliography

We also ensure that we provide an extra page for the references or bibliographies following referencing rules.

Originality & Security

At Grandpaperwriters.com, we guarantee students for the provision of security and original work. All your personal information is handled with confidentiality and is not shared with third parties. Additionally, we ensure that we provide original content with accompanying plagiarism reports to show originality.

24/7 Customer Support

Our customer support team is always available 24/7 to provide instant responses to any queries raised by students.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Grandpaperwriters.com has the best professional essay writers for quality services.

Pricing

Flexible Pricing

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Communication

Admission Help & Client-Writer Contact

Grandpaperwriters.com provides an interactive portal where students can communicate directly to their writers.

Deadlines

Paper Submission

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Reviews

Customer Feedback

Grandpaperwriters.com appreciates feedback from our clients to help us improve the delivery of essay writing services. As such, we are constantly changing our policies to ensure maximum customer and writer satisfaction.