You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 3
Next »
Problem
Think about the type of constructors required by each class and implement them accordingly.
Implement an equals() method on the Item and Product classes. Think about equality for the objects of these classes, what does it mean? Whatever you decide, implement it.
Solution
Starting Point - v03 | Starting Point - 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 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 Account
{
private int account_id;
private LocalDate date_opened;
private ArrayList orders = new ArrayList(10);
public Account()
{
}
public void addOrder( Order anOrder )
{
orders.add( anOrder );
}
public ArrayList getOrders()
{
return orders;
}
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 Account( int accId )
{
account_id = accId;
}
public Account()
{
}
public void addOrder( Order anOrder )
{
orders.add( anOrder );
}
public ArrayList getOrders()
{
return orders;
}
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 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 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;
for (Object obj : items)
{
Item theItem = (Item) obj;
result += theItem.getTotalCost();
}
return result;
}
}
|
public class Order
{
private int order_no;
private double total_cost;
private LocalDate order_date;
private ArrayList items = new ArrayList(1);
public Order()
{
}
public void addItem(Item anItem)
{
items.add(anItem);
}
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;
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;
}
}
|
public class Item
{
private double item_cost;
private int quantity;
private int unique_id;
public Item(double item_cost, int quantity, int unique_id)
{
this.item_cost = item_cost;
this.quantity = quantity;
this.unique_id = 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;
}
}
|
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;
}
}
|
public class Product
{
private String description;
private double price;
private String product_code;
private int quantity;
public Product()
{
}
public Product(String description, double price, String product_code)
{
this.description = description;
this.price = price;
this.product_code = product_code;
}
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;
}
}
|
public class MainUnit
{
public static void main( String []args )
{
Account acc = new Account( 1 );
ScreenWriter sw = Writer.getScreenWriter();
sw.writeAccount(acc);
System.out.println("");
Customer cc = new Customer( acc, "Selvyn", "Wright", LocalDate.of(1965, 5, 8), "Birmingham, UK");
sw.writeCustomer(cc);
System.out.println("");
}
}
|
|
|
|
|
|
Related articles
-
Page:
-
Page:
-
Page:
-
Page:
-
Page: