2016-05-26 12 views
0

Wie kann ich alle Array-Werte drucken, die in private void display car() eingegeben wurden, einschließlich Lizenznummer (String), Stunden und Gebühren. Dann suchen Sie nach der Lizenz Nein (string) in Array in Search car() mit gui und print Ergebnis in Konsole mit Lizenz-Nr. Stunden und Gebühr. Wenn die Suche nicht gefunden wird, wird der Fehler angezeigt.Wie suche ich nach einem String in einem Array und drucke alle Arrays?

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(); 
} 
} 
+2

Warum nicht versuchen Sie es zuerst zu tun? Dies ist kein Code-Schreibdienst. – Lee

Antwort

0

Das ist Ihre Antwort dieses segmenty zu Ihrem Code verwenden,

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"; 
} 

} 
Verwandte Themen