Skip to content

Commit 4be5729

Browse files
committed
Add implementation of two list iterate and set values for one object
1 parent a8dcd2c commit 4be5729

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package lk.himash;
2+
3+
import lk.himash.model.Car;
4+
import lk.himash.util.DB;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Illustration_04 {
10+
public static void main(String[] args) {
11+
12+
List<Car> initDetails = DB.fetchCarsIniDetails();
13+
List<Car> otherDetails = DB.fetchCarsOtherDetails();
14+
List<Car> totalDetails = new ArrayList<>();
15+
16+
// If the both list contains same count of records
17+
for (int i = 0; i < initDetails.size(); i++) {
18+
Car c = new Car();
19+
c.setId(initDetails.get(i).getId());
20+
c.setManufacture(initDetails.get(i).getManufacture());
21+
c.setModel(initDetails.get(i).getModel());
22+
if(i != otherDetails.size()) {
23+
c.setCost(otherDetails.get(i).getCost());
24+
c.setStatus(otherDetails.get(i).isStatus());
25+
}
26+
totalDetails.add(c);
27+
}
28+
29+
// If the both list contains different count of records
30+
for (int i = 0; i < initDetails.size(); i++) {
31+
Car c = new Car();
32+
c.setId(initDetails.get(i).getId());
33+
c.setManufacture(initDetails.get(i).getManufacture());
34+
c.setModel(initDetails.get(i).getModel());
35+
totalDetails.add(c);
36+
}
37+
for (int j = 0; j < otherDetails.size(); j++) {
38+
if (j == totalDetails.size()) {
39+
Car car = new Car(otherDetails.get(j).getCost(), otherDetails.get(j).isStatus());
40+
totalDetails.add(car);
41+
} else {
42+
totalDetails.get(j).setCost(otherDetails.get(j).getCost());
43+
totalDetails.get(j).setStatus(otherDetails.get(j).isStatus());
44+
}
45+
}
46+
System.out.println(totalDetails);
47+
}
48+
49+
}

project/src/lk/himash/model/Car.java

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package lk.himash.model;
2+
3+
public class Car {
4+
5+
private int id;
6+
private String manufacture;
7+
private String model;
8+
private double cost;
9+
private boolean status;
10+
11+
@Override
12+
public String toString() {
13+
return "Car{" +
14+
"id=" + id +
15+
", manufacture='" + manufacture + '\'' +
16+
", model='" + model + '\'' +
17+
", cost=" + cost +
18+
", status=" + status +
19+
'}';
20+
}
21+
22+
public Car(int id, String manufacture, String model, double cost, boolean status) {
23+
this.id = id;
24+
this.manufacture = manufacture;
25+
this.model = model;
26+
this.cost = cost;
27+
this.status = status;
28+
}
29+
30+
public Car() {
31+
}
32+
33+
public Car(int id, String manufacture, String model) {
34+
this.id = id;
35+
this.manufacture = manufacture;
36+
this.model = model;
37+
}
38+
39+
public Car(double cost, boolean status) {
40+
this.cost = cost;
41+
this.status = status;
42+
}
43+
44+
public int getId() {
45+
return id;
46+
}
47+
48+
public void setId(int id) {
49+
this.id = id;
50+
}
51+
52+
public String getManufacture() {
53+
return manufacture;
54+
}
55+
56+
public void setManufacture(String manufacture) {
57+
this.manufacture = manufacture;
58+
}
59+
60+
public String getModel() {
61+
return model;
62+
}
63+
64+
public void setModel(String model) {
65+
this.model = model;
66+
}
67+
68+
public double getCost() {
69+
return cost;
70+
}
71+
72+
public void setCost(double cost) {
73+
this.cost = cost;
74+
}
75+
76+
public boolean isStatus() {
77+
return status;
78+
}
79+
80+
public void setStatus(boolean status) {
81+
this.status = status;
82+
}
83+
}

project/src/lk/himash/util/DB.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package lk.himash.util;
2+
3+
import lk.himash.model.Car;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class DB {
9+
10+
public static List<Car> fetchCarsIniDetails() {
11+
List<Car> cars = new ArrayList<>();
12+
Car car1 = new Car(1, "Toyota", "Axio");
13+
Car car2 = new Car(2, "Honda", "Civic");
14+
Car car3 = new Car(3, "Audi", "A5");
15+
Car car4 = new Car(4, "BMW", "320SI");
16+
cars.add(car1);
17+
cars.add(car2);
18+
cars.add(car3);
19+
cars.add(car4);
20+
return cars;
21+
}
22+
23+
public static List<Car> fetchCarsOtherDetails() {
24+
List<Car> cars = new ArrayList<>();
25+
Car car1 = new Car(30000.00, false);
26+
Car car2 = new Car(15000.00, true);
27+
Car car3 = new Car(80000.00, false);
28+
cars.add(car1);
29+
cars.add(car2);
30+
cars.add(car3);
31+
return cars;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)