Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are interested in investing in some stock. You find that the best stocks for this year include the companies listed below: Company Stock
You are interested in investing in some stock. You find that the best stocks for this year include the companies listed below: Company Stock Price (U.S.) Starbucks NXP Semiconductors $85.77 $117.41 Facebook $202.00 Stitch Fix $23.97 Johnson & Johnson $137.75 Nike $94.14 Centene Corporation $60.44 Apple $267.84 Sprouts Farmer's Market $19.45 DuPont $65.61 Create a class that references the data provided. Remember the naming rule: All class and method names should include your initials at the end of the name. You will need: 1. Private members to store: The company name (String) The price per stock (double) The amount of stock to purchase (int) 2. A static private member called totalCost (of type double) to store the total cost of all shares among all companies (initialized to 0) 3. A constructor that accepts the name of the company and the amount of stock to be purchased. The constructor will call one private method: setStockPrice to set the price of each stock per company 4. A private method called setPrice which accepts no parameters and returns no values. The method will also contain a switch statement that creates a case for each company. No default case is needed. The expression of the switch statement should use the tolowerCase method to change the company name to lower case. o An example of how to set up your switch is shown below: switch (name.toLowerCase()) case "apple": price = 267.84: break; //complete the switch 5. A private method called calCost that accepts no parameters but returns a double value. Cost = price of stock * amount of stock In this method, update the value of totalCost (by adding cost to it) The cost of each stock, NOT totalCost will be returned by this method 6. A public and static method called getTotalCost that accepts no parameters, but returns the totalCost of all the stocks among all the companies 7. AtoString method that: Outputs the company name, price per stock, and the cost for the amount of stock purchased 8 For the cost, call the calCost method you created from within this method Formats the prices to currency (You need to include a NumberFormat object here) Formats the output using the format method from the String class so that the output looks neat and presentable. An example of how to set that up is shown below: String.format("&-20s", name) Additionally, you will create a driver program that uses your version of the BestStocks class. You will need: 1. To prompt the user for the number of companies whose stocks they are interested in. Use input validation to ensure that the user enters a value greater than or equal to 1 for the amount of companies. 2. Local variables to store the company name, and the amount of stock to be pruchased Let's presume that the user enters one of the ten companies listed. 3. To create an array of BestStocks objects, using the value from the amount of companies Example if three companies are selected the array of objects will be of size 3. 4. A loop to ask the user for the company name and the amount of stock The loop should be limited by the amount of companies Use input validation to ensure that the user enters a value greater than or equal to 1 for the amount of stock The loop must include syntax that instantiates each object once the company name and amount of stock are provided 5. Outputs the information for each BestStock object 6. Outputs the totalCost of all the shares among all the companies Call the static method getTotalCost from your BestStocks class here Format the total cost using a NumberFormat object Tip: Do not attempt the format the static variable, as this can cause a syntax error. Samples of the output are shown below: Sample Output 1- (with correct values) How many companies' stocks are you interested in? Amount must be >= 1: Enter name of company 1: Nike Enter amount of stock (>= 1) for Nike: 150 Enter name of company 2: Sprouts Farmer's Market Enter amount of stock (>= 1) for Sprouts Farmer's Market: 250 Enter name of company 3: Stitch Fix Enter amount of stock (>= 1) for Stitch Fix: 100 Enter name of company 4: Starbucks Enter amount of stock (>= 1) for Starbucks: 200 Your companies and stocks are: Nike Sprouts Farmer's Market Stitch Fix Starbucks The total amount is: $38,534.50 $14,121.00 $4,862.50 $2,397.00 $17,154.00 $94.14 $19.45 $23.97 $85.77 Sample Output 2 - (with input validation) How many companies stocks are you interested in? Amount must be >= 1: How many companies stocks are you interested in? Amount must be >= 1: 3 Enter name of company 1: DuPont Enter amnount of stock (>= 1) for DuPont: Amount of stock must be greater than or equal to 1. Re-enter amount of stock for DuPont: 50 Enter name of company 2: Centene Corporation. Enter amount of stock (>= 1) for Centene Corporation: 75 Enter name of company 3: Facebook Enter amount of stock (>= 1) for Facebook: 40 Your companies and stocks are: $65.61 $60.44 $202.00 $3,280.50 $4,533.00 $8,080.00 DuPont Centene Corporation Facebook The total amount is: $15,893.50 Extra Credit (10 points): Use Dialog Boxes to read in and display your output in your version of the BestStocks Driver program!
Step by Step Solution
★★★★★
3.45 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
CODE import javaio import javatextNumberFormat import javautil BestStocks class public class BestStocks member variable declaration private String com...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started