Use this method: In a program that calculates the total profit or loss from the
ID: 3618605 • Letter: U
Question
Use this method:In a program that calculates the total profit or loss from the saleof multiple stocks. The program should ask the user for the numberof stock sales, and the necessary data for each stock sale. Itshould accumulate the profit or loss for each stock sale and thendisplay the total.
import java.util.Scanner; //Needed for Scanner class
public class StockProfit
{
public static void main(String[] args)
{
int sale;
double NS, PP, PC, SP,SC;
double profit;
// Create a Scanner objectfor kb input.
Scanner kb = newScanner(System.in);
// Get answers from theuser
System.out.print("Enter thenumber of Shares: ");
NS = kb.nextDouble();
System.out.print("Enter thePurchase Price per share: ");
PP = kb.nextDouble();
System.out.print("Enter thePurchase Commission paid: ");
PC = kb.nextDouble();
System.out.print("Enter theSale Price per share: ");
SP = kb.nextDouble();
System.out.print("Enter theSale Commission paid: ");
SC = kb.nextDouble();
// Calculation
// You could have used test = caculate_Profit_or_Loss( NS, SP, SC,PP, PC) ; i.e define only one method
// In that method you could have used you formula and returned theresult to test variable
profit = ((NS * SP) - SC) -((NS * PP) + PC);
//----------------------------------------------
// Determine whether thecalculation yields a
// positive value or negativevalue.
//----------------------------------------------
if (profit >0) // if(test > 0) yougained a profit
{
// CallProfit method
doubleans1 = prof(NS, SP, SC, PP, PC);
//Display
}
else if (profit <0) // else you incurred a loss
{
// CallLoss method
doubleans2 = loss(NS, SP, SC, PP, PC);
}
}
// Profit method
public static double prof(double ns, double sp,double sc, double pp, double pc)
{
double result;
result = ((ns * sp) - sc) -((ns * pp) + pc);
System.out.println("Theamount of the profit: "
+ result);
return result;
}
public static double loss(double ns, double sp,double sc, double pp, double pc)
{
double result;
result = ((ns * sp) - sc) -((ns * pp) + pc);
System.out.println("Theamount of the loss: "
+ result);
return result;
}
}
Explanation / Answer
importjava.io.*; //Needed for file classes
class stockSales
{
public static voidmain(String [] args)
{
doubleNS,SP,SC,PP,PC;
doubleProfit;
intn;
// Create aScanner object for keyboard input.
Scannerkeyboard = new Scanner(System.in);
//inputtingnumber of stocks
System.out.println("Enter number of stocks:");
n=keyboard.nextInt();
for(inti=1;i<=n;i++)
{
//inputtingNS
System.out.println("Enter number of shares:");
NS=keyboard.nextDouble();
//inputtingSP
System.out.println("Enter sale price per share:");
SP=keyboard.nextDouble();
//inputtingSC
System.out.println("Enter Sale commission paid:");
SC=keyboard.nextDouble();
//inputtingPP
System.out.println("Enter purchase price for share:");
PP=keyboard.nextDouble();
//inputtingPC
System.out.println("Enter Purchase commission paid:");
PC=keyboard.nextDouble();
//functioncall
Profit=calculateProfit(NS,SP,SC,PP,PC);
if(Profit<0)
System.out.println("Loss forstock"+i+"is:"+Profit);
else
System.out.println("Profit for stock"+i+"is:"+Profit);
}//end for
}
public staticdouble calculateProfit(double NS,double
SP,double SC,double
PP,double PC)
{
double profit;
profit=((NS*SP)-SC)-((NS*PP)+PC);
return profit;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.