Source code: org/mandarax/eca/example/Portfolio.java
1 /*
2 * Copyright (C) 1998-2002 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package org.mandarax.eca.example;
20
21 import java.util.*;
22
23 /**
24 * Class representing a portfolio of investments (e.g., shares).
25 * @author <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
26 */
27 public class Portfolio {
28 private List investments = new ArrayList();
29
30 /**
31 * Constructor.
32 */
33 public Portfolio() {
34 super();
35
36 // add sample data
37 add(new Investment ("MSFT","Microsoft",50,60.45,"9/25/00"));
38 add(new Investment ("BMW","Bayerische Motorenwerke",200,60.45,"9/25/00"));
39 add(new Investment ("LHA","Lufthansa",100,60.45,"9/25/00"));
40 add(new Investment ("IMPAY","Impala Platinum",250,60.45,"9/25/00"));
41 add(new Investment ("TUI","TUI",100,34.77,"6/25/01"));
42 add(new Investment ("AAH3"," AHLERS AG",300,15.5,"1/1/00"));
43 }
44 /**
45 * Add an investment.
46 * @param inv an investment
47 */
48 public void add(Investment inv) {
49 investments.add(inv);
50 }
51 /**
52 * Remove an investment.
53 * @param inv an investment
54 */
55 public void remove(Investment inv) {
56 investments.remove(inv);
57 }
58 /**
59 * Get the total value of all investments.
60 * @return a (double) value
61 */
62 public double getTotalValue() {
63 double value = 0;
64 for (Iterator iter = investments.iterator();iter.hasNext();) {
65 Investment inv = (Investment)iter.next();
66 value = value + inv.getNumber()*inv.getValue();
67 }
68 return value;
69 }
70 /**
71 * Get the relative value of a portfolio in an investment.
72 * @param inv an investment
73 * @return a (double) value
74 */
75 public double getRelativeValue(Investment inv) {
76 return 100*inv.getValue()*inv.getNumber()/getTotalValue();
77 }
78 /**
79 * Get a list of all investments.
80 * @return a list
81 */
82 public List getInvestments() {
83 return investments;
84 }
85
86 }