First a little history, I am relatively new to Java. I had a class a few months back but the professor was absolutely horrible at actually teaching and it really turned me off of the subject. Recently I've been looking back through my textbook and decided to get back into it. So I know a bit but not nearly as much as I should after taking an intro class.
The problem I was working on is a simple stock transaction dealing with buying, selling, and commissions. I am being asked to:
1. display the amount paid for the stock
2. the commission paid after buying the stock
3. the amount the stock sold for
4. the commission paid after selling the stock
5. Display the net profit and both transactions and after commission has been paid
This is one of the first small programs I've written so far:
My question is simply is there an easier or more convenient way I could have done this? Or is this good enough for now and worry about convenience when I know a bit more?PHP Code:public class StockTransactionProgram {
public static void main(String[] args)
{
String input;
int stocksBought;
int stocksSold;
double buyPrice;
double sellPrice;
double comPercent;
double comTotal;
double paid;
double totalPaid;
double sold;
double totalSold;
double netTotal;
DecimalFormat formatter = new DecimalFormat("#0.00");
input = JOptionPane.showInputDialog("How many stocks have been purchased?");
stocksBought = Integer.parseInt(input);
input = JOptionPane.showInputDialog("What price were the stocks bought at?");
buyPrice = Double.parseDouble(input);
paid = stocksBought * buyPrice;
input = JOptionPane.showInputDialog("What is the commission percentage?");
comPercent = Double.parseDouble(input);
comTotal = paid * comPercent;
totalPaid = comTotal + paid;
System.out.println("You paid " + formatter.format(comTotal) + " as a commission.");
System.out.println("And you paid a total of " + formatter.format(totalPaid) + " for " + stocksBought + " shares of stocks.");
input = JOptionPane.showInputDialog("How many stocks have been sold?");
stocksSold = Integer.parseInt(input);
input = JOptionPane.showInputDialog("What price were the stocks sold at?");
sellPrice = Double.parseDouble(input);
sold = stocksSold * sellPrice;
input = JOptionPane.showInputDialog("What is the commission percentage?");
comPercent = Double.parseDouble(input);
comTotal = sold * comPercent;
totalSold = sold - comTotal;
System.out.println("You paid " + formatter.format(comTotal) + " as a commission.");
System.out.println("You made a total of " + formatter.format(totalSold) + " selling " + stocksSold + " shares of stocks.");
netTotal = totalSold - totalPaid;
System.out.println("Your net total is " + formatter.format(netTotal));
}
}
Any tips, tricks, or hints would be greatly appreciated.
PS. I didn't know if there was an easier way to post my code. This seemed to work fine so I used it. If there is let me know :)
