Business object classes.
- By Lowell Kaplan
- June 24, 2001
Java Means Business
Business-oriented models for complex Swing apps
Lowell Kaplan
Listing 1. Business object classes.
public class StockQuote
{
private Stock stock;
private double lastPrice;
private double netChange;
private long volume;
public StockQuote(Stock stock, double lastPrice, double
netChange, long volume)
{
this.stock = stock;
this.lastPrice = lastPrice;
this.netChange = netChange;
this.volume = volume;
}
public Stock getStock()
{
return stock;
}
public double getLastPrice()
{
return lastPrice;
}
public double getNetChange()
{
return netChange;
}
public long getVolume()
{
return volume;
}
public void updateData(StockQuote newQuote)
{
lastPrice = newQuote.getLastPrice();
netChange = newQuote.getNetChange();
volume = newQuote.getVolume();
}
public boolean equals(Object o)
{
if (o instanceof StockQuote)
{
StockQuote q = (StockQuote)o;
if (this.getStock() != null)
return this.getStock().equals(q.getStock());
}
return false;
}
}
public class Stock
{
private String symbol;
private Sector sector;
public Stock(String symbol, Sector sector)
{
this.symbol = symbol;
this.sector = sector;
}
public String getSymbol()
{
return symbol;
}
public Sector getSector()
{
return sector;
}
public boolean equals(Object o)
{
if (o instanceof Stock)
{
Stock s = (Stock)o;
if (this.getSymbol() != null)
return this.getSymbol().equals(s.getSymbol());
}
return false;
}
}
public class Sector
{
private String sectorName;
private Sector parentSector;
public Sector(String sectorName, Sector parentSector)
{
this.sectorName = sectorName;
this.parentSector = parentSector;
}
public String getSectorName()
{
return sectorName;
}
public Sector getParentSector()
{
return parentSector;
}
public boolean isAChildSectorOf(Sector s)
{
Sector currentSector = this;
while (currentSector.getParentSector() != null)
{
Sector parent = currentSector.getParentSector();
if (parent.equals(s))
return true;
currentSector = parent;
}
return false;
}
public boolean equals(Object o)
{
if (o instanceof Sector)
{
Sector s = (Sector)o;
if (this.getSectorName() != null)
return this.getSectorName().
equals(s.getSectorName());
}
return false;
}
public String toString()
{
return getSectorName();
}
}