Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <stdlib.h>
- #include <map>
- #include <unistd.h>
- #define SPACE 4
- using namespace std;
- //REFERENCES
- //it = myMap.find(3);
- //map1.find( 10 );
- //bool empty() const; true if the container size is 0, false otherwise.
- void Space(void){
- for(int i=0;i<SPACE;i++)
- cout<<" ";
- }
- void space(void){
- for(int i=0;i<SPACE-1;i++)
- cout<<" ";
- }
- class Commodity {
- private :
- int id_;
- std::string name_;
- int price_;
- public :
- Commodity() {
- id_ = 0;
- name_ = std::string("");
- price_ = 0;
- }
- Commodity(int id, std::string name, int price) {
- id_ = id;
- name_ = name ;
- price_ = price;
- }
- int printId(void){
- return id_;
- }
- std::string printName(void){
- return name_;
- }
- int printPrice(void){
- return price_;
- }
- // ~Commodity(){}
- };
- class Pos {
- private:
- std::map<int, int> sList; //save code and amount
- std::map<int, Commodity> List;
- int tSum, tAmt;
- public:
- Pos() {
- std::map<int, int> sList;
- std::map<int, Commodity> List;
- int tSum, tAmt;
- tSum=tAmt=0;
- }
- void addsList(int ID, int amount){
- map< int, int >::iterator itersList;
- itersList=sList.find(ID);
- if(itersList!=sList.end()){
- itersList->second=itersList->second+amount;
- }
- else{
- sList[ID]=amount;
- }
- }
- void addList(Commodity co){
- int Id=co.printId();
- List[Id] = co;
- }
- void printsList(void){
- map< int, int >::iterator itersList;
- map< int, Commodity >::iterator iterList;
- int itemSum;
- cout.fill(' ');
- cout<<endl<<"===================================================================="<<endl;
- cout<<"|| Product ID\t";cout.width(25);cout<<left<<"Name"; cout.width(10);cout<<left<<"Price";
- cout.width(10);cout<<left<<"Amount";cout.width(5);cout<<left<<"Sum";cout<<"||";
- cout<<endl<<"|| -------------------------------------------------------------- ||"<<endl;
- tSum=tAmt=0;
- for(itersList = sList.begin(); itersList != sList.end(); ++itersList){
- itemSum=0;
- //initialize for each loop(each item)
- iterList=List.find(itersList->first);
- Commodity co = iterList->second;
- itemSum = co.printPrice() * itersList->second;
- //look ID from sellingList for CommodityList, and get its price.
- tAmt=tAmt+itersList->second;
- tSum=tSum+itemSum;
- cout<<"|| "<<co.printId()<<"\t";
- cout.width(25); cout<<left<<co.printName();
- cout.width(10); cout<<left<<co.printPrice();
- cout.width(10); cout<<left<<itersList->second;
- cout.width(5); cout<<left<<itemSum<<"||"<<endl;
- }
- cout<<"--------------------------------------------------------------------"<<endl;
- cout.width(51);cout<<"|| Total";
- cout.width(10);cout<<left<<tAmt;
- cout.width(5);cout<<left<<tSum;
- cout<<"||"<<endl;
- cout<<"===================================================================="<<endl;
- return ;
- }
- void saveFile(std::string filename) {
- std::ofstream output( filename.c_str(), std::ios::app);
- // output<<id_<<" "<<name_<<" "<<price_<<std::endl;
- map< int, int >::iterator itersList;
- map< int, Commodity >::iterator iterList;
- int itemSum;
- tSum=tAmt=0;
- output<<"ID"<<" "<<"Name"<<" "<<"Price"<<" "<<"Amount"<<" "<<"Total"<<std::endl;
- for(itersList = sList.begin(); itersList != sList.end(); ++itersList){
- itemSum=0;
- //initialize for each loop(each item)
- iterList=List.find(itersList->first);
- Commodity co = iterList->second;
- itemSum = co.printPrice() * itersList->second;
- //look ID from sellingList for CommodityList, and get its price.
- tAmt=tAmt+itersList->second;
- tSum=tSum+itemSum;
- output<<co.printId()<<" "<<co.printName()<<" "<<co.printPrice()<<" "<<itersList->second<<" "<<itemSum<<std::endl;
- }
- output<<tAmt<<" "<<tSum<<std::endl;
- output.close();
- return ;
- }
- void printList(void){
- map< int, Commodity >::iterator iterList;
- cout.fill(' ');
- cout<<endl<<"================================================="<<endl;
- cout<<"|| Product ID\t"; cout.width(25);cout<<left<<"Name"; cout.width(5);cout<<left<<"Price"<< " ||"<<endl;
- cout<<endl<<"|| ------------------------------------------- ||"<<endl;
- for(iterList = List.begin(); iterList != List.end(); ++iterList){
- Commodity co = iterList->second;
- cout<<"|| "<<co.printId()<<"\t";
- cout.width(25); cout<<left<<co.printName();
- cout.width(5); cout<<co.printPrice()<<" ||"<<endl;
- }
- cout<<"================================================="<<endl;
- }
- void Init(void){
- sList.clear();
- tSum=0;
- tAmt=0;
- }
- int chkEmpty(void){
- return sList.empty();
- }
- int chkID(int id){
- map< int, Commodity >::iterator iterList;
- iterList=List.find(id);
- if(iterList!=List.end()){
- return 1;}
- else
- {return 0;}
- }
- int chksID(int id){
- map< int, int >::iterator itersList;
- itersList=sList.find(id);
- if(itersList!=sList.end()){
- return 1;}
- else
- {return 0;}
- }
- string id2Product(int id){
- map< int, Commodity >::iterator iterList;
- iterList=List.find(id);
- Commodity co = iterList->second;
- return co.printName();
- }
- int chkAmt(int id){
- map< int, int >::iterator itersList;
- itersList=sList.find(id);
- return itersList->second;
- }
- void eraseID(int id){
- sList.erase(id);
- }
- int printTSum(void){
- return tSum;
- }
- };
- int main()
- {
- int co_id, co_price;
- char yn;
- std::string co_name;
- int input, id, amt, chk; amt = chk = id = 1;
- Pos pos = Pos();
- //Declaration
- std::ifstream commodity("commodity.txt");
- while( commodity>>co_id>>co_name>>co_price) {
- Commodity co( co_id, co_name, co_price);
- pos.addList(co);
- }
- //Initialization
- system("clear");
- pos.printList();
- while(1){
- chk=0;
- {
- cout<<"Please input 1 to choose, ";
- if(!pos.chkEmpty())cout<<"2 to buy, 3 to cancel, 4 to cancel partly, ";
- cout<<"and 5 to terminate POS"<<endl;
- }
- cin.clear(); cin>>input;
- if(input>5 || input<1){
- system("clear");
- pos.printList();
- if(!pos.chkEmpty())pos.printsList();
- cout<<"Please input integer in 1~5!"<<endl;
- }
- else{
- if((input==2 || input==3 || input==4)&&pos.chkEmpty()){
- system("clear");
- pos.printList();
- }
- else if(input==2){
- system("clear");
- pos.printList();
- pos.printsList();
- cout<<"(to go back, input 0)Input the amount of cash: ";
- while(chk==0){
- cin.clear(); cin>>amt;
- if(amt==0){
- break;
- }
- else if(amt<0){
- system("clear");
- pos.printsList();
- cout<<"(to go back, input 0)Please input valid amount!";}
- else if(amt>0 && amt<pos.printTSum()){
- system("clear");
- pos.printsList();
- cout<<"(to go back, input 0)You have lack of money, you need at least "<<pos.printTSum()-amt<<" of more money";
- }
- else{
- system("clear");
- pos.printsList();
- cout.width(60); cout<<right<<"Your charge will be: "<<amt-pos.printTSum()<<endl;
- cout<<"Are you going to purchase those above?";
- cin.clear(); cin>>yn;
- while(1){
- if(toupper(yn)!='Y'&&toupper(yn)!='N'){}
- else if(toupper(yn)=='Y'){
- system("clear");
- pos.printsList();
- cout<<"Recieved "<<amt<<" of money, and Here is your charge of: "<<amt-pos.printTSum()<<endl<<endl;
- pos.saveFile("History.txt");
- cout<<"Purchase Successfully Recorded"<<endl;
- cout<<"Input any number to proceed...";
- cin.clear(); cin>>input;
- pos.Init();
- system("clear");
- pos.printList();
- chk=1;
- break;
- }
- else if(toupper(yn)=='N'){
- chk=1;
- break;
- }
- }
- }
- }
- }
- else if(input==5){
- system("clear");
- cout<<"Terminating POS..."<<endl<<endl;
- return 0;
- }
- else if(input==4){
- system("clear");
- pos.printList();
- pos.printsList();
- cout<<"The one going to be canceled: ";
- while(chk==0){
- cin.clear(); cin>>id;
- if(!pos.chksID(id) && id!=0){
- system("clear");
- pos.printList();
- pos.printsList();
- cout<<"(to go back, input 0)Please input valid proudct ID!";
- }
- else if(id==0){
- system("clear");
- pos.printList();
- pos.printsList();
- break;
- }
- else if(pos.chksID(id)){
- cout<<"(to go back, input 0)How many (much) would be canceled? ";
- while(1){
- cin.clear(); cin>>amt;
- if(amt==0){
- system("clear");
- pos.printList();
- pos.printsList();
- cout<<"(to go back, input 0)The one going to be canceled: ";
- break;
- }
- if(amt<0){
- system("clear");
- pos.printList();
- pos.printsList();
- cout<<"(to go back, input 0)Please input valid amount!";
- }
- if(pos.chkAmt(id)<amt){
- amt=pos.chkAmt(id); //remove all of them if it exceeds max amount
- }
- while(1){
- system("clear");
- cout<<"<Order of Now>";
- pos.printsList();
- if(amt==pos.chkAmt(id)){
- pos.eraseID(id);
- }
- else{
- pos.addsList(id,-amt);
- }
- cout<<endl<<endl<<"<After Changed>";
- pos.printsList();
- cout<<"Are you sure to cancel "<<amt<<" of "<<pos.id2Product(id)<<"? (y/n)";
- cin.clear(); cin>>yn;
- if(toupper(yn)!='Y' && toupper(yn)!='N'){}
- else if(toupper(yn)=='Y'){
- system("clear");
- pos.printList();
- if(!pos.chkEmpty())pos.printsList();
- if(!pos.chksID(id)){
- cout<<"All of "<<pos.id2Product(id)<<" is (are) removed"<<endl;
- }
- else{
- cout<<amt<<" of "<<pos.id2Product(id)<<" is (are) removed"<<endl;
- }
- chk=1;
- break;
- }
- else if(toupper(yn)=='N'){
- system("clear");
- pos.addsList(id,amt);
- pos.printList();
- pos.printsList();
- chk=1;
- break;
- }
- }
- if(amt>0){
- break;
- }
- }
- }
- }
- }
- else if(input==3){
- while(chk == 0){
- system("clear");
- pos.printsList();
- cout<<"Are you sure to cancel this order? (y/n)";
- cin.clear(); cin>>yn;
- if(yn!='Y'&&yn!='y'&&yn!='N'&&yn!='n'){}
- else if(toupper(yn) == 'Y'){
- pos.Init();
- system("clear");
- pos.printList();
- cout<<"Cancel the order"<<endl;
- chk=1;
- }
- else if(toupper(yn) == 'N'){
- system("clear");
- pos.printList();
- pos.printsList();
- chk=1;
- }
- }
- }
- else if(input==1){
- system("clear");
- pos.printList();
- if(!pos.chkEmpty())pos.printsList();
- cout<<"(to go back, input 0)Input Product ID: ";
- while(1){
- cin.clear(); cin>>id;
- if(id==0){
- system("clear");
- pos.printList();
- if(!pos.chkEmpty())pos.printsList();
- break;}
- if(pos.chkID(id)==0){
- cout<<"(to go back, input 0)Please input valid prouduct ID!"<<endl;
- }
- else{
- cout<<"(to go back, input 0)How many(much) are you going to buy "<<pos.id2Product(id)<<"?";
- while(1){
- cin.clear(); cin>>amt;
- if(amt<0){
- cout<<"(to go back, input 0)Please input valid amount!"<<endl;}
- else if(amt>0){
- pos.addsList(id,amt);
- system("clear");
- pos.printList();
- pos.printsList();
- cout<<amt<<" of "<<pos.id2Product(id)<<" is registered"<<endl;
- // usleep(1000);
- break;
- }
- else if(amt==0){
- system("clear");
- pos.printList();
- if(!pos.chkEmpty())pos.printsList();
- cout<<"(to go back, input 0)Input Product ID: ";
- break;
- }
- }
- if(amt>0) break;
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement