A brokerage firm provides the services to buy and sell stocks for customers. The
ID: 3745855 • Letter: A
Question
A brokerage firm provides the services to buy and sell stocks for customers. The firm charges $8.99 service fee on each transaction, buy or sell. When a customer sells stocks, he/she would like to know if he/she gained or lost on a particular investment. Write a java program that allows the customer to input the number of shares sold, the purchase price of each share, and the selling price of each share. The program calculates and outputs the amount invested, the total service charges, amount gained or lost, and the amount received after selling the stock.
Sample Run:
Enter the buying price of each share: 68.89
Enter the selling price of each share: 79.34
Enter the number of shares: 100
Total amount invested: $ 6896.99
Total amount received: $ 7926.01
Total service charges: $ 15.98
Amount gained or lost: $ 1029.02
Enter the buying price of each share: 68.89
Enter the selling price of each share: 79.34
Enter the number of shares: 100
Total amount invested: $ 6896.99
Total amount received: $ 7926.01
Total service charges: $ 15.98
Amount gained or lost: $ 1029.02
Explanation / Answer
import java.util.Scanner; public class ShareProfit { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the buying price of each share: "); double buyPrice = in.nextDouble(); System.out.print("Enter the selling price of each share: "); double sellPrice = in.nextDouble(); System.out.print("Enter the number of shares: "); int num = in.nextInt(); double profit = num*(sellPrice-buyPrice) - (2*8.99); System.out.printf("Total amount invested: $ %.2f ", buyPrice*num); System.out.printf("Total amount received: $ %.2f ", sellPrice*num); System.out.printf("Total service charges: $ %.2f ", 2*8.99); System.out.printf("Amount gained or lost: $ %.2f ", profit); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.