2014-10-28 65 views
0

我有一個主類Test和另外兩個表示某些特定對象的類(Book,class Order)。從我的課Test我創建了5個Book對象。現在我想創建兩個使用Order類中的方法的Order對象。具體使用setCustomerName(),SetCustomerAddress(),toString()getTotlaPrice()和addBook()。設置setCustomerName()和SetCustomerAddress()的getters和setter後,我在測試類中沒有錯誤。我的問題是,如何在Order類中創建5個Book實例變量,如果它們被調用(從帶有addBook()的Test類),將被填充Test對象中創建的Book對象的成員數據(或參數)以便我可以在同一個類中的其他方法中使用它們。例如,在類Test中,如果我調用addBook(b1),Order類中的addBook()方法應該初始化或填充Book實例變量之一我想這是一個對象?)在Order中創建,並且Test類中引用的成員數據(帶有b#1-5)。這兩個班。我沒有把書類,因爲它只是創建書籍對象。很感謝任何形式的幫助!實例變量(來自另一個類的對象)

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class Test 
{ 
public static void main(String args[]) throws ParseException 
{ 
    SimpleDateFormat fmt = new SimpleDateFormat(Book.DATE_FORMAT); 

    // Creating Book-objects... 
    Book b1 = new Book(1, "Homo Faber", "Max Frisch", fmt.parse("01.01.1957"), -10); 
    Book b2 = new Book(2, "Harry Potter", "J.K. Rowling", fmt.parse("25.7.2000"), 45); 
    Book b3 = new Book(3, "Krieg und Frieden", "Leo Tolstoi", fmt.parse("24.01.1867"), 29); 
    Book b4 = new Book(4, "Freedom", "Jonathan Franzen", fmt.parse("08.06.2010"), 39); 
    Book b5 = new Book(5, "Goedel, Escher, Bach", "Douglas Hofstadter", fmt.parse("05.11.1979"),  42); 

    // Creating two orders containing theses books... 

    Order order = new Order(); 
    order.setCustomerName("Sophie Muster"); 
    order.setCustomerAddress("Mittelstrasse 10, 3011 Bern"); 
    order.addBook(b1);//Here i want to fill one of the Book instance variables (i guess this is  an object to?) created 
    order.addBook(b2);//in the Order class with the member data of the 
    order.addBook(b3);//Book objects referenced (with b#1-5) which i have created above. 
    order.addBook(b4); 
    order.addBook(b4); 
    order.addBook(b5); 
    System.out.println(order); 

    System.out.print("\n"); 

    Order order2 = new Order(); 
    order2.setCustomerName("Woody Allen"); 
    order2.setCustomerAddress("5th Avenue 7, 10001 New York"); 
    order2.addBook(b5); 
    System.out.println(order2); 
} 
} 

public class Order { 

private static int idCounter; 
private int id; 
private String customerName; 
private String customerAddress; 



// The Constructor 
public Order(int tmpId, String tmpCustomerName,String tmpCustomerAddress){ 
    if (idCounter == 1);{ 
     id = 1;} 
    if (idCounter == 2){ 
     id = 2;} 
    if (idCounter == 3);{ 
     id = 3;} 
    if (idCounter == 4){ 
     id = 4;} 
    if (idCounter == 5){ 
     id = 5;} 
    customerName = tmpCustomerName; 
    customerAddress = tmpCustomerAddress; 
    } 

public Order() { 
    id = 0; 
    customerName = "-"; 
    customerAddress = "-"; 
} 

// The methods 
public String toString() 
{ 
    return id + ", " + customerName + ", " + customerAddress; 
} 

public String addBook(){ 
    //HERE with this method i want to add some of the Book objects i have made in Test class 
    // ?? Book b1 = Test.b1(); ?? 

    return "0"; 

} 

public int getTotalPrice(){ 
    return 0; 

} 
public String getCustomerName() 
{ 
    return customerName; 
} 
public String setCustomerName(String tmpCustomerName){ 
    customerName = tmpCustomerName; 
    return customerName; 
} 
public String getCustomerAddress() 
{ 
    return customerAddress; 
} 
public String setCustomerAddress(String tmpCustomerAddress){ 
    customerAddress = tmpCustomerAddress; 
    return customerAddress; 
} 
} 
+2

寫入問題時有時會出現輸入錯誤。 – 2014-10-28 21:15:48

+0

好的,我很抱歉,對不起! – Kisos 2014-10-28 21:17:03

+0

但是,爲什麼要填充訂單類中的數據或在訂單類中創建新的書對象? – conFusl 2014-10-28 21:17:46

回答

0

您可以在類中添加一個列表Order,類似於

private List<Book> books = new ArrayList<Book>(); 

你的方法可能是這樣的:

public String addBook(Book book){ 
    books.add(book); 
    return "Book added"; 
} 

所以,你可以處理,添加,刪除,更新書籍在您的訂單!

+0

首先:當然,我會複製他的答案並在<1分鐘後發佈!第二:這是一個明顯的解決方案,所以我想知道只有兩個這樣的答案!具有Java知識的每個人都會建議這個解但是如果你覺得自己感覺好一些,對我來說沒問題,我可以給你看幾千個相同答案的帖子,所以你可以把它們全部倒下! – conFusl 2014-10-28 21:28:55

+0

謝謝你的回答。最後搞清楚了。 – Kisos 2014-10-29 10:28:26

0

您應該簡單地增加一個List<Book>Order和初始化:

public class Order { 

    List<Book> books = new ArrayList<>(); 

    public String addBook(Book book){ 
     books.add(book); 
     return "0"; 
    } 

    ... 
} 

然後你只需在順序類迭代books拿到總價格,轉換爲字符串,...

相關問題