Problem
Collections are useful components for managing the relationship between objects. Using the class model from the slides, implement any other relationships that were not covered in the slide session, use an ArrayList for the moment. Think carefully how you want to store things and how they will be retrieved.
Implement the getTotalCost() method on the Order class.
Solution
Starting Point - v02 | Solution - v03 |
---|
public class Customer
{
private LocalDate dob;
private String address;
private String fname;
private String lname;
private Account itsAccount;
public Customer()
{
}
public void setAccount( Account acc )
{
itsAccount = acc;
}
public Account getAccount()
{
return itsAccount;
}
public LocalDate getDob()
{
return dob;
}
public void setDob(LocalDate dob)
{
this.dob = dob;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getFname()
{
return fname;
}
public void setFname(String fname)
{
this.fname = fname;
}
public String getLname()
{
return lname;
}
public void setLname(String lname)
{
this.lname = lname;
}
}
|
public class Customer
{
private LocalDate dob;
private String address;
private String fname;
private String lname;
private Account itsAccount;
public Customer( Account acc )
{
itsAccount = acc;
}
public Customer(Account acc, String fname, String lname, LocalDate dob, String address)
{
itsAccount = acc;
this.dob = dob;
this.address = address;
this.fname = fname;
this.lname = lname;
}
...
}
|
public class Account
{
private int account_id;
private LocalDate date_opened;
public Account()
{
}
public LocalDate getDateCreated()
{
return date_opened;
}
public void setDateCreated( LocalDate date )
{
date_opened = date;
}
public void setDateCreated( String date )
{
date_opened = LocalDate.parse(date);
}
public int getAccountId()
{
return account_id;
}
}
|
public class Account
{
private int account_id;
private LocalDate date_opened = LocalDate.now();
private ArrayList orders = new ArrayList(10);
public void addOrder( Order anOrder )
{
orders.add( anOrder );
}
public ArrayList getOrders()
{
return orders;
}
...
}
|
public class Order
{
private int order_no;
private double total_cost;
private LocalDate order_date;
public int getOrderNo()
{
return order_no;
}
public void setOrderNo(int orderno)
{
order_no = orderno;
}
public LocalDate getDateCreated()
{
return order_date;
}
public void setDateCreated(LocalDate date)
{
order_date = date;
}
public void setDateCreated(String date)
{
order_date = LocalDate.parse(date);
}
public double getTotalCost()
{
double result = 0.0;
return result;
}
}
|
public class Order
{
private int order_no;
private double total_cost;
private LocalDate order_date;
private ArrayList items = new ArrayList(1);
public void addItem(Item anItem)
{
items.add(anItem);
}
public int getOrderNo()
{
return order_no;
}
public double getTotalCost()
{
double result = 0.0;
for (Object obj : items)
{
Item theItem = (Item) obj;
result += theItem.getTotalCost();
}
return result;
}
...
}
|
public class Item
{
private double item_cost;
private int quantity;
private int unique_id;
public Item()
{
}
public double getItemCost()
{
return item_cost;
}
public int getQuantity()
{
return quantity;
}
public int getUniqueId()
{
return unique_id;
}
public double getTotalCost()
{
return quantity * item_cost;
}
public void setItem_cost(double item_cost)
{
this.item_cost = item_cost;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public void setUnique_id(int unique_id)
{
this.unique_id = unique_id;
}
}
| No Change |
public class Product
{
private String description;
private double price;
private String product_code;
private int quantity;
public Product()
{
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public String getProductCode()
{
return product_code;
}
public void setProductCode(String product_code)
{
this.product_code = product_code;
}
public int getQuantity()
{
return this.quantity;
}
public void setQuantity( int qty )
{
this.quantity = qty;
}
}
| No Change |
|
|
|
|
|
|
Related articles
-
Page:
-
Page:
-
Page:
-
Page:
-
Page: