JAVA 12
Switch Statement vs Switch Expression Guest on 19th January 2024 02:51:58 AM
  1. enum PaymentStatus {
  2.     UNPAID, PARTPAID, PAID, DISPUTED, UNKNOWN;
  3. }
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         PaymentStatus paymentStatus = PaymentStatus.PARTPAID;
  8.  
  9.         // Switch statement
  10.         String message = "";
  11.         switch (paymentStatus) {
  12.             case UNPAID:
  13.                 message = "The order has not been paid yet. Please make the minimum/full amount to proceed.";
  14.                 break;
  15.             case PARTPAID:
  16.                 message = "The order is partially paid. Some features will not be available. Please check the brochure for details.";
  17.                 break;
  18.             case PAID:
  19.                 message = "The order is fully paid. Please choose the desired items from the menu.";
  20.                 break;
  21.             default:
  22.                 throw new IllegalStateException("Invalid payment status: " + paymentStatus);
  23.         }
  24.  
  25.         System.out.println("Switch statement output: " + message);
  26.  
  27.         // Switch expression
  28.         message = switch (paymentStatus) {
  29.             case UNPAID -> "The order has not been paid yet.";
  30.             case PARTPAID -> "The order is partially paid. Some features will not be available. Please check the brochure for details.";
  31.             case PAID -> "The order is fully paid. Please choose the desired items from the menu.";
  32.             default -> throw new IllegalStateException("Invalid payment status: " + paymentStatus);
  33.         };
  34.  
  35.         System.out.println("Switch expression output: " + message);
  36.     }
  37. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.