Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Market
- {
- public static void main(String[]args)
- throws FileNotFoundException
- {
- String company; // name of company
- double startPrice; // start price of stock
- double endPrice; // end price of stock
- // Get name of file with stock information from user.
- Scanner console = new Scanner(System.in);
- System.out.print("Enter stock filename: ");
- String stockFilename = console.nextLine();
- // Read stock's data from file.
- Scanner stockFile = new Scanner(new FileReader(stockFilename));
- while (fileReader.hasNextLine())
- {
- company = stockFile.nextLine();
- startPrice = stockFile.nextDouble();
- endPrice = stockFile.nextDouble();
- stockFile.close();
- }
- }
- public static void processStock(String name, double startPrice, double endPrice)
- {
- // Determine the company's abbreviation.
- String companyAbbrev;
- // Find left parenthesis in company name (if any).
- int leftParenPos = company.indexOf('(');
- if (leftParenPos != -1) // parenthesis found
- {
- // Look for right parenthesis (assumed to be present)
- // that follows left parenthesis. Then, extract
- // abbreviation from within parentheses.
- int rightParenPos = company.indexOf(')', leftParenPos);
- companyAbbrev = company.substring(leftParenPos + 1, rightParenPos);
- }
- else
- {
- // Make abbreviation from first 4 characters converted
- // to uppercase.
- companyAbbrev = String.format("%.4s", company.toUpperCase());
- }
- // Compute percentage change in price.
- double percentageChange = ((endPrice - startPrice) / startPrice) * 100.0;
- // Separate output with blank line.
- System.out.println();
- // Display company's stock information. Note: conditional (?:)
- // operator will report "Up" even if zero change.
- System.out.printf("%s %.2f %s %.1f%%%n",
- companyAbbrev, endPrice,
- percentageChange < 0.0 ? "Down" : "Up",
- Math.abs(percentageChange)
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement