Part 4 Guidance

Problem

Think about the type of constructors required by each class and implement them accordingly.  This will not be the final version of the classes, they may have to be be modified again.

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 - v03Starting Point - v03
Customer Class
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;
    }
}
Customer Class
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;
    }
}
Account Class
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;
    }
}
Account Class
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;
    }
}
Order Class
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;
    }
}
Order Class
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;
    }
}
Item Class
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;
    }
}
Item Class
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;
    }
}
Product Class
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;
    }
}
Product Class
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;
    }
}
Product Class
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("");
    }
}