我在以下位置收到 ClassCastException:
RVBooking rvbooking = (RVBooking) bookingList.get(iweight);
我想存储用户的 weightvehicle 输入并调用 recordWeight() 方法。抱歉,如果我没有清楚地解释这一点。提前致谢。
堆栈跟踪
FerryBookingSystem [Java Application]
bookingSystem.FerryBookingSystem at localhost:51019
Thread [main] (Suspended (exception ClassCastException))
FerryBookingSystem.recordVehicleWeight() line: 144
FerryBookingSystem.main(String[]) line: 191
C:\Program Files\Java\jre7\bin\javaw.exe (16/05/2013 12:42:10 AM)
预订异常
class BookingException extends Exception{
String message;
String IDs;
public BookingException(String message){
this.message = message;
}
public BookingException(String message, String IDs){
this.message = message;
this.IDs = IDs;
}
public String getIDs(){
return IDs;
}
public String getMessage(){
return message;
}
}
FerryBookingSystem 类
public class FerryBookingSystem {
private static ArrayList<VehicleBooking> bookingList = new ArrayList<VehicleBooking>();
private static ArrayList<RVBooking> rvbookingList = new ArrayList<RVBooking>();
private static Scanner userInput = new Scanner (System.in);
public static int bookingIDExists(String IDs){
if (bookingList.size() == -1){
return -1;
}
for (int i = 0; i < bookingList.size(); i++){
if(bookingList.get(i).getbookingID().equals(IDs)){
return i;
}
}
return -1;
}
public static int rvbookingIDExists(String IDs){
if (rvbookingList.size() == -1){
return -1;
}
for(int i = 0; i < rvbookingList.size(); i++){
if(rvbookingList.get(i).getbookingID().equals(IDs)){
return i;
}
}
return -1;
}
public static boolean recordVehicleWeight(){
String booking_ID;
double weight = 0;
int iweight = 0;
System.out.print("Please enter the booking ID: ");
booking_ID = userInput.nextLine();
if(bookingIDExists(booking_ID) != -1){
if(rvbookingIDExists(booking_ID) != 1){
RVBooking rvbooking = (RVBooking) bookingList.get(iweight);
System.out.print("Please enter the weight of the vehicle: ");
weight = userInput.nextDouble();
iweight = (int)weight;
rvbooking.recordWeight(iweight);
return true;
}
else{
System.out.println("Error - cannot record weight for vehicle booking!");
return false;
}
}
else{
System.out.println("The booking ID does not exist, Please try again.");
return false;
}
}
public static void main (String [] args) throws IOException{
char user;
do{
System.out.println("**** Ferry Ticketing System ****");
System.out.println(" A - Add Vehicle Booking");
System.out.println(" B - Add Recreational Vehicle Booking");
System.out.println(" C - Display Booking Summary");
System.out.println(" D - Update Insurance Status");
System.out.println(" E - Record Recreational Vehicle Weight");
System.out.println(" F - Compile Vehicle Manifest");
System.out.println(" X - Exit");
System.out.print("Enter your selection: ");
String choice = userInput.nextLine().toUpperCase();
user = choice.length() > 0 ? choice.charAt(0) : '\n';
if (choice.trim().toString().length()!=0){
switch (user){
case 'A':
addVehicleBooking();
break;
case 'B':
addRecreationalVehicleBooking();
break;
case 'C':
displayBookingSummary();
break;
case 'D':
updateInsuranceStatus();
break;
case 'E':
recordVehicleWeight();
break;
case 'F':
break;
case 'X':
break;
default:
break;
}
}
}while(user!='X');
}
}