Need Help Converting Program C Python Started Working Python Able Understand Well Provided Q43817405
I need help in converting this program from C++ to Python. Ihave started working on it in Python, but I am not able tounderstand it that well. I have provided the instructions and aswell the C++ code I did. Thank you for your help, if possible!
You will be writing a (rather primitive) online storesimulator. It will have three classes: Product, Customer, andStore. All data members of each class should be marked as private,and the classes should have any get or set methods that will beneeded to access them.
Code:
import mystats # you don’t need “.py” here
some_li = [20, 38, 3, 43, 10, 74]
mean_val = mystats.find_mean(some_li)
print(mean_val)#include
#include”store.hpp”
using namespace std;
//Product ———-
Product::Product(string id, string t, string d, double p, intqa){
idCode = id;
title = t;
description = d;
price = p;
quantityAvailable = qa;
}
string Product::getIdCode(){
return idCode;
}
string Product::getTitle(){
return title;
}
int Product::getQuantityAvailable(){
return price;
}
void Product::decreaseQuantity(){
quantityAvailable–;
}
string Product::getDescription(){
return description;
}
double Product::getPrice(){
return price;
}
//Customer ————
Customer::Customer(string n, string a, bool pm){
name = n;
accountID = a;
premiumMember = pm;
}
void Customer::addProductToCart(string p){
cart.push_back(p);
}
void Customer::emptyCart(){
cart.clear();
}
string Customer::getAccountID(){
return accountID;
}
vector Customer::getCart(){
return cart;
}
bool Customer::isPremiumMember(){
return premiumMember;
}
//Store—————-
void Store::addMember(Customer* c){
members.push_back(c);
}
void Store::addProduct(Product* p){
inventory.push_back(p);
}
void Store::addProductToMemberCart(string pID, string mID){
//check product
Product *p = Store::getProductFromID(pID);
if(p == NULL){
cout<<“nProduct #”< return;
}
//check customer
Customer *c = Store::getMemberFromID(mID);
if(c == NULL){
cout<<“nMember #”< return;
}
//if available add to cart
if(p->getQuantityAvailable() > 0){
c->addProductToCart(pID);
}
else{
cout<<“Sorry, product #”< }
}
void Store::checkOutMember(string mID){
Customer *c = Store::getMemberFromID(mID);
if(c == NULL){
cout<<“Member #”< return;
}
//print cart
if(c->getCart().size() == 0){
cout<<“There are no items in thecart.”;
return;
}
cout<<“n”;
//generate cart
double stotal = 0, total = 0, ship = 0;
for(int i=0;igetCart().size();i++){
Product *p =Store::getProductFromID(c->getCart()[i]);
if(p->getQuantityAvailable() <= 0)
cout<<“Sorry, product#”<getIdCode()<<“, “<getTitle()<<“, is no longeravailable.”;
else{
stotal += p->getPrice();
cout<getTitle()<<” – $”<getPrice();
p->decreaseQuantity();
}
}
//check premium
if(c->isPremiumMember())
total = stotal;
else{
ship = (stotal * 0.07);
total = stotal + ship;
}
cout<<“nSubtotal: $”< cout<<“nShipping Cost:$”< cout<<“nTotal: $”<
}
void Store::productSearch(string str){
bool found = false;
for(int i=0;i
//first search for exact string in string
if (inventory[i]->getTitle().find(str) !=string::npos) {
found = true;
}
else if(inventory[i]->getDescription().find(str) != string::npos){
found = true;
}
//try toggling case of first alphabet
if(str[0] < ‘a’)
str[0] += 32;
else
str[0] -= 32;
//search again with new string
if (inventory[i]->getTitle().find(str) !=string::npos) {
found = true;
}
else if(inventory[i]->getDescription().find(str) != string::npos){
found = true;
}
if(found){
cout<<“n”<getTitle()<<“nIDcode:”<getIdCode()<<“nPrice:”<getPrice()<<“n”<getDescription();
found = false;
}
}
}
//iterate through members
Customer* Store::getMemberFromID(string str){
for(int i=0;i if(str ==members[i]->getAccountID()){
return members[i];
}
}
return NULL;
}
//iterate through inventory
Product* Store::getProductFromID(string str){
for(int i=0;i if(str ==inventory[i]->getIdCode()){
return inventory[i];
}
}
return NULL;
}
int main() {
//dummy products
Product p1(“11”, “cocoa”, “yummy”, 55, 3);
Product p2(“31”, “milk”, “umm yummy”, 23, 1);
//dummy customer
Customer c(“kush”, “2233”, false);
Store s;
s.addProduct(&p1);
s.addProduct(&p2);
s.addMember(&c);
s.addProductToMemberCart(“11”, “2233”);
s.addProductToMemberCart(“31”, “2233”);
s.productSearch(“Milk”);
s.checkOutMember(“2233”);
return 0;
}
Here are the descriptions of methods for the threeclasses:
Product:
A Product object represents a product with an ID code, title,description, price, and quantity available.
- init method – takes as parameters five values with which toinitialize the Product’s id_code, title, description, price, andquantity_available.
- get methods for each of the data members, named get_id_code,get_title, get_description, get_price, andget_quantity_available
- decrease_quantity – decreases the quantity available byone
Customer:
A Customer object represents a customer with a name and anaccount ID. Customers must be members of the Store to make apurchase. Premium members get free shipping.
- init method – takes as parameters three values with which toinitialize the Customer’s name, account_ID, and whether thecustomer is a premium_member.
- you decide how to represent a Customer’s cart
- get methods named get_name and get_account_ID
- is_premium_member – returns whether the customer is a premiummember
- add_product_to_cart – adds the product ID code to theCustomer’s cart
- empty_cart – empties the Customer’s cart
Store:
A Store object represents a store, which has some number ofproducts in its inventory and some number of customers asmembers.
- you decide how to represent a Store’sinventory and members
- init method – whatever initialization needs to be done for yourStore
- add_product – adds a product to the inventory
- add_member – adds a customer to the members
- get_product_from_ID – returns the Product with the matching ID.If no matching ID is found, it returns the special value None
- get_member_from_ID – returns the Customer with the matching ID.If no matching ID is found, it returns the special value None
- product_search – return a sorted list of ID codes for everyproduct whose title or description contains the search string. Thefirst letter of the search string should be case-insensitive, i.e.,a search for “wood” should match Products that have “Wood” in theirtitle or description, and a search for “Wood” should match Productsthat have “wood” in their title or description. You may assume thatthe search string will consist of a single word.
- add_product_to_member_cart – If the product isn’t found in theinventory, return “product ID not found.” If the product was found,but the member isn’t found in the members, return “member ID notfound.” If both are found, and the product is still available, callthe member’s addProductToCart method to add the product and thenreturn “product added to cart.” If the product was not stillavailable, return “product out of stock.” This function does notneed to check how many of that product are available – just thatthere is at least one. It should not change how many are available- that happens during checkout. The same product can be addedmultiple times if the customer wants more than one ofsomething.
- check_out_member – If the member ID isn’t found, raise anInvalidCheckoutError (you’ll need to define thisexception class). Otherwise, return the charge for the member’scart. This will be the total cost of all the items in the cart, notincluding any items that are not in the inventory or are out ofstock, plus the shipping cost. If a product is not out of stock,you should add its cost to the total and decrease the availablequantity of that product by 1. Note that an item can go out ofstock during checkout. For example, if the customer has two of thesame product in their cart, but the store only has one of thatproduct left, the customer will be able to buy the available one,but won’t be able to buy a second one, because it’s now out ofstock. For premium members, the shipping cost is $0. For normalmembers, the shipping cost is 7% of the total cost of the items inthe cart. When the charge for the member’s cart has been tabulated,the member’s cart should be emptied, and the charge amountreturned.
You must include a main function that runs if the file is run asa script, but not if the file is imported. The main function shouldtry to check out a member. If an InvalidCheckoutError is raised, anexplanatory message should be printed for the user (otherwise, thecheckout should proceed normally).
The file must be named Store.py
Expert Answer
Answer to I need help in converting this program from C++ to Python. I have started working on it in Python, but I am not able to …
OR