Results 1 to 6 of 6

Thread: fix my app

  1. #1
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default fix my app

    Help please!
    What I want this code to do is: when you select the make sales from the sales menu, if you enter an isbn number or a book tiltle in the text boxes, and hit search, the code would check if the book is available and display the book informaion in the text area. Then if you press the buy button, the the calculated price will be dispalyed in the text box labeled "your total is" and also decrements the quantity of the books. The BookcatalogFile is not also creating the file that holds the books. I know I am asking a lot, any insight would help!



    PHP Code:
    [CODE]import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.text.JTextComponent;
    import java.text.*;
    
    public class PracticeApp {
    
        public static void main(String[] args)
        {
              //JFrame SalesF = new JFrame(); //SalesFrame();
            SalesFrame SalesF = new SalesFrame();
            SalesF.setVisible(true);
        }
    }
    class SalesFrame extends JFrame
    {
        public SalesFrame()
        {
            setTitle("Sales");
            setPreferredSize(new Dimension(800, 400));
            setResizable(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //this.add(new SalesPanel());
            this.pack();
            centerWindow(this);
    
             JMenuBar menuBar = new JMenuBar();
    
            // Add the menubar to the frame
            setJMenuBar(menuBar);
    
            //JMenuBar menuBar = new JMenuBar();
            JMenu saleMenu = new JMenu("Sales");
            JMenu returnMenu = new JMenu("Returns");
            JMenu inventoryMenu = new JMenu("Inventory");
            JMenu printMenu = new JMenu("Print");
            menuBar.add(saleMenu);
            menuBar.add(returnMenu);
            menuBar.add(inventoryMenu);
            menuBar.add(printMenu);
            // Create and add simple menu item to one of the drop down menu
            JMenuItem saleAction = new JMenuItem("Make sales");
            JMenuItem returnAction = new JMenuItem("Process Returns");
            JMenuItem addNewAction = new JMenuItem("Add New Book");
            JMenuItem availabilityAction = new JMenuItem("Availability");
            JMenuItem printDailyAction = new JMenuItem("Print Daily Report");
            JMenuItem printCatalogAction = new JMenuItem("Print Catalog");
    
            saleMenu.add(saleAction);
            returnMenu.add(returnAction);
            inventoryMenu.add(addNewAction);
            inventoryMenu.add(availabilityAction);
            printMenu.add(printDailyAction);
            printMenu.add(printCatalogAction);
        
            saleAction.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent arg0)
                {
                    SalesPanel sPanel = new SalesPanel();
                    sPanel.titleLabel.setVisible(false);
                    SalesFrame SalesF = new SalesFrame();
                    add(new SalesPanel());
               }
            });
    
             returnAction.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent arg0)
                {
    
                    SalesFrame SalesF = new SalesFrame();
                    add(new SalesPanel());
                    SalesF.setVisible(true);
                    setTitle("Return window");
                    setPreferredSize(new Dimension(700, 350));
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
            });
      }
    
        private void centerWindow(Window w)
        {
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension d = tk.getScreenSize();
            setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
        }
    }
    
    class SalesPanel extends JPanel
    {
        Book newBook = null;
        JPanel sPanel = new JPanel();
        public JLabel isbnLabel, titleLabel, prompt3Label, totalLabel;
        public JTextField isbnTextField, titleTextField, totalTextField; // prompt3List;
        public JButton searchButton, buyButton, cancelButton;
        public JTextArea displayTextArea;
    
    
        public SalesPanel()
        {
            // add panels
            setLayout(new GridBagLayout());
    
            isbnLabel = new JLabel("Enter ISBN");
            add(isbnLabel, getConstraints(0,0,1,1, GridBagConstraints.EAST));
    
            isbnTextField = new JTextField(30);
            isbnTextField.setEditable(true);
            isbnTextField.setFocusable(true);
            add(isbnTextField, getConstraints(1,0,1,1, GridBagConstraints.WEST));
    
            titleLabel = new JLabel("Enter Title");
            add(titleLabel, getConstraints(0,1,1,1, GridBagConstraints.EAST));
    
            titleTextField = new JTextField(30);
            titleTextField.setEditable(true);
            titleTextField.setFocusable(true);
            add(titleTextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));
    
    
            searchButton = new JButton("SEARCH");
            searchButton.addActionListener(//this);
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        Book b;
                        double TAXRATE = 0.5;
                        String str = isbnTextField.getText();
                        String str1 = titleTextField.getText();
                        if ((str.length() == 0)&& (str1.length()== 0))
                            {
                                JOptionPane.showMessageDialog(null, "Either BookTitle "
                                        + "or ISBN code is required",
                                        "Invalid Entry", JOptionPane.ERROR_MESSAGE);
                                isbnTextField.requestFocusInWindow();
                            }
                        else if((str.length() != 0))
                        {
                            //get the book with the ISBN no. in the isbnTextBox from
                            //the BookCcatalogFile
    //                        b.getBook(str);
    
                        }
                        else
                        {
                            //get the book with the ISBN no. in the titleTextBox from
                            //the BookCcatalogFile
    //                        b.getBookByTitle(str1);
    
                        }
                        //dispaly search result in textArea
          //              displayTextArea.setText(readLine from "catalog.txt"")
                   }
            });
            add(searchButton, getConstraints(2,1,1,1, GridBagConstraints.EAST));
    
            displayTextArea = new JTextArea(5, 30);
            displayTextArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(displayTextArea);
            
            add(displayTextArea, getConstraints(1,2,1,1, GridBagConstraints.EAST));
    
       
            buyButton = new JButton("BUY");
            buyButton.addActionListener(//this);
                new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                       Book b;
                        double TAXRATE = 0.5;
                        String str = isbnTextField.getText();
                        String str1 = titleTextField.getText();
                        if ((str.length() == 0)&& (str1.length()== 0))
                            {
                                JOptionPane.showMessageDialog(null, "Either BookTitle "
                                        + "or ISBN code is required",
                                        "Invalid Entry", JOptionPane.ERROR_MESSAGE);
                                isbnTextField.requestFocusInWindow();
                            }
                        else if((str.length() != 0))
                        {
                            //get the book with the ISBN no. in the isbnTextBox from
                            //the BookCcatalogFile
    //                        b.getBook(str);
    
                        }
                        else
                        {
                            //get the book with the ISBN no. in the titleTextBox from
                            //the BookCcatalogFile
    //                        b.getBookByTitle(str1);
    
                        }
                        //dispaly search result in textArea
          //              displayTextArea.setText(readLine from "catalog.txt"")
    
                        //decrement the quantity by 1
    
    //                        int a = b.getQuantity();
    //                        a = a-1;
    //                       b.setQuantity();
    //                        double pr = b.getPrice()+ (1+ TAXRATE);
    
    //                        totalTextField.setText(NumberFormat.getCurrencyInstance().format(pr));
    
    
                            //add book info to sold books list
                    }
                });
            add(buyButton, getConstraints(2,2,1,1, GridBagConstraints.EAST));
    
    
            totalLabel = new JLabel("Your Total is:");
            add(totalLabel, getConstraints(0,3,1,1, GridBagConstraints.EAST));
            
            totalTextField = new JTextField(8);
            totalTextField.setEditable(false);
            totalTextField.setFocusable(false);
            add(totalTextField, getConstraints(1,3,1,1, GridBagConstraints.WEST));
    
        }
        private GridBagConstraints getConstraints(int gridx, int gridy,
        int gridwidth, int gridheight, int anchor)
        {
            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(5, 5, 5, 5);
            c.ipadx = 0;
            c.ipady = 0;
            c.gridx = gridx;
            c.gridy = gridy;
            c.gridwidth = gridwidth;
            c.gridheight = gridheight;
            c.anchor = anchor;
            return c;
        }
    }
    
    
    
    
    
    import java.text.NumberFormat;
    import java.text.ParseException;
    public class Book
    
    {
    	private String isbn;
    	private String title;
    	private double price;
            private int quantity;
    
    	public Book()
    	{
    		this("", "", 0, 0);
    	}
    
    	public Book(String code, String description, double price, int quantity)
    	{
    		this.isbn = isbn;
    		this.title = title;
    		this.price = price;
                    this.quantity = quantity;
    	}
    
    	public void setISBN(String isbn)
    	{
    		this.isbn = isbn;
    	}
    
    	public String getISBN(){
    		return isbn;
    	}
    
    	public void setTitle(String title)
    	{
    		this.title = title;
    	}
    
    	public String getTitle()
    	{
    		return title;
    	}
    
    	public void setPrice(double price)
    	{
    		this.price = price;
    	}
    
    	public double getPrice()
    	{
    		return price;
    	}
            public void setQuantity()
            {
                this.quantity = quantity;
            }
            public int getQuantity()
            {
                return quantity;
            }
    
    	public String getFormattedPrice()
    	{
    		NumberFormat number = NumberFormat.getNumberInstance();
    		number.setMinimumFractionDigits(2);
    		number.setMaximumFractionDigits(2);
    		return number.format(price);
    	}
    
    	public void setFormattedPrice(String formattedPrice)
    	{
    		NumberFormat number = NumberFormat.getNumberInstance();
    		Number n;
    		try
    		{
    			n = number.parse(formattedPrice);
    		}
    		catch (ParseException e)
    		{
    			n = 0;
    		}
    		this.price = n.doubleValue();
    	}
    
        public boolean equals(Object object)
        {
            if (object instanceof Book)
            {
                Book anotherBook = (Book) object;
                if
                (
                    isbn.equals(anotherBook.getISBN()) &&
                    title.equals(anotherBook.getTitle()) &&
                    price == anotherBook.getPrice()
                )
                    return true;
            }
            return false;
        }
    
    	public String toString()
    	{
    		return title;
    	}
    }
    
    
    
    import java.text.NumberFormat;
    import java.text.ParseException;
    public class Book
    
    {
    	private String isbn;
    	private String title;
    	private double price;
            private int quantity;
    
    	public Book()
    	{
    		this("", "", 0, 0);
    	}
    
    	public Book(String code, String description, double price, int quantity)
    	{
    		this.isbn = isbn;
    		this.title = title;
    		this.price = price;
                    this.quantity = quantity;
    	}
    
    	public void setISBN(String isbn)
    	{
    		this.isbn = isbn;
    	}
    
    	public String getISBN(){
    		return isbn;
    	}
    
    	public void setTitle(String title)
    	{
    		this.title = title;
    	}
    
    	public String getTitle()
    	{
    		return title;
    	}
    
    	public void setPrice(double price)
    	{
    		this.price = price;
    	}
    
    	public double getPrice()
    	{
    		return price;
    	}
            public void setQuantity()
            {
                this.quantity = quantity;
            }
            public int getQuantity()
            {
                return quantity;
            }
    
    	public String getFormattedPrice()
    	{
    		NumberFormat number = NumberFormat.getNumberInstance();
    		number.setMinimumFractionDigits(2);
    		number.setMaximumFractionDigits(2);
    		return number.format(price);
    	}
    
    	public void setFormattedPrice(String formattedPrice)
    	{
    		NumberFormat number = NumberFormat.getNumberInstance();
    		Number n;
    		try
    		{
    			n = number.parse(formattedPrice);
    		}
    		catch (ParseException e)
    		{
    			n = 0;
    		}
    		this.price = n.doubleValue();
    	}
    
        public boolean equals(Object object)
        {
            if (object instanceof Book)
            {
                Book anotherBook = (Book) object;
                if
                (
                    isbn.equals(anotherBook.getISBN()) &&
                    title.equals(anotherBook.getTitle()) &&
                    price == anotherBook.getPrice()
                )
                    return true;
            }
            return false;
        }
    
    	public String toString()
    	{
    		return title;
    	}
    }[/CODE]
    Java Code:
    [/PHP]
    Last edited by abetemari; 03-26-2011 at 01:45 PM. Reason: code wrap

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    if you want help you should attempt to do it yourself first, then when you get stuck tell us the compile-error or some other problem and people will help you to fix it. also, no one is going to read your code without indentation, so you code wrap your code in [code] /code tags to preserve any indentation you have already. if you want more help, try to be precise and only copy here the code that is relevent to the question.

    here is some pseudo-code to help you out:
    Java Code:
    //look through every book we have
    for (Book b:ListOfBooks) {
        String isbn = myTextBox.getText()
        //if we match the ISBN numbers, where 'isbn' is the user-input number
        if (b.getISBN() == isbn) {
            //we have a match
            myLabel1.setText(b.getTitle())
            myLabel2.setText(b.getPrice())
            myLabel3.setText(b.getStockCount())
            //end loop
            break;
        }
    }

  3. #3
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Thank you Ozzyman. The reason I included everything is so people can see the entire thing but I did not know about the wrapping the code. I agree keeping the indentation would have made it more readable. the main problem I have in this code is reading from a file and displaying it on the appropriate textfield.

  4. #4
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Here, I actually wrapped the code.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    so this is your main problem i gather:
    Java Code:
        //              displayTextArea.setText(readLine from "catalog.txt"")

    but first, i want to know is it absolutely necessary that you must read the information from a text file? what does that text file contain and can't it be stored in java arrays/objects rather than a text file?

  6. #6
    abetemari is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    I wanted to use a text file to store the ArrayList's information for the ease of printing a report which I want to add a code for later on. However, I want it to start doing something that I can see and if you can, bypass the file and make it work with the ArrayList alone.
    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •