'Matching value from multiple ArrayList in Map - Java
I have to display the greatest value of user cart, and his owner. I have 3 lists, i need to match productId from cartList to productId and price from categoryList + multiply by quantity in cartList, after the greatest sum is calculated i have to match right cart with userName to which this cart belongs. Do you have any ideas how to solve this?
List <UserIN> userList example output:
[UserIN{id=1, name=UserName{firstname='john', lastname='doe'}}, UserIN{id=2, name=UserName{firstname='david', lastname='morrison'}}]
List<SumUserCart> cartList example output:
[SumUserCart{userId=1, products=[ProductsCart{productId=1, quantity=4}, ProductsCart{productId=2, quantity=1}, ProductsCart{productId=3, quantity=6}]}, SumUserCart{userId=1, products=[ProductsCart{productId=2, quantity=4}, ProductsCart{productId=1, quantity=10}, ProductsCart{productId=5, quantity=2}]}]
List<ProductCategories> categoryList example output:
[ProductCategories{Category name='men's clothing', productID=1, price='109.95'}, ProductCategories{Category name='men's clothing', productID=2, price='22.3'}]
That's the way i got these values, its coming from JSON file, it works the same way with other lists:
List<SumUserCart> cartList = new ArrayList<>();
List<Cart> carts = mapper.readValue(data3, typeReference2);
for (Cart c : carts) {
cartList.add(new SumUserCart(c.getUserId(),c.getProducts()));
}
UserIN class:
public class UserIN {
int id;
UserName name;
@Override
public String toString() {
return "UserIN{" +
"id=" + id +
", name=" + name +
'}';
}
public UserIN(int id, UserName name) {
this.id = id;
this.name = name;
}
// getters & setters here
}
UserName class:
public class UserName {
String firstname;
String lastname;
@Override
public String toString() {
return "UserName{" +
"firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
'}';
}
//getters & setters here
public UserName() {}
public UserName(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
ProductCategories class:
public class ProductCategories {
private String categoryName;
private int productId;
private double price;
public ProductCategories(String categoryName, int productId, double price) {
this.categoryName = categoryName;
this.productId = productId;
this.price = price;
}
@Override
public String toString() {
return "ProductCategories{" + "Category name='" + categoryName + '\'' + ", productID=" + productId + ", price='" + price + '\'' + '}';
}
//getters & setters here
}
SumUserCart class:
public class SumUserCart {
int userId;
ProductsCart[] products;
public SumUserCart(int userId, ProductsCart[] products) {
this.userId = userId;
this.products = products;
}
@Override
public String toString() {
return "SumUserCart{" +
"userId=" + userId +
", products=" + Arrays.toString(products) +
'}';
}
public ProductsCart[] getProducts() {
return products;
}
//getters & setters here
}
ProductCart class:
public class ProductsCart {
int productId;
int quantity;
@Override
public String toString() {
return "ProductsCart{" +
"productId=" + productId +
", quantity=" + quantity +
'}';
}
public ProductsCart() {}
public ProductsCart(int productId, int quantity) {
this.productId = productId;
this.quantity = quantity;
}
//getters & setters here
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
