Question: OBJECTIVES Add persistent data storage to your Week 4 Lab using text file input/output. PROBLEM: Stocks4U Portfolio Management System The portfolio management system you developed

OBJECTIVES

Add persistent data storage to your Week 4 Lab using text file input/output.

PROBLEM: Stocks4U Portfolio Management System

The portfolio management system you developed for Stocks4U needs the ability to save and restore a users data from a text file.

FUNCTIONAL REQUIREMENTS

You can code the GUI by hand or use NetBeans GUI Builder Interface.

You will enhance Week 4 GUI to include

a File menu with menu items

a label to display total portfolio value.

Add file input and output operations

StockIO class

Create a StockIO class that is used to read from and write Stock objects to a text file using an ArrayList.

This class should have the following members:

Constant

o DefaultFile name, which holds a default value for the file.

Variables

o filename, which is a variable that can be set to point to a different file.

o Setter for file name ensuring the file name is not empty, if so, set the file name to the default value.

Constructor

o Default, no parameter constructor

o Constructor that accepts the file name to be written to

Methods:

o writeObjectData, which accepts an arraylist of Stock objects and writes the objects to the named file. The method shall return the number of Stock objects returned.

o readObjectData, which determines if the file exists, and if so, reads the Stock objects from the file and stores the objects in an ArrayList, the function then returns the array list.

o Both the functions will include appropriate exception handling.

Graphical User Interface

Note that you will need to add an ArrayList to your GUI class to manage the data to/from the file. It will act as a parallel array to your DefaultListModel. Any time you add a stock, you must add it in BOTH places. Any time you remove a stock, you must remove it in BOTH places.

Menus

Fileexit should exit the program.

Stock add menus items that replicate the Add Stock, Clear Stock, and New Stock button operations.

Stock List - add menu items that replicate the Stock List operations, Save Stocks, Clear Stock List, Retrieve Stocks, Update Price, Delete Stock

The total value of the portfolio should be displayed at all times and updated anytime a stock is added or removed.

Sample GUI

Stock Information

Stock Summary

RUBRIC

CODE STYLE REQUIREMENTS

Include meaningful comments throughout your code.

Use meaningful names for variables.

Code must be properly indented.

Include a comment header at beginning of each file, example below.

/**************************************************** Program Name: ProgramName.java Programmer's Name: Student Name Program Description: Describe here what this program will do ***********************************************************/

week 4 code

package javaapplication26; import java.util.ArrayList; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.util.logging.Level; import java.util.logging.Logger; public class Stocks4u_GUI extends JFrame { DefaultListModel listOfStockItems; ArrayList allStocks; private JList jListStock; private JPanel jPanel1; private JPanel jPanel2; private JScrollPane jScrollPane1; private JTabbedPane jTabbedPane1; private JButton jbtnAddStock; private JButton jbtnRemoveStock; private JLabel jlblAdded; private JLabel jlblCurrentPrice; private JLabel jlblPanel; private JLabel jlblPurchasePrice; private JLabel jlblNumberofShares; private JLabel jlblStockName; private JTextField jtxtStockCP; private JTextField JtfStockName; private JTextField jtxtStockPP; private JTextField JtfNumberOfShares; public Stocks4u_GUI() { initComponents();

allStocks = new ArrayList<>();

listOfStockItems = new DefaultListModel(); }

private void initComponents() {

jTabbedPane1 = new JTabbedPane();

jPanel1 = new JPanel();

jScrollPane1 = new JScrollPane();

jListStock = new JList<>();

jlblPanel = new JLabel();

jbtnRemoveStock = new JButton();

jPanel2 = new JPanel();

jlblStockName = new JLabel();

jlblNumberofShares = new JLabel();

jlblPurchasePrice = new JLabel();

jlblCurrentPrice = new JLabel();

JtfStockName = new JTextField();

JtfNumberOfShares = new JTextField();

jtxtStockPP = new JTextField();

jtxtStockCP = new JTextField();

jbtnAddStock = new JButton();

jlblAdded = new JLabel();

setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE);

setTitle("Portfolio Management");

jTabbedPane1.setFont(new Font("Calibri", 0, 20)); jListStock.setFont(new Font("Calibri", 0, 20));

jListStock.addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) {

jListStockValueChanged(evt); } });

jScrollPane1.setViewportView(jListStock);

jlblPanel.setFont(new Font("Calibri", 0, 20));

