Advertisement
asrori

mobilClass

Mar 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. //
  2. // Created by asrori on 06/03/18.
  3. //
  4.  
  5. #ifndef PRAKTIKUM2_MOBIL_H
  6. #define PRAKTIKUM2_MOBIL_H
  7.  
  8. #include <iostream>
  9.  
  10. class Mobil{
  11.     private:
  12.         int kecepatan;
  13.         int bensin;
  14.  
  15.     public:
  16.         Mobil(){
  17.             this->bensin = 100;
  18.             this->kecepatan = 0;
  19.         }
  20.  
  21.         bool berkendara(int kecepatan){
  22.             bool adaBensin = cekBensin(kecepatan);
  23.  
  24.             if (adaBensin) {
  25.                 (kecepatan < 50) ? this->bensin = this->bensin - 20 : this->bensin = this->bensin - 40;
  26.                 return true;
  27.             } else {
  28.                 return false;
  29.             }
  30.         }
  31.  
  32.         bool cekBensin(int kecepatan){
  33.             if (kecepatan < 50)
  34.                 if ((this->bensin - 20) < 0) return false;
  35.             if (kecepatan >= 50)
  36.                 if ((this->bensin - 40) < 0) return false;
  37.  
  38.             return true;
  39.         }
  40.  
  41.         bool bensinPenuh(){
  42.             if (this->bensin == 100) {
  43.                 return true;
  44.             } else {
  45.                 this->bensin = this->bensin + 20;
  46.                 return false;
  47.             };
  48.         }
  49.  
  50.     int getKecepatan() const {
  51.         return kecepatan;
  52.     }
  53.  
  54.     int getBensin() const {
  55.         return bensin;
  56.     }
  57. };
  58.  
  59. #endif //PRAKTIKUM2_MOBIL_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement