Monday, 15 February 2010

Java inheritance issue -



Java inheritance issue -

i new programming , having issue programme should homecoming output:

portfolio #00001, asd = 42.50, dfas = 45.00, cac = 22.20, bdm = 52.50 portfolio #00002, asd = 42.50, dfas = 45.00, cac = 22.20, bdm = 52.50 portfolio #00001, asd = 43.35, dfas = 45.90, cac = 22.64, bdm = 53.55 portfolio #00002, asd = 43.35, dfas = 45.90, cac = 22.64, bdm = 53.55 portfolio #00001, asd = 41.18, dfas = 43.61, cac = 21.51, bdm = 50.87 portfolio #00002, asd = 41.18, dfas = 43.61, cac = 21.51, bdm = 50.87 portfolio #00001, asd = 43.65, dfas = 46.22, cac = 22.80, bdm = 53.92 portfolio #00002, asd = 43.65, dfas = 46.22, cac = 22.80, bdm = 53.92

currently, programme not returning anything. please help?

here classes :

displayelement interface :

public interface displayelement { public void display(); }

observer interface :

public interface observer { public void update (map<string,double> pricemap); }

you need register protfolios after creation, , before calling setprices(), required observer pattern: programme should :

import java.util.arraylist; import java.util.hashmap; import java.util.map; interface displayelement { void display(); } interface observer { void update (map<string,double> pricemap); } class pricesdisplay implements observer, displayelement { private string ticker; private double price; private subject pricedata; map<string,double> pricemap; pricesdisplay(subject pricedata) { this.pricedata = pricedata; } pricesdisplay(string ticker, subject pricedata) { this.ticker = ticker; this.pricedata = pricedata; } public void update(map<string,double> pricemap) { this.pricemap = pricemap; display(); } public void display() { (map.entry<string, double> entry : pricemap.entryset()) { system.out.printf("\nportfolio #%s, " + "%s = " + "%.2f, ", ticker, entry.getkey(), entry.getvalue()); } } } interface subject{ void registerobserver(observer o); void removeobserver(observer o); void notifyobservers(); void measurementschanged(); void setprices(map<string,double> pricemap); } class pricedata implements subject { private arraylist observers; pricedata pricedata; private map<string,double> pricemap = new hashmap<string,double>(); pricedata() { observers = new arraylist(); } public void registerobserver(observer o) { observers.add(o); } public void removeobserver(observer o) { int = observers.indexof(o); if (i >= 0) { observers.remove(i); } } public void notifyobservers() { (int = 0; < observers.size(); i++) { observer observer = (observer)observers.get(i); observer.update(pricemap); } } public void measurementschanged() { notifyobservers(); } public void setprices(map<string,double> pricemap) { this.pricemap = pricemap; measurementschanged(); } } class test { private static map<string,double> pricemap = new hashmap<string,double>(); private static pricedata pricedata = new pricedata(); public static void main(string[] args) { // found 2 portfolios listeners pricedata. // now, assume both portfolios contain same // collection of investments. pricesdisplay firstportfolio = new pricesdisplay("00001", pricedata); pricesdisplay secondportfolio = new pricesdisplay("00002", pricedata); pricedata.registerobserver(firstportfolio); pricedata.registerobserver(secondportfolio); generateinitialprices(); updateprices(.02); updateprices(-.05); updateprices(.06); } static void generateinitialprices() { pricemap.put("asd", 42.50); pricemap.put("bdm", 52.50); pricemap.put("cac", 22.20); pricemap.put("dfas", 45.00); pricedata.setprices(pricemap); } static void updateprices(double changepercent) { for( string key : pricemap.keyset()) { double v = pricemap.get(key) * (1.0 + changepercent); pricemap.put(key, v); } pricedata.setprices(pricemap); } }

java

No comments:

Post a Comment