Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CODE ---> pastebin dot com slash 3p806crH CODE ---> pastebin dot com slash 3p806crH Please submit the source file bespokeHats.cpp 1. bespokeHats (100 Points) A.

CODE ---> pastebin dot com slash 3p806crH

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

CODE ---> pastebin dot com slash 3p806crH

Please submit the source file bespokeHats.cpp 1. bespokeHats (100 Points) A. Overview: All hats at Bespoke Hats are made and decorated on premises. The hat maker thread makes exactly NUM_HATS_TO_MAKE Hat instances. They place each one in the first HatStore instance. The decorator thread decorates exactly NUM_HATS_TO_MAKE Hat instances. They get each one from the first HatStore, add either one or two accessories, and place them in the second store for the shopper threads. The two shopper threads buy the Hat instances. Each hat has one or two accessories that add or detract from its fanciness. One shopper prefers hats with fanciness of 0 or more . The other shopper prefers hats with fanciness of 0 or less So between the two, all Hat instances should be bought. B. Cut-and-paste the following: * * * bespokeHats.cpp * * * This file defines a program that showcases multi-threaded programming by simulates the manufacture and sale of fancy women's hats. * --- --- --- --- - -- ---- * Version la 2020 February 11 Joseph Phillips ---* * * Compile with: $ g++ bespokeHats.cpp -o bespokeHats -1pthread - & -std=c++11 //--- File inclusion: ---// #include #include #include #include #include #include #include // sleep() //--- Constants: ---/ // PURPOSE: const int To tell how many hats to make. NUM_HATS_TO_MAKE = 16; // PURPOSE: To tell the minimum amount of time to make a hat. const int MIN_NUM_SECS_TO_MAKE_HAT= 5; // PURPOSE: To tell the maximum amount of time to make a hat. const int MAX_NUM_SECS_TO_MAKE_HAT= 8; //--- Classes: ---// // PURPOSE: To hold information on a hat accessory. class Accessory // I. Member vars: // PURPOSE: To hold the name of the accessory; const char* nameCPtr // int PURPOSE: To hold the fanciness level ( is neutral): fancinessLevel_; // II. Disallowed auto-generated methods: // No default constructor: Accessory (); protected : // III. Protected methods: public : // IV. Constructor(s), assignment op(s), factory(s) and destructor: // PURPOSE: To initialize '*this' to have name pointed to by 'newNameCPtr' // and fanciness level 'newFancyLevel'. No return value. Accessory (const char* newNameCPtr, int newFancyLevel nameCPtr_(newNameCPtr), fancinessLevel_(newFancyLevel) // PURPOSE: Accessory To make '*this' a copy of 'source'. No return value. (const Accessory& source nameCPtr_(source.getNameCPtr(), fancinessLevel_(source.getFancinessLevel()) { } // PURPOSE: To resources of '*this', make '*this' a copy of 'source', // and return a reference to '*this'. Accessory& operators (const Accessory& source // I. Application validity chech: if (this == &source) return(*this); // II. Release resources: // III. Copy 'source': nameCPtr = source.getNameCPtr(); fancinessLevel = source.getFancinessLevel(); // IV. Finished: return(*this); // PURPOSE: To release the resources of '*this'. No parameters. // No return value. Accessory // V. Accessors: // PURPOSE: To return the name of the accessory. No parameters. const char* getNameCPtr const { return(nameCPtr_); } // PURPOSE: To return the fanciness level ( is neutral). No parameters. int get Fanciness Level O const { return(fancinessLevel_); } // PURPOSE: To hold the names of the styles of hat. const char* hatStyleNameArray[] = { "Fedora", "Panama hat", "Boater", "Sun hat", "Floppy hat", "Pillbox", "Beret", "Bowler", "Baseball cap", "Cowboy hat" }; // PURPOSE: const int To hold the number of elements in 'hatStyleNameArray[]': NUM_HAT_STYLES = sizeof(hatStyleNameArray)/sizeof(const char*); // PURPOSE: To be be the data-structure telling the names and fanciness- // levels of the known accessories. Accessory accessoryArray[] = { {"a beer can holder", -10}, {"a propeller", {"a miner's lamp", {"3 small feathers", {"a veil", {"a live pigeon", +10}, {"a live pigeon with its own fedora", +12} }; ON.O // PURPOSE: const int To hold the number of elements in accessory Array()': NUM_ACCESSORIES = sizeof(accessory Array)/sizeof(Accessory); // PURPOSE: class To represent a hat. Hat // I. Member vars: // PURPOSE: To keep track of the number of Hat instances that have been // purchased (destroyed). static int count__; // PURPOSE: int To hold the index of the hat style. hatStyleIndex_; // PURPOSE: To hold the accessory(s), if any, on the hat. std::vector accessoryDs_; // PURPOSE: std::string To a description of '*this' hat. description_; // PURPOSE: int To tell how fancy '*this' hat is. fancinessLevel_; // II. Disallowed auto-generated methods: // No copy assignment operator: Hat & operator: (const Hat&); protected: // III. Protected methods: () public : // IV. Constructor(s), // PURPOSE: To make an unadorned hat of a random style. No parameters. // No return value. Hat hatStyleIndex_(rand() % NUM_HAT_STYLES), accessoryDs_O, description (std::string (hatStyleNameArray[hatStyleIndex_]) : fancinessLevel_(0) PURPOSE: // Hat To make '*this' a copy of 'source'. No return value. (const Hat& source hatStyle Index_(source.hatStyle Index_), accessoryDs_(source.accessoryDs_), description_(source.description_), fancinessLevel_(source. fancinessLevel_) // PURPOSE: To release the resources of '*this'. No parameters. // No return value. Hat 0 count__++; // V. Accessors: // PURPOSE: To return the number of Hat instances that have been purchased // (destroyed). static int getCount ( { return(count ); } // PURPOSE: int To return the index of the hat style. No parameters. getHatStyleIndex() const { return(hatStyleIndex_); } // PURPOSE: To return a description of '*this' Hat instance as a C-string. // No parameters. const char* getDescriptionCPtr ( const { return(description_.c_str(); } // // int PURPOSE: To return the fanciness level of '*this' Hat instance. No parameters. get Fanciness Level const { return(fancinessLevel); } // VI. Mutators: // VII. Methods that do main and misc. work of class: // PURPOSE: To add 'accessoryPtr' to the data-structure of accessories to // '*this' Hat. void decorate (const Accessory* accessoryPtr if (accessoryDs_.empty()) description_ += " with "; Ise description_ += " and "; description += accessoryPtr->getNameCPtr(); fancinessLevel += accessoryPtr-> get FancinessLevel(); accessoryDs_.push_back(accessoryptr); // PURPOSE: class To store hats. HatStore // I. Member vars: // PURPOSE: To tell the length of 'array_'. size_t arrayLen_; // PURPOSE: Hat** To hold the address of the Hat being stored, or array_; // PURPOSE: To tell the index into which to place addresses of new Hat // instances. int inIndex_; // PURPOSE: To tell the index into which to remove the address of stored // Hat instances. int outIndex_; To tell how many addresses of Hat instances are in '*this' // PURPOSE: // store. int numItems_j // YOUR VARIABLES HERE // II. Disallowed auto-generated methods: // No default constructor: HatStore // No copy constructor: HatStore (const HatStore&); // No copy assignment op: HatStore& operators (const HatStore&); protected : public : // IV. Constructors: // PURPOSE: To initialize '*this' to an empty store of addresses of Hat // instances. No parameters. HatStore (size_t length arrayLen_(length), array_(NULL), inIndex_(0), out Index_(0), numItems_(0) array_ = (Hat**) calloc(arrayLen_, sizeof(Hat*) ); for (size_t i = 0; i 0) printf("Too bad: we have to " "throw these hats away: " delete(pullout("")); while (getNumItems() > 0); // DESTROY VARIABLES HERE free (array_); // V. Accessors: // PURPOSE: To return the number of items in '*this' store. No parameters. int getNum Items const return(numItems_); // VI. Mutators: // VII. Methods that do misc. and main work of class: // PURPOSE: To place 'hatPtr' in '*this' store. Prints out // 'waitingMsgCPtr' if forced to wait before doing so. No return value. void putin (Hat* hatPtr, const char* waitingMsgCPtr // I. Applicability check: // II. Place 'hatPtr' in '*this': while (getNumItems() >= arrayLen_) printf("%s", waitingMsgCPtr); array_[inIndex_] = hatPtr; inIndex_++; numItems_++; if (inIndex_ >= arrayLen_) inIndex_ = 0; // III. Finished: // PURPOSE: To get and return the address of a Hat instance from '*this' // store. Prints out 'waitingMsgCPtr' if forced to wait before doing so. Hat* pullout (const char* waitingMsgCPtr { // I. Applicability check: // II. Place 'hatPtr' in '*this': while (getNumItems() = arrayLen_) out Index_ = @; // III. Finished: return(toReturn); // TWO structs OR classes HERE // PURPOSE: To keep track of the number of Hat instances that have been // purchased (destroyed). int Hat::count_ = ; // PURPOSE: To sleep a random amount of time between // "MIN_NUM_SECS_TO_MAKE_HAT' and 'MAX_NUM_SECS_TO_MAKE_HAT' seconds. void sleepRandomAmountOfTime sleep (MIN_NUM_SECS_TO_MAKE_HAT + rand() % (MAX_NUM_SECS_TO_MAKE_HAT-MIN_NUM_SECS_TO_MAKE_HAT+1) // PURPOSE: To make 'NUM_HATS_TO_MAKE' Hat instances and put their addresses // in the first hat store. void* hatMaker (void vPtr Hat* HatStore* hatPtr; hatStorePtr = NULL; // CHANGE THAT NULL! for (size_t i = 0; i getDescriptionCPtr()); hatStorePtr-> putIn(hatPtr, "Hat maker: "Hurry it up with the " "decorations! The rack is full!\" " printf("Hat maker: "I've made enough hats today, bye!\" "); return(NULL); / PURPOSE: To take 'NUM_HATS_TO_MAKE' Hat instances from the first hat store, add either 1 or 2 decorations, and put their addresses in the second hat store. hatDecorator (void* VPtr void* Hat* HatStore* HatStore* hatPtr; firstStorePtr secondStorePtr = NULL; // CHANGE THAT NULL! = NULL; // CHANGE THAT NULL! for (size_t i = 0; i pullout("Decorator \"Hey hat maker, get to work!\" "); switch (rand() % 2) case 0: printf("Decorator: \"I will add one decoration to this %s. " ", hatPtr->getDescriptionCPtr() sleep RandomAmountOfTime(); // DECORATE THE HAT break; case 1: printf("Decorator: "I will add two decorations to this %s. " ", hatPtr->getDescriptionCPtr ); sleepRandomAmountOfTime(); sleepRandomAmountOfTime; // DECORATE THE HAT, TWICE break; secondStorePtr-> putIn(hatPtr, "Decorator "Hey ladies, start shopping! " "); printf("Decorator: "No more decorating, I gotta" "clean-up after the remaining pigeons. " " ); return(NULL); // PURPOSE: To obtain addresses of Hat instances from the second hat store, and either purchase them if they are liked or throw return them if they are disliked. hatBuyer (void* vPtr void* Hat* HatStore* hatPtr; storePtr = NULL; // CHANGE THAT NULL! while (Hat::getCount() pullout("Shopper "I thought there were hats here!?!\" "); if ( true // CHANGE THAT true! printf("Shopper \"This %s is gorgeous! hatPtr->getDescriptionCPtr I'll take it!\" ", // GET RID OF THE HAT else I'll return it!\" ", printf("Shopper "Yuck! This %s is ugly! hatPtr->getDescriptionCPtr ); 1/ RETURN hatPtr back to the store sleepRandomAmountOfTime(); "I've done enough damage on my credit card today. " "); printf("Shopper: return(NULL); int main (int char* argc, argv[ ] HatStore HatStore for DecoratorStore (2); for ShoppersStore (NUM_HATS_TO_MAKE/2); // YOUR CODE HERE srand(getpid()); // YOUR CODE HERE while (Hat::getCount() decorate(accessoryArray + (rand() % NUM_ACCESSORIES)); either one or two times depending on the number of decorations added. 5. From the votr coming into hatShopper(), obtain the address of the second Hatstore and the bool value telling if there is a preference for fancy hats. Then, inside the while loop: . If the shopper prefers fancy hats and hatPtr->getFancinessLevel() >= 0, or if the shopper prefers non-fancy hats and hatPtr->getFancinessLevel() // sleep() //--- Constants: ---/ // PURPOSE: const int To tell how many hats to make. NUM_HATS_TO_MAKE = 16; // PURPOSE: To tell the minimum amount of time to make a hat. const int MIN_NUM_SECS_TO_MAKE_HAT= 5; // PURPOSE: To tell the maximum amount of time to make a hat. const int MAX_NUM_SECS_TO_MAKE_HAT= 8; //--- Classes: ---// // PURPOSE: To hold information on a hat accessory. class Accessory // I. Member vars: // PURPOSE: To hold the name of the accessory; const char* nameCPtr // int PURPOSE: To hold the fanciness level ( is neutral): fancinessLevel_; // II. Disallowed auto-generated methods: // No default constructor: Accessory (); protected : // III. Protected methods: public : // IV. Constructor(s), assignment op(s), factory(s) and destructor: // PURPOSE: To initialize '*this' to have name pointed to by 'newNameCPtr' // and fanciness level 'newFancyLevel'. No return value. Accessory (const char* newNameCPtr, int newFancyLevel nameCPtr_(newNameCPtr), fancinessLevel_(newFancyLevel) // PURPOSE: Accessory To make '*this' a copy of 'source'. No return value. (const Accessory& source nameCPtr_(source.getNameCPtr(), fancinessLevel_(source.getFancinessLevel()) { } // PURPOSE: To resources of '*this', make '*this' a copy of 'source', // and return a reference to '*this'. Accessory& operators (const Accessory& source // I. Application validity chech: if (this == &source) return(*this); // II. Release resources: // III. Copy 'source': nameCPtr = source.getNameCPtr(); fancinessLevel = source.getFancinessLevel(); // IV. Finished: return(*this); // PURPOSE: To release the resources of '*this'. No parameters. // No return value. Accessory // V. Accessors: // PURPOSE: To return the name of the accessory. No parameters. const char* getNameCPtr const { return(nameCPtr_); } // PURPOSE: To return the fanciness level ( is neutral). No parameters. int get Fanciness Level O const { return(fancinessLevel_); } // PURPOSE: To hold the names of the styles of hat. const char* hatStyleNameArray[] = { "Fedora", "Panama hat", "Boater", "Sun hat", "Floppy hat", "Pillbox", "Beret", "Bowler", "Baseball cap", "Cowboy hat" }; // PURPOSE: const int To hold the number of elements in 'hatStyleNameArray[]': NUM_HAT_STYLES = sizeof(hatStyleNameArray)/sizeof(const char*); // PURPOSE: To be be the data-structure telling the names and fanciness- // levels of the known accessories. Accessory accessoryArray[] = { {"a beer can holder", -10}, {"a propeller", {"a miner's lamp", {"3 small feathers", {"a veil", {"a live pigeon", +10}, {"a live pigeon with its own fedora", +12} }; ON.O // PURPOSE: const int To hold the number of elements in accessory Array()': NUM_ACCESSORIES = sizeof(accessory Array)/sizeof(Accessory); // PURPOSE: class To represent a hat. Hat // I. Member vars: // PURPOSE: To keep track of the number of Hat instances that have been // purchased (destroyed). static int count__; // PURPOSE: int To hold the index of the hat style. hatStyleIndex_; // PURPOSE: To hold the accessory(s), if any, on the hat. std::vector accessoryDs_; // PURPOSE: std::string To a description of '*this' hat. description_; // PURPOSE: int To tell how fancy '*this' hat is. fancinessLevel_; // II. Disallowed auto-generated methods: // No copy assignment operator: Hat & operator: (const Hat&); protected: // III. Protected methods: () public : // IV. Constructor(s), // PURPOSE: To make an unadorned hat of a random style. No parameters. // No return value. Hat hatStyleIndex_(rand() % NUM_HAT_STYLES), accessoryDs_O, description (std::string (hatStyleNameArray[hatStyleIndex_]) : fancinessLevel_(0) PURPOSE: // Hat To make '*this' a copy of 'source'. No return value. (const Hat& source hatStyle Index_(source.hatStyle Index_), accessoryDs_(source.accessoryDs_), description_(source.description_), fancinessLevel_(source. fancinessLevel_) // PURPOSE: To release the resources of '*this'. No parameters. // No return value. Hat 0 count__++; // V. Accessors: // PURPOSE: To return the number of Hat instances that have been purchased // (destroyed). static int getCount ( { return(count ); } // PURPOSE: int To return the index of the hat style. No parameters. getHatStyleIndex() const { return(hatStyleIndex_); } // PURPOSE: To return a description of '*this' Hat instance as a C-string. // No parameters. const char* getDescriptionCPtr ( const { return(description_.c_str(); } // // int PURPOSE: To return the fanciness level of '*this' Hat instance. No parameters. get Fanciness Level const { return(fancinessLevel); } // VI. Mutators: // VII. Methods that do main and misc. work of class: // PURPOSE: To add 'accessoryPtr' to the data-structure of accessories to // '*this' Hat. void decorate (const Accessory* accessoryPtr if (accessoryDs_.empty()) description_ += " with "; Ise description_ += " and "; description += accessoryPtr->getNameCPtr(); fancinessLevel += accessoryPtr-> get FancinessLevel(); accessoryDs_.push_back(accessoryptr); // PURPOSE: class To store hats. HatStore // I. Member vars: // PURPOSE: To tell the length of 'array_'. size_t arrayLen_; // PURPOSE: Hat** To hold the address of the Hat being stored, or array_; // PURPOSE: To tell the index into which to place addresses of new Hat // instances. int inIndex_; // PURPOSE: To tell the index into which to remove the address of stored // Hat instances. int outIndex_; To tell how many addresses of Hat instances are in '*this' // PURPOSE: // store. int numItems_j // YOUR VARIABLES HERE // II. Disallowed auto-generated methods: // No default constructor: HatStore // No copy constructor: HatStore (const HatStore&); // No copy assignment op: HatStore& operators (const HatStore&); protected : public : // IV. Constructors: // PURPOSE: To initialize '*this' to an empty store of addresses of Hat // instances. No parameters. HatStore (size_t length arrayLen_(length), array_(NULL), inIndex_(0), out Index_(0), numItems_(0) array_ = (Hat**) calloc(arrayLen_, sizeof(Hat*) ); for (size_t i = 0; i 0) printf("Too bad: we have to " "throw these hats away: " delete(pullout("")); while (getNumItems() > 0); // DESTROY VARIABLES HERE free (array_); // V. Accessors: // PURPOSE: To return the number of items in '*this' store. No parameters. int getNum Items const return(numItems_); // VI. Mutators: // VII. Methods that do misc. and main work of class: // PURPOSE: To place 'hatPtr' in '*this' store. Prints out // 'waitingMsgCPtr' if forced to wait before doing so. No return value. void putin (Hat* hatPtr, const char* waitingMsgCPtr // I. Applicability check: // II. Place 'hatPtr' in '*this': while (getNumItems() >= arrayLen_) printf("%s", waitingMsgCPtr); array_[inIndex_] = hatPtr; inIndex_++; numItems_++; if (inIndex_ >= arrayLen_) inIndex_ = 0; // III. Finished: // PURPOSE: To get and return the address of a Hat instance from '*this' // store. Prints out 'waitingMsgCPtr' if forced to wait before doing so. Hat* pullout (const char* waitingMsgCPtr { // I. Applicability check: // II. Place 'hatPtr' in '*this': while (getNumItems() = arrayLen_) out Index_ = @; // III. Finished: return(toReturn); // TWO structs OR classes HERE // PURPOSE: To keep track of the number of Hat instances that have been // purchased (destroyed). int Hat::count_ = ; // PURPOSE: To sleep a random amount of time between // "MIN_NUM_SECS_TO_MAKE_HAT' and 'MAX_NUM_SECS_TO_MAKE_HAT' seconds. void sleepRandomAmountOfTime sleep (MIN_NUM_SECS_TO_MAKE_HAT + rand() % (MAX_NUM_SECS_TO_MAKE_HAT-MIN_NUM_SECS_TO_MAKE_HAT+1) // PURPOSE: To make 'NUM_HATS_TO_MAKE' Hat instances and put their addresses // in the first hat store. void* hatMaker (void vPtr Hat* HatStore* hatPtr; hatStorePtr = NULL; // CHANGE THAT NULL! for (size_t i = 0; i getDescriptionCPtr()); hatStorePtr-> putIn(hatPtr, "Hat maker: "Hurry it up with the " "decorations! The rack is full!\" " printf("Hat maker: "I've made enough hats today, bye!\" "); return(NULL); / PURPOSE: To take 'NUM_HATS_TO_MAKE' Hat instances from the first hat store, add either 1 or 2 decorations, and put their addresses in the second hat store. hatDecorator (void* VPtr void* Hat* HatStore* HatStore* hatPtr; firstStorePtr secondStorePtr = NULL; // CHANGE THAT NULL! = NULL; // CHANGE THAT NULL! for (size_t i = 0; i pullout("Decorator \"Hey hat maker, get to work!\" "); switch (rand() % 2) case 0: printf("Decorator: \"I will add one decoration to this %s. " ", hatPtr->getDescriptionCPtr() sleep RandomAmountOfTime(); // DECORATE THE HAT break; case 1: printf("Decorator: "I will add two decorations to this %s. " ", hatPtr->getDescriptionCPtr ); sleepRandomAmountOfTime(); sleepRandomAmountOfTime; // DECORATE THE HAT, TWICE break; secondStorePtr-> putIn(hatPtr, "Decorator "Hey ladies, start shopping! " "); printf("Decorator: "No more decorating, I gotta" "clean-up after the remaining pigeons. " " ); return(NULL); // PURPOSE: To obtain addresses of Hat instances from the second hat store, and either purchase them if they are liked or throw return them if they are disliked. hatBuyer (void* vPtr void* Hat* HatStore* hatPtr; storePtr = NULL; // CHANGE THAT NULL! while (Hat::getCount() pullout("Shopper "I thought there were hats here!?!\" "); if ( true // CHANGE THAT true! printf("Shopper \"This %s is gorgeous! hatPtr->getDescriptionCPtr I'll take it!\" ", // GET RID OF THE HAT else I'll return it!\" ", printf("Shopper "Yuck! This %s is ugly! hatPtr->getDescriptionCPtr ); 1/ RETURN hatPtr back to the store sleepRandomAmountOfTime(); "I've done enough damage on my credit card today. " "); printf("Shopper: return(NULL); int main (int char* argc, argv[ ] HatStore HatStore for DecoratorStore (2); for ShoppersStore (NUM_HATS_TO_MAKE/2); // YOUR CODE HERE srand(getpid()); // YOUR CODE HERE while (Hat::getCount() decorate(accessoryArray + (rand() % NUM_ACCESSORIES)); either one or two times depending on the number of decorations added. 5. From the votr coming into hatShopper(), obtain the address of the second Hatstore and the bool value telling if there is a preference for fancy hats. Then, inside the while loop: . If the shopper prefers fancy hats and hatPtr->getFancinessLevel() >= 0, or if the shopper prefers non-fancy hats and hatPtr->getFancinessLevel()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

Explain why many households do not participate in the stock market.

Answered: 1 week ago

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago