Stock transaction

The problem

Last month Sally purchased some stock in ABC CO, Inc. Here are the details of the purchase:

  • the number of shares that Joe purchased was 1,000
  • When Joe purchased stock, he paid $32.87 per share
  • Joe paid his stockbroker a commission that amounted to 2% of the amount he paid for the stock.

Two weeks later Joe sold the stock. Here are the details of the sale:

  • the number of shares that joe sold was 1,000
  • He sold the stock for $33.92 per share.
  • He paid his stockbroker another commission that amounted to 2% of the amount he received for the stock.

Write a program that displays the following information:

  • the amount of money Joe paid for the stock.
  • the amount of commission Joe paid his broker when he bought the stock.
  • the amount that Joe sold the stock for.
  • the amount of commission Joe paid his broker when he sold the stock.
  • display the amount of profit that Joe made after selling his stock and paying the two commissions to his broker. (if the amount of profit that your program displays is a negative number, then Joe lost money on the transaction.)

Breaking it down

Create constants

static final int NUM_SHARES = 1000; // Number of shares
static final double PURCHASE_PRICE = 32.87; // Purchase price per share
static final double SELLING_PRICE = 33.92; // Selling price per share
static final double BROKER_COMM_RATE = 0.02; // Broker commission rate

Define calculation methods

/**
 * Method should calculate the purchase price of stock not including
 * brokers fees.
 *
 * @param numberOfShares
 * @param purchasePrice
 * @return stock purchase price
 */
static double stockPurchasePrice (double numberOfShares, double purchasePrice) {
    return numberOfShares * purchasePrice;
}

/**
 * Method should calculate the brokers commission
 * @param stockPurchase
 * @param brokersCommissionRate
 * @return brokers commission
 */
static double brokersCommission (double stockPurchase, double brokersCommissionRate) {
    return stockPurchase * brokersCommissionRate;
}

/**
 * Method should calculate the total amount paid which is
 * sum of stock purchase price and the commission of the sale.
 *
 * @param stockPurchase
 * @param purchaseComm
 * @return total amount paid
 */
static double totalAmountPaid (double stockPurchase, double purchaseComm) {
    return stockPurchase + purchaseComm;
}

/**
 * Method should calculate the amount of stock sale
 *
 * @param numberOfShares
 * @param sellingPrice
 * @return stock sale amount
 */
static double stockSale (double numberOfShares, double sellingPrice) {
    return numberOfShares * sellingPrice;
}

/**
 * Method should calculate the commission on a sale
 *
 * @param numberOfShares
 * @param sellingPrice
 * @param brokersCommissionRate
 * @return commission from the sale
 */
static double sellingCommission (double numberOfShares, double sellingPrice, double brokersCommissionRate) {
    return (numberOfShares * sellingPrice) * brokersCommissionRate;
}

/**
 * Method should calculate the total amount received from the sale.  This amount
 * is the total sale of the stock - the selling commission.
 *
 * @param stockSale
 * @param sellingCommission
 * @return total amount received from sale
 */
static double totalAmountRecieved (double stockSale, double sellingCommission) {
    return stockSale - sellingCommission;
}

/**
 * Method should calculate a profit or loss.  If a profit was made, the amount
 * will be positive. If a loss was incurred, the amount will be negative.
 *
 * @param amountRecieved
 * @param amountPaid
 * @return profit OR loss
 */
static double profitOrLoss (double amountRecieved, double amountPaid) {
    return amountRecieved - amountPaid;
}

Set variables by calling calculation methods

double stockPurchase = stockPurchasePrice (NUM_SHARES, PURCHASE_PRICE);

double purchaseComm = brokersCommission (stockPurchase, BROKER_COMM_RATE) ;

double amountPaid = totalAmountPaid (stockPurchase, purchaseComm);

double stockSale = stockSale(NUM_SHARES, SELLING_PRICE);

double sellingComm = sellingCommission(NUM_SHARES, SELLING_PRICE, BROKER_COMM_RATE);

double amountRecieved = totalAmountRecieved (stockSale, sellingComm);

double profitOrLoss = profitOrLoss (amountRecieved, amountPaid);

Display results

System.out.println("You paid $" + stockPurchase + " for the stock.");
System.out.println("You paid his broker a commission of $"
        + purchaseComm + " on the purchase.");
System.out.println("So, You paid a total of $" + amountPaid + "\n");

System.out.println("You sold the stock for $" + stockSale);
System.out.println("You paid his broker a commission of $"
        + sellingComm + " on the sale.");
System.out.println("So, You recieved a total of $" + amountRecieved
        + "\n");
System.out.println("You's profit or loss: $" + profitOrLoss);

Output

You paid $32870.0 for the stock.
You paid his broker a commission of $657.4 on the purchase.
So, You paid a total of $33527.4

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

You's profit or loss: $-285.8000000000029

Unit tests

@Test
public void test_totalAmountRecieved () {
    assertEquals(7.5, StockTransaction.totalAmountRecieved(10d, 2.50), 0);
}

@Test
public void test_totalAmountPaid () {
    assertEquals(12.5, StockTransaction.totalAmountPaid(10d, 2.50), 0);
}

@Test
public void test_stockSale () {
    assertEquals(25, StockTransaction.stockSale(10d, 2.50), 0);
}

@Test
public void test_stockPurcasePrice () {
    assertEquals(25, StockTransaction.stockPurchasePrice(10d, 2.50), 0);
}

@Test
public void test_profitORLoss_positive () {
    assertEquals(100, StockTransaction.profitOrLoss(200, 100), 0);
}

@Test
public void test_profitORLoss_negative () {
    assertEquals(-100, StockTransaction.profitOrLoss(100, 200), 0);
}


@Test
public void test_selling_commission () {
    assertEquals(150, StockTransaction.sellingCommission(150, 50, .02), 0);
}

@Test
public void test_brokers_commission () {
    assertEquals(5000, StockTransaction.brokersCommission(100, 50), 0);
}

Level Up

  • Format ouput using number formatter
  • Modify the program to prompt the user to enter in number of shares, purchase price, selling price and broker rate
  • have fun by adding a particular message for profit. For instnace, if someone lost more than 15% in the sale of the stock, then display a message 'Rough day - want to come over for a beer?'. Vise versa, in the event they had a killer day > 20 %, display a message 'Where we going out for dinner!'
  • Include the % of gain or loss percentage similar message to profit, if a user is using a broker that has a commission rate of > X% 'hey you have heard of tdameritrade, right?'