Saturday 10 May 2014

Receipt issuing Application in plain C++(console);

/*
Author:   Gideon Asare
Product:  Shop receipt prog
Language: c-pluplus
Contact:  kofiowusu847@gmail.com
blog:     http://quickerprogramming.blogspot.com

*/
/*---------------------------------------------------------------------------------------------------------------*/

#include <iostream>     //this file is responsible for input and output of data
#include <ctime>            // it contains time elements */
#include <fstream>         //  fstream means file stream and its a library use to write to files in c++. it helps create files */

using namespace std;         // the namespace std preprocessors enables the program to use c++ standard   library


class timeteller{

public:
    void screenprompts();
    void date_and_time();
};

void timeteller::date_and_time(){
time_t currentime = time(0);
char* dt =ctime(&currentime);
cout << dt << endl;
}
void timeteller::screenprompts(){
cout << "Use the Tap key to space" << endl;
 cout << "Press CTR Z to quit " << endl;
 cout <<"you can tap twice if not align for the first entry only\n" << endl;
}

static  string product;                          //static variables are always outside any function or class
static double amount;
static unsigned int quantity;


class sales{
private:
                                                            // variable that are accessed only by the sales class
    string Productname;
    string numbersold;
    string currency;
    string total;
public:
                                                        //prototypes of the functions for the sales class
    sales(timeteller obj);
    void head(string Pro,string Qt, string cu,string tot);
    void  main_heading();
    double productprice();
    string getpro();
    string getcurrency();
    string getnumbersold();
    string gettotal();
};
    sales::sales(timeteller obj){ // passing the timeller class object to sales constructor;
        obj.screenprompts();       // using the object to access the functions of timeteller

    }

    //
    void sales::head(string Pro,string Qt, string cu,string tot){
    Productname = Pro, numbersold = Qt, currency = cu, total = tot;
    ofstream thefile("receipt.txt");
    thefile << Productname <<"\t" << numbersold << "\t" <<currency <<"\t" << total << endl;
    }

    //these similar functions returns the variables
    string sales::getpro(){
    return Productname; // return the variable; Productname;
    }

    string sales::getnumbersold(){
        return numbersold;// return the variable numbersold;
    }

    string sales::getcurrency(){
        return currency;
    }

    string sales::gettotal(){
        return total;
    }


    // the function sales is basically doing the calculations

    double sales::productprice(){                                                               /*  */
    ofstream thefile("receipt.txt");
    string header = " SUBA SHOPPING CENTRE\n P.O.BOX 40\n AMERICAN HOUSE\n EAST LEGON ACCRA\n ";
    thefile << header;

    cout << header << endl;

    cout << "----------------------------------" << endl;       // horizontal line to the user
    cout << "ProName    ""  Quantity""  Amount"<<endl;
    thefile<< "----------------------------------\n";         // horizontal  line to the file
    double finaltotal = 0 ;
                                                                // this is the grand total variable
    while(cin>> product >> quantity >> amount){

        double subtotal = 0;                                // the subtotal variable and initialized to 0;

        subtotal = quantity * amount ;                      //
        finaltotal += subtotal;                              // the sum of subtotal is grand total

        /* this line writes to thefile created that txt file is name receipt.txt*/
        thefile << product <<"\t"<< quantity <<"\t"<< amount<<"\t"<< subtotal<< endl;
    }
                                                            // formatting on the txt file.
     thefile << "\t\t\t\tGrand Total" << endl;
     thefile << "\t\t\t\tGhc" <<  finaltotal;
    }

/* Execution of the functions at main*/
/* standard c++ simplifies the main function by not explicitly using argv[] argc parameters*/
int main()
{
     timeteller tim;
    sales on(tim);
    tim.date_and_time();
    on.head("Prod","Qty","Amt","Total");
    on.productprice();

    return 0;
}