I have product object and two class AsiaProductValidation and ProductValidation.AsiaProductValidation is called when region is Asia. ProductValidation is parent class which has some functions that can be used in AsiaProductValidation. I want to know can i write validate method in AsiaProductValidation to reuse some checks written in parent class
class Product{
int id,
long price;
String region;
Product(int id, long price, String region){
this.id = id;
this.price = price;
this.region = region;
}
// getters for id,price and locId
}
Class ProductValidation{
public static validate(List<Product> list, int qty, int totalAvailableQty){
if(list.size() == 0)
return //some expceptio
if(qty < totalAvailableQty){
return //some exception
}
if(qty == totalAvailableQty){
calculate(list);
}
}
private long[] calculate(List<Product> list){
return list.stream().mapTolong(p -> p.getPrice()).toArrat()l
}
}
class AsiaProductValidation{
public static validate(List<Product> list, int qty, int totalAvailableQty){
// First two conditions stay same as above
if(list.size() == 0)
return //some expceptio
if(qty < totalAvailableQty){
return //some exception
}
//only calculate function changes, how can i use inheritance here to avoid writing above two
checks in this class. and also call above two checks when this class is called?
calculate(list);
}
private long[] calculate(List<Product> list){
return list.stream().mapTolong(p -> p.getPrice() < 100 ? 0 : p.getPrice()).toArray();l
}
}
class RegionCalculate{
public void call(List<Product> list, int qty, int totalAvailableQty ){
if(region == Asia){
AsiaProductValidation.validate(qty,totalAvailableQty )
}
else{
ProductValidation.validate(qty,totalAvailableQty)
}
}
}
long[] price = {40, 90, 40};
List<Product> list = new ArrayList();
list.add(new Product(1,100, "ASIA"));
list.add(new Product(1,110, "AUS));
list.add(new Product(1,90, "EUROPE"));
RegionCalculate reg = new RegionCalculate();
reg.call(list, 1100, 500);