Write a Java program to print the net asset value for shares
Write a Java program to print the net asset value for shares held by a user.
The program will read data from a file called “GOOGprices.csv” to retrieve the date and value per share (adjCose). The only columns of interest in this comma separated value file are the first and the last column. These are the “Date” column and the “Adj Close” column respectively. We can find the totalValue
by:
totalValue
=
numberOfShares
*
adjClose
The numberOfShares will be retrieved as a user input when the program begins. The totalValue must be represented with two decimal places. We are not concerned with rounding errors/inaccuracies.
Example program output:
Welcome
to
the
Asset
Management
System
Input
the
number
of
GOOG
shares:
2
Your
GOOG
assets:
Date
Number
of
Shares
Total
Value
10/24/16
2
1626.22
10/21/16
2
1598.74
10/20/16
2
1593.94
10/19/16
2
1603.00
10/18/16
2
1590.52
10/17/16
2
1559.92
10/14/16
2
1557.06
10/13/16
2
1556.38
10/12/16
2
1572.28
10/11/16
2
1566.14
Goodbye
Solution
//import statements
import java.io.BufferedReader;
import java.util.Scanner;
import java.io.FileReader;
public class Stocks{
public static void main(String[] args) {
//declaring csv name
String csvFile = \"GOOGprices.csv\";
//BufferedReader to read csv
BufferedReader br = null;
String line = \"\";
//defining comma as the delimiter
String cvsSplitBy = \",\";
//taking number of shares as input
System.out.println(\"Welcome to the Asset Management System\");
System.out.println(\"Input the number of GOOG shares: \");
int numOfShares;
Scanner sc = new Scanner(System.in);
numOfShares = sc.nextInt();
System.out.println(\"Your GOOG assets: \");
//reading csv file
try {
br = new BufferedReader(new FileReader(csvFile));
System.out.println(\"Date\"+\" \"+\"Number of Shares\"+\" \"+\"Total Value\");
while ((line = br.readLine()) != null) {
//reading each day\'s stock
String[] day_stock = line.split(cvsSplitBy);
String date = day_stock[0];
float adjClose = Float.parseFloat(day_stock[day_stock.length-1]);
//calculating total value give adjClose and numOfShares
float totalValue = numOfShares * adjClose;
System.out.println(date+\" \"+numOfShares+\" \"+totalValue);
}
System.out.println(\"Goodbye!\");
}catch(Exception e){
System.out.println(e);
}
}
}


