How do I correctly implement user-defined exceptions that can bethrown by one of the methods as part of the validation or errorchecking, specifically for the carName object?
12 13 package cmis242week5; 14 15 import java.util.Scanner; 16 17 public class WK5V 18 19 static Scanner input = new Scanner (System.in); // create scanner 20 // parent class public static class VehicleRental { 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 292828828685 60 61 63 64 66 67 // attributes private int daysRented; { // constructor public VehicleRental (int daysRented) { this daysRented = daysRented; } // end constructor // get methods public int getDays Rented () { return this. daysRented; } // calculate rental fee public double calculateTotalFee () { double totalFee = daysRented * 35; return totalFee; } } // end VehicleRental Class // Car class public static class CarRental extends VehicleRental { // attributes public char carType; public String carName; // constructor. public CarRental (char carType, int daysRented) { } super (daysRented); this.carType = carType; } // overloading constructor // for renting a specific car public CarRental (char carType, int daysRented, String carName) { super (daysRented); this.carName = carName; this.carType = carType; Sunday, July 3, 2022, 6:42 PM // overloaded constructor public CarRental (int daysRented) { Page 1
69 70 1 22 73 74 75 75 77 10 71 72 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 } // get method public char getCarType () { return carType; } public String getCarName () { return carName; } super (daysRented); // calculate fee for car type. @Override public double calculateTotalFee () { double totalFee = getDaysRented () * 35; if (! (carName == null)) { totalFee += 20; } } if (carType == 'E') { // economy vehicle totalFee += 95.97; } else if (carType == 'S') { // standard vehicle totalFee += 97.99; // method to display car rental information public String toString() { } else if (carType == 'F') { // fullsize vehicle totalFee += 98; } return totalFee; } "1 Name = return "Car [ Type = "+getCarType () + getCarName () + ", Days = " + getDaysRented () + "]"; I } } // end Car class. // Van class public static class VanRental extends Vehicle Rental { } // attributes private char vanType; private int numSeats; Sunday, July 3, 2022, 6:42 PM // constructor public VanRental (char vanType, int numSeats, int daysRented) { super (daysRented); this.vanType = vanType; this.numSeats = numSeats; //overloading public VanRental (int numSeats, int daysRented) { super (daysRented); this.numSeats = numSeats; Page 2 11 +
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 // get methods public char getVanType () { return vanType; 11 } public int getNumSeats () { return numSeats; } // calculate fee for van type @Override public double calculateTotalFee () { } } double totalFee = getDaysRented ()* 35; if (vanType == 'M') { // minivan vehicle selection totalFee += 45; if (numSeats >= 5) { totalFee += 20; getNumSeats () } } } else if (vanType == 'P') { // passenger vehicle selection totalFee += 50; if (numSeats >= 12) { totalFee += 30; } } else{ // if user doesn't have a preference totalFee += 40; if (numSeats>=10) { totalFee += 25; // display information public String toString() { return "Van [ Type = " + } } return totalFee; + ", Days = " + getDays Rented () + "1"; Sunday, July 3, 2022, 6:42 PM } // method to add car public static void addCarRental () { System.out.println("Are you renting an economy, standard, or " + "fullsize car + "[E, S, or F]: "); char carType = input.next().toUpperCase().charAt(0); if (! (carType== 'E' || carType== 'S' || carType== 'F')) { System.out.println("Invalid car selection! \n"); return; getVanType() + + getVanType () + ", Number of seats = return; } String carName = ""; System.out.println("Do you want to rent a specific car? [y or n]: "); char choice = input.next().toUpperCase().charAt(0); if (! (choice == 'Y' || choice == 'N')) { System.out.println ("Invalid car selection! \n"); 11 Page 3
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 if (choice == 'Y') { System.out.println("Enter Car Name: "); carName = input.next(); System.out.println ("How many days are you renting the car?: "); int daysRented = input.nextInt (); } // create instance of car and display CarRental car; if (choice == 'Y') { car = new CarRental (carType, daysRented, carName); } else { car = new CarRental (carType, daysRented); } // display car information System.out.print (car.toString()); System.out.printf("\nTotal fee = $%.2f\n", car.calculateTotalFee () ); System.out.println(" "); } // method to add van public static void addVanRental() { System.out.println ("Are you renting a minivan, passenger van, + "or no preference [M, P or O]: "); char vanType = input.next().toUpperCase().charAt(0); if (! (vanType== 'M' || vanType== 'P' || vanType== '0')) { System.out.println("Invalid van selection! \n"); } Sunday, July 3, 2022, 6:42 PM System.out.println("Enter the number of seats you need: "); int numSeats = input.nextInt (); System.out.println ("How many days are you renting the van?"); int daysRented = input.nextInt (); } else { // create instance of van if (vanType == '0') { VanRental van = new VanRental (numSeats, daysRented); System.out.print (van.toString()); System.out.printf("Total fee = $%.2f\n", van.calculateTotalFee () ); } } // end van class. VanRental van1 = new VanRental (vanType, numSeats, daysRented); System.out.print (vanl.toString() ); System.out.printf ("Total fee = $%.2f\n", vanl.calculateTotalFee () ); public static void main(String[] args) { while (true) { // menu selections System.out.println("Vehicle rental program: \n"); System.out.println ("1: Rent a Car"); System.out.println("2: Rent a Van"); System.out.println ("9: Exit program\n"); System.out.println("Enter your selection: "); 11 Page 4
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264} int choice input.nextInt (); switch (choice) { case 1: addCarRental(); break; case 2: } addvanRental(); break; case 9: = System.out.println("Thank you for renting with us. Goodbye!"); System.exit(0); break; default: break; Sunday, July 3, 2022, 6:42 PM System.out.println("Invalid, choose from the menu.\n"); Page 5
How do I correctly implement user-defined exceptions that can be thrown by one of the methods as part of the validation
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am