public class Test
{
public static void main (String[] args)
{
Broker myBroker = new Broker (Broker.SELLER);
System.out.println ("Lucent stock price: " + myBroker.getPrice("LU"));
System.out.println ("Nortel stock price: " + myBroker.getPrice("NT"));
}
}
class Broker
{
private String[] myStockList = {"LU", "NT", "CSCO", "DELL", "INTC"};
private final double SELLER_PREMIUM = 0.98;
private final double BUYER_PREMIUM = 1.02;
public static final int SELLER = 1;
public static final int BUYER = 2;
private int clientType;
public Broker (int clientType)
{
this.clientType = clientType;
}
public double getPrice (String symbol)
{
double price = 0;
Stock stock = StockFactory.getStock (symbol);
boolean isValidStock = false;
for (int i = 0; i < myStockList.length; i++)
{
if (myStockList[i].equals (stock.getSymbol()))
{
isValidStock = true;
break;
}
}
if (!isValidStock)
{
return -1;
}
if (stock.isActive())
{
if (stock.isPremium())
{
if (this.clientType == SELLER)
{
price = Market.getPrice (symbol) * SELLER_PREMIUM;
}
else
{
price = Market.getPrice (symbol) * BUYER_PREMIUM;
}
}
else
{
price = Market.getPrice (symbol);
}
}
return price;
}
}
class StockFactory
{
private static HashMap stockMap = new HashMap ();
public static Stock getStock (String symbol)
{
if (stockMap.containsKey(symbol))
{
return (Stock) stockMap.get(symbol);
}
Stock stock = new Stock (symbol);
stockMap.put (symbol, stock);
return stock;
}
}
class Stock
{
private String symbol;
private boolean isPremium = true;
public Stock (String symbol)
{
this.symbol = symbol;
}
public boolean isActive ()
{
return true;
}
public boolean isPremium ()
{
return isPremium;
}
public String getSymbol ()
{
return symbol;
}
public void setPremium (boolean isPremium)
{
this.isPremium = isPremium;
}
}
class Market
{
public static double getPrice (String symbol)
{
if ("LU".equals(symbol))
{
return 80.0;
}
else if ("NT".equals(symbol))
{
return 45.0;
}
return 0.0;
}
}
javac Test.java
java Test
이전 글 : ASP.NET 가이드 8. 컨트롤 처리 및 NHibernate
다음 글 : [포토샵] 디카로 로모(비네팅)효과 만들기
최신 콘텐츠