In a program that calculates the total profit or loss from the saleof multiple s
ID: 3696190 • Letter: I
Question
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 then display the total.
Method is below
import java.util.Scanner;
class StockProfit{
public static void main(String[]args){
Scanner keyboard = new Scanner(System.in);
//creating integers that'll be used for user input
int Ns,Sp,Sc,Pp,Pc;
int profit;
System.out.println("Enter the number of shares");//number of shares
Ns=keyboard.nextInt();
System.out.println("Enter the purchase price per share");//purchase price per share
Pp=keyboard.nextInt();
System.out.println("Enter the purchase commission paid");//purchase commission paid
Pc=keyboard.nextInt();
System.out.println("Enter the sale price per share");//sale price per share
Sp=keyboard.nextInt();
System.out.println("Enter the sales commission paid");//sales commission paid
Sc=keyboard.nextInt();
keyboard.close();
profit = ((Ns*Sp)-Sc)-((Ns*Pp)+Pc);
if (profit > 0)
{
gain(profit);//profit created
}
else
{
loss(profit);//loss created
}
}
public static void gain(int green){
System.out.println("Your total profit was" +green);//printing profit
}
public static void loss(int red){
System.out.println("Your total loss was" +red);//printing loss
//I'm unsure how to account for profit of 0.
}
}
Explanation / Answer
package stockprofit;
import java.util.Scanner;
class StockProfit{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
//creating integers that'll be used for user input
int Ns,Sp,Sc,Pp,Pc;
int profit;
System.out.println("Enter the number of shares");//number of shares
Ns=keyboard.nextInt();
System.out.println("Enter the purchase price per share");//purchase price per share
Pp=keyboard.nextInt();
System.out.println("Enter the purchase commission paid");//purchase commission paid
Pc=keyboard.nextInt();
System.out.println("Enter the sale price per share");//sale price per share
Sp=keyboard.nextInt();
System.out.println("Enter the sales commission paid");//sales commission paid
Sc=keyboard.nextInt();
keyboard.close();
profit = ((Ns*Sp)-Sc)-((Ns*Pp)+Pc);
if (profit >= 0)
{
gain(profit);//profit created
}
else
{
loss(profit);//loss created
}
}
public static void gain(int green){
System.out.println("Your total profit was " +green);//printing profit
}
public static void loss(int red){
System.out.println("Your total loss was " +red);//printing loss
}
}
output: profit 0 case
Enter the number of shares
2
Enter the purchase price per share
150
Enter the purchase commission paid
20
Enter the sale price per share
170
Enter the sales commission paid
20
Your total profit was 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.