sorry I think am too new to comprehend this coding ones

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babs115
    New Member
    • Jun 2015
    • 18

    sorry I think am too new to comprehend this coding ones

    [After moving the whole code from line 51 - 79 it is now showing the error cannot find symbol variable productId]

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Bookshop extends JFrame implements ActionListener
    {
     
     JTextField productIdText;
     JTextField productNameText;
     JTextField productCostText;
     JTextField productyearOfPublicationText;
     JTextField productpublishingHouseText;
    
     JButton submit;
     Product [] productList = new Product [100];//productList array to store Person objects
     int numberOfProduct = 0;// counter to know how many objects are in the array
     
     
     public void Product(int productId,String productName,double cost,int yearOfPublication,String publishingHouse)
        {
           this.productId = productId;
           this.productName = productName;
           this.cost = cost;
           this.yearOfPublication = yearOfPublication;
           this.publishingHouse = publishingHouse;
           
     
                setSize(400,400);
                setLayout(new FlowLayout());
                
                productIdText = new JTextField(5);        
                add(productIdText);
                
                productNameText = new JTextField(5);        
                add(productNameText);
                
                productCostText = new JTextField(5);        
                add(productCostText);
                
                productyearOfPublicationText = new JTextField(5);        
                add(productyearOfPublicationText);
                
                productpublishingHouseText = new JTextField(5);        
                add(productpublishingHouseText);
                
                submit = new JButton("click");
                submit.addActionListener(this);
                add(submit);
                
            
                setVisible(true);
            }   
     
    public void actionPerformed(ActionEvent e)
    {
            if(e.getSource() == submit)// get the data from textfields
            {
                int id =  Integer.parseInt(productIdText.getText());
                String name = productNameText.getText();
                double cost = Double.parseDouble(productCostText.getText());
                int yearOfPublication = Integer.parseInt(productyearOfPublicationText.getText()); 
                String publishingHouse = productpublishingHouseText.getText();
                //System.out.println(id);
                //System.out.println(name);
                
                // put new object into array
                productList[numberOfProduct] = new Product(id,name,cost,yearOfPublication,publishingHouse);
                numberOfProduct++;
            }
        
        }
    
    public static void main(String []args)
        { 
           new Bookshop();
        }
    
    class Product
    {
       private int productId;
       private String productName;
       private double cost;
       private int yearOfPublication;
       private String publishingHouse;
    
    
     public Product(int productId,String productName,double cost,int yearOfPublication,String publishingHouse)
         {
    //        this.productId = productId;
    //        this.productName = productName;
    //        this.cost = cost;
    //        this.yearOfPublication = yearOfPublication;
    //        this.publishingHouse = publishingHouse;
    //        
    //       
    //             setSize(400,400);
    //             setLayout(new FlowLayout());
    //             
    //             productIdText = new JTextField(5);        
    //             add(productIdText);
    //             
    //             productNameText = new JTextField(5);        
    //             add(productNameText);
    //             
    //             productCostText = new JTextField(5);        
    //             add(productCostText);
    //             
    //             productyearOfPublicationText = new JTextField(5);        
    //             add(productyearOfPublicationText);
    //             
    //             productpublishingHouseText = new JTextField(5);        
    //             add(productpublishingHouseText);
    //             
    //             submit = new JButton("click");
    //             submit.addActionListener(this);
    //             add(submit);
    //             
    //         
    //             setVisible(true);
    //         
        }
    
    public void setSize(int x,int y)
    {
    }
    
    public void setproductIdText(int productIdText)
        {
         this.productIdText=productIdText;
        }
    
    public void setproductNameText(String productNameText)
        {
          this.productNameText = productNameText;
        }
    public void setId(int productId)
       {
         this.productId = productId;
       }
       
    public int getproductId()
       {
           return productId;
       }
    public void setproductName(String productName)
       {
         this.productName = productName;
       }
       
    public String getproductName()
       {
           return productName;
       }
       
    public void setcost(double cost)
       {
         this.cost = cost;
       }
       
    public double getcost()
       {
           return cost;
       }
       
    public void setyearOfPublication(int yearOfPublication)
       {
         this.yearOfPublication = yearOfPublication;
       }
       
    public int getyearOfPublication()
       {
           return yearOfPublication;
       }
       
    public void setpublishingHouse(String publishingHouse)
       {
         this.publishingHouse = publishingHouse;
       }
       
    public String getpublishingHouse()
       {
           return publishingHouse;
       }
    }
    
    class Book extends Product
    {
     private String author;
     private int isbn;
     private int numberOfPages;
    
    
    public Book(String author, int isbn, int numberOfPages, int productId, String productName, double cost,
                           int yearOfPublication, String publishingHouse)
        {
         super(productId,productName,cost,yearOfPublication,publishingHouse);      
         this.author = author;
         this.isbn = isbn;
         this.numberOfPages = numberOfPages; 
        }
    }
    
    class Software extends Product
    {
     private int ram;
     private int processor;
     
     
     
     public Software(int ram,int processor,int productId, String productName, double cost,
                      int yearOfPublication, String publishingHouse)
         {
             super(productId,productName,cost,yearOfPublication,publishingHouse);      
             this.ram = ram;
             this.processor = processor; 
         }
         
    }
    }
    the section is marked in the attachment
    Attached Files
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    first, rename method in line 19 from "Product" to "showProduc t". Only classes should start with capital letter.

    Line 21 to 25 are unnecessary. the product data of one product should only be stored in class Product and not in Bookshop. So just delete these lines. Line 89 to 93 was the right place to store these data, so do not comment-out these lines.

    Just remember: the bookshop only stores the array of products. It does not care what is inside a product. Each product stores its own data, which can be different for each product. If you attempt to store the data of one product in the bookshop, then where should the data of the other 99 products be stored? Do you want to make an array of productIds, another array of productNames etc. in the bookshop to store all this data, and then access by array index? That is a historical programming way and has bad performance regarding deleting/inserting/accessing books and that is why the new object-orientated way was invented. Always keep the data together, that is what you have the class Product defined for!

    Comment

    Working...