Question: Please edit this code to have the Premium Amount, Discount Amount, and Net Premium Amount display in a JOptionPane.showMessageDialog instead of in the Run I/O.
Please edit this code to have the Premium Amount, Discount Amount, and Net Premium Amount display in a JOptionPane.showMessageDialog instead of in the Run I/O.
1 //import java.util.Scanner; 2 import javax.swing.JFrame; 3 import javax.swing.JOptionPane; 4 public class DiscountCalculator { 5 6 //private static Scanner scanner = new Scanner(System.in); 7 8 public static void main(String[] args) { 9 10 JFrame f; 11 f=new JFrame(); 12 boolean repeat; 13 14 String rating; 15 16 int policy; 17 18 double premium, rebate =0, net; 19 20 do { 21 22 policy = getPolicyYears(); 23 24 premium = getPremiumAmount(); 25 26 if(policy>3){ 27 28 rating = getRating(); 29 30 rebate = getDiscount(premium, policy, rating); 31 32 net = premium - rebate; 33 34 JOptionPane.showMessageDialog(f, "Policy Tenure : "+ policy); 35 36 JOptionPane.showMessageDialog(f, "Rating : " + rating); 37 38 System.out.printf("Premium Amount : $ %10.2f ",premium); 39 40 if(net!=0) { 41 42 System.out.printf("Discount Amount : $ %10.2f ",rebate); 43 44 System.out.printf("Net Premium Amount : $ %10.2f ",net); 45 46 } 47 48 }else{ 49 50 JOptionPane.showMessageDialog(f, "Policy Tenure : "+ policy); 51 52 System.out.printf("Premium Amount : $ %10.2f ",premium); 53 54 JOptionPane.showMessageDialog(f, "Info *** you are not eligible for any discount ***"); 55 56 } 57 58 //JOptionPane.showMessageDialog(f, " "); 59 60 repeat = repeat(); 61 62 } while (repeat); 63 64 } 65 66 public static String getRating() { 67 68 JFrame f; 69 70 f = new JFrame(); 71 72 String rating; 73 74 while (true) { 75 76 //System.out.print("Enter rating \'Excellent\' or \'Above Average\' or \'Average\': "); 77 78 rating = JOptionPane.showInputDialog(f,"Enter rating \'Excellent\' or \'Above Average\' or \'Average\': "); 79 80 if (rating.equals("Excellent")) break; 81 82 else if (rating.equals("Above Average")) break; 83 84 else if (rating.equals("Average")) break; 85 86 else { 87 88 JOptionPane.showMessageDialog(f, "Error: Invalid rating entered."); 89 90 } 91 92 } 93 94 return rating; 95 96 } 97 98 public static double getPremiumAmount() { 99 100 JFrame f; 101 f=new JFrame(); 102 double premium = 0; 103 104 do { 105 106 try { 107 108 //System.out.print("Enter premium amount: "); 109 110 String input = JOptionPane.showInputDialog(f,"Enter premium amount: "); 111 112 premium = Double.parseDouble(input); 113 114 } catch (NumberFormatException e) { 115 116 JOptionPane.showMessageDialog(f, "Error: Invalid number"); 117 118 premium = 0; 119 120 } 121 122 } while (premium <= 0); 123 124 return premium; 125 126 } 127 128 public static int getPolicyYears() { 129 130 JFrame f; 131 f=new JFrame(); 132 133 int policy = 0; 134 135 do { 136 137 try { 138 139 //System.out.print("Enter the number of years policy policy: "); 140 141 String input = JOptionPane.showInputDialog(f,"Enter the number of years policy policy: "); 142 143 policy = Integer.parseInt(input); 144 145 } catch (NumberFormatException e) { 146 147 JOptionPane.showMessageDialog(f, "Error: Invalid number"); 148 149 policy = 0; 150 151 } 152 153 } while (policy <= 0); 154 155 return policy; 156 157 } 158 159 public static double getDiscount(double premium, int years, String rating) { 160 161 if (years <= 3) return 0; 162 163 if (rating.equals("Excellent")) { 164 165 return premium * 0.15; 166 167 } else if (rating.equals("Above Average")) { 168 169 return premium * 0.10; 170 171 } else if (rating.equals("Average")) { 172 173 return premium * 0.05; 174 175 } else return 0; 176 177 } 178 179 public static boolean repeat() { 180 181 JFrame f; 182 f=new JFrame(); 183 String repeat = ""; 184 185 do { 186 187 //System.out.print("Do you want to calculate again Y=Yes N=No: "); 188 189 repeat = JOptionPane.showInputDialog(f,"Do you want to calculate again Y=Yes N=No: "); 190 191 } while (!(repeat.equals("Y") || repeat.equals("N"))); 192 193 return repeat.equals("Y"); 194 195 } 196 197 } 198
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
