Advertisement
karlakmkj

Conditional stm - switch & if-then-elseif

Dec 1st, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. public class Order {
  2.   boolean isFilled;
  3.   double billAmount;
  4.   String shipping;
  5.  
  6.   public Order(boolean filled, double cost, String shippingMethod) {
  7.         if (cost > 24.00) {
  8.       System.out.println("High value item!");
  9.     }
  10.     isFilled = filled;
  11.     billAmount = cost;
  12.     shipping = shippingMethod;
  13.   }
  14.  
  15.   public void ship() {
  16.     if (isFilled) {
  17.       System.out.println("Shipping");
  18.       System.out.println("Shipping cost: " + calculateShipping());
  19.     } else {
  20.       System.out.println("Order not ready");
  21.     }
  22.   }
  23.  
  24.   //using switch statement
  25.   public double calculateShipping() {
  26.     double shippingCost;
  27.        
  28.     switch (shipping){
  29.       case "Regular":
  30.         shippingCost = 0;
  31.         break;
  32.       case "Express":
  33.         shippingCost = 1.75;
  34.         break;
  35.       default:
  36.         shippingCost = 0.50;  
  37.     }
  38.    
  39.     return shippingCost; //remember to return the variable
  40.     }
  41.    
  42.     /*
  43.     //using if-then-elseif statement  
  44.     public double calculateShipping() {
  45.         // declare conditional statement here
  46.     if (shipping == "Regular"){
  47.       return 0;
  48.     }
  49.     else if (shipping == "Express"){
  50.       return 1.75;
  51.     }  
  52.     else return 0.50;
  53.     }
  54.    
  55.     */
  56.    
  57.   public static void main(String[] args) {
  58.     // do not alter the main method!
  59.     Order book = new Order(true, 9.99, "Express");
  60.     Order chemistrySet = new Order(false, 72.50, "Regular");
  61.    
  62.     book.ship();
  63.     chemistrySet.ship();
  64.   }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement