实例变量(来自另一个类的对象)

时间:2014-10-28 21:12:38

标签: java class object instance

我有一个主类Test和另外两个(类Book,类Order)代表一些特定对象。从我的课程Test I创建5个Book对象。现在我想创建两个使用Order类方法的Order对象。具体使用setCustomerName(),SetCustomerAddress(),toString()getTotlaPrice()和addBook()。在为setCustomerName()和SetCustomerAddress()设置getter和setter之后,我在Test Class中没有错误。我的问题是如何在Order类中创建5个Book实例变量,如果调用它们将填充Test类中创建的Book对象的成员数据(或参数)(来自带有addBook()的Test类),所以我可以在同一个类中的其他方法中使用它们。例如在类Test中,如果我调用addBook(b1),Order类中的addBook()方法应该初始化或填充其中一个Book实例变量(我猜这是一个对象?)在Order中创建,其中包含Test类中引用的成员数据(使用b#1-5)。这是两个类。我没有把Book类,因为它只是创建了Book对象。非常感谢任何帮助!

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 个答案:

答案 0 :(得分:0)

您只需在List<Book>中添加Order并初始化它:

public class Order {

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

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

    ...
}

然后,您只需在订单类中迭代books即可获得总价,转换为字符串,...

答案 1 :(得分:0)

您可以在类Order中添加List,例如

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

您的方法可能如下所示:

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

因此,您可以在订单中处理,添加,删除,更新图书!