From 4be57290846dd698b9f932db63b8120494e9d2ee Mon Sep 17 00:00:00 2001 From: yomalbandara Date: Thu, 7 Sep 2023 21:24:15 +0530 Subject: [PATCH] Add implementation of two list iterate and set values for one object --- project/src/lk/himash/Illustration_04.java | 49 +++++++++++++ project/src/lk/himash/model/Car.java | 83 ++++++++++++++++++++++ project/src/lk/himash/util/DB.java | 34 +++++++++ 3 files changed, 166 insertions(+) create mode 100644 project/src/lk/himash/Illustration_04.java create mode 100644 project/src/lk/himash/model/Car.java create mode 100644 project/src/lk/himash/util/DB.java diff --git a/project/src/lk/himash/Illustration_04.java b/project/src/lk/himash/Illustration_04.java new file mode 100644 index 0000000..245e975 --- /dev/null +++ b/project/src/lk/himash/Illustration_04.java @@ -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 initDetails = DB.fetchCarsIniDetails(); + List otherDetails = DB.fetchCarsOtherDetails(); + List 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); + } + +} diff --git a/project/src/lk/himash/model/Car.java b/project/src/lk/himash/model/Car.java new file mode 100644 index 0000000..2790c33 --- /dev/null +++ b/project/src/lk/himash/model/Car.java @@ -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; + } +} \ No newline at end of file diff --git a/project/src/lk/himash/util/DB.java b/project/src/lk/himash/util/DB.java new file mode 100644 index 0000000..92fe981 --- /dev/null +++ b/project/src/lk/himash/util/DB.java @@ -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 fetchCarsIniDetails() { + List 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 fetchCarsOtherDetails() { + List 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; + } + +}