mercredi 30 mars 2016

If statements not working properly

Ok so what I wasn't to do is loop the program using a while loop. The user will select stock e.g. Sony enter whether he/she wants to buy or sell shares and then enter the quantity. They will be redirected back to the button until he/she clicks on print total to view the cost of all shares. What I want to do is set an overall price using if statements, but if the user does not go over one of the if statements e.g. does not buy Apple then an nullpointerexception is thrown, help me, how do I fix my if statements. The method where the If statements go is called setOverallTotal()

public class RunProject extends JPanel{

JTable jt;
int option;
private double overall=0;
private double fullApple;
private double fullSony;
private double fullFacebook;
private double fullNokia;
Account account = new Account("","");
Apple apple = new Apple ("",0,"");
Sony sony = new Sony ("",0,"");
Facebook facebook = new Facebook ("",0,"");
Nokia nokia = new Nokia ("",0,"");
NumberFormat money = NumberFormat.getCurrencyInstance();

public RunProject (){

    apple.setPrice();
    sony.setPrice();
    facebook.setPrice();
    nokia.setPrice();

    String [] columns = {"Stock Name","Stock Founded", "Stock Status", "Stock Price(atm)" };
    String [][]data = {{apple.getStockName(),"Started April 1st, 1976",apple.getStockStatus(),String.valueOf(money.format(apple.getPrice()))+" from £74.75"},
        {sony.getStockName(), "Started 7th May, 1946", sony.getStockStatus(),String.valueOf(money.format(sony.getPrice()))+" from £18.17"},
        {facebook.getStockName(), "Started 4th February, 2004", facebook.getStockStatus(),String.valueOf(money.format(facebook.getPrice()))+" from £80.60"},
    {nokia.getStockName(),"Started 1st January, 1865", nokia.getStockStatus(),String.valueOf(money.format(nokia.getPrice()))+" from £4.18"}};

    jt = new JTable(data,columns);
    jt.setPreferredScrollableViewportSize(new Dimension(550,80));
    jt.setFillsViewportHeight(true);

    JFrame jf = new JFrame();
    jf.setTitle("Alireza Shahali's Stock Market");
    jf.setSize(900, 200);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.add(jt);

    account.setFirstName();
    account.setSurname();
    account.setAccountNumber();
    JOptionPane.showMessageDialog(null,"YOUR ACCOUNT DETAILS\n\nFirst Name: "+account.getFirstName()+"\nSurname: "+account.getSurname()+
    "\nAccount Number: "+account.getAccountNumber());

    do{
        ArrayList<String> buttonChoice = new ArrayList<String>();
        buttonChoice.add("Apple");
        buttonChoice.add("Sony");
        buttonChoice.add("Facebook");
        buttonChoice.add("Nokia");
        buttonChoice.add("Print Total");
        buttonChoice.add("Exit");
        // making an arraylist of strings called buttonChoice

        Object [] options = buttonChoice.toArray();
        // turning the arraylist to standard array and saving it in Object array called options

        option = JOptionPane.showOptionDialog(null, "Select a Stock. Click on Print Total to display cost of selected Shares.", "Alireza's Stock Market",
        JOptionPane.PLAIN_MESSAGE, JOptionPane.QUESTION_MESSAGE, null, options,buttonChoice.get(0));

        setOverallTotal();

        switch(option){
            case 0:
                apple.setBuyingOrSelling();
                apple.shares();
                break;

            case 1:
                sony.setBuyingOrSelling();
                sony.shares();
                break;

            case 2:
                facebook.setBuyingOrSelling();
                facebook.shares();
                break;

            case 3:
                nokia.setBuyingOrSelling();
                nokia.shares();
                break;

            case 4:
                fullApple = apple.getPrice() * apple.getShares();
                fullSony = sony.getPrice() * sony.getShares();
                fullNokia = nokia.getPrice() * nokia.getShares();
                fullFacebook = facebook.getPrice() * facebook.getShares();

                JOptionPane.showMessageDialog(null, apple.getStockName()+"\nStock ID: "+apple.getStockID()+"\n"+apple.getStockDescription()+
                "\n\nYou would like to: "+apple.getBuyingOrSelling() + "\n" +apple.getShares()+ " shares.\nStock is currently: "+apple.getStockStatus()
                +"\nCost of each Share: "+money.format(apple.getPrice())+"\nTotal: " + money.format(fullApple)+"\n-------------------------------------------------------"
                + "\n"+sony.getStockName()+"\nStock ID: "+sony.getStockID()+"\n"+sony.getStockDescription()+
                "\n\nYou would like to: "+sony.getBuyingOrSelling() + "\n" +sony.getShares()+ " shares.\nStock is currently: "+sony.getStockStatus()
                +"\nCost of each Share: "+money.format(sony.getPrice())+"\nTotal: " + money.format(fullSony)+"\n-------------------------------------------------------"
                + "\n"+facebook.getStockName()+"\nStock ID: "+facebook.getStockID()+"\n"+facebook.getStockDescription()+
                "\n\nYou would like to: "+facebook.getBuyingOrSelling() + "\n" +facebook.getShares()+ " shares.\nStock is currently: "+facebook.getStockStatus()
                +"\nCost of each Share: "+money.format(facebook.getPrice())+"\nTotal: " + money.format(fullFacebook)+"\n-------------------------------------------------------"
                + "\n"+nokia.getStockName()+"\nStock ID: "+nokia.getStockID()+"\n"+nokia.getStockDescription()+
                "\n\nYou would like to: "+nokia.getBuyingOrSelling() + "\n" +nokia.getShares()+ " shares.\nStock is currently: "+nokia.getStockStatus()
                +"\nCost of each Share: "+money.format(nokia.getPrice())+"\nTotal: " + money.format(fullNokia)+"\n-------------------------------------------------------"
                + "\nAccount Holder: "+account.getFirstName()+" "+account.getSurname()+ "//Account Number: " + account.getAccountNumber()
                +" //Account changes: "+money.format(getOverallTotal()));
                break;

            case 5:
                System.exit(0);
        }
    }while(option!=5);

}

public void setOverallTotal(){
    if (apple.getBuyingOrSelling().equals("BUY")){
        overall -= fullApple;
    }
    else if(apple.getBuyingOrSelling().equals("SELL")){          
        overall+= fullApple;
    } 
}

public double getOverallTotal (){
    return overall;
}

Aucun commentaire:

Enregistrer un commentaire