CIS355A_Koszelak_iLab_Week5.docx - Week 5 Lab Code for...

This preview shows page 1 - 6 out of 24 pages.

Week 5 Lab Code for Nicholas Koszelak CIS355a Professor Ricky Sethi Stocks4U_IO.java: /****************************************************************************** * Program Name: Stocks4U_IO.java * Programmer's Name: Nicholas Koszelak * Program Description: This portion of the total program is used to save and * retrieve data from a file. *****************************************************************************/ package stocks4u; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; /** * * @author sagan */ public class Stocks4U_IO { //attributes
private String fileName; public Stocks4U_IO() { fileName = "StockFile.txt"; } public Stocks4U_IO(String fileName) { this.fileName = fileName; } //behaviors public ArrayList<Stock> getData() { ArrayList<Stock> filedata = new ArrayList<Stock>(); try { BufferedReader readerbuffer = new BufferedReader(new FileReader(fileName)); String userInput = ""; StringTokenizer tokens; System.out.println("Reading your file"); //receive first line userInput = readerbuffer.readLine(); while(userInput != null) { System.out.println(userInput); //seperate each string tokens = new StringTokenizer(userInput, ","); String companyName = tokens.nextToken(); double numberofShares = Double.parseDouble(tokens.nextToken());
float purchasePrice = Float.parseFloat(tokens.nextToken()); float currentPrice = Float.parseFloat(tokens.nextToken()); //prints lines to file System.out.println("Company: " + companyName); System.out.println("Shares: " + numberofShares); System.out.println("Purchase Price: " + purchasePrice); System.out.println("Current Price: " + currentPrice); //Add stock information to the arraylist Stock stk = new Stock(companyName, numberofShares, purchasePrice, currentPrice); filedata.add(stk); userInput = readerbuffer.readLine(); } readerbuffer.close(); } catch(Exception ex) { System.err.println("Error occurred" + ex.toString()); } return filedata; } public void saveStock(ArrayList<Stock>theStocks) { //how to save our data from the array list to the file StockFile.txt try { BufferedWriter output = new BufferedWriter(new FileWriter(fileName, true));
//individually add each element to array list for(Stock stk: theStocks) { output.write(stk.getCompanyName() + ','); output.write("" + stk.getNumberOfShares() + ','); output.write(""+ stk.getPurchasePrice() + ','); output.write("" + stk.getCurrentPrice()); output.newLine(); } output.close(); } catch(IOException exc) { System.err.println("Error occurred" + exc.toString()); } } //accessors and return public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } } Stocks4U_GUI.java: /****************************************************************************** * Program Name: Stocks4U_GUI.java
* Programmer's Name: Nicholas Koszelak * Program Description: GUI that was built in the developer and coded further * to include additional coding to use the components made in the developer, and * the Sock and Stocks4U classes. *****************************************************************************/ package stocks4u; import java.text.DecimalFormat; import java.util.ArrayList; import javax.swing.DefaultListModel; import javax.swing.JOptionPane; public class Stocks4U_GUI extends javax.swing.JFrame { //attributes for the GUI DefaultListModel<Stock>stocklist = new DefaultListModel<Stock>(); ArrayList<Stock>stockList = new ArrayList<Stock>(); //set total value double finalTotal; private double finalTotal() { double total = 0; double ttl = 0; for(Stock stk: stockList) { if(stk instanceof Stock) { ttl = stk.getCurrentPrice()*stk.getnumberofShares();

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture