Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions project/src/lk/himash/Illustration_04.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package lk.himash;

import lk.himash.model.Car;
import lk.himash.util.DB;

import java.util.ArrayList;
import java.util.List;

public class Illustration_04 {
public static void main(String[] args) {

List<Car> initDetails = DB.fetchCarsIniDetails();
List<Car> otherDetails = DB.fetchCarsOtherDetails();
List<Car> totalDetails = new ArrayList<>();

// If the both list contains same count of records
for (int i = 0; i < initDetails.size(); i++) {
Car c = new Car();
c.setId(initDetails.get(i).getId());
c.setManufacture(initDetails.get(i).getManufacture());
c.setModel(initDetails.get(i).getModel());
if(i != otherDetails.size()) {
c.setCost(otherDetails.get(i).getCost());
c.setStatus(otherDetails.get(i).isStatus());
}
totalDetails.add(c);
}

// If the both list contains different count of records
for (int i = 0; i < initDetails.size(); i++) {
Car c = new Car();
c.setId(initDetails.get(i).getId());
c.setManufacture(initDetails.get(i).getManufacture());
c.setModel(initDetails.get(i).getModel());
totalDetails.add(c);
}
for (int j = 0; j < otherDetails.size(); j++) {
if (j == totalDetails.size()) {
Car car = new Car(otherDetails.get(j).getCost(), otherDetails.get(j).isStatus());
totalDetails.add(car);
} else {
totalDetails.get(j).setCost(otherDetails.get(j).getCost());
totalDetails.get(j).setStatus(otherDetails.get(j).isStatus());
}
}
System.out.println(totalDetails);
}

}
83 changes: 83 additions & 0 deletions project/src/lk/himash/model/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package lk.himash.model;

public class Car {

private int id;
private String manufacture;
private String model;
private double cost;
private boolean status;

@Override
public String toString() {
return "Car{" +
"id=" + id +
", manufacture='" + manufacture + '\'' +
", model='" + model + '\'' +
", cost=" + cost +
", status=" + status +
'}';
}

public Car(int id, String manufacture, String model, double cost, boolean status) {
this.id = id;
this.manufacture = manufacture;
this.model = model;
this.cost = cost;
this.status = status;
}

public Car() {
}

public Car(int id, String manufacture, String model) {
this.id = id;
this.manufacture = manufacture;
this.model = model;
}

public Car(double cost, boolean status) {
this.cost = cost;
this.status = status;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getManufacture() {
return manufacture;
}

public void setManufacture(String manufacture) {
this.manufacture = manufacture;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public double getCost() {
return cost;
}

public void setCost(double cost) {
this.cost = cost;
}

public boolean isStatus() {
return status;
}

public void setStatus(boolean status) {
this.status = status;
}
}
34 changes: 34 additions & 0 deletions project/src/lk/himash/util/DB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lk.himash.util;

import lk.himash.model.Car;

import java.util.ArrayList;
import java.util.List;

public class DB {

public static List<Car> fetchCarsIniDetails() {
List<Car> cars = new ArrayList<>();
Car car1 = new Car(1, "Toyota", "Axio");
Car car2 = new Car(2, "Honda", "Civic");
Car car3 = new Car(3, "Audi", "A5");
Car car4 = new Car(4, "BMW", "320SI");
cars.add(car1);
cars.add(car2);
cars.add(car3);
cars.add(car4);
return cars;
}

public static List<Car> fetchCarsOtherDetails() {
List<Car> cars = new ArrayList<>();
Car car1 = new Car(30000.00, false);
Car car2 = new Car(15000.00, true);
Car car3 = new Car(80000.00, false);
cars.add(car1);
cars.add(car2);
cars.add(car3);
return cars;
}

}