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 12 of 12

Thread: Update Stock Code

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Update Stock Code

    I'm trying to figure out how to update stock from entering in a number in a text field and then either clicking the add stock or delete stock button to update the stock.



    //These are imported classes, within the code you will see codes with the same name
    //this is because I am refering to classes by name so I dont have to keep typing
    //import javax.swing. 
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import java.awt.TextArea;
    import java.awt.event.ActionEvent;
    import java.text.DecimalFormat;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.*;
     
     
     
    public class UpdateStock extends JFrame implements ActionListener {
        //This code is creating text fields for the user to enter data, buttons for the user to click on
        //and a text area where text is displayed. The decimal format code sets the format for the price
        //once it's displayed. 
        JTextField stockNo = new JTextField(7);
        JTextField newItem = new JTextField(7);
        JTextField deleteItem = new JTextField(7);
        JLabel numOneLabel = new JLabel("Add Stock:");
        JLabel numTwoLabel = new JLabel("Enter Stock Number:");
        JLabel numthreeLabel = new JLabel("Delete Stock:");
        TextArea information = new TextArea(6, 70);
        JButton check = new JButton("Add Stock");
        JButton delete = new JButton("Delete Stock");
        JButton update = new JButton("Check Stock");
        DecimalFormat pounds = new DecimalFormat("£#,##0.00");
        JPanel jp = new JPanel();
        JLabel jl = new JLabel();
        ImageIcon icon3 = new ImageIcon("plus.png");
        ImageIcon icon2 = new ImageIcon("minus.png");
     
        public static void main(String[] args) {
     
            UpdateStock us = new UpdateStock();
        }
        String key;
        int extra;
        public int getQuantity() { return quantity; }
        public int quantity;
     
     
        public UpdateStock() {
            //This code allows us to create the size, title and settings for the pop up menu.
            //This is where you can add the buttons and text fields/areas, you can tell the box
            //where it will be popping up. The code listens for if the buttons are clicked 
            //then activates a code. 
            setLayout(new BorderLayout());
            setBounds(100, 100, 650, 400);
            setTitle("Update Stock");
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            JPanel top = new JPanel();
            //top.add(new JLabel("Enter Stock Number:"));
            top.add(numTwoLabel);
            top.add(stockNo);
            top.setBackground(Color.CYAN);
            add(top);
            top.add(update);
            check.addActionListener(this);
            update.addActionListener(this);
            add("North", top);
            JPanel middle = new JPanel();
            middle.add(numOneLabel);
            middle.add(newItem);
            middle.add(check);
            middle.add(numthreeLabel);       
            middle.add(deleteItem);
            middle.add(delete);
            middle.add(information);
            middle.add(jp);
            middle.add(new JLabel("Enter in a number from 1 - 6 to Update the Stock"));
            middle.setBackground(Color.WHITE);
            add("Center", middle);
            check.setIcon(icon3);
            delete.setIcon(icon2);
    //This code is saying if I want box to be rezideable and visible to the user.
            setResizable(false);
            setVisible(true);
        }
    //This code is performing an action for when text is entered into the text field
    //if the text in the field is unrecognisable then the code will display an error message
    //and if the text is found relateable then it will display the price in pounds format
    //and how many there are in stock.     
        public void actionPerformed(ActionEvent e) {
            if (e.getSource()== update) {
            String key = stockNo.getText();
            String name = StockData.getName(key);
            if (name == null) {
                information.setText("No such item in stock");
            }
            else {
                information.setText(name);
                information.append("\nPrice: " + pounds.format(StockData.getPrice(key)));
                information.append("\nNumber in stock: " + StockData.getQuantity(key));
                information.append("\nDescription: " + StockData.Description(key));
     
     
            }
     
     
            }
        } public void update(String key, int extra, ActionEvent e) {
      //  if (e.getSource()== check) {
        //JOptionPane.showMessageDialog (null, "This is a message box");
       // StockData.Item item = stock.get(key);
       // if (item != null) item.quantity += extra;
        }
        }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Update Stock Code

    And what's the problem? What does that code do? Does it compile? Does it run? Does it give you some kind of error?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Update Stock Code

    Quote Originally Posted by KevinWorkman View Post
    And what's the problem? What does that code do? Does it compile? Does it run? Does it give you some kind of error?
    The code runs a table which contains text fields and buttons. The problem is I want to update the quantity of an item by entering in a number to the text field and when i click either the add or delete button it will take the number I entered in the text field and take it away (or add) from the quantity. Each item has an item number so I will first enter the item number into one of the fields and then the number I want to update the quantity will be in another field. That's how the code will know where to update the number to.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Update Stock Code

    Quote Originally Posted by JaytheProgrammer View Post
    The code runs a table which contains text fields and buttons. The problem is I want to update the quantity of an item by entering in a number to the text field and when i click either the add or delete button it will take the number I entered in the text field and take it away (or add) from the quantity. Each item has an item number so I will first enter the item number into one of the fields and then the number I want to update the quantity will be in another field. That's how the code will know where to update the number to.
    Okay, and which part of this is giving you trouble?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Update Stock Code

    Quote Originally Posted by KevinWorkman View Post
    Okay, and which part of this is giving you trouble?
    I don't know where to start to get the "Add Stock" & "Delete Stock" button to work. I need to make sure the add stock button adds the number that was put into the text field and added to the quantity that is already there and same for the Delete Stock button.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Update Stock Code

    get the "Add Stock" & "Delete Stock" button to work.
    Is the action listener method called when the button is pressed? Add a println() statement to print out a message to show if the listener method is called.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Update Stock Code

    Quote Originally Posted by Norm View Post
    Is the action listener method called when the button is pressed? Add a println() statement to print out a message to show if the listener method is called.
    The action listener is called

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Update Stock Code

    That's good. What does the listener need to do now? Can you explain what problems you are having here?
    Where is the data it needs? How should it get that data and what should it do with the results?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Update Stock Code

    Quote Originally Posted by Norm View Post
    That's good. What does the listener need to do now? Can you explain what problems you are having here?
    Where is the data it needs? How should it get that data and what should it do with the results?
    Picture1.jpg
    When I enter in a number the item information comes up (when I click "Check Stock") and you can see the quantity but now when i enter the number into those text fields beside Add Stock and Delete Stock. I want to either add stock to whats already there or delete. The image should make things more clearer.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Update Stock Code

    I want to either add stock to whats already there
    Seems there are two steps. Which step are you having problems with?
    Get the number
    and add it to whats already there
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Update Stock Code

    Quote Originally Posted by Norm View Post
    Get the number and add it to whats already there
    YESS exactly this. I don't know how I can take the number that is in the text field and add it to the current quantity of the item.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Update Stock Code

    how I can take the number that is in the text field
    Read the API doc for the textfield to see how to get its contents.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with Stock Profit Program
    By pherigo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 30th, 2013, 09:55 AM
  2. Need my code to update a variable
    By Olympaphibian89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 13th, 2012, 02:18 PM
  3. Need help with calculating stock code.
    By joregorn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 22nd, 2012, 07:06 PM
  4. stock charts...
    By theChameleon in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 05:11 AM
  5. Java stock project help
    By lotus in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 12th, 2009, 04:16 AM