/**********************************************************************************************************************/ // Program Title: DoStocks // File Name: DoStocks.java // Input File Name: combstks.txt // Purpose: To comply with the Assignment specifications, specifically: // // 1) Import the necessary Java class libraries: // java.io.* // java.util.* // // 2) Execute the program using the command shown below: // java DoStocks // // 3) Read the data from an external text file named combstks.txt // // 4) The file input will be used to create a report that looks like this: // // Composite Value Stock Report // ==================================================== // The sum of the Opening prices: _____________ // The sum of the Opening prices / 4: _____________ // The sum of the High prices: _____________ // The sum of the High prices / 4: _____________ // The sum of the Low prices: _____________ // The sum of the Low prices / 4: _____________ // The sum of the Closing prices: _____________ // The sum of the Closing prices / 4: _____________ // // First Record Stock Report // ==================================================== // The symbol for the stock is: _____________ // The stock trade date is: _____________ // The opening price for the day is: _____________ // The high price for the day is: _____________ // The low price for the day is: _____________ // The closing price for the day is: _____________ // The total volume for the day is: _____________ // The total change for the day is: _____________ // // Last Record Stock Report // ==================================================== // The symbol for the stock is: _____________ // The stock trade date is: _____________ // The opening price for the day is: _____________ // The high price for the day is: _____________ // The low price for the day is: _____________ // The closing price for the day is: _____________ // The total volume for the day is: _____________ // The total change for the day is: _____________ // // Use a While loop to find out HOW MANY Records and do the Sumations // // Use a For loop to do the split() for breaking out fields // // Use an Assignment with a Multiplication * 4 to get the results // // // // // Programming Language: Java 2, J2SE 5 // JDK Version: 1.5.0.06 // Development Operating System: Windows XP, SP 2 // Development Hardware: Intel Pentium III, 550 MHz, IBM ThinkPad T20 // Author: William Favre Slater, III // Date Created: April 10 - 23, 2006 // Class: CMSC120 - Java Programming // Professor Louis Lilakos, Instructor // Marist College - IDCP Data Center Technology Program // /**************************************************************************************************************/ import java.io.*; // import the java.io package import java.util.*; // import the java.util package public class DoStocks { public static void main (String[] args) throws Exception { try { // Prepare for some time calculations // code & technique borrowed from Head First Java, 2nd edition // by Kathy Sierra and Bert Bates Calendar c1 = Calendar.getInstance(); long start_time = c1.getTimeInMillis(); System.out.println(" "); System.out.println(" "); System.out.println("The start time is " + c1.getTime()); System.out.println(" "); System.out.println(" "); // Initialize all variables String line = null; // line will contain the current record being read String line1 = null; // line1 will contain the first record of data String lastline = null; // lastline will contain the second record of data String currentline = null; // currentline will contain the current record for splitting int icount = (int) 0; // index for "while" loop int imax = (int) 0; // max value for "while" loop int ifield = (int) 0; // index for "for" loop for field processing within the record // Report Variables Section String text1 = "The symbol for the stock is: "; // Field 1 explanation String text2 = "The stock trade date is: "; // Field 2 explanation String text3 = "The opening price for the day is: "; // Field 3 explanation String text4 = "The high price for the day is: "; // Field 4 explanation String text5 = "The low price for the day is: "; // Field 5 explanation String text6 = "The closing price for the day is: "; // Field 6 explanation String text7 = "The total volume for the day is: "; // Field 7 explanation String calc_text1 = "The absolute change for the day is: "; // Calculated Field 1 explanation String calc_text2 = "The percentage change for the day is: " ; // Calculated Field 2 String stock_symbol = null; // Stock Symbol String trade_date = null; // Trade Date int volume = (int) 0; // Volume variable double opening_price = 0.0; // Opening Price in double precision for calculation double closing_price = 0.0; // Closing Price in double precision for calculation double low_price = 0.0; // Low Price in double precision for calculation double high_price = 0.0; // High Price in double precision for calculation double total_change = 0.0; // Total Change in Price double total_percent_change = 0.0; // Total Percent Change in Price double sum_of_opening_price = 0.0; // sum of all opening price values double sum_of_closing_price = 0.0; // sum of all closing price values double sum_of_low_price = 0.0; // sum of all low price values double sum_of_high_price = 0.0; // sum of all high price values double sum_of_opening_price_div_by_4 = 0.0; // sum of all opening price values / 4 double sum_of_closing_price_div_by_4 = 0.0; // sum of all closing price values / 4 double sum_of_low_price_div_by_4 = 0.0; // sum of all low price values / 4 double sum_of_high_price_div_by_4 = 0.0; // sum of all high price values / 4 // ============================================================== // Input Section // ============================================================== // Read the combstks.txt file and print the contents to the screen // Preparation with java.io objects that will be used to read the file // File is a class in the java.io package used for accessing files on disk // for test purposes, we use this test data file and comment it out when we go "live." // File myFile = new File("combstks_test_data.txt"); File myFile = new File("combstks.txt"); // FileReader is a java.io connection stream for characters that connect to a text file FileReader fileReader = new FileReader (myFile); // associate the FileReader to a BufferedReader class from java.io for more efficient reading. // It will go back to the file to ReadOnly mode BufferedReader reader = new BufferedReader(fileReader); // This while loop does two primary things // 1) it reads the records from the BufferedReader object called "reader" // and prints these to the screen // 2) it loads defines the string variables, line1, lastline, and line3 so // they may be used by the program to parse and create a report // Test While Loop to find the number of records in the file // We may do the summations here also System.out.println(" "); System.out.println(" "); System.out.println("************* Please Stand-by... Stock Data Is Being Processed ************"); System.out.println(" "); System.out.println(" "); while ((line = reader.readLine()) != null) { currentline = line; // store line into currentline. String[] result_00 = currentline.split(","); // use the split() method icount++; // increment counter for while loop imax = icount; // imax will contain the total number of records when we stop // process the first record if (icount == (int) 1) { line1 = line; } // we found the last record when we exit this while loop. lastline = line; for (String token:result_00) { switch (ifield) { case 0 : break; case 1 : break; case 2 : opening_price = Double.parseDouble(token); // convert string to double break; case 3 : high_price = Double.parseDouble(token); // convert string to double break; case 4 : low_price = Double.parseDouble(token); // convert string to double break; case 5 : closing_price = Double.parseDouble(token); // convert string to double break; case 6 : break; } // end of switch statement // Populate the Sum Variables with data from each record. ifield++; // increment the switch counter } // End of For Loop sum_of_opening_price = sum_of_opening_price + opening_price; // sum of all opening price values sum_of_closing_price = sum_of_closing_price + closing_price; // sum of all closing price values sum_of_low_price = sum_of_low_price + low_price; // sum of all low price values sum_of_high_price = sum_of_high_price + high_price; // sum of all high price values // printing each record (line) to the screen to show that the file // for debugging and demonstration purposes only! // System.out.println("Contents of Record No. " + icount + " = " +line); //while there are still text lines in combstks.txt continue; } // end of while sum_of_opening_price_div_by_4 = sum_of_opening_price / 4.0; // sum of all opening price values / 4 sum_of_closing_price_div_by_4 = sum_of_closing_price / 4.0; // sum of all closing price values / 4 sum_of_low_price_div_by_4 = sum_of_low_price / 4.0; // sum of all low price values / 4 sum_of_high_price_div_by_4 = sum_of_high_price / 4.0 ; // sum of all high price values / 4 System.out.println(" "); System.out.println("There are " + imax + " records in the file named combstks.txt."); System.out.println(" "); System.out.println("The first record is:"); System.out.println(line1); System.out.println(" "); System.out.println("The last record is:"); System.out.println(lastline); System.out.println(" "); System.out.println(" "); System.out.println(" "); System.out.println("******************* End of Raw Data to Be Processed *******************"); System.out.println(" "); reader.close() ; // close file when finished reading stock data System.out.println("Composite Value Stock Report"); System.out.println("===================================================="); System.out.println("The sum of the Opening prices: $ " + sum_of_opening_price); System.out.println("The sum of the Opening prices / 4 = $ " + sum_of_opening_price_div_by_4); System.out.println("The sum of the High prices = $ " + sum_of_high_price); System.out.println("The sum of the High prices / 4 = $ " + sum_of_high_price_div_by_4); System.out.println("The sum of the Low prices = $ " + sum_of_low_price); System.out.println("The sum of the Low prices / 4 = $ " + sum_of_low_price_div_by_4); System.out.println("The sum of the Closing prices= $ " + sum_of_closing_price); System.out.println("The sum of the Closing prices / 4 = $ " + sum_of_closing_price_div_by_4); // ============================================================== // String Data Parsing Section // ============================================================== // I had hoped to try this three different ways and benchmark it, showing the start and finish times // --------------------------------------------------------------------------------------- // Data Parsing Method 1: // --------------------------------------------------------------------------------------- // (was going to print Section Title and Start time) System.out.println(" "); System.out.println("*********************** System Console Report *************************"); System.out.println(" "); System.out.println("=================================================== "); System.out.println(" Using the split() Method - Report the Data"); System.out.println("=================================================== "); System.out.println(" "); System.out.println(" "); System.out.println(" "); System.out.println("First Stock Record Report "); System.out.println("===================================================================== "); System.out.println(" "); String[] result_01 = line1.split(","); icount = (int) 0; for (String token:result_01) { switch (icount) { case 0 : System.out.println(text1 + token); // Stock Symbol break; case 1 : System.out.println(text2 + token + " ( YYYYMMDD format )"); // Trade Date break; case 2 : System.out.println(text3 + token); // Opening Price opening_price = Double.parseDouble(token); // convert string to double break; case 3 : System.out.println(text4 + "$ " + token); // High Price break; case 4 : System.out.println(text5 + "$ " + token); // Low Price break; case 5 : System.out.println(text6 + "$ " + token); // Closing Price closing_price = Double.parseDouble(token); // convert string to double break; case 6 : volume = Integer.parseInt(token); // Turn volume into an integer from a String volume = volume * 100; // Multiple Volume * 100 System.out.println(text7 + volume); // Daily Volume break; } // end of switch statement icount++; // increment the switch counter } // end of for statement // Perform Record 1 Calculations total_change = (closing_price - opening_price); // Calculate Total Change in Price total_percent_change = (((closing_price - opening_price) / opening_price) * 100); // Calculate Total Percent Change in Price System.out.println(calc_text1 + " $ " + total_change); // Total Change in Price System.out.println(calc_text2 + total_percent_change + " % "); // Total Percent Change in Price // ---------------------------------------------------------------------- // Processing The Last Record's Data... // ---------------------------------------------------------------------- System.out.println(" "); System.out.println(" "); System.out.println("Last Record Stock Report "); System.out.println("===================================================================== "); System.out.println(" "); String[] result_02 = lastline.split(","); icount = (int) 0; for (String token:result_02) { switch (icount) { case 0 : System.out.println(text1 + token); // Stock Symbol break; case 1 : System.out.println(text2 + token + " ( YYYYMMDD format )"); // Trade Date break; case 2 : System.out.println(text3 + "$ " + token); // Opening Price opening_price = Double.parseDouble(token); // convert string to double break; case 3 : System.out.println(text4 + "$ " + token); // High Price break; case 4 : System.out.println(text5 + "$ " + token); // Low Price break; case 5 : System.out.println(text6 + "$ " + token); // Closing Price closing_price = Double.parseDouble(token); // convert string to double break; case 6 : volume = Integer.parseInt(token); // Turn volume into an integer from a String volume = volume * 100; // Multiple Volume * 100 System.out.println(text7 + volume); // Daily Volume break; } // end of switch statement icount++; // increment the switch counter } // end of for statement // Perform Last Stock Record Calculations total_change = (closing_price - opening_price); // Calculate Total Change in Price total_percent_change = (((closing_price - opening_price) / opening_price) * 100); // Calculate Total Percent Change in Price System.out.println(calc_text1 + " $ " + total_change); // Total Change in Price System.out.println(calc_text2 + total_percent_change + " % "); // Total Percent Change in Price System.out.println(" "); System.out.println("There are " + imax + " records in the file named combstks.txt."); System.out.println(" "); System.out.println("The first record is:"); System.out.println(line1); System.out.println(" "); System.out.println("The last record is:"); System.out.println(lastline); System.out.println(" "); System.out.println("***************** End of System Console Report ******************"); System.out.println(" "); // ============================================================== // Program Shutdown Section // ============================================================== // Prepare for some more time calculations // code & technique borrowed from Head First Java, 2nd edition // by Kathy Sierra and Bert Bates Calendar c2 = Calendar.getInstance(); long finish_time = c2.getTimeInMillis(); System.out.println("The finish time is " + c2.getTime()); long elapsed_time = finish_time - start_time; System.out.println("Whoa! The elapsed time is " + (elapsed_time/1000) + " seconds."); // Wrap up things and stop the program System.out.println(" "); System.out.println("OK... We are shutting down the program."); System.out.println(" "); System.out.println("Goodbye!"); System.exit( 0 ); // exit the program gracefully } // end of try catch(Exception ex) { ex.printStackTrace(); // for error trapping and reporting } // end of catch block } // end of main() } // end of class ShowStockPrices // =================================================================================== // Assumptions: // 1) This program can be written, tested, and debugged in a finite amount of time // 2) The text file named stkprice.txt will reside in the same directory. // 3) It's permissible to display the raw stock data in the console output as verification that // that the file was successfully opened. // 4) The program's final output will be created in the command line console window. // 5) The absolute change and percentage change calculation formula provided in the // assignment description are accurate and sufficient to complete the assignment // meaning no additional formula research, or embellishment is required. // 6) It's permissible to display the program execution calculation also in the console output // 7) It's permissible to display a report with the following information: // // ********************** System Console Report *********************** // // Composite Value Stock Report // ==================================================== // The sum of the Opening prices: _____________ // The sum of the Opening prices / 4: _____________ // The sum of the High prices: _____________ // The sum of the High prices / 4: _____________ // The sum of the Low prices: _____________ // The sum of the Low prices / 4: _____________ // The sum of the Closing prices: _____________ // The sum of the Closing prices / 4: _____________ // // First Stock Record Report // ==================================================== // The symbol for the stock is: _____________ // The stock trade date is: _____________ // The opening price for the day is: _____________ // The high price for the day is: _____________ // The low price for the day is: _____________ // The closing price for the day is: _____________ // The total volume for the day is: _____________ // The total change for the day is: _____________ // // Last Stock Record Report // ==================================================== // The symbol for the stock is: _____________ // The stock trade date is: _____________ // The opening price for the day is: _____________ // The high price for the day is: _____________ // The low price for the day is: _____________ // The closing price for the day is: _____________ // The total volume for the day is: _____________ // The total change for the day is: _____________ // // **************** End of System Console Report **************** // // 8) The program should always end gracefully, and clean up after itself // as it shuts down. // // ===================================================================================