This is my second day into C++ and I wanted to get a small review from someone experienced with the language. I'm coming from Python so some stuff seems kind of weird. My plan is to build a little "web store" with this.
#include <string>
#include <vector>
#include <iostream>
#include <initializer_list>
enum class ItemCategory{
FOOD,
CLOTHES
};
class Item{
private:
int id;
std::string item_name;
double item_price;
bool item_available;
int stock;
ItemCategory item_category;
public:
static int id_ref;
Item(){}
Item(std::string _name, double _price, ItemCategory _category);
Item(std::string _name, double _price, int _stock, ItemCategory _category);
Item& operator=(const Item& i);
//Getters
const int getId(){return id;}
const std::string getName(){return item_name;}
const double getPrice(){return item_price;}
const bool getAvailable(){return item_available;}
const int getStock(){return stock;}
const ItemCategory getCategory(){return item_category;}
const std::string getCategoryString();
//Setters
void setName(std::string n){item_name = n;}
void setPrice(double p){item_price = p;}
void setAvaiable(bool b){item_available = b;}
void setStock(int s){stock = s;}
void setCategory(ItemCategory ic){item_category = ic;}
};
//Copy operator
Item& Item::operator=(const Item& i){
return *this;
}
//Keep track of all Items created to index them.
int Item::id_ref = 0;
const std::string Item::getCategoryString(){
switch(item_category){
case (ItemCategory::FOOD):
return "Food";
case (ItemCategory::CLOTHES):
return "Clothes";
}
}
Item::Item(std::string _name, double _price, ItemCategory _category):
id{Item::id_ref++}, item_name{_name}, item_price{_price},
item_available{true}, stock{1}, item_category{_category}{}
Item::Item(std::string _name, double _price, int _stock, ItemCategory _category):
id{Item::id_ref++}, item_name{_name}, item_price{_price},
item_available{true}, stock{_stock}, item_category{_category}{}
class Cart{
private:
std::vector<Item> customer_cart;
public:
Cart(){}//Empty cart.
Cart(std::initializer_list<Item> lst):customer_cart{lst}{}
void push(Item i){customer_cart.push_back(i);}
void printAllItems();
//TODO: add range checking to subscription
Item& operator[](int i){return customer_cart[i];}
};
void Cart::printAllItems(){
std::cout << "Id Name Category Price Stock" << std::endl;
for(auto i:customer_cart){
std::cout << i.getId() << " " << i.getName() << " " << i.getCategoryString() << " " << i.getPrice() << " " << i.getStock() << std::endl;
}
}
class Customer{
private:
int id;
std::string customer_name;
std::string customer_mail;
std::string customer_password; //authentication
Cart cart;
public:
static int id_ref;
Customer(std::string _name, std::string _mail, std::string _password);
Customer(std::string _name, std::string _mail, std::string _password,
std::initializer_list<Item> lst);
//Getters
const std::string getName(){return customer_name;}
const std::string getMail(){return customer_mail;}
const std::string getPassword(){return customer_password;}
const Cart& getCart(){return cart;}
//Setters
void setName(std::string n){customer_name = n;}
void setMail(std::string m){customer_mail = m;}
void setPassword(std::string p){customer_password = p;}
//Utility
void showAllItems(){cart.printAllItems();}
};
int Customer::id_ref = 0;
Customer::Customer(std::string _name, std::string _mail, std::string _password):
id{Item::id_ref++}, customer_name{_name}, customer_mail{_mail}, customer_password{_password},
cart(){}
Customer::Customer(std::string _name, std::string _mail, std::string _password,
std::initializer_list<Item> lst):
id{Item::id_ref++}, customer_name{_name}, customer_mail{_mail}, customer_password{_password},
cart(lst){}
This is how I would use it:
#include "sales.h"
#include <iostream>
using namespace std;
int main(){
Item i1("apple", 20, ItemCategory::FOOD);
Item i2("shirt", 45, ItemCategory::CLOTHES);
Customer c("randomName", "really_cool@gmail.com", "whatever");
cout << c.getName() << endl;
Cart cart = c.getCart();
cart.push(i1);
cart.printAllItems();
cout << endl;
//Constructor with init_list
Customer b("anotherName", "cool_email@gmail.com", "testpass", {i1, i2});
b.showAllItems();
}