Answer to Question #316231 in Java | JSP | JSF for mayonnaise

Question #316231

Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to input the number of shares sold, the purchase price of each share, and the selling price of each share. The program outputs the amount invested, the total service charges, amount gained or lost, and the amount received after selling the stock.

1
Expert's answer
2022-03-23T13:45:32-0400
import java.util.Scanner;

public class BrokerageFirm {

    public static double calculationAmountServiceCharges(double serviceCharges, int numberOfSharesSold, double priceOfShare){
        return ((numberOfSharesSold*priceOfShare)*serviceCharges)/100;
    }

    public static double calculationAmountInvested(int numberOfSharesSold, double purchasePriceOfEachShare,double serviceCharges){
        return (numberOfSharesSold*purchasePriceOfEachShare)+calculationAmountServiceCharges(serviceCharges,numberOfSharesSold,purchasePriceOfEachShare);
    }

    public static double calculationAmountAfterSelling(int numberOfSharesSold, double sellingPriceOfEachShare,double serviceCharges){
        return (numberOfSharesSold*sellingPriceOfEachShare)-calculationAmountServiceCharges(serviceCharges,numberOfSharesSold,sellingPriceOfEachShare);
    }
    public static double calculationTotalServiceCharges(double serviceCharges, int numberOfSharesSold, double purchasePriceOfEachShare, double sellingPriceOfEachShare){
        return calculationAmountServiceCharges(serviceCharges,numberOfSharesSold,purchasePriceOfEachShare)+calculationAmountServiceCharges(serviceCharges,numberOfSharesSold,sellingPriceOfEachShare);
    }

    public static double calculationGainedOrLostAmount(double serviceCharges, int numberOfSharesSold, double purchasePriceOfEachShare, double sellingPriceOfEachShare){
        return (calculationAmountInvested(numberOfSharesSold,purchasePriceOfEachShare,serviceCharges)-calculationAmountAfterSelling(numberOfSharesSold,sellingPriceOfEachShare,serviceCharges));
    }

    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        double serviceCharges = 1.5;
        System.out.print("Input the number of shares sold: ");
        int numberOfSharesSold = in.nextInt();
        System.out.print("Input  the purchase price of each share: ");
        double purchasePriceOfEachShare = in.nextInt();
        System.out.print("Input the selling price of each share: ");
        double sellingPriceOfEachShare = in.nextInt();

        System.out.println("Amount invested: "+calculationAmountInvested(numberOfSharesSold,purchasePriceOfEachShare,serviceCharges));
        System.out.println("Total service charges: "+calculationTotalServiceCharges(serviceCharges,numberOfSharesSold,purchasePriceOfEachShare,sellingPriceOfEachShare));
        System.out.println("Amount received after selling the stock: "+calculationAmountAfterSelling(numberOfSharesSold,sellingPriceOfEachShare,serviceCharges));
        if(calculationGainedOrLostAmount(serviceCharges,numberOfSharesSold,purchasePriceOfEachShare,sellingPriceOfEachShare)>=0){
            System.out.println("Amount lost: "+calculationGainedOrLostAmount(serviceCharges,numberOfSharesSold,purchasePriceOfEachShare,sellingPriceOfEachShare));
        }
        else{
            System.out.println("Amount gained: "+(-calculationGainedOrLostAmount(serviceCharges,numberOfSharesSold,purchasePriceOfEachShare,sellingPriceOfEachShare)));
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS