Skip to content

Commit

Permalink
Starting predictions
Browse files Browse the repository at this point in the history
Adding comments
Fixing UI in MainPage (changes position of elements in y-axis)
  • Loading branch information
Dhruv-0-Arora committed Aug 5, 2022
1 parent 3927ebe commit df39516
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 97 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# StockMarket
This project coded in java uses java's built in swift package and Yahoo Finance to display different companies stock value. Then, it analyzes trends and predicts what the stock value will be in the next couple of months.

To run: download project and navigate to src/main/java/me/dhruvarora/Main.java file.
Next, run the main method and the wait for project to compile.
Finally, the project should have finished compling and will automatically open a new window you can enjoy!

Debugging: make sure that you have a java development kit (JDK) and java runtime environment (JRE) installed on your computer

Packaging: unfortunately this project cannot be packaged into a jar due to complications with importing files and reading them inside the jar.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
Expand Down
31 changes: 13 additions & 18 deletions src/main/java/me/dhruvarora/UI/ChangeStocksPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
import me.dhruvarora.Utility.ChangeStockPage.InputField;

public class ChangeStocksPage extends JPanel {
// stores the frame content
private final JComponent content;
// stores the frame instance
private final Frame frame;
// stores global variable for cluster component constraints
/* Global variables */
private final JComponent content; // stores the frame content
private final Frame frame; // stores the frame instance
private GridBagConstraints constraints;
// stores instance of invisible red text reading "Invalid Ticker Symbol!"
private final JLabel invalidFieldInput;
// stores instance of input field
private final InputField InputField = new InputField();
// stores instance of button to submit field
private final ChangeStockButton changeStockButton;

private final JLabel invalidFieldInput; // stores instance of invisible red text reading "Invalid Ticker Symbol!"
private final InputField InputField = new InputField(); // stores instance of input field
private final ChangeStockButton changeStockButton; // stores instance of submit field button
private final StockData stockData;

/*
Expand Down Expand Up @@ -94,21 +90,19 @@ public ChangeStocksPage(JComponent content, Frame frame, StockData stockData)
(frame.getHeight() / 2) - (cluster.getHeight() / 2));
this.add(cluster);

/*
* creates button to navigate back to mainPage
*
* end of cluster (back button is added straight to extended JPanel)
*/
/* end of cluster */

// creates button to navigate back to mainPage
this.add(new BackButton(frame));

// creates invisible text for invalid input
invalidFieldInput = invalidInput();
// setting location below cluster
// setting location of text below cluster
invalidFieldInput.setLocation(
(int) Math.round(cluster.getLocation().getX()
+ ((cluster.getSize().width / 2) - invalidFieldInput.getSize().getWidth() / 2)),
(int) cluster.getLocation().getY() + cluster.getSize().height + 20);
this.add(invalidFieldInput);
this.add(invalidFieldInput); // adding it to JPanel

}

Expand Down Expand Up @@ -171,6 +165,7 @@ private JLabel invalidInput() {

/*
* Function to make invalidInput visible
* used in ChangeStockButton class
*/
public void inputIsInvalid() {
invalidFieldInput.setVisible(true);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/me/dhruvarora/UI/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class Frame extends JFrame {
* stocks that they would like to watch in form of a graph.
*/
private ChangeStocksPage changeStocksPage;
// instance of StockData class (this instance is avalible throughout the entire
// project)
private StockData stockData;

/*
Expand Down Expand Up @@ -57,7 +59,7 @@ public Frame() throws FileNotFoundException, IOException, ParseException {
content.setBackground(Color.darkGray);

/*
* initializing mainJPanel
* initializing mainJPanel & global stockData instance
*/
this.stockData = new StockData();
mainPage = new MainPage(content, this, this.stockData);
Expand All @@ -67,7 +69,7 @@ public Frame() throws FileNotFoundException, IOException, ParseException {
setVisible(true);

/*
* listens if the window is resized
* window RESIZING event
*
* if run, then will delete and recreate open JPanel
*/
Expand All @@ -92,6 +94,7 @@ public void componentResized(ComponentEvent componentEvent) {
* function to set the desktop getImage
*
* works for macOS, windows, and linux
* TODO: have it work for jar files
*/
private void setIcons(String path) {
// windows & linux
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/me/dhruvarora/UI/MainPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
import me.dhruvarora.Utility.MainPage.AddOrRemoveStock;
import yahoofinance.Stock;

/*
* MainPage is the first page displayed when project is launched
*
* extention of JPanel and is converted into a JScrollPane
* format: scroll pane with graphs at top and buttons underneath
*/
public class MainPage extends JPanel {
// stores the frame content
private JComponent content;
Expand All @@ -35,6 +41,8 @@ public class MainPage extends JPanel {
// stores refresh button instance for initializing and making default button
private Refresh refresh;
private StockData stockData;
// number of months that the app will predict
private final int predictionsCount = 2;

/*
* CONSTRUCTOR
Expand Down Expand Up @@ -92,9 +100,10 @@ private void createContentPane()
/*
* interates through StockMarket.json
*
* creates graphs for each stock in json file
* creates graphs and their titles for each stock in json file
*/
for (int i = 0; i < stockData.getWatchListLength(); i++) {

/*
* using YahooFinanceAPI to get the stock current price
* after getting current price, creates a title for the graph including the
Expand Down Expand Up @@ -160,7 +169,9 @@ private void createContentPane()
private Graph createGraph(StockData stockData, String ticker)
throws IOException, ParseException {
ArrayList<Double> dataSet = stockData.getGraphData(ticker);
Graph newGraphPanel = new Graph(dataSet);
// TODO: fully implement prediction data
// dataSet = stockData.addPredictionData(dataSet, predictionsCount, ticker);
Graph newGraphPanel = new Graph(dataSet, predictionsCount);
return newGraphPanel;
}

Expand Down
24 changes: 22 additions & 2 deletions src/main/java/me/dhruvarora/Utility/MainPage/AddOrRemoveStock.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@

import me.dhruvarora.UI.Frame;

/*
* AddOrRemoveStock class is a JButton which is displayed at the bottom of the MainPage
*
* overrides default paint functions for JButton and makes rounded corners
*/
public class AddOrRemoveStock extends JButton {
private final Color textColor = Color.white;
private final Color backgroundColor = new Color(67, 129, 255);
private final Color borderColor = Color.BLACK;
private final Frame frame;
private Shape shape;

/*
* CONSTRUCTOR
*
* takes instance of frame
*/
public AddOrRemoveStock(Frame frame) {
this.frame = frame;
this.frame = frame; // setting global frame variable

// setting defaults
setVerticalTextPosition(AbstractButton.CENTER);
setHorizontalTextPosition(AbstractButton.CENTER);
setText("Add/Remove Stock");
Expand All @@ -36,21 +47,30 @@ public AddOrRemoveStock(Frame frame) {
setPreferredSize(new Dimension(150, 75));
setFont(new Font("Arial", Font.PLAIN, 20));
setAlignmentX(CENTER_ALIGNMENT);

// allowing button to have rounded corners
setFocusPainted(false);
setBorderPainted(false);

// listener for when the button is pressed
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
AddOrRemoveStock.this.frame.swapScreens();
AddOrRemoveStock.this.frame.swapScreens(); // will swap to change stocks page
} catch (IOException | ParseException e1) {
e1.printStackTrace();
}
}
});
}

/*
*
*
* overriden function to round the corners of the button
*/

protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 30, 30);
Expand Down
Loading

0 comments on commit df39516

Please sign in to comment.