2017-10-05 3 views
0

私はまだJavaでかなり新しいので、私がここで激しく思っていると躊躇しないでください。ドライバクラスにあるいくつかのフィールドを持つJavaでオブジェクトを作成するにはどうすればよいですか?

私は、複数のオブジェクトの青写真のクラス、メニュークラス、およびドライバクラスを持つJavaプログラムを持っています。ドライバクラスはmenuを呼び出します。メニュークラスでは、4つのフィールドのうち1つだけをインスタンス化しながら顧客オブジェクトを作成します。フィールドは一意のIDフィールドです。私はドライバクラスにあるArrayListから他の3つのフィールドを取得したい。別のクラスのArrayListから顧客オブジェクトを選択するにはどうすればよいですか?

私が作成しようとしている最初のオブジェクト。

public class Customer { 

private int id; 
private String name; 
private String address; 
private String phone; 

    public static int count = 100; 

public Customer(String name, String address, String phone) { 

    this.id = count; 
    this.name = name; 
    this.address = address; 
    this.phone = phone; 
      count++; 
} 

}

予約がAirlineDriverと呼ばれる顧客ドライバクラスの

public class Reservation { 

static Scanner scan = new Scanner(System.in); 
private Customer customer; 
private Flight flight; 
private int partySize; 
private double reservationCost; 



final private double FIRST_CLASS_COST = 850.00; 
final private double ECONOMY_COST = 450.00; 

public Reservation(Customer customer, Flight flight, int partySize, double reservationCost) { 
    this.customer = customer; 
    this.flight = flight; 
    this.partySize = partySize; 
    this.reservationCost = reservationCost; 
} 

を有し、お客様のArrayListがあります。以下のコードでは、Customerオブジェクトを作成してドライバのArrayListにCustomersの1つを取得する必要がある場合、予約を作成するにはどうすればよいですか?

public class Menu { 
static Scanner scan = new Scanner(System.in); 


public Reservation createReservation() { 

    Customer cust = new Customer(); 
    Flight flight; 
    Reservation reservation; 

    System.out.println("Are you a returning customer? (Y or N)"); 
    String w = scan.nextLine(); 
    while (!"Y".equals(w) || !"y".equals(w) || !"N".equals(w) || !"n".equals(w)) { 
     System.out.println("Incorrect key, please enter Y for Yes, and N for No."); 
     w = scan.nextLine(); 
    } 
    if (w.equalsIgnoreCase("Y") || w.equalsIgnoreCase("y")) { 
     System.out.println("Welcome back and thank you for flying with us."); 
     System.out.println("What is your Customer ID?"); 
     int custID = scan.nextInt(); 

    } 

お客様が既に存在する場合は、既にこのArrayListに含まれています。

public class AirlineDriver { 

private static Scanner files; 

public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    ArrayList<Customer> cust = new ArrayList<Customer>(); 

答えて

0

ことを確認し、あなたはCustomerクラス内のすべてのgetterメソッドを宣言しました。なぜなら、私は宣言しなかったからです。 ArrayList<Customer>

for(Customer custom : cust) 
{ 
    // call all your getter method from Customer class. 
    String customerName = custom.getName(); 
} 
+0

AirlineDriverはメニュー駆動型のプログラムです。別のクラスに顧客を作成する場合、ArrayListを反復処理するにはどうすればよいですか? MenuクラスをAirlineDriverに移動する必要がありますか? – miler4salem

+0

@ miler4salemあなたは、移入するために移入されたリストを取得する必要があります – Ravi

関連する問題