Answer:
import java.util.Scanner;
public class ANot {
public static void main(String[] args) {
System.out.print("Please Enter your name: ");
Scanner in = new Scanner (System.in);
String name = in.nextLine();
System.out.print("Please Enter Total Number of Shares Bought: ");
int totalShares = in.nextInt();
System.out.print("Welcome "+name+" Please How much did you pay for each unit of share: ");
double buyAmount = in.nextDouble();
System.out.print( "Please How much did you sell each unit: ");
double sellAmount = in.nextDouble();
System.out.print("Please Enter the Total transaction fees paid to the broker (For buying and selling): ");
double transFee = in.nextDouble();
//Outputs
System.out.println("\t TRANSACTION REPORT FOR "+name);
System.out.println("Total Shares Bought is: "+totalShares);
System.out.println("Total Amount paid for the shares is: "+buyAmount*totalShares);
System.out.println("Total Amount for Selling of the share is "+sellAmount*totalShares);
System.out.println("Total transaction fees is: "+ transFee);
// Output Profit or Loss
if(((sellAmount*totalShares)-(buyAmount*totalShares)+(transFee))>0){
System.out.println("The total amount of profit is " +
" "+((sellAmount*totalShares)-(buyAmount*totalShares)+(transFee)));
}
else {
System.out.println("The total amount of loss is " +
" "+((sellAmount*totalShares)-(buyAmount*totalShares)+(transFee)));
}
}
}
Explanation:
- Receive all user inputs with the Scanner Class
- Calculate total amount for shares bought by multiplying unit price by total number bought
- Calculate total amount for sale of the share in same way as step 2 above
- determine profit and loss by subtracting (step2 from step3)
- Print out profit or loss