jlblPanel.setText("Total Gain / Loss : ");

jbtnRemoveStock.setFont(new Font("Calibri", 0, 20));

jbtnRemoveStock.setText("Remove Stock");

jbtnRemoveStock.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jbtnRemoveStockActionPerformed(evt); } }); GroupLayout jPanel1Layout = new GroupLayout( jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup( GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup( jPanel1Layout.createParallelGroup( GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1).addGroup(jPanel1Layout .createSequentialGroup().addGroup( jPanel1Layout.createParallelGroup( GroupLayout.Alignment.LEADING). addComponent(jlblPanel).addGroup( jPanel1Layout.createSequentialGroup() .addGap(75, 75, 75).addComponent(jbtnRemoveStock, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))). addGap(0, 92, Short.MAX_VALUE))) .addContainerGap())); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup( GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap().addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(jlblPanel) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(jbtnRemoveStock, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE) .addContainerGap(32, Short.MAX_VALUE)) );

jTabbedPane1.addTab("Show Stocks", jPanel1);

jlblStockName.setFont(new Font("Calibri", 0, 20));

jlblStockName.setText("Company Name");

jlblNumberofShares.setFont(new Font("Calibri", 0, 20));

jlblNumberofShares.setText("Number of shares");

jlblPurchasePrice.setFont(new Font("Calibri", 0, 20));

jlblPurchasePrice.setText("Purchase Price");

jlblCurrentPrice.setFont(new Font("Calibri", 0, 20));

jlblCurrentPrice.setText("Current Price");

JtfStockName.setFont(new Font("Calibri", 0, 20)); JtfNumberOfShares.setFont(new Font("Calibri", 0, 20)); jtxtStockPP.setFont(new Font("Calibri", 0, 20)); jtxtStockCP.setFont(new Font("Calibri", 0, 20)); jbtnAddStock.setFont(new Font("Calibri", 0, 20));

jbtnAddStock.setText("Add Stock");

jbtnAddStock.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) {

jbtnAddStockActionPerformed(evt); } });

jlblAdded.setFont(new Font("Calibri", 0, 20));

jlblAdded.setText(" ");

GroupLayout jPanel2Layout = new GroupLayout(jPanel2);

jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup( GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup()

.addComponent(jlblStockName) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(JtfStockName, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)) .addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()

.addComponent(jlblNumberofShares) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(JtfNumberOfShares, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)) .addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout .createSequentialGroup()

.addComponent(jlblPurchasePrice) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE) .addComponent(jtxtStockPP, GroupLayout.PREFERRED_SIZE,200, GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup( GroupLayout.Alignment.LEADING)

.addComponent(jlblCurrentPrice, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jlblAdded,

GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup( GroupLayout.Alignment.LEADING)

.addComponent(jtxtStockCP, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE,200, GroupLayout.PREFERRED_SIZE)

.addComponent(jbtnAddStock,

GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE,200, GroupLayout.PREFERRED_SIZE)))) .addContainerGap()) );

jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup( GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout .createParallelGroup(

GroupLayout.Alignment.CENTER)

.addComponent(jlblStockName)

.addComponent(JtfStockName, GroupLayout.PREFERRED_SIZE,35, GroupLayout.PREFERRED_SIZE)) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup( GroupLayout.Alignment.CENTER)

.addComponent(jlblNumberofShares)

.addComponent(JtfNumberOfShares, GroupLayout.PREFERRED_SIZE,35, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup( GroupLayout.Alignment.CENTER).addComponent(jlblPurchasePrice)

.addComponent(jtxtStockPP,GroupLayout.PREFERRED_SIZE,35, GroupLayout.PREFERRED_SIZE)).addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup( GroupLayout.Alignment.BASELINE)

.addComponent(jtxtStockCP, GroupLayout.PREFERRED_SIZE,35, GroupLayout.PREFERRED_SIZE)

.addComponent(jlblCurrentPrice)) .addPreferredGap(

LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout .createParallelGroup( GroupLayout.Alignment.BASELINE)

.addComponent(jbtnAddStock, GroupLayout.PREFERRED_SIZE,35, GroupLayout.PREFERRED_SIZE)

.addComponent(jlblAdded)) .addGap(82, 82, 82)));

jPanel2Layout.linkSize(SwingConstants.VERTICAL, new Component[] { jlblStockName,JtfStockName });

jPanel2Layout.linkSize(SwingConstants.VERTICAL, new Component[] { jlblNumberofShares,JtfNumberOfShares });

jTabbedPane1.addTab("Add Stock", jPanel2);

GroupLayout layout = new GroupLayout( getContentPane()); getContentPane().setLayout(layout);

layout.setHorizontalGroup( layout.createParallelGroup( GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jTabbedPane1) .addContainerGap()) );

layout.setVerticalGroup( layout.createParallelGroup(

GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap()

.addComponent(jTabbedPane1,

GroupLayout.PREFERRED_SIZE,250, GroupLayout.PREFERRED_SIZE) .addContainerGap( GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }

private void jbtnAddStockActionPerformed( ActionEvent evt) {

if (JtfStockName.getText().isEmpty() || JtfNumberOfShares.getText().isEmpty() || jtxtStockPP.getText().isEmpty() || jtxtStockCP.getText().isEmpty()) {

JOptionPane.showMessageDialog(null, "All fields are mandatory"); } else {

String NOS; NOS = JtfStockName.getText();

try {

int numberOfShares = Integer .parseInt(JtfNumberOfShares.getText()); float PPS = Float .parseFloat(jtxtStockPP.getText()); float CPS = Float .parseFloat(jtxtStockCP.getText());

if (numberOfShares <= 0|| PPS <= 0 || CPS <= 0) {

JOptionPane.showMessageDialog(null, "Enter all VALUES > 0"); return; } else { allStocks.add( new StocksItems(NOS, numberOfShares, PPS,CPS)); }

listOfStockItems.addElement(NOS); jListStock.setModel(listOfStockItems);

jlblAdded.setText("stock: " + NOS + " added!"); }

catch (NumberFormatException e) {

System.out.println("error: " + e);

JOptionPane.showMessageDialog(null, "Replace: " + e.getMessage() + " with number!"); return; } } }

private void clearTextFields() {

JtfStockName.setText(""); JtfNumberOfShares.setText(""); jtxtStockPP.setText(""); jtxtStockCP.setText(""); }

private void jListStockValueChanged( ListSelectionEvent evt) {

try {

int index = jListStock.getSelectedIndex();

int size = listOfStockItems.size();

if (size == 0) {

jlblPanel.setText("Gain / Loss : "); } else {

jlblPanel.setText(allStocks.get(index) .getLossOrGain()); } }

catch (IndexOutOfBoundsException e) {

System.out.println("Out of bounds exception"); }

clearTextFields();

jlblAdded.setText(""); }

private void jbtnRemoveStockActionPerformed( ActionEvent evt) {

int index = jListStock.getSelectedIndex();

allStocks.remove(index);

listOfStockItems.remove(index);

jListStock.setSelectedIndex(index); jListStock.ensureIndexIsVisible(index); }

public static void main(String args[]) {

for (UIManager.LookAndFeelInfo info : UIManager .getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) { break; } } EventQueue.invokeLater(new Runnable() {

public void run() {

new Stocks4u_GUI().setVisible(true); } }); } }

public class StocksItems {

private String Name, LoG; private int NumberOfShares; private float PP, CP; private StocksItems stock;

public StocksItems() {

Name = ""; NumberOfShares = 0; PP = 0; CP = 0; }

public StocksItems(String SN, int SQ,float SPP, float SCP) { super();

setName(SN);

setQuantity(SQ);

setPurchasePrice(SPP);

setCurrentPrice(SCP); }

public String getName() { return Name; }

public void setName(String name) { Name = name; }

public void setLossOrGain(String LoG) {

this.LoG = LoG; }

public int getQuantity() { return NumberOfShares; }

public void setQuantity(int NumberOfShares) {

this.NumberOfShares = NumberOfShares; }

public float getPurchasePrice() { return PP; }

public void setPurchasePrice(float PP) {

this.PP = PP; }

public float getCurrentPrice() { return CP; }

public void setCurrentPrice(float CP) {

this.CP = CP; }

public StocksItems getStock() { return stock; }

public void setStock(StocksItems stock) {

this.stock = stock; }

public String getLossOrGain() {

if (CP > PP) {

LoG = "Gain of +$"+ ((CP - PP) * NumberOfShares); }

else if (CP == PP) {

LoG="Stock has neither (gained/lossed) value."; } else { LoG = "Loss of -$" + ((PP - CP) * NumberOfShares); } return LoG; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!