metalni

OOP Labs 3 Avtomobil

May 30th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. //class Date
  6. class Date{
  7.     private:
  8.         int year;
  9.         int month;
  10.         int day;
  11.     public:
  12.         Date(){}
  13.         Date(int god, int mesec, int den);
  14.         Date(const Date &orig);
  15.         void print();
  16. };
  17.  
  18. Date::Date(int god, int mesec, int den){
  19.     this->year=god;
  20.     this->month=mesec;
  21.     this->day=den;
  22. }
  23. Date::Date(const Date &orig){
  24.     this->year=orig.year;
  25.     this->month=orig.month;
  26.     this->day=orig.day;
  27. }
  28. void Date::print(){
  29.     cout << this->year <<"." << this->month << "." << this->day << endl;
  30. }
  31. //class Person
  32. class Person {
  33.     private:
  34.         char name[20];
  35.         char surname[20];
  36.     public:
  37.         Person();
  38.         Person(const char * ime, const char * prezime);
  39.         void print();
  40. };
  41.  
  42. Person::Person(){
  43.     strcpy(this->name,"not specified");
  44.     strcpy(this->surname, "not specified");
  45. }
  46. Person::Person(const char * ime, const char * prezime){
  47.     strcpy(this->name, ime);
  48.     strcpy(this->surname, prezime);
  49. }
  50. void Person::print(){
  51.     cout << this->name <<" " << this->surname << endl;
  52. }
  53.  
  54. //class Car
  55. class Car{
  56.     private:
  57.         Person owner;
  58.         Date dateOfPurchase;
  59.         float price;
  60.     public:
  61.         Car();
  62.         Car(const Person lik, const Date date, float cena);
  63.         void print();
  64.         float getPrice();
  65. };
  66. Car::Car(){
  67.     this->price=0.0;
  68. }
  69. Car::Car(const Person lik, const Date date, float cena){
  70.     this->owner=lik;
  71.     this->dateOfPurchase=date;
  72.     this->price=cena;
  73. }
  74. void Car::print(){
  75.     owner.print();
  76.     dateOfPurchase.print();
  77.     cout << "Price: " << this->price << endl;
  78. }
  79. float Car::getPrice(){
  80.     return this->price;
  81. }
  82.  
  83. //cheaperThan
  84. void cheaperThan(Car * cars, int numCars, float price){
  85.     for(int i=0; i<numCars; i++){
  86.         if(cars[i].getPrice() < price)
  87.             cars[i].print();
  88.     }
  89. }
  90.  
  91. //main
  92. int main() {
  93.     char name[20];
  94.     char lastName[20];
  95.     int year;
  96.     int month;
  97.     int day;
  98.     float price;
  99.  
  100.     int testCase;
  101.     cin >> testCase;
  102.  
  103.     if (testCase == 1) {
  104.         cin >> name;
  105.         cin >> lastName;
  106.         Person lik(name, lastName);
  107.  
  108.         cin >> year;
  109.         cin >> month;
  110.         cin >> day;
  111.         Date date(year, month, day);
  112.  
  113.         cin >> price;
  114.         Car car(lik, date,  price);
  115.  
  116.         car.print();
  117.     }
  118.     else if (testCase == 2) {
  119.         cin >> name;
  120.         cin >> lastName;
  121.         Person lik(name, lastName);
  122.  
  123.         cin >> year;
  124.         cin >> month;
  125.         cin >> day;
  126.         Date date(Date(year, month, day));
  127.  
  128.         cin >> price;
  129.         Car car(lik, date,  price);
  130.         car.print();
  131.     }
  132.     else {
  133.         int numCars;
  134.         cin >> numCars;
  135.  
  136.         Car cars[10];
  137.         for (int i = 0; i < numCars; i++) {
  138.             cin >> name;
  139.             cin >> lastName;
  140.             Person lik(name, lastName);
  141.  
  142.             cin >> year;
  143.             cin >> month;
  144.             cin >> day;
  145.             Date date(year, month, day);
  146.  
  147.             cin >> price;
  148.             cars[i] = Car(lik, date,  price);
  149.         }
  150.         float priceLimit;
  151.         cin >> priceLimit;
  152.         cheaperThan(cars, numCars, priceLimit);
  153.     }
  154.  
  155.  
  156.     return 0;
  157. }
Add Comment
Please, Sign In to add comment