0

私は 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');
}

}

4

2 に答える 2

1

bookingListタイプのオブジェクトを格納することを意図しているように見え、リストから取得中にVehicleBookingそのオブジェクトをキャストしようとしていますRVBooking

private static ArrayList<VehicleBooking> bookingList = new ArrayList<VehicleBooking>();

RVBookingのスーパークラスでない限りVehicleBookingVehicleBookingにキャストできませんRVBooking

于 2013-05-15T14:50:56.370 に答える