The code is expected to to take input from user and in the form of shipping location and weight and if then throw SamePlaceException if source and destination is same and WeightException if total weight is less than the current object weight. If the input is shipping source = BOM, shipping dest = BOM; it throws SamePlaceException. if the total weight of already stored shipments is 50kgs and a new item is being added of 60kgs the it will throw a WeightException
class Implementation{
public String validator(Shipping details) throws WeightException, SamePlaceException {
if(details.sourcePlace.equals(details.destinationPlace)) {
throw new SamePlaceException("source and destination cannot be same");
}
if(details.netWeight > details.totalWeight) {
throw new WeightException("net weight cannt be greater than total weight");
}
return "shipping details are valid";
}
public float totalBill(Shipping details) {
float totalBill;
try {
validator(details);
totalBill = details.totalWeight*5;
return totalBill;
}
catch(SamePlaceException e) {
return 0.0f;
}
catch(WeightException e) {
return 0.0f;
}
catch(Exception e) {
return -1.0f;
}
class SamePlaceException extends Exception{
private static final long serialVersionUID = 1L;
public SamePlaceException(String str){
super(str);
}
class WeightException extends Exception{
private static final long serialVersionUID = 1L;
public WeightException(String str){
super(str);
}