2016-05-26 6 views
0

ライセンス番号(文字列)、時間および料金を含むprivate void display car()に入力されたすべての配列値を出力するにはどうすればいいですか? 次に、guiを使用してSearch car()で配列のライセンス番号(文字列)を検索し、ライセンス番号のコンソールで結果を表示します。時間と手数料。検索が見つからない場合、エラーが表示されます。配列内の文字列を検索してすべての配列を出力するにはどうすればよいですか?

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

これは、あなたの答えはあなたのコードにこのsegmentyを使用しているのですか?これはコード作成サービスではありません。 – Lee

答えて

0

あなたは、次の操作を行うことができます:車の表示方法を定義するために、あなたの車のクラスで 1. @Override the toString() methodを。 2. Arrays.toString(a)を使用して、車のアレイを印刷します。

0

なぜあなたが最初にそれをやってみません

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

} 
関連する問題