Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 63 additions & 59 deletions src/main/java/account/Portfolio.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package account;

import org.decimal4j.util.DoubleRounder;
import stocks.StockEnum;
import stocks.Stock;
import stocks.Transaction;
import utilities.Console;
import utilities.Messages;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.Map;

public class Portfolio {
private Map<StockEnum,Integer> portfolio;
private ArrayList<Stock> mainPortfolio;
Console console = new Console(System.in,System.out);
private Double buyingPower;
private Double currentPortfolioValue;

public Portfolio() {
this.portfolio = new HashMap<>();
this.mainPortfolio = new ArrayList<>();
this.buyingPower = 2500.00;
this.currentPortfolioValue = 0.00;
}
Expand All @@ -25,84 +29,84 @@ public Double getCurrentPortfolioValue() {
return currentPortfolioValue + buyingPower;
}

public boolean addStockToBasePortfolio(StockEnum stock, Integer numberOfShares){
double costOfShares = stock.getOpen() * numberOfShares;
if (costOfShares < buyingPower){
if(portfolio.containsKey(stock)){
portfolio.replace(stock, portfolio.get(stock),portfolio.get(stock) + numberOfShares);
} else {
portfolio.put(stock,numberOfShares);
public void addStockToPortfolio(Stock stock){
mainPortfolio.add(stock);
}

public void purchaseStock(String stockSymbol, String month, Integer numOfShares){
Double costOfPurchase = DoubleRounder.round(Stock.checkStockPrice(stockSymbol,month) * numOfShares,2);
if(costOfPurchase < buyingPower){
if(checkToSeeIfOwnStock(stockSymbol)){
getStockFromPortfolio(stockSymbol).addTransaction(Transaction.makeTransaction(stockSymbol,month,numOfShares));
} else {
//TODO resolve way to get name for stock per symbol
Stock stock = new Stock(stockSymbol,"TempName");
stock.addTransaction(Transaction.makeTransaction(stockSymbol,month,numOfShares));
addStockToPortfolio(stock);
}
buyingPower = buyingPower - costOfPurchase;
} else{
console.println(Messages.notEnough);
}
currentPortfolioValue = currentPortfolioValue + DoubleRounder.round((costOfShares),2);
buyingPower = DoubleRounder.round((buyingPower - costOfShares),2);
return true;
} else {
return false;
}
}

public Double getEquityOfShare(String stock){
for (Map.Entry<StockEnum, Integer> s : portfolio.entrySet()) {
if (stock.equalsIgnoreCase(String.valueOf(s.getKey()))){
return DoubleRounder.round((s.getKey().getOpen() * s.getValue()),2);
public Boolean checkToSeeIfOwnStock(String stockSymbol){
Boolean result = false;
for (Stock s : mainPortfolio) {
if(s.symbol.equalsIgnoreCase(stockSymbol)){
result = true;
break;
}
}
return null;
return result;
}

public Integer getNumberOfShares(String stock){
for(Map.Entry<StockEnum,Integer> s : portfolio.entrySet()){
if (stock.equalsIgnoreCase(String.valueOf(s.getKey()))){
return s.getValue();
public Stock getStockFromPortfolio(String stockSymbol){
for (Stock s: mainPortfolio) {
if(s.symbol.equalsIgnoreCase(stockSymbol)){
return s;
}
}
return null;
}

public Double getPortfolioDiversityOfShare(String stock){
Double result = 0.00;
Double equityOfShare = getEquityOfShare(stock);
if(!stock.equalsIgnoreCase("cash")){
result = DoubleRounder.round ((equityOfShare/getCurrentPortfolioValue()),2);
}else{
result = DoubleRounder.round ((getBuyingPower()/getCurrentPortfolioValue()),2);
public void updateCurrentPortfolioValue(String month){
Double newCurrentValue = 0.0;
for (Stock s: mainPortfolio) {
s.updateCurrentStockPrice(s.symbol,month);
newCurrentValue += getEquityOfStock(s);
}

return result;
currentPortfolioValue = newCurrentValue;
}

public Double getDiversityPercentage(String stock){
return (getPortfolioDiversityOfShare(stock)*100);
public Double getEquityOfStock(Stock stock){
return DoubleRounder.round((stock.getCurrentStockPrice() * stock.getTotalNumOfShares()),2);
}


public String getPositionOfShare(String stock){
StockEnum currentStock = getStock(stock);
public String getPositionOfStock(String stockSymbol){
StringBuilder builder = new StringBuilder();
builder.append("****YOUR POSITION****")
.append(String.format("\nSymbol = %s\nShares = %d\nEquity = %.2f\nDiversity = %.2f",
currentStock.getSymbol(), getNumberOfShares(stock),getEquityOfShare(stock),getDiversityPercentage(stock)));
builder.append("%");
Stock stock = getStockFromPortfolio(stockSymbol);
builder.append("Name : ")
.append(stock.name)
.append("\nSymbol : ")
.append(stock.symbol)
.append("\nNumber of Shares : ")
.append(stock.totalNumOfShares)
.append("\nCurrent Price : ")
.append(stock.currentStockPrice)
.append("\nEquity : ")
.append(stock.valueOfPosition);
return builder.toString();
}

private StockEnum getStock(String stock){
for (Map.Entry<StockEnum, Integer> s: portfolio.entrySet()){
if (String.valueOf(s.getKey()).equalsIgnoreCase(stock)){
return s.getKey();
}
}
return null;
}

public String getAllPositions(){
public String getAllPositionsFromPortfolio(){
StringBuilder builder = new StringBuilder();
for (Map.Entry<StockEnum,Integer> s: portfolio.entrySet()) {
builder.append(getPositionOfShare(String.valueOf(s.getKey())))
for (Stock s : mainPortfolio) {
builder.append("*******************")
.append("\n")
.append(getPositionOfStock(s.symbol))
.append("\n");
}
return builder.toString();
}


}
2 changes: 1 addition & 1 deletion src/main/java/sql/SqlController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public static void connectSqlServer(){
System.exit(0);
}
System.out.println("Database still open");

}

public static void insertStock(){
Expand All @@ -43,6 +42,7 @@ public static void insertStock(){

public static TransactionMeta getStock(String stockSymbol, String month){
TransactionMeta.TransactionMetaBuilder builder = new TransactionMeta.TransactionMetaBuilder();
connectSqlServer();
try{
Statement statement = connection.createStatement();
String sql = "SELECT * FROM " + stockSymbol +
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/stocks/Stock.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package stocks;

import org.decimal4j.util.DoubleRounder;
import sql.SqlController;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -38,12 +41,19 @@ public String getInfo(){
return info;
}

//TODO - CONNECT TO DB PER DATE
public Double getCurrentStockPrice(){
return currentStockPrice;
}

//TODO - CONNECT TO DB PER DATE
public void updateCurrentStockPrice(String stockSymbol,String month){
currentStockPrice = checkStockPrice(stockSymbol,month);
valueOfPosition = DoubleRounder.round(totalNumOfShares * currentStockPrice,2);
}

public static Double checkStockPrice(String stockSymbol,String month){
return SqlController.getStock(stockSymbol,month).getClose();
}

public Double getValueOfPosition(){
return valueOfPosition;
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/stocks/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package stocks;

import org.decimal4j.util.DoubleRounder;
import sql.SqlController;

public class Transaction {
Expand Down Expand Up @@ -28,13 +29,11 @@ public Double getCostPerShare(){
}

public Double calculateTransactionTotal(){
return costPerShare * numOfShare;
return DoubleRounder.round(costPerShare * numOfShare,2);
}

public Transaction makeTransaction(){
SqlController.connectSqlServer();
TransactionMeta.TransactionMetaBuilder builder = TransactionMeta.TransactionMetaBuilder.newInstance();

return null;
public static Transaction makeTransaction(String stockSymbol,String month,Integer numOfShares){
TransactionMeta transactionMeta = SqlController.getStock(stockSymbol,month);
return new Transaction(transactionMeta,numOfShares);
}
}
2 changes: 2 additions & 0 deletions src/main/java/utilities/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Messages {
public static String enterLastName = "Please enter your last name.";
public static String startingGuide = "Lets get you started!\nWe have created a basic portfolio for with a buying power of $2500.\nUse these funds to purchase stocks from the list below.";
public static String chooseNum = "Choose a number to make a selection:";
public static String notEnough = "Sorry, you don't have enough funds to make that purchase";



Expand All @@ -31,4 +32,5 @@ public static String startingStocks(){
}



}
Loading