Question & Answer: The purpose of this project is to work will pointers and dynamic memory. The project will be an app for customers to buy boo…..

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

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++

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 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. Output is shown below. Please note that in sample run given in your question, the price and quantity are interchanged, so the outputs shown in your question are wrong in terms of the numbers. You can verify that by looking at each of the price and quantity in the inventory.txt and comparing them with the output shown when you choose menu option 1 i.e. List books to buy.

The program given below works as intended and shows correct quantity and price. Please 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();

void setTitle(char* title);
const char* getTitle();

void setAuthor(char* author);
const char* getAuthor() ;

void setQuantity(int qty);
int getQuantity();

void setPrice(double price);
double getPrice();

void display();
~Book();
};

#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::setTitle(char* title)
{
if(this->title != NULL) delete this->title;
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
}
const char* Book::getTitle()
{
return title;
}

void Book::setAuthor(char* author)
{
if(this->author != NULL)
delete this->author;
this->author = new char[strlen(author) + 1];
strcpy(this->author, author);
}

const char* Book::getAuthor()
{
return author;
}

void Book::setQuantity(int qty)
{
this->quantity = qty;
}
int Book::getQuantity()
{
return quantity;
}

double Book::getPrice()
{
return price;
}
void Book::setPrice(double price)
{
this->price = price;
}

void Book::display()
{
cout << “Title: ” << title << endl;
cout << “Author: ” << author << endl;
cout << “Count: ” << quantity << endl;
cout << “Price: ” << price << endl;
cout << “===========================” << endl;
}

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 **inventoryBooks;
int numInventoryBooks;

//cart related fields
Book *cartBooks[50]; //array of pointers to books
int cartQuantity[50];
int numCartItems;
int totalCartQuantity;
double totalCartPrice;
double discount;

//private functions
void calculateDiscount();
void readFromFile(char *fname);
int searchInventoryBook(char *title);
int searchCartBook(char *title);

public:
Inventory(char* filename);
void listInventoryBooks();
void listShoppingCart();
bool addBookToCart(char *title, int qty );
bool removeBookFromCart(char *title);

int getInventoryCopies(char *title);
void showTotalPrice();
bool isBookInCart(char *title);
void checkOut();

~Inventory();

};

#endif /* inventory_h */

inventory.cpp

#include “inventory.h”
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
Inventory::Inventory(char *filename)
{
totalCartQuantity = 0;
totalCartPrice = 0;
numInventoryBooks = 0;
numCartItems = 0;
readFromFile(filename);
}

//create inventory of books from file contents
void Inventory::readFromFile(char *filename)
{
ifstream input(filename);

if(!input.is_open())
{
cout << “Input file ” << filename << ” could not be opened ” << endl;
return;
}

char line[250];
double price;
int qty;

input >> numInventoryBooks;

inventoryBooks = new Book*[numInventoryBooks];
for(int i = 0; i < numInventoryBooks; i++)
{
inventoryBooks[i] = new Book;

input.ignore(); //remove newline

input.getline(line, 250);
inventoryBooks[i]->setTitle(line);

input.getline(line, 250);
inventoryBooks[i]->setAuthor(line);

input >> price;
inventoryBooks[i]->setPrice(price);

input >> qty;
inventoryBooks[i]->setQuantity(qty);
}
input.close();
}
//searches the inventory for the specified title and returns index, -1 if not found
int Inventory::searchInventoryBook(char *title)
{
for(int i = 0; i < numInventoryBooks; i++)
{
if(strcmp(inventoryBooks[i]->getTitle(),title) == 0)
return i;
}
return -1;
}
//searches the cart for the specified title and returns index if found, -1 if not found
int Inventory::searchCartBook(char *title)
{
for(int i = 0 ; i < numCartItems; i++)
if(strcmp(cartBooks[i]->getTitle(), title) == 0)
return i;
return -1;
}

//returns true if specified title is in cart, false otherwise
bool Inventory::isBookInCart(char *title)
{
return searchCartBook(title) != -1;
}

//returns the number of copies in inventory
int Inventory::getInventoryCopies(char *title)
{
int idx = searchInventoryBook(title);
if(idx == -1)
return 0;
else
return inventoryBooks[idx]->getQuantity();
}

//display all inventory books
void Inventory::listInventoryBooks()
{
for(int i = 0; i < numInventoryBooks; i++)
{
inventoryBooks[i]->display();
}
}

//display the shopping cart
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();
}

//adds a book to cart with specified quantity. If book title already present, then only new
// quantity is added to existing quantity
bool Inventory::addBookToCart(char *title, int qty )
{

int cartidx = searchCartBook(title); //check if already present in cart
if(cartidx == -1) //not in cart
{
//check if the boook is available in inventory and enough copies available
int invenidx = searchInventoryBook(title);
if(invenidx == 0 || inventoryBooks[invenidx]->getQuantity() < qty)
return false;

cartidx = numCartItems;
cartBooks[cartidx] = inventoryBooks[invenidx];
cartQuantity[cartidx] = qty;
numCartItems++;
}
else //already in cart, increase quantity
{
cartQuantity[cartidx] += qty;
}
totalCartQuantity += qty;
totalCartPrice += qty * cartBooks[cartidx]->getPrice();
//reduce the quantity in inventory
cartBooks[cartidx]->setQuantity( cartBooks[cartidx]->getQuantity() – qty);
calculateDiscount(); //recalculate discount
return true;
}

//removes the specified title from cart if found and returns true, returns false if not found
bool Inventory::removeBookFromCart(char *title)
{
int idx = searchCartBook(title); //get index of
if(idx == -1)
return false;
else
{
//add back to inventory
cartBooks[idx]->setQuantity(cartBooks[idx]->getQuantity() + cartQuantity[idx]);
totalCartPrice -= cartQuantity[idx] * cartBooks[idx]->getPrice();
totalCartQuantity -= cartQuantity[idx];
//move other 1 position up
for(int i = idx; i < numCartItems – 1; i++ )
cartBooks[i] = cartBooks[i+1];
numCartItems –;
return true;
}
}

//displays discount, total before and after discount
void Inventory::showTotalPrice()
{
cout << “Total before discount: $” << totalCartPrice << endl;
cout << “Discount is: ” << discount << endl;
cout << “Total: $” << ((1-discount) * totalCartPrice) << endl;
cout << endl;
}

//calculate the discount %
void Inventory::calculateDiscount()
{
int howmany5s = totalCartQuantity / 5;
discount = 5 * howmany5s / 100.0;
}

//print checkout message
void Inventory::checkOut()
{
cout << “*** CHECKOUT *** ” << endl;
double finalTotal = totalCartPrice * ( 1 – discount);
cout << “Final Total is: $” << finalTotal << endl;
cout << “Final discount is: ” << discount << endl;
cout << “You saved: $” << (totalCartPrice – finalTotal) << endl;
cout << “**** THANK YOU FOR SHOPPING WITH US ****” << endl;
}

//destructor to delete all instances of book and dynamically created array
Inventory::~Inventory()
{
for(int i = 0; i < numInventoryBooks;i++)
delete inventoryBooks[i];
delete []inventoryBooks;
}

bookmain.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] = “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();
switch(choice)
{
case 1:
inventory.listInventoryBooks();
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.listInventoryBooks();
cout << “Which book would you like to add? ” << endl;
cin.getline(title, 250);
}
int available = inventory.getInventoryCopies(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;
}

input file : 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

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? 2
2 copies of Alice in Wonderland added to your cart.
Total before discount: $29.98
Discount is: 0.00
Total: $29.98

——– 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)?
Around the World in 80 Days
How many copies? 10
Sorry we don’t have that many copies in stock. We have 3 available
Please re-enter a quantity less than this.
1
1 copies of Around the World in 80 Days added to your cart.
Total before discount: $41.97
Discount is: 0.00
Total: $41.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: 5
*** CHECKOUT ***
Final Total is: $41.97
Final discount is: 0.00
You saved: $0.00
**** THANK YOU FOR SHOPPING WITH US ****
amoeba-2:bookstore raji$ ./a.out
############## 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
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: 3
Which book would you like to add (type ‘p’ to see list of books)?
Around the World in 80 Days
How many copies? 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: 5
*** CHECKOUT ***
Final Total is: $76.89
Final discount is: 0.05
You saved: $4.05

Still stressed from student homework?
Get quality assistance from academic writers!