Advertisement
Guest User

Untitled

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