Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FIX ERRORS IN THE CODE. IT MUST COMPILE import javax.swing.JOptionPane; public class DCBusTours { // Constants private static final int MAX_NUM_BUS_TOURS = 15; private static

FIX ERRORS IN THE CODE. IT MUST COMPILE

import javax.swing.JOptionPane;

public class DCBusTours { // Constants private static final int MAX_NUM_BUS_TOURS = 15; private static final double TICKET_COST = 10.0;

// Fields private BusTour[] busTours = new BusTour[MAX_NUM_BUS_TOURS]; private int numBusTours = 0;

// Menu options private static final String CREATE_BUS_TOUR = "Create Bus Tour"; private static final String REMOVE_BUS_TOUR = "Remove Bus Tour"; private static final String SELL_TOUR_TICKET = "Sell Tour Ticket"; private static final String DISPLAY_BUS_TOURS = "Display Bus Tours"; private static final String EXIT = "Exit";

// Main method public static void main(String[] args) { DCBusTours app = new DCBusTours(); app.run(); }

// Helper methods

// Displays a menu and returns the user's choice private String getMenuChoice() { String[] options = {CREATE_BUS_TOUR, REMOVE_BUS_TOUR, SELL_TOUR_TICKET, DISPLAY_BUS_TOURS, EXIT}; return (String) JOptionPane.showInputDialog(null, "Choose an option:", "DC Bus Tours", JOptionPane.PLAIN_MESSAGE, null, options, options[0]); }

// Displays an error message with the specified text private void showError(String message) { JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE); }

// Creates a new bus tour private void createBusTour() { if (numBusTours >= MAX_NUM_BUS_TOURS) { showError("Maximum number of bus tours reached."); return; }

String tourId = null; String tourName = null; double tourCost = 0.0;

// Get the tour ID while (tourId == null) { String input = JOptionPane.showInputDialog("Enter the Bus Tour Identifier:"); if (input == null) { return; // User clicked Cancel } try { tourId = validateTourId(input); } catch (IllegalArgumentException e) { showError(e.getMessage()); } }

// Get the tour name while (tourName == null) { String input = JOptionPane.showInputDialog("Enter the Bus Tour Name:"); if (input == null) { return; // User clicked Cancel } tourName = input.trim(); if (tourName.isEmpty()) { showError("Bus Tour Name cannot be empty."); tourName = null; } }

// Get the tour cost while (tourCost <= 0.0) { String input = JOptionPane.showInputDialog("Enter the General Ticket Cost:"); if (input == null) { return; // User clicked Cancel } try { tourCost = Double.parseDouble(input); if (tourCost <= 0.0) { throw new IllegalArgumentException("Ticket cost must be positive."); } } catch (NumberFormatException e) { showError("Invalid ticket cost."); } catch (IllegalArgumentException e) { showError(e.getMessage()); } }

// Create the new bus tour BusTour newTour = new BusTour(tourId, tourName, tourCost); busTours[numBusTours++] = newTour;

JOptionPane.showMessageDialog(null, "Bus Tour created successfully: " + newTour.toString(), "DC Bus Tours", JOptionPane.INFORMATION_MESSAGE); }

// Removes an existing bus tour private void remove() { // If no bus tours have been created, display an error message and return if (numBusTours == 0) { JOptionPane.showMessageDialog(null, "No bus tours have been created yet."); return; }

// Create a list of the bus tours String[] tourList = new String[numBusTours]; for (int i = 0; i < numBusTours; i++) { tourList[i] = busTours[i].getId() + " - " + busTours[i].getName(); }

// Display a dialog box to select a bus tour to remove String selectedTour = (String) JOptionPane.showInputDialog( null, "Select a bus tour to remove:", "Remove Bus Tour", JOptionPane.PLAIN_MESSAGE, null, tourList, tourList[0]);

// If the user clicked cancel or closed the dialog box, return if (selectedTour == null) { return; }

// Find the index of the selected bus tour in the array int index = -1; for (int i = 0; i < numBusTours; i++) { if (tourList[i].equals(selectedTour)) { index = i; break; } }

// If the index is invalid, display an error message and return if (index == -1) { JOptionPane.showMessageDialog(null, "Invalid bus tour selected."); return; }

// Remove the selected bus tour from the array and decrement the number of bus tours busTours[index] = null; for (int i = index; i < numBusTours - 1; i++) { busTours[i] = busTours[i + 1]; } busTours[numBusTours - 1] = null; numBusTours--;

// Display a success message JOptionPane.showMessageDialog(null, "Bus tour successfully removed."); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions