You are on page 1of 6

import java.time.

LocalDate;
import java.time.format.DateTimeFormatter;
/**
*
* @author david.north
*
*/
public class Car {

// instance variables
private String serialNumber;
private String make;
private String model;
private int year;
private String color;
private int mileage;
private int speed;
private char gear;
private LocalDate datePurchased;

// default constructor
public Car ()
{
this.mileage=0;
this.gear ='P';
this.speed = 0;
}
/**
*
* @param serialNumber
* @param make
* @param model
* @param year
* @param color
* @param date
*/
public Car (String serialNumber,String make, String model, int year, String color,
String date)
{
this();
this.serialNumber = serialNumber;
this.make = make;
this.model = model;
this.year = year;
this.color = color;
this.setDatePurchased(LocalDate.parse(date,
DateTimeFormatter.ofPattern("MM/dd/yyyy")));
}

// getters and setters


public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public char getGear() {
return gear;
}
public void setGear(char gear) {
this.gear = gear;
}

public LocalDate getDatePurchased() {


return datePurchased;
}
public void setDatePurchased(LocalDate datePurchased) {
this.datePurchased = datePurchased;
}

public int timeToTravel(int miles) {


return miles / getSpeed() ;
}

public void changeGear() {


if (speed == 0) setGear('1');
else if (speed > 15 && speed < 25 && gear != '2') setGear('2');
else if (speed >= 25 && speed < 40 && gear != '3') setGear('3');
else if (speed >= 40 && gear != '4') setGear('4');

public String toString() {


return year +" "+make+" "+model+" Purchased:"+datePurchased;
}

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.TreeMap;

public class Dealer {

private String name;


private String address;
private TreeMap<String,Car> cars;
private int numberCars;
public Dealer()
{
cars = new TreeMap<String,Car>();
numberCars = 0;
}

public Dealer(String name, String address)


{
this();
this.setName(name);
this.setAddress(address);
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public TreeMap<String,Car> getCars() {


return cars;
}

public void addCar(Car car) {


cars.put(car.getSerialNumber(),car);
}

public void removeCar(Car car) {


cars.remove(car);
}

public int getCarCount() {


return cars.size();
}

public ArrayList<Car> oldInventory(String date)


{
ArrayList<Car> oldList = new ArrayList<Car>();
LocalDate oldDate = LocalDate.parse(date,
DateTimeFormatter.ofPattern("MM/dd/yyyy"));
for (Car car : getCars().values())
{
if (car.getDatePurchased().isBefore(oldDate))
{
oldList.add(car);
}

}
return oldList;
}

public Car getCarForID(String serialNumber)


{
return getCars().get(serialNumber);
}

public String toString() {


return name;
}
}

public class Test {

public static void main(String[] args) {

Dealer dealer;

dealer = new Dealer("David's Car Lot","Edmond, OK");

Car car1 = new Car("JX123456708","Chevrolet","Malibu",1976,"Maroon",


"09/01/1977");
dealer.addCar(car1);

Car car2 = new


Car("LG12312489","Triump","TR6",1973,"Green","08/01/1984");
dealer.addCar(car2);

Car car3 = new


Car("TY12348908","Alpha","Spyder",1985,"Red","06/01/1995");
dealer.addCar(car3);
Car car4 = new Car("QU12463484","Porche","911-
Cabriolet",1984,"Red","10/01/2000");
dealer.addCar(car4);

System.out.println(dealer+ " car lot:");


for (Car car: dealer.getCars().values())
{
System.out.println(car);
}

System.out.println("Old Cars");
for(Car car: dealer.oldInventory("01/01/1990")) {
System.out.println(car);
}

System.out.println("get car for id : TY12348908");


System.out.println(dealer.getCarForID("TY12348908"));
}
}