Question

IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5...

IN c++

i post this question 5 times. hope this time somebody answer..

16.5 Homework 5

OVERVIEW

This homework builds on Hw4(given in bottom)

We will modify the classes to do a few new things

  • We will add copy constructors to the Antique and Merchant classes.
  • We will overload the == operator for Antique and Merchant classes.
  • We will overload the + operator for the antique class.
  • We will add a new class the "MerchantGuild."

Please use the provided templates for the Antique and Merchant classes, as we simplified their functionality for this homework. Also, you should use the provided main.cpp to test your classes.

Antique Class

  • Overload the == operator
    • one antique is equal to another if the name and price are the same
  • Overload the + operator
    • sometimes you want to sell antiques together, such as fork and knife pairs
Antique fork;
Antique knife;
fork.setName("fork");
fork.setPrice(3.25);
knife.setName("knife");
knife.setPrice(2.25);
Antique Silverware = fork + knife;
  • Silverware.name = "fork and knife"
  • Silverware.price = 5.50

Merchant Class

  • modify the class you implemented for Homework 4 to use dynamic arrays for holding antiques and their quantities
    • the default constructor will initialize things to zero or nullptr
    • the copy constructor will perform a deep copy
    • the constructor with one value will initialize revenue to the float and everything else to zero or nullptr
    • the destructor will free all allocated memory (delete pointers and delete[ ] dynamic arrays)
  • Overload the assignment operator ( = )
    • this will work almost exactly like the copy constructor Except, there may already be allocated memory in the pointers you must delete first
  • Overload the == operator
    • one merchant is equal to another if:
      • their revenue is equal
      • they have the same number of antiques
      • The antiques are the same and in the same order
      • They have the same quantity of each antique
  • Add an addAntique function
    • this is a void function that takes as arguments an antique object and an int quantity of that antique. It will add that antique and its quantity to the end of the respective dynamic arrays. The arrays will be 1 size larger after the addition.

MerchantGuild Class

This class will hold a dynamic array of merchants of size guildSize. It also keeps track of the number of members it has.

It has the following constructors:

  • constructor with parameter size
    • sets guildSize to size, allocates members array and sets numMem to 0. If size < 1, use default value 10
  • copy constructor
    • this will perform a deep copy

It also has a destructor

  • free the allocated memory

It overloads the assignment operator (=)

  • this works like the copy constructor except there is allocated memory you have to delete first

It has the following member functions:

  • addMember

    • this is a void function that takes as an argument a Merchant object, adds it to the end of its array of members, and increases it the number of members by 1. if the array is full (numMem == guildSize), do not add the new member and print "Guild is full."
  • getMembers

    • returns the pointer to the dynamic array

//antique.h

#include

#include

#include

#include

using namespace std;

#ifndef antique_h

#define antique_h

class Antique {

private:

string name;

float price;

public:

Antique();

   bool operator==(const Antique &other);

   Antique operator+(const Antique &other);

string toString();

void setName(string NAME);

void setPrice(float PRICE);

string getName() const;

float getPrice() const;

};

#endif /* antique_h */

//antique.cpp

#include "antique.h"

using namespace std;

void Antique::setName(string NAME) {

name = NAME;

}

void Antique::setPrice(float PRICE) {

price = PRICE;

}

string Antique::getName() const {

return name;

}

float Antique::getPrice() const {

return price;

}

string Antique::toString() {

//string price_s;

ostringstream streamObj;

streamObj << fixed;

streamObj << setprecision(2);

streamObj << price;

return name + ": $" + streamObj.str();

}

Antique::Antique() {

name = "";

price = 0;

}

/*

   put == operator overload here

*/

/*

   put + operator overload here

*/

//merchant.h

#include "antique.h"

#include

#include

using namespace std;

#ifndef merchant_h

#define merchant_h

class Merchant {

private:

/*

put the dynamic arrays here

*/

   int numAnt;

float revenue;

public:

Merchant();

Merchant(float r);

~Merchant();

Merchant(const Merchant ©);

/*

   == operator overload here

   addAntique function here

*/

};

#endif /* merchant_h */

//merchant.cpp

#include "merchant.h"

using namespace std;

//default constructor here

//constructor with only a float value

//destructor here

// copy constructor

//==operator overload here

//add antique here

//MerchantGuild.h

#include "merchant.h"

using namespace std;

#ifndef MerchantGuild_h

#define MerchantGuild_h

class MerchantGuild

{

public:

MerchantGuild();

MerchantGuild(const MerchantGuild ©);

~MerchantGuild();

MerchantGuild& operator=(const MerchantGuild &other);

void addMember(Merchant newM);

Merchant* getMembers();

private:

Merchant* members;

int guildSize;

int numMem;

};

#endif

//MerchantGuild.cpp

*/

*/

//main.cpp

#include

#include "MerchantGuild.h"

#include "merchant.h"

#include "antique.h"

using namespace std;

int main() {

   cout << "KEY: " << "True is: " << true << " False is: " << false << endl << endl;

   // antique tests

Antique a1, a2;

a1.setName("fork");

a1.setPrice(3.25);

a2.setName("knife");

a2.setPrice(2.50);

cout << "antique test" << endl;

Antique a3 = a1 + a2;

cout << a3.toString() << endl;

cout << bool(a1 == a2) << " : Ans=0" << endl;

cout << bool(a1 == a1) << " : Ans=1" << endl;

  

// merchant tests

Merchant m1(1.2), m2(2.5);

cout << "merchant test" << endl;

m1.addAntique(a1, 2);

m1.addAntique(a2, 5);

m2.addAntique(a3, 3);

cout << bool(m1 == m2) << " : Ans=0" << endl;

cout << bool(m1 == m1) << " : Ans=1" << endl;

Merchant m3(m1);

cout << bool(m3 == m1) << " : Ans=1" << endl;

  

//merchant guild tests

MerchantGuild mg1;

cout << "merchant guild tests" << endl;

mg1.addMember(m1);

  

Merchant* tmp = mg1.getMembers();

cout << bool(tmp[0] == m1) << " : Ans=1" << endl;

  

mg1.addMember(m2);

  

tmp = mg1.getMembers();

cout << bool(tmp[0] == m1 && tmp[1] == m2) << " : Ans=1" << endl;

   return 0;

}

This is hw 4 Q in case if u need

Overview: You have been greeted by a traveling sales merchant who is selling some wares. You will have access to view the items he is selling and will be able to purchase his wares according to his prices.

Objective: You will be creating a program that focuses on the use of classes and objects. For this assignment, you will be creating a Merchant class and an Antique class.

Main function

In the main function, perform the following tasks:

1) Create two arrays of size 10: one of antique objects and one of integers to represent the quantity of antique

2) Read from standard input the name of the file that stores the initial status of the Merchant's stock

3) Read the content of the file into the array of Antique objects and quantities. The file format is as follows:

<Antique 1 name>, <Antique 1 price>, <Antique 1 quantity>
<Antique 2 name>, <Antique 2 price>, <Antique 2 quantitiy>
...
<Antique 10 name>, <Antique 10 price>, <Antique 10 quantitiy>

You can assume the file will always contain 10 antiques in the above format.

4) Use the two arrays as arguments to create a Merchant object.

5) - budget (float): variable used to store the amount of money the customer can spend. Take in user input for customer's budget like the following :

Enter in budget: $

6) Present the user with the following menu:

Make a selection:
1 - Haggle
2 - View menu
3 - Select an antique
4 - Leave

If the user enters either 1-4, it will call the corresponding member functions of Merchant class (see below). If the user enters anything but 1-4, then it should output to the screen and return to the menu:

Invalid option.

Antique Class

Each Antique object being sold by a Merchant and will have the following attributes:

  • name (string)
  • price (float)

And will have the following member functions:

  • mutators (setName, setPrice)
  • accessors (getName, getPrice)
  • default constructor that sets name to "" (blank string) and price to 0 when a new Antique object is created
  • a function toString that returns a string with the antique information in the following format
<Antique.name>: $<price.2digits>

Merchant Class

A Merchant class will be able to hold 10 different Antique objects and interact with the user to sell to them. It should include the following attributes:

  • antiques (array): An Antique array of 10 objects
  • quantities (array): An Integer array representing the quantity of each Antique object in stock. This can range from 0 to 10.
  • revenue (float): variable used to store the total amount of money earned by the Merchant after each sale

And will have the following member functions:

  • Merchant (constructor): the constructor takes as arguments an array of Antiques and an array of quantities to use them to initialize antiques and quantities. revenue should be set to 0.
  • haggle: function that decreases each and every Antique object's price by 10%. It will print the following:
You have successfully haggled and everything is 10% off.

Customer cannot haggle more than once. If he tries to haggle more than once, it will print the following:

Sorry, you have already haggled.
  • printMenu: function that prints the items available for sale in the following format:
1) <Antique name1>: $<price.2digits>
2) <Antique name2>: $<price.2digits>
...
10) <Antique name10>: $<price.2digits>
  • selectAntique: this function handles item selection and sale. It begins by printing the message (and ending with a newline):
Enter antique number:

The antique number is read from standard input and stored. If the antique is not available (quantity = 0), print the following:

Sorry! Antique is out of stock.

If the user has an insufficient budget, print the following:

Insufficient funds.

Otherwise, if the sale was successful, decrease the user's budget to reflect the sale, add to revenue, and reduce the quantity of the item by one. Print the following to standard output:

Enjoy your <Antique.name>!

And append to a text file called "log2.txt" that acts as an itemized receipt like following:

Sold <Antique.name> for $<price.2digits>
  • leave: this function terminates the program, and appends the budget and revenue to a text file called "log2.txt" at the end:
Total revenue: $<revenue.2digits>
Remaining budget: $<budget.2digits>
0 0
Add a comment Improve this question Transcribed image text
Answer #1

below is required code.

let me know if you have any doubtt or problem. thank you.

=========================================================

antique.h

=======================

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

#ifndef antique_h
#define antique_h

class Antique {
private:
   string name; // name of Antique item
   float price; // price of antique item
public:
   Antique(); // constructor
   Antique(const Antique& other); // copy constructor
   // overloaded operators
   bool operator==(const Antique& other);
   Antique& operator+(const Antique& other);
   // return string represantation of item
   string toString();
   // mutators
   void setName(string NAME);
   void setPrice(float PRICE);
   // accessors
   string getName() const;
   float getPrice() const;
};

#endif /* antique_h */

==========================

antique.cpp

==========================

#include "antique.h"
using namespace std;

void Antique::setName(string NAME) {
   name = NAME;
}

void Antique::setPrice(float PRICE) {
   price = PRICE;
}

string Antique::getName() const {
   return name;
}

float Antique::getPrice() const {
   return price;
}

string Antique::toString() {
   ostringstream streamObj;
   streamObj << fixed;
   streamObj << setprecision(2);
   streamObj << price;
   return name + ": $" + streamObj.str();
}

Antique::Antique() {
   name = "";
   price = 0;
}

Antique::Antique(const Antique& other) {
   this->name = other.name;
   this->price = other.price;
}

bool Antique::operator==(const Antique& other) {
   if (this->name.compare(other.name) == 0) {
       if (this->price == other.price) {
           return true;
       }
   }
   return false;
}

Antique& Antique::operator+(const Antique& other) {
   // create a new Antique object to return
   Antique* newAntique = new Antique();
   newAntique->name = this->name + " and " + other.name;
   newAntique->price = this->price + other.price;
   return *newAntique;
}

===========================

merchant.h

===========================

#include "antique.h"
using namespace std;

#ifndef merchant_h
#define merchant_h

class Merchant {
private:
   Antique* items; // array of Antique items
   int* quantities; // number of coresponding Antique items
   int numAnt; // number of items
   float revenue; // total revenue generated by merchant
public:
   Merchant(); // default constructor
   Merchant(float r); // constructor with parameter
   Merchant(const Merchant& other); // copy constructor
   ~Merchant(); // destructor
   // overloaded operators
   Merchant& operator=(const Merchant& other);
   bool operator==(const Merchant& other);  
   // add new item to merchant's selling list
   void addAntique(Antique& item, int quantity);
};

#endif /* merchant_h */

=========================

merchant.cpp

=========================

#include "merchant.h"
using namespace std;

Merchant::Merchant() {
   // initialize data
   numAnt = 0;
   revenue = 0.0;
   items = nullptr;
   quantities = nullptr;
}

Merchant::Merchant(float r) {
   numAnt = 0;
   revenue = r;
   items = nullptr;
   quantities = nullptr;
}

Merchant::Merchant(const Merchant& other) {
   // use assignment operator to create copy
   *this = other;
}

Merchant::~Merchant() {
   // free memory
   delete[] items;
   delete[] quantities;
}

Merchant& Merchant::operator=(const Merchant& other) {
   // delte any array already filled
   delete[] items;
   delete[] quantities;
   // copy revenue and num of items
   this->numAnt = other.numAnt;
   this->revenue = other.revenue;
   // create a new array of item
   items = new Antique[numAnt]; // this initialize all items with default values
   quantities = new int[numAnt];
   // copy each Antique item
   for (int i = 0; i < numAnt; i++) {
       // copy value of each item
       this->items[i].setName(other.items[i].getName());
       this->items[i].setPrice(other.items[i].getPrice());
       this->quantities[i] = other.quantities[i];
   }
   return *this;
}

bool Merchant::operator==(const Merchant& other) {
   // compare number of items and revanue
   if (this->numAnt == other.numAnt && this->revenue == other.revenue) {
       // compare each item in order
       for (int i = 0; i < numAnt; i++) {
           // can not use != operator as it is not implemented for Antique class
           if (!(this->items[i] == other.items[i] && this->quantities[i] == other.quantities[i])) {
               // found a item that is not equal in both merchant
               return false;
           }  
       }
       // all items checked and they are equal
       return true;
   }
   return false;
}

void Merchant::addAntique(Antique& item, int quantity) {
   // create an array of new size
   Antique* newItems = new Antique[numAnt + 1];
   int* newQuantities = new int[numAnt + 1];
   // copy old items
   for (int i = 0; i < numAnt; i++) {
       newItems[i].setName(items[i].getName());
       newItems[i].setPrice(items[i].getPrice());
       newQuantities[i] = quantities[i];
   }
   // add new item to end of array
   newItems[numAnt].setName(item.getName());
   newItems[numAnt].setPrice(item.getPrice());
   newQuantities[numAnt] = quantity;
   // delete old arrays
   delete[] items;
   delete[] quantities;
   // re-assign array
   items = newItems;
   quantities = newQuantities;
}

===========================

MerchantGuild.h

===========================

#include "merchant.h"
using namespace std;

#ifndef MerchantGuild_h
#define MerchantGuild_h

class MerchantGuild {
public:
   MerchantGuild(); // default constructor
   MerchantGuild(int size); // constructor with parameter
   MerchantGuild(const MerchantGuild& other); // copy constructor
   ~MerchantGuild(); // destructor
   MerchantGuild& operator=(const MerchantGuild& other); //overloaded assignment operation
   void addMember(Merchant& newM); //add new member to guild
   Merchant* getMembers(); // return array of members
private:
   Merchant* members; // currently registered member
   int guildSize; // total capacity of guild
   int numMem; // number of member registered
};

#endif

===========================

MerchantGuild.cpp

===========================

#include "MerchantGuild.h"

MerchantGuild::MerchantGuild() {
   guildSize = 10;
   numMem = 0;
   members = new Merchant[guildSize];
}

MerchantGuild::MerchantGuild(int size) {
   // check size of guild
   if (size < 1) {
       guildSize = 10;
   }
   else {
       guildSize = size;
   }
   numMem = 0;
   members = new Merchant[guildSize];
}

MerchantGuild::MerchantGuild(const MerchantGuild& other) {
   // use assignment operation to copy other guild
   *this = other;
}

MerchantGuild::~MerchantGuild() {
   delete[] members;
}

