Write a program to accept inputs from a user including yourName, numbershares sh
ID: 3843475 • Letter: W
Question
Write a program to accept inputs from a user including yourName, numbershares shares (only whole shares allowed) purchased of some stock (for example the Microsoft stock symbol is MSFT) at the price of buyPrice per share and paid the stockbroker $15 buyTransactionFee. Two weeks later, the person sold the numberShares shares at sellPrice per share and paid another $10 for the sellTransactionFee. Create the necessary input, process and output variables, choosing appropriate data types, and write a Java program to accept input, calculate and display the following: 1. dollar amount paid for the shares 2. dollar amount of the shares sold 3. total transaction fee paid to the broker (including both buy and sell) - Use constants/final 4. amount of profit (or loss) made after selling the shares.Explanation / Answer
Hi, Below is your code: -
Shares.java
import java.util.Scanner;
public class Shares {
public static void main(String[] args) {
String name, stockName;
int numOfShares;
double buyPrice, salePrice;
double buyTransactionFee = 15.0;
double sellTransactionFee = 10.0;
Scanner sc = new Scanner(System.in);
System.out.print("What's your name?");
name = sc.next();
System.out.print("What stock are you purchasing?");
stockName = sc.next();
System.out.print("How many shares bought?");
numOfShares = Integer.parseInt(sc.next());
System.out.print("Buy Price?");
buyPrice = Double.parseDouble(sc.next());
System.out.print("Sale Price?");
salePrice = Double.parseDouble(sc.next());
sc.close();
double purAmount = numOfShares * buyPrice;
double saleAmount = numOfShares * salePrice;
double transFee = buyTransactionFee + sellTransactionFee;
double netProfit = saleAmount - (purAmount + transFee);
System.out.println();
System.out.println();
System.out.println(" Statement of " + stockName + " Transactions for " + name);
System.out.println();
System.out.println("Number of Shares Purchased: " + numOfShares);
System.out.println("Amount of Purchase: $" + purAmount);
System.out.println("Amount of sale: $" + saleAmount);
System.out.println("Transaction Fees Paid: $" + transFee);
System.out.println("Net Profit: $" + netProfit);
}
}
Sample Run:-
What's your name?Joseph
What stock are you purchasing?MSTF
How many shares bought?250
Buy Price?28.31
Sale Price?30.79
Statement of MSTF Transactions for Joseph
Number of Shares Purchased: 250
Amount of Purchase: $7077.5
Amount of sale: $7697.5
Transaction Fees Paid: $25.0
Net Profit: $595.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.