fork download
  1. // Online Java compiler
  2. // Use this editor to write, compile and run your Java code online
  3.  
  4. public class StockTransaction
  5. {
  6. public static void main(String[] args)
  7. {
  8. final int NUM_SHARES = 1000; // Number of shares
  9. final double PURCHASE_PRICE = 32.87; // Purchase price per share
  10. final double SELLING_PRICE = 33.92; // Selling price per share
  11. final double BROKER_COMM_RATE = 0.02; // Broker commission rate
  12. // Calculate the cost of the stock (without the
  13. // broker's commission) and store the result
  14. // in stockPurchase.
  15. double stockPurchase = (NUM_SHARES * PURCHASE_PRICE);
  16. // Calculate the broker's commission on the purchase and
  17. // store the result in purchaseComm.
  18. double purchaseComm = stockPurchase * BROKER_COMM_RATE;
  19. // Calculate the total amount Rila paid for the stock plus the // broker's commission and store the result in amountPaid.
  20. double amountPaid = stockPurchase + purchaseComm;
  21. // Calculate the amount Rila sold the stock for and store
  22. // the result in stockSale.
  23. double stockSale = NUM_SHARES * SELLING_PRICE;
  24. // Calculate the broker's commission on the sale and
  25. // store the result in sellingComm.
  26. double sellingComm = (NUM_SHARES * SELLING_PRICE) *BROKER_COMM_RATE;
  27. // Calculate the amount that Rila actually received after
  28. // selling the stock and paying his broker the sales
  29. // commission, and store the result in amountRecieved.
  30. double amountRecieved = stockSale - sellingComm;
  31. // Calculate the amount of profit or loss, and store the
  32. // result in profitOrLoss. If a profit was made, the amount
  33. // will be positive. If a loss was incurred, the amount
  34. // will be negative.
  35. double profitOrLoss = amountRecieved - amountPaid;
  36. // Display the results.
  37. System.out.println("Rila paid Rs." + stockPurchase +" for the stock.");
  38. System.out.println("Rila paid his broker a commission of Rs." + purchaseComm + " on the purchase.");
  39. System.out.println("So, Rila paid a total of Rs." + amountPaid + "\n");
  40. System.out.println("Rila sold the stock for Rs." + stockSale);
  41. System.out.println("Rila paid his broker a commission of Rs." + sellingComm + " on the sale.");
  42. System.out.println("So, Rila recieved a total of Rs." + amountRecieved + "\n");
  43. System.out.println("Rila's profit or loss: Rs." + profitOrLoss);
  44.  
  45. }
  46. }
  47.  
Success #stdin #stdout 0.14s 52568KB
stdin
Standard input is empty
stdout
Rila paid Rs.32870.0 for the stock.
Rila paid his broker a commission of Rs.657.4 on the purchase.
So, Rila paid a total of Rs.33527.4

Rila sold the stock for Rs.33920.0
Rila paid his broker a commission of Rs.678.4 on the sale.
So, Rila recieved a total of Rs.33241.6

Rila's profit or loss: Rs.-285.8000000000029