MerchantGuild& MerchantGuild::operator=(const MerchantGuild& other) {
   // delete any allocated array
   delete[] members;
   // copy number of member and size
   this->guildSize = other.guildSize;
   this->numMem = other.numMem;
   // create a new array
   members = new Merchant[guildSize];
   // copy the array
   for (int i = 0; i < numMem; i++) {
       this->members[i] = other.members[i];
   }
   return *this;
}

void MerchantGuild::addMember(Merchant& newM) {
   // add new memeber if guild is not full
   if (numMem < guildSize) {
       // add new member at end of list
       members[numMem] = newM;
       numMem++;
   }
   else {
       cout << "Guild is full." << endl;
   }
}

Merchant* MerchantGuild::getMembers() {
   return members;
}

=======================

main.cpp

=======================

#include "MerchantGuild.h"
#include "merchant.h"
#include "antique.h"
using namespace std;

int main() {
   cout << "KEY: " << "True is: " << true << " False is: " << false << endl << endl;
   // antique tests
   Antique a1, a2;
   a1.setName("fork");
   a1.setPrice(3.25);
   a2.setName("knife");
   a2.setPrice(2.50);
   cout << "antique test" << endl;
   Antique a3 = a1 + a2;
   cout << a3.toString() << endl;
   cout << bool(a1 == a2) << " : Ans=0" << endl;
   cout << bool(a1 == a1) << " : Ans=1" << endl;

   // merchant tests
   Merchant m1(1.2), m2(2.5);
   cout << "merchant test" << endl;
   m1.addAntique(a1, 2);
   m1.addAntique(a2, 5);
   m2.addAntique(a3, 3);
   cout << bool(m1 == m2) << " : Ans=0" << endl;
   cout << bool(m1 == m1) << " : Ans=1" << endl;
   Merchant m3(m1);
   cout << bool(m3 == m1) << " : Ans=1" << endl;

   //merchant guild tests
   MerchantGuild mg1;
   cout << "merchant guild tests" << endl;
   mg1.addMember(m1);
   Merchant* tmp = mg1.getMembers();
   cout << bool(tmp[0] == m1) << " : Ans=1" << endl;
   mg1.addMember(m2);
   tmp = mg1.getMembers();
   cout << bool(tmp[0] == m1 && tmp[1] == m2) << " : Ans=1" << endl;

   return 0;

}

Add a comment
Know the answer?
Add Answer to:
IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Antique Class Each Antique object being sold by a Merchant and will have the following attributes:...

    Antique Class Each Antique object being sold by a Merchant and will have the following attributes: name (string) price (float) And will have the following member functions: mutators (setName, setPrice) accessors (getName, getPrice) default constructor that sets name to "" (blank string) and price to 0 when a new Antique object is created a function toString that returns a string with the antique information in the following format <Antique.name>: $<price.2digits> Merchant Class A Merchant class will be able to hold...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • You are given a partial implementation of two classes. GroceryItem is a class that holds the...

    You are given a partial implementation of two classes. GroceryItem is a class that holds the information for each grocery item. GroceryInventory is a class that holds an internal listing of GroceryItems as well as the tax rate for the store. Your inventory could be 100 items it could be 9000 items. You won’t know until your program gets the shipment at runtime. For this you should use the vector class from the C++ standard library. I've completed the GroceryItem.h...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  •       C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for...

          C++ -- Vector of Bank Accounts (15 pts) Please help! :( Use csegrid or Clion for this part Add on to the lab12a solution(THIS IS PROVIDED BELOW). You will add an overload operator function to the operator << and a sort function (in functions.h and functions.cpp)    Print out the customer’s name, address, account number and balance using whatever formatting you would like    Sort on account balance (hint: you can overload the < operator and use sort from the...

  • C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; }...

    C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; } bool checkinventoryprice(float price) {    return price > 0; } void endProgram() {    system("pause");    exit(0); } void errorID(string str) {    cout << "Invalid Id!!! " << str << " should be greater than 0" << endl; } void errorPrice(string str) {    cout << "Invalid Price!!!" << str << " should be greater than 0" << endl; } int inputId() {...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT