WRITE A JAVA PROGRAM
1. Assignment Description
Write a stock transaction program that calculates a customer's profit (or loss) within a certain period of time.
1) First, the program need to get user's name, the stock's code, number of shares and the price he/she purchased the stock, it also asks the user for the price he/she sold the stock later on. The program then compute the following:
The amount of money the customer paid for the stock.
The amount of commission the customer paid when he/she bought the stock
The amount that the cusotmer sold the stock for.
The amount of commission the customer paid his broker when he sold the stock
The amount of profit or loss that the customer had after selling his stock and paying both buy & sell commissions.
See below for the sample run of the program.
2) Assume for both buying or selling the stock, the agent charges 2% of the total amount from customer. (Hint: this should be declared as a constant)
3) Your program also need to meet the specifications stated below:
For the input and output, the exact spacing is not necessary, but what's important is that your program's output looks good and the columns align, i.e. your program's output should be very similar to our sample output. You should consider using escape sequence such as \t, \n to achieve this.
Pick and use identifiers which are self-descriptive. One-letter variable names should not be used. As an example, if you were referring to the stock's purchasing price, the variable needed might be declared as double purchasePrice; instead of double x;
All ouput price and amounts should be formatted as currency by using DecimalFormat class.
Depends on whether the profit is postivie or negative, your program should display different messages on screen, see our two output test cases for this. To achieve this, you will need to use simple if..else statement. Check Lab #2 if you don't know how to do this.
Note: User input is in bold.
Enter Your Name: John Smith
Enter the stock code you bought: APPL
At what price did you buy it: 176.42
How many shares did you buy: 100
At what price did you sell it: 208.8
******** Stock Transaction Report *********
Customer Name: John
Smith
Stock
Code:
APPL
Number of Shares: 100
Purchase Price:
$176.42
Purchase Amt:
$17642.00
Purchase Commission: $352.84
Sell
Price:
$208.80
Sell
Amt:
$20880.00
Sell Commission: $417.60
Profit
is:
$2467.56
Test Case 1 Input:
Joe Dow UBER 45.0 3500 33.06
Test Case 1 Output:
******** Stock Transaction Report ********* Customer Name: Joe Dow Stock Code: UBER Number of Shares: 3500 Purchase Price: $45.00 Purchase Amt: $157500.00 Purchase Commission: $3150.00 Sell Price: $33.06 Sell Amt: $115710.00 Sell Commission: $2314.20 You lose: $47254.20
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! ===========================================================================
import java.util.Scanner; public class StockTrader { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter Your Name: "); String name = scanner.nextLine(); System.out.print("Enter the stock code you bought: "); String code = scanner.nextLine(); System.out.print("At what price did you buy it: "); double buyingPrice = scanner.nextDouble(); System.out.print("How many shares did you buy: "); int shares = scanner.nextInt(); System.out.print("At what price did you sell it: "); double sellingPrice = scanner.nextDouble(); System.out.println("******** Stock Transaction Report *********"); System.out.println("Customer Name: " + name); System.out.println("Stock Code: " + code); System.out.println(); System.out.println("Number of Shares: " + shares); System.out.printf("Purchase Price: $%.2f\n", buyingPrice); System.out.printf("Purchase Amt: $%.2f\n", buyingPrice * shares); System.out.printf("Purchase Commission: $%.2f\n", buyingPrice * shares * 0.02); System.out.println(); System.out.println("Sell Price: $" + sellingPrice); System.out.printf("Sell Amt: $%.2f\n", sellingPrice * shares); System.out.printf("Sell Commission: $%.2f\n", sellingPrice * shares * 0.02); double netCost = buyingPrice * shares * (1.02); double netSelling = sellingPrice * shares * (1.02); if (netCost < netSelling) { System.out.printf("\nProfit is: $%.2f\n", (netSelling - netCost)); } else { double totalLoss = (buyingPrice - sellingPrice) * shares + sellingPrice * shares * 0.02 + buyingPrice * shares * 0.02; System.out.printf("\nLoss is: $%.2f\n", (totalLoss)); } } }
Get Answers For Free
Most questions answered within 1 hours.