In: Computer Science
Hello guys.
Below is a test im struggling with I have to do it in Java and use
OOP concepts.
Create.
A House class with instance variables erfNumber, location,
noOfRooms,
etc.
Mandatory properties: status(is the asset owned or sold), number
of
years owned, selling price, and buying price.
At least two overloaded constructors that can be used to
instatiate
House objects.
Getter and setter methods for all properties you have
created.
A method checkProfitOrLoss() that returns the profit or loss that
can be
made if the property is sold (sellingPrice-buyingPrice).
A custom toString() method to print out all the properties of a
House
object.
A static counter to keep track of the number of each object
created.
At least 5 more property or vehicle classes such as Farm, Flat,
Bakkie,
RentalCar, etc.
A driver class named AssetManager.
An array of five House objects. The same applies to other classes
created
in Section B.
Methods to add (remember to keep the sizes of the arrays in mind),
sell
assets (sets the status of an asset at a given index of an array to
sold), and
print the contents of each of the objects for each array. Each
method to
have proper Javadoc comments.
Code to enter from input five of each of the assets into respective
arrays
using the add methods just created.
Printing out the contents of all the arrays using the print methods
you have
created to provide an initial asset report.
Change the current value of at least 10 objects from the arrays
using the
getter and setter methods.
Print out the contents of the assets that can be sold at a profit
using the
checkProfitOrLoss method and the methods you have provided an
asset
sales report.
/***************************Assest.java***************/
/**
* The Class Vehicle.
*/
public class Vehicle extends Assest {
/** The make. */
private String make;
/** The model. */
private String model;
/** The year. */
private String year;
/** The price. */
private double price;
/** The sellig price. */
private double selligPrice;
/** The buying price. */
private double buyingPrice;
/**
* Instantiates a new vehicle.
*
* @param make the make
* @param model the model
* @param year the year
* @param ownerName the owner name
*/
public Vehicle(String make, String model, String year,
String ownerName, int assestId) {
super(ownerName, assestId);
this.make = make;
this.model = model;
this.year = year;
}
/**
* Instantiates a new vehicle.
*
* @param make the make
* @param model the model
* @param year the year
* @param price the price
* @param ownerName the owner name
*/
public Vehicle(String make, String model, String year,
double price, String ownerName, int assestId) {
super(ownerName, assestId);
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
/**
* Gets the make.
*
* @return the make
*/
public String getMake() {
return make;
}
/**
* Sets the make.
*
* @param make the new make
*/
public void setMake(String make) {
this.make = make;
}
/**
* Gets the model.
*
* @return the model
*/
public String getModel() {
return model;
}
/**
* Sets the model.
*
* @param model the new model
*/
public void setModel(String model) {
this.model = model;
}
/**
* Gets the year.
*
* @return the year
*/
public String getYear() {
return year;
}
/**
* Sets the year.
*
* @param year the new year
*/
public void setYear(String year) {
this.year = year;
}
/**
* Gets the price.
*
* @return the price
*/
public double getPrice() {
return price;
}
/**
* Sets the price.
*
* @param price the new price
*/
public void setPrice(double price) {
this.price = price;
}
/**
* Gets the sellig price.
*
* @return the sellig price
*/
public double getSelligPrice() {
return selligPrice;
}
/**
* Sets the sellig price.
*
* @param selligPrice the new sellig price
*/
public void setSelligPrice(double selligPrice) {
this.selligPrice =
selligPrice;
}
/**
* Gets the buying price.
*
* @return the buying price
*/
public double getBuyingPrice() {
return buyingPrice;
}
/**
* Sets the buying price.
*
* @param buyingPrice the new buying price
*/
public void setBuyingPrice(double buyingPrice) {
this.buyingPrice =
buyingPrice;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Vehicle [make=" + make + ",
model=" + model + ", year=" + year + ", price=" + price +
"]";
}
/*
* (non-Javadoc)
*
* @see Assest#checkProfitOrLoss()
*/
@Override
public double checkProfitOrLoss() {
return selligPrice -
buyingPrice;
}
/*
* (non-Javadoc)
*
* @see Assest#print()
*/
@Override
public void print() {
System.out.println(this.toString());
}
}
/***********************************House.java*********************/
/**
* The Class House.
*/
public class House extends Assest {
/** The erf number. */
private int erfNumber;
/** The location. */
private String location;
/** The no of rooms. */
private int noOfRooms;
/** The status. */
private String status;
/** The no of years owned. */
private int noOfYearsOwned;
/** The selling price. */
private double sellingPrice;
/** The buying price. */
private double buyingPrice;
/** The number of houses. */
private static int numberOfHouses;
/**
* Instantiates a new house.
*
* @param status the status
* @param noOfYearsOwned the no of years owned
* @param sellingPrice the selling price
* @param buyingPrice the buying price
* @param ownerName the owner name
*/
public House(String status, int noOfYearsOwned, double
sellingPrice, double buyingPrice, String ownerName,
int assestId)
{
super(ownerName, assestId);
this.status = status;
this.noOfYearsOwned =
noOfYearsOwned;
this.sellingPrice =
sellingPrice;
this.buyingPrice =
buyingPrice;
numberOfHouses++;
}
/**
* Instantiates a new house.
*
* @param erfNumber the erf number
* @param location the location
* @param noOfRooms the no of rooms
* @param status the status
* @param noOfYearsOwned the no of years owned
* @param sellingPrice the selling price
* @param buyingPrice the buying price
* @param ownerName the owner name
*/
public House(int erfNumber, String location, int
noOfRooms, String status, int noOfYearsOwned, double
sellingPrice,
double
buyingPrice, String ownerName, int assestId) {
super(ownerName, assestId);
this.erfNumber = erfNumber;
this.location = location;
this.noOfRooms = noOfRooms;
this.status = status;
this.noOfYearsOwned =
noOfYearsOwned;
this.sellingPrice =
sellingPrice;
this.buyingPrice =
buyingPrice;
numberOfHouses++;
}
/**
* Gets the erf number.
*
* @return the erf number
*/
public int getErfNumber() {
return erfNumber;
}
/**
* Sets the erf number.
*
* @param erfNumber the new erf number
*/
public void setErfNumber(int erfNumber) {
this.erfNumber = erfNumber;
}
/**
* Gets the location.
*
* @return the location
*/
public String getLocation() {
return location;
}
/**
* Sets the location.
*
* @param location the new location
*/
public void setLocation(String location) {
this.location = location;
}
/**
* Gets the no of rooms.
*
* @return the no of rooms
*/
public int getNoOfRooms() {
return noOfRooms;
}
/**
* Sets the no of rooms.
*
* @param noOfRooms the new no of rooms
*/
public void setNoOfRooms(int noOfRooms) {
this.noOfRooms = noOfRooms;
}
/**
* Gets the status.
*
* @return the status
*/
public String getStatus() {
return status;
}
/**
* Sets the status.
*
* @param status the new status
*/
public void setStatus(String status) {
this.status = status;
}
/**
* Gets the no of years owned.
*
* @return the no of years owned
*/
public int getNoOfYearsOwned() {
return noOfYearsOwned;
}
/**
* Sets the no of years owned.
*
* @param noOfYearsOwned the new no of years
owned
*/
public void setNoOfYearsOwned(int noOfYearsOwned)
{
this.noOfYearsOwned =
noOfYearsOwned;
}
/**
* Gets the selling price.
*
* @return the selling price
*/
public double getSellingPrice() {
return sellingPrice;
}
/**
* Sets the selling price.
*
* @param sellingPrice the new selling price
*/
public void setSellingPrice(double sellingPrice)
{
this.sellingPrice =
sellingPrice;
}
/**
* Gets the buying price.
*
* @return the buying price
*/
public double getBuyingPrice() {
return buyingPrice;
}
/**
* Sets the buying price.
*
* @param buyingPrice the new buying price
*/
public void setBuyingPrice(double buyingPrice) {
this.buyingPrice =
buyingPrice;
}
/**
* Gets the number of houses.
*
* @return the number of houses
*/
public static int getNumberOfHouses() {
return numberOfHouses;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "House [erfNumber=" +
erfNumber + ", location=" + location + ", noOfRooms=" + noOfRooms +
", status="
+ status + ", noOfYearsOwned=" + noOfYearsOwned
+ ", sellingPrice=" + sellingPrice + ", buyingPrice="
+ buyingPrice + "]";
}
/*
* (non-Javadoc)
*
* @see Assest#checkProfitOrLoss()
*/
public double checkProfitOrLoss() {
return sellingPrice -
buyingPrice;
}
/*
* (non-Javadoc)
*
* @see Assest#print()
*/
@Override
public void print() {
System.out.println(this.toString());
}
}
/**************************************Vehicle.java*****************/
/**
* The Class Vehicle.
*/
public class Vehicle extends Assest {
/** The make. */
private String make;
/** The model. */
private String model;
/** The year. */
private String year;
/** The price. */
private double price;
/** The sellig price. */
private double selligPrice;
/** The buying price. */
private double buyingPrice;
/**
* Instantiates a new vehicle.
*
* @param make the make
* @param model the model
* @param year the year
* @param ownerName the owner name
*/
public Vehicle(String make, String model, String year,
String ownerName, int assestId) {
super(ownerName, assestId);
this.make = make;
this.model = model;
this.year = year;
}
/**
* Instantiates a new vehicle.
*
* @param make the make
* @param model the model
* @param year the year
* @param price the price
* @param ownerName the owner name
*/
public Vehicle(String make, String model, String year,
double price, String ownerName, int assestId) {
super(ownerName, assestId);
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
/**
* Gets the make.
*
* @return the make
*/
public String getMake() {
return make;
}
/**
* Sets the make.
*
* @param make the new make
*/
public void setMake(String make) {
this.make = make;
}
/**
* Gets the model.
*
* @return the model
*/
public String getModel() {
return model;
}
/**
* Sets the model.
*
* @param model the new model
*/
public void setModel(String model) {
this.model = model;
}
/**
* Gets the year.
*
* @return the year
*/
public String getYear() {
return year;
}
/**
* Sets the year.
*
* @param year the new year
*/
public void setYear(String year) {
this.year = year;
}
/**
* Gets the price.
*
* @return the price
*/
public double getPrice() {
return price;
}
/**
* Sets the price.
*
* @param price the new price
*/
public void setPrice(double price) {
this.price = price;
}
/**
* Gets the sellig price.
*
* @return the sellig price
*/
public double getSelligPrice() {
return selligPrice;
}
/**
* Sets the sellig price.
*
* @param selligPrice the new sellig price
*/
public void setSelligPrice(double selligPrice) {
this.selligPrice =
selligPrice;
}
/**
* Gets the buying price.
*
* @return the buying price
*/
public double getBuyingPrice() {
return buyingPrice;
}
/**
* Sets the buying price.
*
* @param buyingPrice the new buying price
*/
public void setBuyingPrice(double buyingPrice) {
this.buyingPrice =
buyingPrice;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Vehicle [make=" + make + ",
model=" + model + ", year=" + year + ", price=" + price +
"]";
}
/*
* (non-Javadoc)
*
* @see Assest#checkProfitOrLoss()
*/
@Override
public double checkProfitOrLoss() {
return selligPrice -
buyingPrice;
}
/*
* (non-Javadoc)
*
* @see Assest#print()
*/
@Override
public void print() {
System.out.println(this.toString());
}
}
/*******************************AssestManager.java*******************/
public class AssetManager {
private Assest[] assests;
private int capacity;
private int index;
public AssetManager() {
this.index = 0;
this.capacity = 5;
assests = new
Assest[capacity];
}
public void addAssest(Assest asset) {
if (index == capacity) {
capacity = 2 *
capacity;
resize(2 *
capacity);
}
assests[index] = asset;
index++;
}
private void resize(int i) {
Assest[] assestsCopy = new
Assest[i];
for (int j = 0; j <
assests.length; j++) {
assestsCopy[j] = assests[j];
}
assests = assestsCopy;
}
public void sellAssest(Assest assest) {
int loc = 0, flag = 0;
try {
for (int i = 0;
i < assests.length; i++) {
if (assests[i].equals(assest)) {
loc = i;
flag = 1;
}
}
} catch (Exception e) {
}
if (flag == 1) {
System.arraycopy(assests, loc + 1, assests, loc, assests.length - 1 - loc);
} else {
System.out.println("Assest not found!");
}
}
public void printArray() {
try {
for (Assest
assest : assests) {
assest.print();
}
} catch (Exception e) {
}
}
}
/****************************TestAssest.java*****************/
// TODO: Auto-generated Javadoc
/**
* The Class TestAsset.
*/
public class TestAsset {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
AssetManager mngr = new
AssetManager();
House house = new House("owned", 5,
5000, 4000, "JKS", 101);
mngr.addAssest(house);
mngr.addAssest(new House("owned",
5, 5000, 4000, "MS", 102));
mngr.addAssest(new House("owned",
5, 5000, 4000, "Virat", 103));
mngr.addAssest(new House("sold", 5,
5000, 4000, "JKS", 104));
mngr.addAssest(new House("owned",
5, 5000, 4000, "JKS", 105));
mngr.addAssest(new Vehicle("Hero",
"top model", "2019", "MS", 106));
mngr.addAssest(new Vehicle("Bajaj",
"top model", "2018", "JKS", 107));
mngr.addAssest(new Vehicle("Hero",
"top model", "2017", "MS", 108));
mngr.addAssest(new Vehicle("Hero",
"top model", "2018", "Virat", 109));
mngr.addAssest(new Vehicle("Hero",
"top model", "2019", "JKS", 110));
mngr.printArray();
System.out.println("\nAfter
selling\n");
mngr.sellAssest(house);
mngr.printArray();
}
}
/*****************output******************/
House [erfNumber=0, location=null, noOfRooms=0, status=owned,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
House [erfNumber=0, location=null, noOfRooms=0, status=owned,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
House [erfNumber=0, location=null, noOfRooms=0, status=owned,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
House [erfNumber=0, location=null, noOfRooms=0, status=sold,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
House [erfNumber=0, location=null, noOfRooms=0, status=owned,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
Vehicle [make=Hero, model=top model, year=2019, price=0.0]
Vehicle [make=Bajaj, model=top model, year=2018, price=0.0]
Vehicle [make=Hero, model=top model, year=2017, price=0.0]
Vehicle [make=Hero, model=top model, year=2018, price=0.0]
Vehicle [make=Hero, model=top model, year=2019, price=0.0]
After selling
House [erfNumber=0, location=null, noOfRooms=0, status=owned,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
House [erfNumber=0, location=null, noOfRooms=0, status=owned,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
House [erfNumber=0, location=null, noOfRooms=0, status=sold,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
House [erfNumber=0, location=null, noOfRooms=0, status=owned,
noOfYearsOwned=5, sellingPrice=5000.0, buyingPrice=4000.0]
Vehicle [make=Hero, model=top model, year=2019, price=0.0]
Vehicle [make=Bajaj, model=top model, year=2018, price=0.0]
Vehicle [make=Hero, model=top model, year=2017, price=0.0]
Vehicle [make=Hero, model=top model, year=2018, price=0.0]
Vehicle [make=Hero, model=top model, year=2019, price=0.0]
Please let me know if you have any doubt or modify the answer, Thanks :)