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.
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)));
}
}
}
Comments
Leave a comment