Java serious OOP Practice

Problem statement: Write a program to simulate a car dealership sales process. We will have employees, working selling vehicles to customers.

you have to think about nouns(a person, name or thing) while doing OOP, we have 4 nouns in the problem statement.

Dealership.java

package com.zakilive;

public class Dealership {

    public static void main(String[] args)
    {

        Customer cust1=new Customer("Tom", "123 Anything st", 25000); //cust1 is a variable
//        cust1.setName("Tom"); //cust1 is referencing the things on heap
//        cust1.setAddress("123 Anything st");
//        cust1.setCashOnHand(25000);

//        Vehicle vehicle=new Vehicle("Honda","Accord", 15000); //we are passing through constructor
//        vehicle.setMake("Honda");
//        vehicle.setModel("Accord");
//        vehicle.setPrice(15000);

        Vehicle vehicle = new Vehicle("BMW","M3",20000);

        Employee emp=new Employee();

        cust1.purchaseCar(vehicle, emp, false); //false

        Vehicle car=new Vehicle("BMW","M3",20000);

        boolean value=car.equals(vehicle);
        System.out.println(value);

        /***
         *
         * handleCustomer(Customer cust, boolean finance, Vehicle vehicle)
         *  if(finance==true)
         *      runCreditHistory(Customer cust, double doubleAmount)
         *  else if(vehicle.getPrice()<=cust.getCashOnHand()):
         *      processTransaction(Customer cust,Vehicle vehicle)
         *   else:
         *      tell customer bring more money
         *
         *
         * //getters allow us to encapsulate data
         *
         */


    }

}

Customer.java

package com.zakilive;

public class Customer {
    private String name;  //this are only accessible in this class
    private String address;
    private double cashOnHand;

    public Customer(String name, String address, double cashOnHand) {
        this.name = name;
        this.address = address;
        this.cashOnHand = cashOnHand;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public double getCashOnHand() {
        return cashOnHand;
    }

    public void setCashOnHand(double cashOnHand) {
        cashOnHand+=500;
        this.cashOnHand = cashOnHand;
    }


    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        address+="Dealership City";
        this.address = address;
    }


    public void purchaseCar(Vehicle vehicle, Employee emp, boolean finance) //method signature
    {
        emp.handleCustomer(this, finance, vehicle);  //"this" actually shares the instances of a class to the another class method


    }
}

Vehicle.java

package com.zakilive;

public class Vehicle {
    private String make;
    private String model;
    private double price;

    public Vehicle(String make, String model, double price) {
        this.make = make;
        this.model = model;
        this.price = price;
    }

    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 double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public String toString() {
        return "Vehicle {" +
                "make='" + make + '\'' +
                ", model='" + model + '\'' +
                ", price=" + price +
                '}';
    }
}

Employee.java

package com.zakilive;

public class Employee {

    public void handleCustomer(Customer cust, boolean finance, Vehicle vehicle)
    {
        if(finance==true)
        {
            //customer pays in loan
            double loanAmount=vehicle.getPrice()-cust.getCashOnHand();
            runCreditHistroy(cust,loanAmount);

        }
        else if (vehicle.getPrice()<=cust.getCashOnHand())   //reason for using getters is encapsulating and the data hiding objects, setters to set also for the same purpose
        {
            //customer pays in cash
            processTransaction(cust,vehicle);
        }
        else{
            System.out.println(" Customer will need more money to purchase vehicle: "+vehicle);
        }
    }

    public void runCreditHistroy(Customer cust, double loanAmount)
    {
        System.out.println("Ran credit history for customer....");
        System.out.println("Customer has been approved to purchase the vehicle");
    }

    public void processTransaction(Customer cust, Vehicle vehicle){
        System.out.println("Customer has purchased the vehicle: "
                +vehicle+" for the price "+vehicle.getPrice());

    }
}

Output:
if false in purchaseCar()

Customer has purchased the vehicle: Vehicle {make='BMW', model='M3', price=20000.0} for the price 20000.0
false

if true in purchaseCar()

Ran credit history for customer....
Customer has been approved to purchase the vehicle
false

 

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.