0

How can i print all the array values entered in private void display car() including licence no.(String), hours and fee. then search for licence No(string) in array in Search car()using gui and print result in console with licence no. hours and fee. If search is not found display error.

Scanner inputMenuChoice = new Scanner(System.in);



private int getMenuItem()
{
    System.out.println("\nPlease select from the following");
    System.out.println(ENTER_CAR + ". Enter licence plate and hours stayed");
    System.out.println(DISPLAY_CARS + ". Display all licence plates, hours and fees");
    System.out.println(DISPLAY_STATISTICS + ". Display Statistics");
    System.out.println(SEARCH_CARS + ". Search for car");
    System.out.println(EXIT + ". Exit the application");
    System.out.print("Enter choice==> ");
    return inputMenuChoice.nextInt();
}


private void processCars()
{
    int choice = getMenuItem();
    while (choice != EXIT)
    {
        switch (choice)
        {
            case ENTER_CAR:
                enterCar();
                break;
            case DISPLAY_CARS:
                displayAllCars();
                break;
            case DISPLAY_STATISTICS:
                displayStatistics();
                break;
            case SEARCH_CARS:
                searchCar();
                break;
            default:
                System.out.println("ERROR choice not recognised");
        }
        choice = getMenuItem();
    }
}


private void enterCar()
{

            String licPlate="",hr="";
            int d=1,carMax,numHr;
            int i=0;
            Scanner sa=new Scanner(System.in);
                                                                                                //for(int i=0;i<a.length;i++)
            {
                licPlate=JOptionPane.showInputDialog(null, "Please enter the license plate of the car");
                while(licPlate.isEmpty())
                {
                    if(licPlate.isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "Error - Licence Plate cannot be blank");
                        licPlate=JOptionPane.showInputDialog(null, "Please enter the license plate of the car");
                    }
                }
                hr=JOptionPane.showInputDialog(null, "Enter the number of hour(s) car was parked (1-12)");
                numHr=Integer.parseInt(hr);
                while(numHr>12 || numHr<1)
                {
                    if(numHr>12 || numHr<1)
                    {
                        JOptionPane.showMessageDialog(null, "Error - Hours must be between 1 and 12", "Error", JOptionPane.ERROR_MESSAGE);
                        hr=JOptionPane.showInputDialog(null, "Enter the number of hour(s) car was parked (1-12)");
                    }
                }
                System.out.printf("Details for car %d entered\n",d);
                a[i]=new Car(licPlate,numHr);
                d++;
            }
        {System.out.println(a[i]);}
        i++;

}


private void displayAllCars()
{
}


private void displayStatistics()
{
}


private void searchCar()
{
}


public static void main(String [] args)
{
    CarPark app = new CarPark();

    app.processCars();
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

0

You can do the following: 1. @Override the toString() method in your Car class in order to define how a car is displayed. 2. Use Arrays.toString(a) to print the array of Cars.

Community
  • 1
  • 1
0

This is your answer use this segmenty to your code,

class CarSearch
{
public static void main (String[] args) throws java.lang.Exception
{
    ArrayList<Car> carList= new ArrayList<Car>();
    carList.add(new Car("sdfguis3edha",10,9));
    carList.add(new Car("gfujuukedha",15,29));
    carList.add(new Car("uou9is3edha",99,98));

System.out.println(carList); //print all details

//search

//Search for licence "gfujuukedha"
boolean found=false;
for(Car c: carList){
    if(c.getLicence().equalsIgnoreCase("gfujuukedha"))
{   System.out.println("found details "+ c);
found=true;
    }
}
if(!found){
    System.out.println("This car is not there");
}
}
}

class Car{
String  licence;
int hours, fee;

Car(String licence, int hours, int fee ){
    this.licence= licence;
    this.hours=hours;
    this.fee = fee;
}

String getLicence(){
    return this.licence;
}

int getHours(){
    return this.hours;
}

int getFee(){
    return this.fee;
}

 public String toString() { 
return "Licence: "+this.licence+" Hours: "+ this.hours+" Fees: "+this.fee+"\n";
 }

 }
Sumon Mal
  • 71
  • 4