简体   繁体   中英

Instance Variables (object from another class)

I have one main class Test and two others (class Book , class Order) that represent some specific objects. From my class Test I create 5 Book objects. Now i want to create two Order objects that use methods from Order class. To be specific use setCustomerName(), SetCustomerAddress(), toString() getTotlaPrice() and addBook().After i set getters and setters for setCustomerName() and SetCustomerAddress() i have no errors for them in the Test Class. My question is how can i create 5 Book instance variables in the Order class that will be filled with the member data (or parameters) of the Book objects created in the Test class if they are called(from Test class with addBook()), so that i can use them in the other methods in the same class.So for example in class Test if i call addBook(b1) the addBook() method which is in the Order class should initialize or fill one of the Book instance variables (i guess this is an object to?) created in Order with the member data of the one referenced (with b#1-5) in the Test class. These are the two classes. I havent put the Book class because it just creates the Book object. Any help is very much appreciated!

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

You should simply add an List<Book> in Order and initialize it :

public class Order {

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

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

    ...
}

Then you simply iterate books in the order class to get total price, convert to String, ...

You can add a List in the class Order, something like

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

Your method could look like:

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

So you can handle, add, remove, update books in your order!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM