Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help with simple Shopping cart math

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with simple Shopping cart math

    Please, i need help. I am Java stupid and have to take the class for an electronics program. I have it written out and now i really need the items to add up when you input a quantity and hit the check out. Please help. Tell me where to input it and how to write the expression.


    Here is my code.
    package lab3;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    /**
     *
     * @author NICK WALKER
     */
     public class GuiFrame extends JFrame implements ActionListener {
     
     
     
     
        Container Cart = getContentPane();
        GridLayout Grid = new GridLayout(7,2);
     
     
        JCheckBox Buck = new JCheckBox("Buck $0.50");
        JTextField BuckT = new JTextField(); 
        JCheckBox A = new JCheckBox("A $1.00");
        JTextField AT = new JTextField();
        JCheckBox Roo = new JCheckBox("Roo $2.00");
        JTextField RooT = new JTextField();
        JCheckBox Bon = new JCheckBox("Bon $5.00");
        JTextField BonT = new JTextField();
        JCheckBox Zai = new JCheckBox("Zai $10.00");
        JTextField ZaiT = new JTextField();
        JButton checkout = new JButton("Checkout");
        JButton blank = new JButton(); 
        JLabel ordertotal = new JLabel("Order Total $");
        JTextField $due = new JTextField();
     
     
     
        int total =  0;
     
        double Bucks = .50;
        double As = 1.00;
        double Roos = 2.00;
        double Bons = 5.00;
        double Zais = 10.00;
     
     
     
     
     
     
       public GuiFrame(){
         this.setTitle("We Love Just One Movie.com shopping cart");
         this.setSize(400,350);
         this.setLocationRelativeTo(null);
     
         BuckT.setEditable(false);
         AT.setEditable(false);
         RooT.setEditable(false);
         BonT.setEditable(false);
         ZaiT.setEditable(false);
         blank.setEnabled(false);
         $due.setVisible(false);
     
         Cart.setLayout(Grid);
     
     
     
     
     
     
         addComponents();
          registerEventGenerators();
     
     
     
     
     }
     
     
     
     
     
        @Override
        public void actionPerformed(ActionEvent e) {
     
            if (e.getSource()==Buck){        
            BuckT.setEditable(true);
            }
     
            if (e.getSource()==A){           
                AT.setEditable(true);       
            }
     
            if (e.getSource()==Roo){
                RooT.setEditable(true);               
            }
     
            if (e.getSource()==Bon){
                BonT.setEditable(true);
            }
     
            if (e.getSource()==Zai){
                ZaiT.setEditable(true);           
            }
     
            if (e.getSource()==checkout){
                $due.setVisible(true);
            }
     
        }      
     
     
     
        private void addComponents() {
     
            Cart.add(Buck);        
            Cart.add(BuckT);
            Cart.add(A);
            Cart.add(AT);
            Cart.add(Roo);
            Cart.add(RooT);
            Cart.add(Bon);
            Cart.add(BonT);
            Cart.add(Zai);
            Cart.add(ZaiT);
            Cart.add(checkout);
            Cart.add(blank);       
            Cart.add(ordertotal);
            Cart.add($due);
     
     
     
     
        }
     
        private void registerEventGenerators() {        
            checkout.addActionListener(this);
            Buck.addActionListener(this);
            BuckT.addActionListener(this);
            A.addActionListener(this);
            AT.addActionListener(this);
            Roo.addActionListener(this);
            Roo.addActionListener(this);
            Bon.addActionListener(this);
            BonT.addActionListener(this);
            Zai.addActionListener(this);
            ZaiT.addActionListener(this);
     
     
     
        }
     
     
    }
    Last edited by helloworld922; November 5th, 2011 at 08:01 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Need help with simple Shopping cart math

    You may want to set the JFrame itself to visible first so it can actually show up.

    Also, you should tell it to set the default close operation to EXIT_ON_CLOSE or something. Otherwise it will continue to run even after the window is closed.

    Also, I'm thinking you probably don't want the actual due category at the bottom to be editable. That's supposed to be determined by the other text area values.

    I'm assuming you want to use integer values in the text fields, for quantities.

    If so,

    int value = Integer.parseInt(textField.getText());

    Also, I might add that JTextField is already visible by default so that set visible isn't doing much when you set

    $due to visible.

    Also, your code doesn't have a main method.
    Last edited by javapenguin; November 6th, 2011 at 09:34 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with simple Shopping cart math

    Thank you. Still a little confused where i use that integer value at. I also did set it to visible.

    Is this what you were talking about.

    package lab3;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    /**
    *
    * @author NICK WALKER
    */
    public class GuiFrame extends JFrame implements ActionListener {



    Container Cart = getContentPane();
    GridLayout Grid = new GridLayout(7,2);


    JCheckBox Buck = new JCheckBox("Buck $0.50");
    JTextField BuckT = new JTextField();
    JCheckBox A = new JCheckBox("A $1.00");
    JTextField AT = new JTextField();
    JCheckBox Roo = new JCheckBox("Roo $2.00");
    JTextField RooT = new JTextField();
    JCheckBox Bon = new JCheckBox("Bon $5.00");
    JTextField BonT = new JTextField();
    JCheckBox Zai = new JCheckBox("Zai $10.00");
    JTextField ZaiT = new JTextField();
    JButton checkout = new JButton("Checkout");
    JButton blank = new JButton();
    JLabel ordertotal = new JLabel("Order Total $");
    JTextField $due = new JTextField();



    public GuiFrame(){
    this.setTitle("We Love Just One Movie.com shopping cart");
    this.setSize(400,350);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    BuckT.setEditable(false);
    AT.setEditable(false);
    RooT.setEditable(false);
    BonT.setEditable(false);
    ZaiT.setEditable(false);
    blank.setEnabled(false);
    $due.setVisible(true);

    Cart.setLayout(Grid);






    addComponents();
    registerEventGenerators();




    }





    @Override
    public void actionPerformed(ActionEvent e) {

    if (e.getSource()==Buck){
    BuckT.setEditable(true);
    }

    if (e.getSource()==A){
    AT.setEditable(true);
    }

    if (e.getSource()==Roo){
    RooT.setEditable(true);
    }

    if (e.getSource()==Bon){
    BonT.setEditable(true);
    }

    if (e.getSource()==Zai){
    ZaiT.setEditable(true);
    }

    if (e.getSource()==checkout){
    $due.setVisible(true);
    }

    }



    private void addComponents() {

    Cart.add(Buck);
    Cart.add(BuckT);
    Cart.add(A);
    Cart.add(AT);
    Cart.add(Roo);
    Cart.add(RooT);
    Cart.add(Bon);
    Cart.add(BonT);
    Cart.add(Zai);
    Cart.add(ZaiT);
    Cart.add(checkout);
    Cart.add(blank);
    Cart.add(ordertotal);
    Cart.add($due);




    }

    private void registerEventGenerators() {
    checkout.addActionListener(this);
    Buck.addActionListener(this);
    BuckT.addActionListener(this);
    A.addActionListener(this);
    AT.addActionListener(this);
    Roo.addActionListener(this);
    Roo.addActionListener(this);
    Bon.addActionListener(this);
    BonT.addActionListener(this);
    Zai.addActionListener(this);
    ZaiT.addActionListener(this);



    }


    }
    Last edited by I hate Java!; November 6th, 2011 at 08:34 PM.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Need help with simple Shopping cart math

    Sorry, I was wrong about the ActionListeners on the text field.

    You probably don't need them.

    All you'd actually need is a

    int value = Integer.parseInt(BuckT.getText());

    int value2 = Integer.parseInt(ZaiT.getText());

    and so on to get the values from your text fields.

    All you'd need actually was to do this inside the ActionListener of the Checkout button.

    I'm assuming you'd want to multiply those values gotten times those values like 5.00, etc, and then return some kind of result.

    Get the int values for each using the Integer.parseInt() like shown above and multiply each of those values with their corresponding double value, which is what I think you wanted.

    Then to add them together and get

    double d = [that value];

    Then set that final text field text like this

    thatTextField.setText((Double(d)).toString());

Similar Threads

  1. Shopping Program Willing to pay
    By ulikecourtney in forum Paid Java Projects
    Replies: 1
    Last Post: September 27th, 2011, 12:59 PM
  2. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  3. Validating fields in a js shopping cart
    By rockc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 02:37 PM
  4. Question on my math
    By SwEeTAcTioN in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 25th, 2009, 05:42 PM
  5. on-line shopping
    By sriraj.kundan in forum Web Frameworks
    Replies: 13
    Last Post: August 1st, 2009, 02:03 PM

Tags for this Thread