Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1.  
  2. public class Car {
  3.     // instance variables
  4.     private String carMake, carModel, carColor, carSetOwner, currentOwner, hasCatalytic;
  5.     private int carYearModel, carNumOfDoors, carCylinderCapacity, carMilesPerGallon, carInitialPrice, carPurchasePrice, noOfOwner;
  6.     private boolean carHasCatalytic;
  7.    
  8.     /**
  9.      * Constructor for objects of class Car
  10.      */
  11.     public Car(String make, String model, int yearModel, String color, int numOfDoors, int cylinderCapacity,  int milesPerGallon, int initialPrice, boolean hasCatalytic) {
  12.         // initialise instance variables
  13.         carMake = make;
  14.         carModel = model;
  15.         carYearModel = yearModel;
  16.         carColor = color;
  17.         noOfOwner = 0;
  18.         if (numOfDoors > 1) {
  19.             carNumOfDoors = numOfDoors;
  20.         } else {
  21.             System.out.println("Invalid number of doors!");
  22.         }
  23.         if (cylinderCapacity > 0) {
  24.             carCylinderCapacity = cylinderCapacity;
  25.         } else {
  26.             System.out.println("Invalid amount for cylinder capacity!");
  27.         }
  28.         if (milesPerGallon > 0) {
  29.             carMilesPerGallon = milesPerGallon;
  30.         } else {
  31.             System.out.println("Invalid amount for mile per gallon!");
  32.         }
  33.         if (initialPrice >= 0) {
  34.             carInitialPrice = initialPrice;
  35.         } else {
  36.             System.out.println("Initial price cannot be less than 0!");
  37.         }
  38.         carHasCatalytic = hasCatalytic;
  39.         checkCarConverter();
  40.     }
  41.    
  42.     //an accessor method that returns a string (Car's make)
  43.     public String getMake() {
  44.         return carMake;
  45.     }
  46.    
  47.     //an accessor method that returns a string (Car's model)
  48.     public String getModel() {
  49.         return carModel;
  50.     }
  51.    
  52.     /**
  53.      * A void method that opens a message box containing car's details depending on whether it has been ever been sold or not.
  54.      * It also has an if then else statement which is used to translate boolean's value into a string to be used in the messge box
  55.      */
  56.     public void printDetails() {
  57.         if (carHasCatalytic == true) {
  58.             hasCatalytic = "does";
  59.         } else {
  60.             hasCatalytic = "does not";
  61.         }
  62.        
  63.         if (noOfOwner == 0) {
  64.             System.out.println("This vehicle is a " + carColor + " " + carCylinderCapacity + "cc " + carMake + " " + carModel + " " + carYearModel + " model with " + carNumOfDoors +
  65.             " doors. It has not been sold once yet. It does " + carMilesPerGallon + " miles per gallon and " + hasCatalytic + " have a catalytic converter. The original purchase price is £" + carInitialPrice
  66.             + ".");
  67.         } else if(noOfOwner > 0) {
  68.         System.out.println("This vehicle is a " + carColor + " " + carCylinderCapacity + "cc " + carMake + " " + carModel + " " + carYearModel + " model with " + carNumOfDoors +
  69.             " doors. It has had " + noOfOwner + " owner(s) and its present owner is " + currentOwner
  70.             + ". It does " + carMilesPerGallon + " miles per gallon and " + hasCatalytic + " have a catalytic converter. The original purchase price was £" + carInitialPrice
  71.             + " and the price is now £" + carPurchasePrice + ".");
  72.         }
  73.     }
  74.    
  75.     //a mutator method used to set the price of the car prior to selling it
  76.     public void setPurchasePrice(int purchasePrice) {
  77.         carPurchasePrice = purchasePrice;
  78.     }
  79.    
  80.     //a mutator method used to set the name of the owner who is going to buy the vehicle
  81.     public void setOwner(String ownerName) {
  82.         carSetOwner = ownerName;
  83.     }
  84.    
  85.     //a void method that sells the car. It increases the number of owners by 1 and sets the current owner of the vehicle
  86.     public void sell() {
  87.         noOfOwner += 1;
  88.         currentOwner = carSetOwner;
  89.     }
  90.    
  91.     //a mutator method that takes a boolean as an input and configures vehicle's catalytic converter
  92.     public void goingCleaner(boolean putCatalyticConverter) {
  93.         if (putCatalyticConverter == true) {
  94.             carHasCatalytic = true;
  95.         } else {
  96.             carHasCatalytic = false;
  97.         }
  98.     }
  99.    
  100.     //a void method that checks if the vehicle is fitted with a catalytic converter and prompts a message depending on it
  101.     public void checkCarConverter() {
  102.         if (carHasCatalytic == true) {
  103.             System.out.println ("Be aware your car could be less polluting");
  104.         } else {
  105.             System.out.println ("This car is better for the enviroment");
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement