372,127 views
22 votes
22 votes
Create a class called StockTester that has the following fucntionality. a. Create a main method with an ArrayList named dataStock that stores objects of type Stock. b. Add code that reads the content of StockInfo.csv and places it in dataStock. c. Create a new Stock object named newStock using the constructor in Question 1. Set the values to the following. Stock name: Gamma, Stock purchase date: 03/01/20, Number of Shares of Stock: 100, Stock Price: 50.5. Add newStock to dataStock. d. Print the information associated with newStock using printStock. e. Using the method requiredReturn, determine the rate of return required for your stock in Pitsco to have a value of $4,000 in 3 years. Print the result to the screen so that the user can clearly read the result.

User Kalimag
by
3.0k points

1 Answer

20 votes
20 votes

Solution :

public class
$\text{Stock}$ {

private
$\text{String}$ stockName,
$\text{purchaseDate}$;

private
$\text{int}$ nShares;

private
$\text{double}$ price;

public
$\text{Stock}()$

{

this.stockName = "";

this.purchaseDate = "";

this.nShares = 0;

this.price = 0.0;

}

public Stock(String stockName, String purchaseDate, int nShares, double price) {

this.stockName = stockName;

this.purchaseDate = purchaseDate;

this.nShares = nShares;

this.price = price;

}

public String getStockName() {

return stockName;

}

public void setStockName(String stockName) {

this.stockName = stockName;

}

public String getPurchaseDate() {

return purchaseDate;

}

public void setPurchaseDate(String purchaseDate) {

this.purchaseDate = purchaseDate;

}

public int getnShares() {

return nShares;

}

public void setnShares(int nShares) {

this.nShares = nShares;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public String toString()

{

return("Stock name: " + this.stockName + "\\Purchase date: " + this.purchaseDate

+ "\\Number of shares of stock: " + this.nShares + "\\Price: $" + String.format("%,.2f", this.price));

}

public void printStock()

{

System.out.println("Stock name: " + this.stockName + "\\Purchase date: " + this.purchaseDate

+ "\\Number of shares of stock: " + this.nShares + "\\Price: $" + String.format("%,.2f", this.price)

+ "\\");

}

}

StockTester.java (Driver class)

import
$\text{java.io.}$File;

import
$\text{java.io.}$File
$\text{NotFound}$Exception;

import
$\text{java.util.}$ArrayList;

import
$\text{java.util.}$Scanner;


$\text{public}$ class StockTester {

private static final String FILENAME = "StockInfo
$.$csv";

public static
$\text{void}$ main(
$\text{String}[]$ args)

{

ArrayList
$<\text{stock}>$ dataStock =
$\text{readData}$(FILENAME);

System.out.println("Initial stocks:");

for(Stock s : dataStock)

s.printStock();

System.out.println("Adding a new Stock object to the list..");

Stock newStock = new Stock("Gamma", "03/01/20", 100, 50.5);

dataStock.add(newStock);

System.out.println("\\Stocks after adding the new Stock..");

for(Stock s : dataStock)

s.printStock();

Stock targetStock = dataStock.get(3);

double reqReturn = requiredReturn(targetStock, 4000, 3);

System.out.println("Required rate of return = " + String.format("%.2f", reqReturn) + "%");

}

private static ArrayList<Stock> readData(String filename)

{

ArrayList<Stock> stocks = new ArrayList<>();

Scanner fileReader;

try

{

fileReader = new Scanner(new File(filename));

while(fileReader.hasNextLine())

{

String[] data = fileReader.nextLine().trim().split(",");

String stockName = data[0];

String purchaseDate = data[1];

int nShares = Integer.parseInt(data[2]);

double price = Double.parseDouble(data[3]);

stocks.add(new
$\text{Stock}$(stockName,
$\text{purcahseDate}$, nShares, price));

}

fileReader.close();

}catch(FileNotFoundException fnfe){

System.out.println(filename + " cannot be found!");

System.exit(0);

}

return stocks;

}

private static double requiredReturn(Stock s, double targetPrice, int years)

{

double reqReturn;

double initialPrice = s.getPrice() * s.getnShares();

reqReturn = ((targetPrice - initialPrice) / initialPrice * years) * 100;

return reqReturn;

}

}

User Newmangt
by
3.2k points