You are on page 1of 6

  Textbook Solutions Expert Q&A Practice 

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / question 1: recommender system you are required to …

Question: Question 1: Recommender System You are required to make a… Post a question
Answers from our experts for your tough
homework questions

USE C++ Enter question

Continue to post
1 question remaining

Snap a photo from your


phone to post a question
We'll send you a one-time download
link

888-888-8888 Text me

By providing your phone number, you agree to receive a one-tim


automated text message with a link to get the app. Standard
messaging rates may apply.

My Textbook Solutions

Fortran... Organizatio... Study...

3rd Edition 13th Edition 9th Edition

View all solutions

Show transcribed image text

Expert Answer

Anonymous answered this


Was this answer helpful? 0 0
16 answers

#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#include <vector>

using namespace std;

class Customer
{
   static int _ID;
   int m_id;
   string m_name;
public:
   Customer(string name)
   {
       m_id = _ID++;
       m_name = name;
   }
   const string& getName() const
   {
       return m_name;
   }

   friend std::ostream& operator<< (std::ostream& out, const Customer& customer);


};

class Item
{
   static int _ID;
   int m_id;
   string m_name;
   int m_category;
public:
   Item(string name, int category)
   {
       m_id = _ID++;
       m_name = name;
       m_category = category;
   }

   const string& getName() const


   {
       return m_name;
   }

   const int getCategory() const {


       return m_category;
   }

   friend std::ostream& operator<< (std::ostream& out, const Item& item);


};

class Store
{
   Customer** m_customers;
   Item** m_items;
   int m_cus_count;
   int m_item_count;

   vector<vector<int>> record;

public:
   Store(Customer** cus, Item** items, int cc, int ic)
   {
       this->m_customers = cus;
       this->m_items = items;
       this->m_cus_count = cc;
       this->m_item_count = ic;

       record = vector<vector<int>>(cc, vector<int>());


   }

   void DisplayAllItems()
   {
       for (int i = 0; i < m_item_count; ++i)
           cout << *(m_items[i]) << endl;
       cout << endl;
   }

   void ViewHistory(int customer)


   {
       for (auto item_index : record[customer])
           cout << *(m_items[item_index]) << endl;

       cout << endl;


   }
   void ViewRecommendations(int customer)
   {
       vector<int> categories;
       for (auto index : record[customer])
       {
           if (std:: nd(categories.begin(), categories.end(), m_items[index]->getCategory()) == categories.end())
               categories.push_back(m_items[index]->getCategory());
       }

       for (int i = 0; i < this->m_item_count; ++i)


       {
           Item* item = m_items[i];
           int cat = item->getCategory();

           if (std:: nd(categories.begin(), categories.end(), cat) != categories.end())


               cout << *item << endl;
       }
       cout << endl;
   }

   void Puchase(int cid, int iid)


   {
       record[cid].push_back(iid);
   }

   ~Store()
   {
       for (int i = 0; i < this->m_item_count; ++i)
           delete m_items[i];

       for (int i = 0; i < this->m_cus_count; ++i)


           delete m_customers[i];

       delete[] m_customers;


       delete[] m_items;
   }
};

std::ostream& operator<< (std::ostream& out, const Customer& customer)


{
   out << "Customer Id: " << customer.m_id << ", Name: " << customer.m_name;
   return out;
}

std::ostream& operator<< (std::ostream& out, const Item& item)


{
   out << "ItemId: " << item.m_id << ", Name: " << item.m_name;
   return out;
}

// initialise IDs
int Customer::_ID{ 0 };
int Item::_ID{ 0 };

Customer** generateCustomers(int &cus_count)


{
   cus_count = 25;
   Customer** customers = new Customer * [cus_count];

   for (int i = 0; i < cus_count; ++i)


   {
       customers[i] = new Customer("Customer " + to_string(i + 1));
   }

   return customers;
}

Item** generateItems(int &item_count)


{
   item_count = 30;
   int* arr = new int[item_count];

   for (int i = 0; i < item_count; ++i)


       arr[i] = i;

   // shuffle categories
   shuffle(arr, arr + item_count, default_random_engine(0));

   Item** items = new Item * [item_count];


   for (int i = 0; i < item_count; ++i)
       items[i] = new Item("Item " + to_string(i + 1), (arr[i] / 10) + 1);

   delete[] arr;
   return items;
}

int main()
{
   int cus_count;
   int item_count;

   auto customers = generateCustomers(cus_count);


   auto items = generateItems(item_count);

   Store s(customers, items, cus_count, item_count);

   int selection = -1;


   int cur_customer = -1;

   do
   {
       cout << "Menu" << endl;
       cout << "1. View all items" << endl;
       cout << "2. View purchase history" << endl;
       cout << "3. View recommend items" << endl;
       cout << "4. Login as new customer" << endl;
       cout << "5. Purchase Item" << endl;
       cout << "6. Exit" << endl;

       cin >> selection;

       switch (selection)


       {
       case 1:
           s.DisplayAllItems();
           break;

       case 2:
           if (cur_customer < 0 || cur_customer >= cus_count)
               cout << "Kindly login rst" << endl;
           else
               s.ViewHistory(cur_customer);
           break;

       case 3:
           if (cur_customer < 0 || cur_customer >= cus_count)
               cout << "Kindly login rst" << endl;
           else
               s.ViewRecommendations(cur_customer);
           break;

       case 4:
           cout << "Enter new customer ID: ";
           int temp;
           cin >> temp;

           if (temp < 0 || temp >= cus_count)


               cerr << "Invalid Customer" << endl << endl;
           else
               cur_customer = temp;
           break;

       case 5:
           if (cur_customer < 0 || cur_customer >= cus_count)
               cout << "Kindly login rst" << endl;
           else
           {
               int pid;
               cout << "Enter Item ID: ";
               cin >> pid;
               if (pid < 0 || pid >= item_count)
                   cerr << "Invalid Item ID" << endl << endl;
               else
                   s.Puchase(cur_customer, pid);
           }
           break;
       }
   } while (selection != 6);

   return 0;
}


View comments (1)


Practice with similar questions

Q: TASK SHOULD BE DONE IN C++

A: See answer

Q: Write a code in c++ Question 1: Recommender System You are required to make a very simple recommender system
for an online store. When a customer buys an item from the store, your system should maintain the history, i.e., that
customer X bought item Y. Next time the customer logs into the store, they should see items related to the one they
bought. For representing relatedness...

A: See answer 100% (1 rating)

Up next for you in Computer Science

Use Python 3 to do the Web programming


following question:

See more questions for


subjects you study
Solution Image

See answer See answer

Questions viewed by other students

Q: Write a code in c++ Question 1: Recommender System You are required to make a very simple recommender system
for an online store. When a customer buys an item from the store, your system should maintain the history, i.e., that
customer X bought item Y. Next time the customer logs into the store, they should see items related to the one they
bought. For representing relatedness...

A: See answer 100% (1 rating)

Q: Question 3: Calendar Application (write code in c++) In this program, you will develop a calendar application that can
display the full calendar for any month of any year and allow the user to add notes against any date. Implement the
following functionality: 1. Display calendar for any month of any year 2. Add a note against any date 3. View all notes
sorted by date 4. Edit or...

A: See answer

Show more 

COMPANY LEGAL & POLICIES CHEGG PRODUCTS AND SERVICES CHEGG NETWORK CUSTOMER SERVICE

About Chegg Advertising Choices Cheap Textbooks Mobile Apps EasyBib Customer Service
Chegg For Good Cookie Notice Chegg Coupon Sell Textbooks Internships.com Give Us Feedback
College Marketing General Policies Chegg Play Solutions Manual Thinkful Help with eTextbooks
Corporate Development Intellectual Property Rights Chegg Study Help Study 101 Help to use EasyBib Plus
Investor Relations Terms of Use College Textbooks Textbook Rental Manage Chegg Study
Jobs Global Privacy Policy eTextbooks Used Textbooks Subscription

Join Our Affiliate Program DO NOT SELL MY INFO Flashcards Digital Access Codes Return Your Books

Media Center Honor Code Learn Chegg Money Textbook Return Policy

Site Map Honor Shield Chegg Math Solver

© 2003-2021 Chegg Inc. All rights reserved.

You might also like