pusi kurac

mail@pastecode.io avatar
unknown
c_cpp
a year ago
5.6 kB
4
Indexable
Never
#include <iostream>
#include <fstream>
#include <string>


using namespace std;




string* splitString(string article){
     
    string arg;

    string::size_type pos = article.find("|");
    if(article.npos != pos) {
        arg = article.substr(pos + 1);
        article = article.substr(0, pos);
    }
    string *odvojeno = new string[2];
    odvojeno[0] = article;
    odvojeno[1] = arg;

    return odvojeno;
}

void readFile(){
    fstream myFile;
    myFile.open("C:\\Users\\Slaven\\Desktop\\cplus\\baza.txt", ios::in);
               
    if(myFile.is_open()){        
        string line;
        cout << "+----------------------------------------+\n|               ARTIKLI\n+----------------------------------------+\n";
        while(getline(myFile,line)){
            string *tekst = splitString(line);

            cout << "| " + tekst[0]  + " " + tekst[1] +" KM" << endl;
        }
        cout << "+----------------------------------------+\n";
        myFile.close();
    }
                    
}

void narudzba(){

    string porudzba[1000][2];
    fstream myFile;
    int i =0;
    myFile.open("C:\\Users\\Slaven\\Desktop\\cplus\\baza.txt", ios::in);
    cout<< "\nZa povratak nazad upisite x\n\nAko ste završili poručivanje upišite ok\n  Upisite naziv artikla koji zelite da porucite: \n  ";
    
    bool postojiPorudzba = false;
    float cijenaPoruzb = 0;

    while(true){

        string naziv;
 

         if(postojiPorudzba){
            cout << "\n+----------------------------------------+\n|                NARUDŽBA                |\n+----------------------------------------+";
            

            for(int j = 0;j <= i; j++){
                cout << "\n| " + porudzba[j][0] + " " + porudzba[j][1];
            }
            cout << "\n+----------------------------------------+\n";

        }
    
        
        getline(cin,naziv);
        
        

            if(naziv == "x"){
                return ;
        }     else if(naziv == "ok"){
                
        }

        bool postoji = false;
        string line;

        

        while(getline(myFile,line)){

            string *line2 = splitString(line);

            cout << "-----------\n";
            cout << naziv + " " + line2[0] + "\n";
            cout << naziv + " " + line2[1];
            cout << "\n-----------\n";
           
           if(naziv == line2[0]){
            

            porudzba[i][0] = line2[0];
            porudzba[i][1] = line2[1];
            i++;
            postoji = true;
            postojiPorudzba = true;

            cijenaPoruzb+= stof(line2[1]);
            cout << cijenaPoruzb;

            cout << "\n Artikal naručen\n";

            break;

            
            
           }
        }

        if(postoji == false){
            cout << "\n Artikal ne postoji, pokušajte ponovo\n";
    
        } 


    }
   

}

void deleteFile(){
    fstream myFile;
    myFile.open("C:\\Users\\Slaven\\Desktop\\cplus\\baza.txt", ios::in);

    if(myFile.is_open()){     

        string naziv;
        cout << "\nUnesite naziv artikla koji zelite da obrisete \nza povratak nazak upisite x : ";
        getline(cin,naziv);

        if(naziv == "x"){
            return ;
        }

        bool postoji = false;
        string line;
        while(getline(myFile,line)){
           if(naziv ==  splitString(line)[0]){
            postoji = true;
            cout << "\n Artikal obrisan\n";
           }
        }

        if(postoji == false){
            cout << "\n Artikal ne postoji, pokušajte ponovo\n";
        } 

        myFile.close();
            
        

    }
    deleteFile();
}


               
void writeFile(){
    fstream myFile;
    myFile.open("C:\\Users\\Slaven\\Desktop\\cplus\\baza.txt", ios::app);

    if(myFile.is_open()){

    string naziv;
    string cijena;    
    cout << "\nZa povratak nazak upisite x.\n";      
    cout << "\n  Unesite naziv artikla: "; 
    getline(cin,naziv);

     if(naziv == "x"){
            return ;
        } else if(naziv.empty()){
            cout << "\n+---------------------------------+\n| Pogrešan unos! pokušajte ponovo |\n+---------------------------------+\n";
            writeFile();
            return ;
        }

    cout << "\n  Unesite cijenu artikla: "; 
    getline(cin,cijena);

     if(cijena == "x"){
            return ;
        } else if(cijena.empty()){
            cout << "\nPogrešan unos! pokušajte ponovo\n";
            writeFile();
            return ;
        }
    string nazivSaCijenom = "\n" + naziv + "|" + cijena; 
    myFile << nazivSaCijenom;                
    }
    myFile.close();
}


int main()
{



    while(true){
        cout << "\n +----------------------------+ \n | Unesite redni broj komande | \n +----------------------------+ \n | 1. Prikaz svih artikala    | \n +----------------------------+ \n | 2. Unos novog artikla      | \n +----------------------------+ \n | 3. Brisanje artikla        | \n +----------------------------+ \n | 4. Narudzba                | \n +----------------------------+ \n";
        string input1;
       getline(cin,input1);

        int input = stoi(input1);

        if(input == 1){
            readFile();

        }
        if(input == 2){
            writeFile();
        }
        if(input == 3){
            deleteFile();

        }
        if(input == 4){
            narudzba();

        } 
    
    }
}