summaryrefslogtreecommitdiff
path: root/oop/09-polymorphism/CustomerApp.java
blob: 79e1c3153f991d69a9de971402af28a2fb099489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class CustomerApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List list = new LinkedList();
		
		Customer c = new Customer(1, "Kamal", "Nugegoda", "123456", "654321");
		list.add(c);

		c = new Customer(2, "Nimal", "Colombo", "2234343", "2343433");
		list.add(c);

		c = new PremiumCustomer(1, "Sunil", "Kohuwala", "23443434", "9888234",
				"a", "Give him whatever he ask");
		list.add(c);
		
		list.add(new Date());
		
		Iterator itr = list.iterator();
		
		CustomerDetailsFormatter formatter = new CustomerDetailsFormatter();
		
		while (itr.hasNext()) {
			Customer customer = (Customer) itr.next(); // Convert "java.lang.Object" to "Customer"
			String formattedString = formatter.getFormattedDescription(customer);
			System.out.println(formattedString);
		}

		/*
		Account a1 = new SavingsAccount(); // Coercion (implicit conversion) 
		a1.accountClassMethod();
		
		Account a1 = someMethod();
		SavingsAccount sa = (SavingsAccount) a1; // Down casting
		sa.savingsAccountClassMethod();
		*/
	}
}