Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. public class Market
  2. {
  3.  
  4. public static void main(String[]args)
  5. throws FileNotFoundException
  6. {
  7.  
  8. String company; // name of company
  9. double startPrice; // start price of stock
  10. double endPrice; // end price of stock
  11.  
  12. // Get name of file with stock information from user.
  13.  
  14. Scanner console = new Scanner(System.in);
  15. System.out.print("Enter stock filename: ");
  16. String stockFilename = console.nextLine();
  17.  
  18. // Read stock's data from file.
  19. Scanner stockFile = new Scanner(new FileReader(stockFilename));
  20. while (fileReader.hasNextLine())
  21. {
  22.  
  23. company = stockFile.nextLine();
  24. startPrice = stockFile.nextDouble();
  25. endPrice = stockFile.nextDouble();
  26. stockFile.close();
  27. }
  28. }
  29.  
  30. public static void processStock(String name, double startPrice, double endPrice)
  31. {
  32.  
  33. // Determine the company's abbreviation.
  34.  
  35. String companyAbbrev;
  36.  
  37. // Find left parenthesis in company name (if any).
  38. int leftParenPos = company.indexOf('(');
  39.  
  40. if (leftParenPos != -1) // parenthesis found
  41. {
  42. // Look for right parenthesis (assumed to be present)
  43. // that follows left parenthesis. Then, extract
  44. // abbreviation from within parentheses.
  45. int rightParenPos = company.indexOf(')', leftParenPos);
  46. companyAbbrev = company.substring(leftParenPos + 1, rightParenPos);
  47. }
  48. else
  49. {
  50. // Make abbreviation from first 4 characters converted
  51. // to uppercase.
  52. companyAbbrev = String.format("%.4s", company.toUpperCase());
  53. }
  54.  
  55. // Compute percentage change in price.
  56. double percentageChange = ((endPrice - startPrice) / startPrice) * 100.0;
  57.  
  58. // Separate output with blank line.
  59. System.out.println();
  60.  
  61. // Display company's stock information. Note: conditional (?:)
  62. // operator will report "Up" even if zero change.
  63. System.out.printf("%s %.2f %s %.1f%%%n",
  64. companyAbbrev, endPrice,
  65. percentageChange < 0.0 ? "Down" : "Up",
  66. Math.abs(percentageChange)
  67. );
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement