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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
*/target/**
.classpath
.project
.settings/idea
.settings/idea
target/
42 changes: 42 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@
<groupId>org.example</groupId>
<artifactId>untitled1</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.4.01</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.11</version>
</dependency>
<dependency>
<groupId>org.decimal4j</groupId>
<artifactId>decimal4j</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>


</project>
Expand Down
108 changes: 108 additions & 0 deletions src/main/java/account/Portfolio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package account;

import org.decimal4j.util.DoubleRounder;
import stocks.StockEnum;

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

public class Portfolio {
private Map<StockEnum,Integer> portfolio;
private Double buyingPower;
private Double currentPortfolioValue;

public Portfolio() {
this.portfolio = new HashMap<>();
this.buyingPower = 2500.00;
this.currentPortfolioValue = 0.00;
}

public Double getBuyingPower() {
return buyingPower;
}

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);
}
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);
}
}
return null;
}

public Integer getNumberOfShares(String stock){
for(Map.Entry<StockEnum,Integer> s : portfolio.entrySet()){
if (stock.equalsIgnoreCase(String.valueOf(s.getKey()))){
return s.getValue();
}
}
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);
}

return result;
}

public Double getDiversityPercentage(String stock){
return (getPortfolioDiversityOfShare(stock)*100);
}


public String getPositionOfShare(String stock){
StockEnum currentStock = getStock(stock);
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("%");
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(){
StringBuilder builder = new StringBuilder();
for (Map.Entry<StockEnum,Integer> s: portfolio.entrySet()) {
builder.append(getPositionOfShare(String.valueOf(s.getKey())))
.append("\n");
}
return builder.toString();
}


}
4 changes: 4 additions & 0 deletions src/main/java/account/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package account;

public class User {
}
27 changes: 27 additions & 0 deletions src/main/java/controller/ApiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package controller;

import kong.unirest.HttpResponse;
import kong.unirest.Unirest;

public class ApiController {

public static String createApiQuery(String symbol){
StringBuilder builder = new StringBuilder();
//beginning part of the get() method
builder.append("https://alpha-vantage.p.rapidapi.com/query?datatype=json&symbol=")
.append(symbol.toUpperCase())
.append("&function=TIME_SERIES_MONTHLY");

return builder.toString();
}

public static String fetchApiQuery(String yourQuery){
HttpResponse<String> response = Unirest.get(yourQuery)
.header("x-rapidapi-host", "alpha-vantage.p.rapidapi.com")
.header("x-rapidapi-key", "9c88018860msh40e297080f45ec6p13b769jsnc02453b1e456")
.asString();
response.getBody();
return response.getBody();
}

}
60 changes: 60 additions & 0 deletions src/main/java/sql/SqlController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package sql;

import java.sql.*;

public class SqlController {
private static Connection connection = null;

public static void connectSqlServer(){
try{
Class.forName("org.postgresql.Driver");
connection = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/stocks");
connection.setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");

}

public static void insertStock(){
try {
Statement insert = connection.createStatement();
String sql = "INSERT INTO _2020_03 (TICKER, OPEN, HIGH, LOW, CLOSE, VOLUME) " +
"VALUES ('MSFT', 165.3100, 175.0000, 138.5800, 145.7000, 636200296);";
insert.executeUpdate(sql);
insert.close();
connection.commit();
connection.close();
} catch (SQLException e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Records created successfully");
}

public static void getStock(String stock, String month){
try{
Statement insert = connection.createStatement();
String sql = "SELECT * FROM " + month +
" WHERE TICKER = '" + stock.toUpperCase() + "';";
ResultSet rs = insert.executeQuery(sql);
while (rs.next()){
Double open = rs.getDouble("open");
Double high = rs.getDouble("high");
Double low = rs.getDouble("low");
Double close = rs.getDouble("close");
Integer volume = rs.getInt("volume");
System.out.println(new StringBuilder().append(open + " ").append(high + " ")
.append(low + " ").append(close + " ").append(volume));
}
} catch (SQLException e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Records created successfully");
}
}
4 changes: 4 additions & 0 deletions src/main/java/stocks/Stock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package stocks;

public class Stock {
}
81 changes: 81 additions & 0 deletions src/main/java/stocks/StockEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package stocks;

import java.util.Date;

public enum StockEnum {

//------------------------------------STOCK INFO FOR 2008-12-31---------------------------------------

MICROSOFT("MSFT", 19.87, 21.25, 18.47, 19.44, 1546943400),
APPLE("AAPL", 91.30, 103.60, 84.55, 85.35, 721923100),
VISA("V", 51.00, 57.67, 47.53, 52.45, 141021400),
JPMORGANCHASE("JPM", 30.67, 37.70, 24.61, 31.53, 1154247300),
JOHNSONJOHNSON("JNJ", 57.66, 60.25, 54.95, 59.83, 315194900),
COCACOLA("COKE", 42.65, 46.00, 38.51, 45.96, 425300),
CHEVRON("CVX", 76.50, 81.92, 68.22, 73.97, 392060500),
BRITISHPETROLEUM("BP", 45.96, 50.10, 41.54, 46.74, 149495200),
EXXON("XOM", 77.89, 83.64, 72.68, 79.83, 996412900);

//AAPL,V,JNJ,COKE,CVX,BP,XOM,JPM,MSFT
private String symbol;
private Double open;
private Double high;
private Double low;
private Double close;
private Integer volume;

StockEnum(String symbol, Double open, Double high, Double low, Double close, Integer volume) {
this.symbol = symbol;
this.open = open;
this.high = high;
this.low = low;
this.close = close;
this.volume = volume;
}

public String getSymbol() {
return symbol;
}

public Double getOpen() {
return open;
}

public Double getHigh() {
return high;
}

public Double getLow() {
return low;
}

public Double getClose() {
return close;
}

public Integer getVolume() {
return volume;
}

public void setNewPrice(Double newPrice){
open = newPrice;
}

public String getAllInfo() {
StringBuilder builder = new StringBuilder();
String space = " ";
builder.append(getSymbol())
.append(space)
.append(getOpen())
.append(space)
.append(getHigh())
.append(space)
.append(getLow())
.append(space)
.append(getClose())
.append(space)
.append(getVolume());
return builder.toString();
}

}
Loading