#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
class cGOODS{
private:
public:
int goodsId_, goodsCount_;
double goodsPrice_;
string goodsName_;
cGOODS() : goodsId_(1), goodsName_("Empty"), goodsPrice_(1), goodsCount_(1){}
cGOODS(int goodsId, string goodsName, double goodsPrice, int goodsCount) : goodsCount_(goodsCount), goodsId_(goodsId), goodsName_(goodsName), goodsPrice_(goodsPrice){}
//cGOODS::cGOODS(cGOODS &cGoods){ *this = cGoods; }
double getGoodsSum()
{
return goodsCount_ * goodsPrice_;
}
};
class UI{
protected:
template <typename T> static void InputValue(const char *message, T value){
cout << message; cin >> value;
}
static void Pause(){
cin.clear();
cin.sync();
cin.get();
}
static void OutputLine(){
cout << "\n******************************************************************\n";
}
static void GetTimeNow(string &DateToday, string &TimeNow){
char *temp = new char[4];
time_t t = time(NULL);
struct tm *t_m = localtime(&t);
_itoa(t_m->tm_mday, temp, 10);
DateToday = temp;
if (strlen(temp) < 2)
DateToday = "0" + DateToday;
DateToday += '.';
_itoa(t_m->tm_mon + 1, temp, 10);
if (strlen(temp) < 2)
DateToday += "0" + temp[0];
else DateToday += temp;
DateToday += '.';
_itoa(t_m->tm_year + 1900, temp, 10);
DateToday += temp;
*temp = NULL;
TimeNow = _itoa(t_m->tm_hour, temp, 10);
if (strlen(temp) < 2) TimeNow = "0" + TimeNow;
else TimeNow += temp;
*temp = NULL;
TimeNow += ':';
_itoa(t_m->tm_min, temp, 10);
if (strlen(temp) < 2)
TimeNow += ("0" + temp[0]);
else TimeNow += temp;
*temp = NULL;
TimeNow += ':';
_itoa(t_m->tm_sec, temp, 10);
if (strlen(temp) < 2)
TimeNow += ("0" + temp[0]);
else TimeNow += temp;
}
};
#pragma region "ХЗ"
class Check :UI
{
private:
vector<cGOODS> goods_;
int receiptId_;
string receiptDate_;
string receiptTime_;
void add(){
system("CLS");
cGOODS thisGoods;
OutputLine();
InputValue("\nID товара: ", thisGoods.goodsId_);
InputValue("Имя товара: ", thisGoods.goodsName_);
InputValue("Цена товара: ", thisGoods.goodsPrice_);
InputValue("Количество товара: ", thisGoods.goodsCount_);
OutputLine();
goods_.push_back(thisGoods);
Pause();
}
void del(){
system("CLS");
int Id = 0;
InputValue("Введите ID товара: ", Id);
if (searchGoodsById(Id) == -1) cout << "Данный товар не найден!" << endl;
else
{
deleteGoodsByIndex(searchGoodsById(Id));
cout << "Товар удалено!" << endl;
}
Pause();
}
void chn(){
system("CLS");
int Id = 0, count = 0;
string name;
double price = 0;
InputValue("Введите ID товара: ", Id);
int newId = 0;
if (searchGoodsById(Id) == -1) cout << "Данный товар не найден!" << endl;
else
{
OutputLine();
InputValue("\nID товара: ", newId);
InputValue("Имя товара: ", name);
InputValue("Цена товара: ", price);
InputValue("Количество товара: ", count);
OutputLine();
changeGoodsByIndex(searchGoodsById(Id), name, price, count, newId);
}
Pause();
system("CLS");
}
void prnt(){
system("CLS");
OutputLine();
cout << "ID чека: " << this->receiptId_ << endl << "Дата создания: " << this->receiptDate_ << endl << "Время создания: " << this->receiptTime_ << endl << "*************************" << endl;
for (int i(0); i < this->goods_.size(); i++) getGoodsInfoByIndex(i);
cout << "Сумма: " << getAllSum() << " рублей" << endl << "Спасибо за покупку !" << endl << "*************************" << endl;
Pause();
}
public:
Check() :receiptId_(1), receiptDate_("01.01.2015"), receiptTime_("00:00:00"){}
Check(int receiptId, string receiptDate, string receiptTime) : receiptId_(receiptId), receiptDate_(receiptDate), receiptTime_(receiptTime){}
Check(Check &receipt)
{
*this = receipt;
}
cGOODS operator[](int i)
{
return goods_[i];
}
void Menu()
{
GetTimeNow(receiptDate_, receiptTime_);
int command;
InputValue("Введите ID чека: ", receiptId_);
system("CLS");
while (true){
OutputLine();
cout << "ID чека: " << this->receiptId_ << endl << "Дата создания: " << this->receiptDate_ << endl << "Время создания: " << this->receiptTime_;
OutputLine();
cout << "Действия:" << endl;
cout << "1. Добавить товар в чек;" << endl;
cout << "2. Удалить товар из чека;" << endl;
cout << "3. Изменить товар в чеке" << endl;
cout << "4. Распечатать чек;" << endl;
cout << "5. Выход;" << endl;
cout << "Выбор: ";
cin >> command;
switch (command){
case 1:{ add(); break; }
case 2:{ del(); break; }
case 3:{ chn(); break; }
case 4:{ prnt(); break; }
case 5:{ return; }
default:
{
cout << "Ничего не выбрано, повторите попытку! " << endl;
Pause();
break;
}
}
}
}
int searchGoodsById(int goodsId)
{
if (this->goods_.size() < 2 && this->goods_[0].goodsId_ == goodsId) return 0;
if (this->goods_.size() < 2 && this->goods_[0].goodsId_ != goodsId) return -1;
int left = 0, right = this->goods_.size(), middle;
while (left <= right){
middle = left + (right - left) / 2;
if (goodsId < this->goods_[middle].goodsId_) right = middle - 1;
else if (goodsId > this->goods_[middle].goodsId_) left = middle + 1;
else return middle;
}
return -1;
}
void Check::deleteGoodsByIndex(int goodsIndex)
{
this->goods_.erase(this->goods_.begin() + goodsIndex);
}
void Check::addGoods(cGOODS goods)
{
this->goods_.push_back(goods);
}
void Check::changeGoodsByIndex(int goodsIndex, string goodsName, double goodsPrice, int goodsCount, int goodsId)
{
this->goods_[goodsIndex].goodsPrice_ = goodsPrice;
this->goods_[goodsIndex].goodsName_ = goodsName;
this->goods_[goodsIndex].goodsCount_ = goodsCount;
this->goods_[goodsIndex].goodsId_ = goodsId;
}
void Check::getGoodsInfoByIndex(int goodsIndex)
{
OutputLine();
cout << "ID товара: " << this->goods_[goodsIndex].goodsId_ << endl << "Имя товара: " << this->goods_[goodsIndex].goodsName_ << endl << "Цена единицы товара: " << this->goods_[goodsIndex].goodsPrice_ << endl << "Количество товара: " << this->goods_[goodsIndex].goodsCount_ << endl << "Сумма: " << this->goods_[goodsIndex].getGoodsSum() << endl;
OutputLine();
}
double Check::getAllSum()
{
double sum = 0;
for (int i(0); i < this->goods_.size(); i++)sum += goods_[i].getGoodsSum();
return sum;
}
};
#pragma endregion
int main()
{
setlocale(0, "");
Check rec;
rec.Menu();
return 0;
}