Write a Java program to calculate and display the following: 1. the dollar amount paid for the shares 2. the dollar amount of the shares sold 3. the total transaction fee paid to the broker (including both buy and sale) 4. the amount of profit (or loss) made after selling the shares.

Respuesta :

ijeggs

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:

  1. Receive all user inputs with the Scanner Class
  2. Calculate total amount for shares bought by multiplying unit price by total number bought
  3. Calculate total amount for sale of the share in same way as step 2 above
  4. determine profit and loss by subtracting (step2 from step3)
  5. Print out profit or loss