You are to create a stock market simulation game which has m
Solution
Company.java
package stock;
import java.util.ArrayList;
public class Company extends Entity{
int numOfStocks;
double stockPrice;
public Company(String name, int id) {
super(name, 30000, id);
numOfStocks = 1000;
stockPrice = 30;
stocks = new ArrayList<Stock>();
}
}
StockExchange.java
package stock;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
//represent the stock book which folds all the information about each stock
class StockSellReport
{
int stockID;
//parallel arrays to hold the companies or players and their the bid amounts
ArrayList<String> entityNames;
ArrayList<Double> bidAmounts;
int buyerID;
String sellerName;
StockSellReport(int id)
{
this.stockID = id;
entityNames = new ArrayList<String>();
bidAmounts = new ArrayList<Double>();
}
}
public class StockExchange {
ArrayList<StockSellReport> stocks;
public StockExchange() {
stocks = new ArrayList<StockSellReport>();
}
public void addStock(int id, ArrayList<String> entityNames, ArrayList<Double> amounts, int soldID, String seller)
{
StockSellReport stock = new StockSellReport(id);
stock.entityNames = entityNames;
stock.bidAmounts = amounts;
stock.buyerID = soldID;
stock.sellerName = seller;
stocks.add(stock);
}
public void bidProcess(Stock stock, ArrayList<String> names, ArrayList<Double> amounts, String sellerName)
{
double highestAmount = 0;
String name = \"\";
int buyerid = 0;
for(int i=0; i<amounts.size(); i++)
{
//System.out.println(\"Name: \" + names.get(i) + \" Amount: \" + amounts.get(i));
if(amounts.get(i) > highestAmount)
{
highestAmount = amounts.get(i);
name = names.get(i);
buyerid = i; //buyer id
}
}
addStock(stock.stockID, names, amounts, buyerid, sellerName);
System.out.println(\"Stock Sold: \" + stock.stockID + \" at \" + highestAmount + \" to \" + name);
}
//writing the stock report to file
public void writeStockReport()
{
try {
FileWriter writer = new FileWriter(\"StockReport.txt\");
for(int i=0; i<stocks.size(); i++)
{
writer.write(\"\ \ Stock ID: \" + stocks.get(i).stockID);
writer.write(\"\ Seller Name: \" + stocks.get(i).sellerName);
for(int j=0; j<stocks.get(i).entityNames.size(); j++)
{
writer.write(\"\ Bidder: \" + stocks.get(i).entityNames.get(j));
writer.write(\" Bid Amount: \" + stocks.get(i).bidAmounts.get(j));
}
int id = stocks.get(i).buyerID;
writer.write(\"\ Stock Sold to : \" + stocks.get(i).entityNames.get(id));
}
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Entity.java
package stock;
import java.util.ArrayList;
public class Entity {
String name;
double bankbalance;
int AccountNumber;
int id;
ArrayList<Stock> stocks;
private RandomGenerator random;
public Entity(String name, double initialBalance, int id) {
this.name = name;
this.bankbalance = initialBalance;
this.id = id;
random = new RandomGenerator();
}
public boolean wantsToSellStock()
{
if(random.generateNumber(0, 2) == 1)
return true;
else return false;
}
public double bid()
{
return random.generateNumber(5, 30)*0.9;
}
}
RandomGenerator.java
package stock;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class RandomGenerator {
public int generateNumber(int min, int max)
{
return min + (int)(Math.random() * max);
}
public Set<Integer> generateRandomBidders()
{
Random random = new Random();
int numberOfBidders = generateNumber(1, 7);
Set<Integer> set = new HashSet<Integer>(numberOfBidders);
while(set.size()< numberOfBidders) {
while (set.add(random.nextInt(7)) != true)
;
}
assert set.size() == numberOfBidders;
return set;
}
}
Stock.java
package stock;
public class Stock {
private static int ID = 2000;
double latestSellingPrice;
int stockID;
Stock(double amount)
{
stockID = ++ID;
latestSellingPrice = amount;
}
}
Bank.java
package stock;
package stack;
import java.util.ArrayList;
public class Bank{
//entities are both companies and players
ArrayList<Entity> entities;
static int accountNumber = 1000;
// to track the highest and second highest balance bank accounts
private Entity highestBalanceAcc;
private Entity secondhighestBalAcc;
Bank()
{
entities = new ArrayList<Entity>();
highestBalanceAcc = null;
secondhighestBalAcc = null;
}
//withdrawing from an account
public boolean withDraw(double amount, int accNum)
{
boolean isJobDone = false;
for(int i=0; i<entities.size(); i++)
{
if(entities.get(i).AccountNumber == accNum)
{
if(entities.get(i).bankbalance >= amount)
{
entities.get(i).bankbalance -= amount;
isJobDone = true;
}
}
}
return isJobDone;
}
//depositing to an account
public boolean deposit(double amount, int accNum)
{
boolean isJobDone = false;
for(int i=0; i<entities.size(); i++)
{
if(entities.get(i).AccountNumber == accNum)
{
entities.get(i).bankbalance += amount;
isJobDone = true;
}
}
return isJobDone;
}
//creating an account
public boolean createAccount(Entity entity)
{
entity.AccountNumber = accountNumber;
entities.add(entity);
if(highestBalanceAcc == null)
{
highestBalanceAcc = entity;
}
else
{
if(secondhighestBalAcc != null)
{
if(entity.bankbalance > secondhighestBalAcc.bankbalance)
secondhighestBalAcc = entity;
}
else
secondhighestBalAcc = entity;
}
accountNumber++;
return true;
}
//updating interest in each account
public void updateInterest()
{
for(int i=0; i<entities.size(); i++)
{
if(entities.get(i).AccountNumber == highestBalanceAcc.AccountNumber || entities.get(i).AccountNumber == secondhighestBalAcc.AccountNumber)
{
entities.get(i).bankbalance += entities.get(i).bankbalance * 0.05;
}
else
entities.get(i).bankbalance += entities.get(i).bankbalance * 0.03;
}
}
}
Player.java
package stock;
public class Player extends Entity{
public Player(String name, int id) {
super(name, 2000, id);
}
}
StockExchangeDriver.java
package stock;
import java.util.ArrayList;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
public class StockExchangeDriver extends TimerTask{
static Bank bank;
static StockExchange stockExchange;
int stockCount = 0;
static Timer timer;
static RandomGenerator randomNum;
/**
* @param args
*/
public static void main(String[] args) {
randomNum = new RandomGenerator();
bank = new Bank();
stockExchange = new StockExchange();
timer = new Timer();
timer.scheduleAtFixedRate(new StockExchangeDriver(), 0, 1000);
//5 players in total
Entity player1 = new Player(\"Sherlock\", 1);
Entity player2 = new Player(\"John\", 2);
Entity player3 = new Player(\"Anderson\", 3);
Entity player4 = new Player(\"Lestrade\", 4);
Entity player5 = new Player(\"Molly\", 5);
bank.createAccount(player1);
bank.createAccount(player2);
bank.createAccount(player3);
bank.createAccount(player4);
bank.createAccount(player5);
//3 companies in total
Entity comapny1 = new Company(\"Microft Inc.\", 6);
Entity company2 = new Company(\"Moriarity Inc.\", 7);
Entity company3 = new Company(\"Sebastian Corporation\", 8);
bank.createAccount(comapny1);
bank.createAccount(company2);
bank.createAccount(company3);
}
//running based on timing
@Override
public void run() {
Stock stock = new Stock(randomNum.generateNumber(5, 30));
ArrayList<String> playerNmes = new ArrayList<String>();
ArrayList<Double> amounts = new ArrayList<Double>();
//selling player id
int SellingplayerID = randomNum.generateNumber(1, 8);
String sellerName = \"\";
//getting the random numbers representing the player ids
Set<Integer> BiddingPlayers = randomNum.generateRandomBidders();
//getting the amounts and names for all the players
for(int j=0; j<bank.entities.size(); j++)
{
if(BiddingPlayers.contains(bank.entities.get(j).id) && bank.entities.get(j).id != SellingplayerID)
{
playerNmes.add(bank.entities.get(j).name);
amounts.add(bank.entities.get(j).bid());
}
if(bank.entities.get(j).id == SellingplayerID)
sellerName = bank.entities.get(j).name;
}
stockExchange.bidProcess(stock,playerNmes, amounts, sellerName);
stockCount++;
if(stockCount == 10 || stockCount == 20)
{
bank.updateInterest();
}
if(stockCount == 30)
{
timer.cancel();
stockExchange.writeStockReport();
}
}
}
OUTPUT:
Stock Sold: 2001 at 23.400000000000002 to Lestrade
Stock Sold: 2002 at 28.8 to Sherlock
Stock Sold: 2003 at 22.5 to Lestrade
Stock Sold: 2004 at 25.2 to John
Stock Sold: 2005 at 30.6 to Anderson
Stock Sold: 2006 at 21.6 to Molly
Stock Sold: 2007 at 22.5 to Anderson
Stock Sold: 2008 at 28.8 to Anderson
Stock Sold: 2009 at 24.3 to Sherlock
Stock Sold: 2010 at 21.6 to John
Stock Sold: 2011 at 11.700000000000001 to John
Stock Sold: 2012 at 29.7 to Microft Inc.
Stock Sold: 2013 at 21.6 to Sherlock
Stock Sold: 2014 at 8.1 to Sherlock
Stock Sold: 2015 at 28.8 to John
Stock Sold: 2016 at 29.7 to Sherlock
Stock Sold: 2017 at 27.0 to Microft Inc.
Stock Sold: 2018 at 20.7 to Anderson
Stock Sold: 2019 at 27.0 to Microft Inc.
Stock Sold: 2020 at 30.6 to John
Stock Sold: 2021 at 19.8 to Microft Inc.
Stock Sold: 2022 at 30.6 to Lestrade
Stock Sold: 2023 at 28.8 to Microft Inc.
Stock Sold: 2024 at 22.5 to Lestrade
Stock Sold: 2025 at 23.400000000000002 to Lestrade
Stock Sold: 2026 at 26.1 to Microft Inc.
Stock Sold: 2027 at 27.0 to Lestrade
Stock Sold: 2028 at 27.900000000000002 to Molly
Stock Sold: 2029 at 8.1 to Sherlock
Stock Sold: 2030 at 15.3 to Molly
OUTPUT FILE WITH ALL TRANSACTION:
Stock ID: 2001
Seller Name: Anderson
Bidder: Lestrade Bid Amount: 23.400000000000002
Bidder: Molly Bid Amount: 13.5
Stock Sold to : Lestrade
Stock ID: 2002
Seller Name: John
Bidder: Sherlock Bid Amount: 28.8
Bidder: Anderson Bid Amount: 22.5
Bidder: Lestrade Bid Amount: 18.900000000000002
Bidder: Molly Bid Amount: 26.1
Stock Sold to : Sherlock
Stock ID: 2003
Seller Name: John
Bidder: Sherlock Bid Amount: 4.5
Bidder: Anderson Bid Amount: 11.700000000000001
Bidder: Lestrade Bid Amount: 22.5
Stock Sold to : Lestrade
Stock ID: 2004
Seller Name: Sherlock
Bidder: John Bid Amount: 25.2
Bidder: Anderson Bid Amount: 4.5
Bidder: Lestrade Bid Amount: 9.0
Bidder: Molly Bid Amount: 23.400000000000002
Bidder: Microft Inc. Bid Amount: 8.1
Stock Sold to : John
Stock ID: 2005
Seller Name: John
Bidder: Anderson Bid Amount: 30.6
Bidder: Microft Inc. Bid Amount: 22.5
Stock Sold to : Anderson
Stock ID: 2006
Seller Name: Anderson
Bidder: Sherlock Bid Amount: 15.3
Bidder: John Bid Amount: 4.5
Bidder: Molly Bid Amount: 21.6
Stock Sold to : Molly
Stock ID: 2007
Seller Name: Molly
Bidder: Sherlock Bid Amount: 4.5
Bidder: John Bid Amount: 15.3
Bidder: Anderson Bid Amount: 22.5
Bidder: Lestrade Bid Amount: 22.5
Stock Sold to : Anderson
Stock ID: 2008
Seller Name: Sherlock
Bidder: John Bid Amount: 27.0
Bidder: Anderson Bid Amount: 28.8
Bidder: Lestrade Bid Amount: 11.700000000000001
Bidder: Molly Bid Amount: 18.0
Bidder: Microft Inc. Bid Amount: 9.9
Stock Sold to : Anderson
Stock ID: 2009
Seller Name: John
Bidder: Sherlock Bid Amount: 24.3
Stock Sold to : Sherlock
Stock ID: 2010
Seller Name: Sebastian Corporation
Bidder: Sherlock Bid Amount: 18.0
Bidder: John Bid Amount: 21.6
Bidder: Anderson Bid Amount: 18.900000000000002
Bidder: Lestrade Bid Amount: 9.9
Bidder: Molly Bid Amount: 11.700000000000001
Bidder: Microft Inc. Bid Amount: 20.7
Stock Sold to : John
Stock ID: 2011
Seller Name: Anderson
Bidder: John Bid Amount: 11.700000000000001
Bidder: Lestrade Bid Amount: 7.2
Bidder: Molly Bid Amount: 10.8
Bidder: Microft Inc. Bid Amount: 9.0
Stock Sold to : John
Stock ID: 2012
Seller Name: Lestrade
Bidder: Sherlock Bid Amount: 9.9
Bidder: John Bid Amount: 13.5
Bidder: Anderson Bid Amount: 28.8
Bidder: Microft Inc. Bid Amount: 29.7
Stock Sold to : Microft Inc.
Stock ID: 2013
Seller Name: Moriarity Inc.
Bidder: Sherlock Bid Amount: 21.6
Bidder: John Bid Amount: 17.1
Bidder: Lestrade Bid Amount: 9.9
Bidder: Microft Inc. Bid Amount: 10.8
Stock Sold to : Sherlock
Stock ID: 2014
Seller Name: Sebastian Corporation
Bidder: Sherlock Bid Amount: 8.1
Stock Sold to : Sherlock
Stock ID: 2015
Seller Name: Lestrade
Bidder: Sherlock Bid Amount: 5.4
Bidder: John Bid Amount: 28.8
Bidder: Anderson Bid Amount: 18.0
Bidder: Molly Bid Amount: 14.4
Bidder: Microft Inc. Bid Amount: 7.2
Stock Sold to : John
Stock ID: 2016
Seller Name: Sebastian Corporation
Bidder: Sherlock Bid Amount: 29.7
Bidder: John Bid Amount: 13.5
Bidder: Anderson Bid Amount: 27.900000000000002
Stock Sold to : Sherlock
Stock ID: 2017
Seller Name: Moriarity Inc.
Bidder: Sherlock Bid Amount: 23.400000000000002
Bidder: John Bid Amount: 6.3
Bidder: Anderson Bid Amount: 8.1
Bidder: Lestrade Bid Amount: 6.3
Bidder: Molly Bid Amount: 22.5
Bidder: Microft Inc. Bid Amount: 27.0
Stock Sold to : Microft Inc.
Stock ID: 2018
Seller Name: Lestrade
Bidder: John Bid Amount: 18.0
Bidder: Anderson Bid Amount: 20.7
Stock Sold to : Anderson
Stock ID: 2019
Seller Name: Anderson
Bidder: Sherlock Bid Amount: 24.3
Bidder: Lestrade Bid Amount: 8.1
Bidder: Microft Inc. Bid Amount: 27.0
Stock Sold to : Microft Inc.
Stock ID: 2020
Seller Name: Sebastian Corporation
Bidder: John Bid Amount: 30.6
Bidder: Lestrade Bid Amount: 26.1
Bidder: Microft Inc. Bid Amount: 20.7
Stock Sold to : John
Stock ID: 2021
Seller Name: John
Bidder: Microft Inc. Bid Amount: 19.8
Stock Sold to : Microft Inc.
Stock ID: 2022
Seller Name: Sebastian Corporation
Bidder: John Bid Amount: 9.9
Bidder: Anderson Bid Amount: 9.9
Bidder: Lestrade Bid Amount: 30.6
Bidder: Molly Bid Amount: 23.400000000000002
Bidder: Microft Inc. Bid Amount: 22.5
Stock Sold to : Lestrade
Stock ID: 2023
Seller Name: John
Bidder: Sherlock Bid Amount: 13.5
Bidder: Anderson Bid Amount: 14.4
Bidder: Lestrade Bid Amount: 8.1
Bidder: Molly Bid Amount: 16.2
Bidder: Microft Inc. Bid Amount: 28.8
Stock Sold to : Microft Inc.
Stock ID: 2024
Seller Name: Microft Inc.
Bidder: Sherlock Bid Amount: 18.900000000000002
Bidder: John Bid Amount: 18.0
Bidder: Anderson Bid Amount: 20.7
Bidder: Lestrade Bid Amount: 22.5
Bidder: Molly Bid Amount: 20.7
Stock Sold to : Lestrade
Stock ID: 2025
Seller Name: Sebastian Corporation
Bidder: Sherlock Bid Amount: 16.2
Bidder: John Bid Amount: 16.2
Bidder: Anderson Bid Amount: 5.4
Bidder: Lestrade Bid Amount: 23.400000000000002
Bidder: Molly Bid Amount: 15.3
Bidder: Microft Inc. Bid Amount: 9.0
Stock Sold to : Lestrade
Stock ID: 2026
Seller Name: Sebastian Corporation
Bidder: Sherlock Bid Amount: 5.4
Bidder: John Bid Amount: 9.0
Bidder: Anderson Bid Amount: 24.3
Bidder: Molly Bid Amount: 10.8
Bidder: Microft Inc. Bid Amount: 26.1
Stock Sold to : Microft Inc.
Stock ID: 2027
Seller Name: Moriarity Inc.
Bidder: Lestrade Bid Amount: 27.0
Stock Sold to : Lestrade
Stock ID: 2028
Seller Name: Sherlock
Bidder: John Bid Amount: 19.8
Bidder: Anderson Bid Amount: 8.1
Bidder: Lestrade Bid Amount: 17.1
Bidder: Molly Bid Amount: 27.900000000000002
Stock Sold to : Molly
Stock ID: 2029
Seller Name: Lestrade
Bidder: Sherlock Bid Amount: 8.1
Bidder: Anderson Bid Amount: 6.3
Stock Sold to : Sherlock
Stock ID: 2030
Seller Name: Anderson
Bidder: John Bid Amount: 7.2
Bidder: Lestrade Bid Amount: 7.2
Bidder: Molly Bid Amount: 15.3
Stock Sold to : Molly

















