0

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);
}
  • Can you provide some input – Dinesh May 20 '21 at 05:33
  • 1
    Your description sums it up: _If the input is shipping source = BOM, shipping dest = BOM; it throws SamePlaceException_ and _if `details.netWeight > details.totalWeight` it throws WeightException_ is exactly what happens. So what is the question? – Thomas Kläger May 20 '21 at 05:51
  • @ThomasKläger I want to know the program flow and the logic that has been applied for creating the custom exceptions. I don't know why this is used "private static final long serialVersionUID = 1L" – Abhishek Sharma May 20 '21 at 06:31
  • A serializable class should declare it `serialVersionUID`. Since exceptions extend from `java.lang.Throwable` and Throwable implements `Serializable` they need a `serialVersionUID` too. You can find more information about `serialVersionUID` at https://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it . – Thomas Kläger May 20 '21 at 14:18

0 Answers0