Question: Write a Java program to do the following: 1. Read in data from a text file that contains stock transactions. YOUR PROGRAM SHOULD NOT CONTAIN
Write a Java program to do the following:
1. Read in data from a text file that contains stock transactions. YOUR PROGRAM SHOULD NOT CONTAIN AN ABSOLUTE PATH TO THE FILE, ONLY THE FILE NAME (i.e., stocks.txt) 2. Read each line in the file and apply the transactions (i.e., buy or sell in the quantity indicated) and keep track of a running balance. 3. Display the final balance in the account after all of the transactions have been applied. Requirements: In addition to fulfilling the interaction described above your program must also 1. Have the main class named TransactionProcessor
2. Read from the file stocks.txt. This file as the following format with the first column containing the stock ID, the second column containing the action to perform (B for buy and S for sell), the third column contains the number of shares to buy or sell and the last column contains the unit price. stocks.txt
3. Apply each transaction and keep a running tally of the balance. Do not worry if you show more or fewer decimal places with the results or if the alignment is off. Bonus: Your first priority is to get a functioning solution to this problem. Once that is complete, you are encouraged [but not required] to enhance your program by performing checks (e.g., do sufficient funds exist in the account to make a purchase, does the user own the number of stocks that are to be sold), using a modular designed, etc. If you provide such improvements, you may earn up to 5 bonus points towards your score on this assignment. Remember: Use good programming practices such as proper use of white space, sufficient in-line comments to explain your code and provide the header that is available on Blackboard. You should also look at the rubric on Blackboard to see how your homework will be assessed. Create and use user-defined methods as you warranted. Make sure that all methods have headers and your program clearly identifies yours algorithm as comments.
import java.io.File; import java.io.IOException; import java.util.Scanner; public class TransactionProcessor { public static void main(String[] args) throws IOException,NumberFormatException { File f = new File("stocks.txt"); Scanner scnr = new Scanner(f); while(scnr.hasNextLine()) { String line = scnr.nextLine(); String[] fields = line.split("\t"); int stockID = Integer.parseInt(fields[0]); char action = fields[1].charAt(0); int quantity = Integer.parseInt(fields[2]); double price = Double.parseDouble(fields[3]); System.out.println(stockID + ", " + action + ", " + quantity + ", " + price); } } } SAMPLE TEXT: ("stocks.txt")
0 B 4 25 1 B 10 5.50 2 B 100 3.50 1 S 2 10 2 B 50 4 1 S 50 10
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
