Viktoria_Lab9

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.4 kB
1
Indexable
Never
#include <iostream>
#include <cstring>

using namespace std;

const int StrLen = 250;

class CarOwner
{
    private:
    char FirstName[20], SecondName[20];
    char Country[20];    
    int PhoneNumber, CarID;
    char CarBrand[10];
  
    public:
    char *GetFirstName() { return FirstName; }
    char *GetSecondName() { return SecondName; }
    char *GetCountry() { return Country; }
    char *GetCarBrand() { return CarBrand; }
    
    int GetPhoneNumber(){ return PhoneNumber; }
    int GetCarID(){ return CarID; }
    
    void SetFirstName(char *firstName) { strcpy(FirstName, firstName);}
    void SetSecondName(char *secondName) { strcpy(SecondName, secondName);}
    void SetCountry(char *country) { strcpy(Country, country);}
    void SetCarBrand(char *carBrand) { strcpy(CarBrand, carBrand);}
    void SetCarID(int carID) { CarID = carID; }
    void SetPhoneNumber(int phoneNumber) { PhoneNumber = phoneNumber; }
};

istream &operator>>(istream &stream, CarOwner &obj)
{
    char str[StrLen];
    int phoneNumber, carID;
    
    cout << "\nEnter First name: ";
    cin.getline(str, StrLen);
    obj.SetFirstName(str);
    
    cout << "\nEnter Second name: ";
    cin.getline(str, StrLen);
    obj.SetSecondName(str);
    
    //cin.ignore();
    cout << "\nEnter phone number: ";
    cin >> phoneNumber;
    obj.SetPhoneNumber(phoneNumber);
    
    cin.ignore();
    cout << "\nEnter country: ";
    cin.getline(str, StrLen);
    obj.SetCountry(str);
    
    cout << "\nEnter car brand: ";
    cin.getline(str, StrLen);
    obj.SetCarBrand(str);
    
    
    cout << "\nEnter car ID: ";
    cin >> carID;
    obj.SetCarID(carID);
    cin.ignore();
    return stream;
}
ostream &operator<<(ostream &stream, CarOwner &obj)
{
    stream 
    << " " << obj.GetFirstName()
    << " " << obj.GetSecondName()
    << " " << obj.GetPhoneNumber()
    << " " << obj.GetCountry()
    << " " << obj.GetCarBrand()
    << " " << obj.GetCarID();
    
    return stream;
}

void InitArray(CarOwner*, int);
void DisplayArray(CarOwner*, int);
void DisplayChoise(CarOwner*, int, char*, char*);

int main()
{
    int length;
    char secondName[20], carBrand[10];
    
    
    cout << "\nEnter car owners count: ";
    cin >> length;
    
    CarOwner *carOwners = new CarOwner[length];
    InitArray(carOwners, length);
    
    cout << "\nThe list of carOwners:\n";
    DisplayArray(carOwners, length);
    
    
    cout << "\nEnter second name: ";
    cin.getline(secondName, StrLen);
    
    cout << "\nEnter car brand: ";
    cin.getline(carBrand, StrLen);

    
    DisplayChoise(carOwners, length, secondName, carBrand);
    
    delete []carOwners;
    return 0;
}

void InitArray(CarOwner *carOwners, int len)
{
    cin.ignore();
    for(int i = 0; i < len; i++)
    {
        cout << flush << "\nEnter the information about " << i + 1 << "-th " << "car owner";
        cin >> carOwners[i];
    }
}
void DisplayArray(CarOwner *carOwners, int len)
{
    for(int i = 0; i < len; i++)
    {
        cout << carOwners[i] << endl;
    }
}
void DisplayChoise(CarOwner *carOwners, int len, char *secondName, char *carBrand)
{
    for(int i = 0; i < len; i++)
    {
        CarOwner carOwner = carOwners[i];
        if(strcmp(carOwner.GetSecondName(), secondName) == 0 && strcmp(carOwner.GetCarBrand(), carBrand) == 0)
            cout << carOwner << endl;
    }
}