Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. //==============================================================================class Package
  2.  
  3. class Package{
  4.  
  5.   private int weight;  //in grams
  6.   private String dest; //destination
  7.  
  8.   //"magic" country codes/destinations, set as constants
  9.   public static final String CAN = "CAN";
  10.   public static final String USA = "USA";
  11.  
  12.   public Package(int weight, String dest){
  13.     this.weight = weight;
  14.     this.dest = dest;
  15.   }
  16.  
  17.  public double cost() {
  18.      return 0.0;
  19.  }
  20.  
  21.   public int getWeight(){
  22.     return weight;
  23.   }
  24.  
  25.   public int time() {
  26.     if(getDest().equals(Package.CAN)){
  27.         return 3;
  28.       }
  29.       else if(getDest().equals(Package.USA)){
  30.         return 4;
  31.       }
  32.       else{
  33.         return 6;
  34.       }
  35.   }
  36.  
  37.   public String getDest(){
  38.     return dest;
  39.   }
  40.   public String toString(){
  41.     return weight + "g," + dest;
  42.   }
  43.  
  44.  
  45.  
  46. }//End class Package
  47.  
  48.  
  49. //=========================================================================subclass Overnight
  50. class Overnight extends Package{
  51.   public Overnight(int weight, String dest){
  52.     super(weight,dest);
  53.   }
  54.  
  55.   public double cost(){
  56.     if(getDest().equals(Package.CAN)){
  57.       return Math.round((3.5*getWeight())*100)/100;
  58.     }
  59.     else{
  60.       return Math.round((5.5*getWeight())*100)/100;
  61.     }
  62.   }
  63.  
  64.   public int time(){
  65.     return 1;
  66.   }
  67.  
  68.   public String toString(){
  69.     return "Overnight," + super.toString();
  70.   }
  71. }//End class Overnight
  72.  
  73.  
  74. //=========================================================================subclass Priority
  75. class Priority extends Package{
  76.   public Priority(int weight, String dest){
  77.     super(weight,dest);
  78.   }
  79.  
  80.   public double cost(){
  81.     if(getWeight() <=500){
  82.       return 4.25;
  83.     }
  84.     else if(getWeight() >500 && getWeight() <=1000){
  85.       return 5.75;
  86.     }
  87.     else{
  88.       return 7.00;
  89.     }
  90.   }
  91.  
  92.   public int time(){
  93.       return super.time();
  94.   }
  95.  
  96.   public String toString(){
  97.     return "Priority," + super.toString();
  98.   }
  99. }//End class Priority
  100.  
  101.  
  102. //=========================================================================subclass Economy
  103. class Economy extends Package{
  104.  
  105.   private int volume; //volume of package in cm³
  106.  
  107.   public Economy(int weight, String dest, int volume){
  108.     super(weight,dest);
  109.     this.volume = volume;
  110.   }
  111.  
  112.   public double cost(){
  113.     if(volume <=10000){
  114.       return 2.50;
  115.     }
  116.     else{
  117.       return 3.25;
  118.     }
  119.   }
  120.  
  121.   public int time(){
  122.     return super.time() * 3;
  123.   }
  124.  
  125.   public String toString(){
  126.     return "Economy," + super.toString() + "," + volume + "cm³";
  127.   }
  128. }//End class Priority
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement