Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Car {
- // instance variables
- private String carMake, carModel, carColor, carSetOwner, currentOwner, hasCatalytic;
- private int carYearModel, carNumOfDoors, carCylinderCapacity, carMilesPerGallon, carInitialPrice, carPurchasePrice, noOfOwner;
- private boolean carHasCatalytic;
- //Constructor for objects of class Car
- public Car(String make, String model, int yearModel, String color, int numOfDoors, int cylinderCapacity, int milesPerGallon, int initialPrice, boolean hasCatalytic) {
- // initialise instance variables
- carMake = make;
- carModel = model;
- carYearModel = yearModel;
- carColor = color;
- noOfOwner = 0;
- if (numOfDoors > 1) {
- carNumOfDoors = numOfDoors;
- } else {
- System.out.println("Invalid number of doors!");
- }
- if (cylinderCapacity > 0) {
- carCylinderCapacity = cylinderCapacity;
- } else {
- System.out.println("Invalid amount for cylinder capacity!");
- }
- if (milesPerGallon > 0) {
- carMilesPerGallon = milesPerGallon;
- } else {
- System.out.println("Invalid amount for mile per gallon!");
- }
- if (initialPrice >= 0) {
- carInitialPrice = initialPrice;
- } else {
- System.out.println("Initial price cannot be less than 0!");
- }
- carHasCatalytic = hasCatalytic;
- checkCarConverter();
- }
- //an accessor method that returns a string (Car's make)
- public String getMake() {
- return carMake;
- }
- //an accessor method that returns a string (Car's model)
- public String getModel() {
- return carModel;
- }
- /**
- * A void method that opens a message box containing car's details depending on whether it has been ever been sold or not.
- * 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
- */
- public void printDetails() {
- if (carHasCatalytic == true) {
- hasCatalytic = "does";
- } else {
- hasCatalytic = "does not";
- }
- if (noOfOwner == 0) {
- 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 + ".");
- } else if(noOfOwner > 0) {
- 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 + ".");
- }
- }
- //a mutator method used to set the price of the car prior to selling it
- public void setPurchasePrice(int purchasePrice) {
- carPurchasePrice = purchasePrice;
- }
- //a mutator method used to set the name of the owner who is going to buy the vehicle
- public void setOwner(String ownerName) {
- carSetOwner = ownerName;
- }
- //a void method that sells the car. It increases the number of owners by 1 and sets the current owner of the vehicle
- public void sell() {
- noOfOwner += 1;
- currentOwner = carSetOwner;
- }
- //a mutator method that takes a boolean as an input and configures vehicle's catalytic converter
- public void goingCleaner(boolean putCatalyticConverter) {
- if (putCatalyticConverter == true) {
- carHasCatalytic = true;
- } else {
- carHasCatalytic = false;
- }
- }
- //a void method that checks if the vehicle is fitted with a catalytic converter and prompts a message depending on it
- public void checkCarConverter() {
- if (carHasCatalytic == true) {
- System.out.println ("Be aware your car could be less polluting");
- } else {
- System.out.println ("This car is better for the enviroment");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement