By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,132 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,132 IT Pros & Developers. It's quick & easy.

sorry I think am too new to comprehend this coding ones

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

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Bookshop extends JFrame implements ActionListener
  6. {
  7.  
  8.  JTextField productIdText;
  9.  JTextField productNameText;
  10.  JTextField productCostText;
  11.  JTextField productyearOfPublicationText;
  12.  JTextField productpublishingHouseText;
  13.  
  14.  JButton submit;
  15.  Product [] productList = new Product [100];//productList array to store Person objects
  16.  int numberOfProduct = 0;// counter to know how many objects are in the array
  17.  
  18.  
  19.  public void Product(int productId,String productName,double cost,int yearOfPublication,String publishingHouse)
  20.     {
  21.        this.productId = productId;
  22.        this.productName = productName;
  23.        this.cost = cost;
  24.        this.yearOfPublication = yearOfPublication;
  25.        this.publishingHouse = publishingHouse;
  26.  
  27.  
  28.             setSize(400,400);
  29.             setLayout(new FlowLayout());
  30.  
  31.             productIdText = new JTextField(5);        
  32.             add(productIdText);
  33.  
  34.             productNameText = new JTextField(5);        
  35.             add(productNameText);
  36.  
  37.             productCostText = new JTextField(5);        
  38.             add(productCostText);
  39.  
  40.             productyearOfPublicationText = new JTextField(5);        
  41.             add(productyearOfPublicationText);
  42.  
  43.             productpublishingHouseText = new JTextField(5);        
  44.             add(productpublishingHouseText);
  45.  
  46.             submit = new JButton("click");
  47.             submit.addActionListener(this);
  48.             add(submit);
  49.  
  50.  
  51.             setVisible(true);
  52.         }   
  53.  
  54. public void actionPerformed(ActionEvent e)
  55. {
  56.         if(e.getSource() == submit)// get the data from textfields
  57.         {
  58.             int id =  Integer.parseInt(productIdText.getText());
  59.             String name = productNameText.getText();
  60.             double cost = Double.parseDouble(productCostText.getText());
  61.             int yearOfPublication = Integer.parseInt(productyearOfPublicationText.getText()); 
  62.             String publishingHouse = productpublishingHouseText.getText();
  63.             //System.out.println(id);
  64.             //System.out.println(name);
  65.  
  66.             // put new object into array
  67.             productList[numberOfProduct] = new Product(id,name,cost,yearOfPublication,publishingHouse);
  68.             numberOfProduct++;
  69.         }
  70.  
  71.     }
  72.  
  73. public static void main(String []args)
  74.     { 
  75.        new Bookshop();
  76.     }
  77.  
  78. class Product
  79. {
  80.    private int productId;
  81.    private String productName;
  82.    private double cost;
  83.    private int yearOfPublication;
  84.    private String publishingHouse;
  85.  
  86.  
  87.  public Product(int productId,String productName,double cost,int yearOfPublication,String publishingHouse)
  88.      {
  89. //        this.productId = productId;
  90. //        this.productName = productName;
  91. //        this.cost = cost;
  92. //        this.yearOfPublication = yearOfPublication;
  93. //        this.publishingHouse = publishingHouse;
  94. //        
  95. //       
  96. //             setSize(400,400);
  97. //             setLayout(new FlowLayout());
  98. //             
  99. //             productIdText = new JTextField(5);        
  100. //             add(productIdText);
  101. //             
  102. //             productNameText = new JTextField(5);        
  103. //             add(productNameText);
  104. //             
  105. //             productCostText = new JTextField(5);        
  106. //             add(productCostText);
  107. //             
  108. //             productyearOfPublicationText = new JTextField(5);        
  109. //             add(productyearOfPublicationText);
  110. //             
  111. //             productpublishingHouseText = new JTextField(5);        
  112. //             add(productpublishingHouseText);
  113. //             
  114. //             submit = new JButton("click");
  115. //             submit.addActionListener(this);
  116. //             add(submit);
  117. //             
  118. //         
  119. //             setVisible(true);
  120. //         
  121.     }
  122.  
  123. public void setSize(int x,int y)
  124. {
  125. }
  126.  
  127. public void setproductIdText(int productIdText)
  128.     {
  129.      this.productIdText=productIdText;
  130.     }
  131.  
  132. public void setproductNameText(String productNameText)
  133.     {
  134.       this.productNameText = productNameText;
  135.     }
  136. public void setId(int productId)
  137.    {
  138.      this.productId = productId;
  139.    }
  140.  
  141. public int getproductId()
  142.    {
  143.        return productId;
  144.    }
  145. public void setproductName(String productName)
  146.    {
  147.      this.productName = productName;
  148.    }
  149.  
  150. public String getproductName()
  151.    {
  152.        return productName;
  153.    }
  154.  
  155. public void setcost(double cost)
  156.    {
  157.      this.cost = cost;
  158.    }
  159.  
  160. public double getcost()
  161.    {
  162.        return cost;
  163.    }
  164.  
  165. public void setyearOfPublication(int yearOfPublication)
  166.    {
  167.      this.yearOfPublication = yearOfPublication;
  168.    }
  169.  
  170. public int getyearOfPublication()
  171.    {
  172.        return yearOfPublication;
  173.    }
  174.  
  175. public void setpublishingHouse(String publishingHouse)
  176.    {
  177.      this.publishingHouse = publishingHouse;
  178.    }
  179.  
  180. public String getpublishingHouse()
  181.    {
  182.        return publishingHouse;
  183.    }
  184. }
  185.  
  186. class Book extends Product
  187. {
  188.  private String author;
  189.  private int isbn;
  190.  private int numberOfPages;
  191.  
  192.  
  193. public Book(String author, int isbn, int numberOfPages, int productId, String productName, double cost,
  194.                        int yearOfPublication, String publishingHouse)
  195.     {
  196.      super(productId,productName,cost,yearOfPublication,publishingHouse);      
  197.      this.author = author;
  198.      this.isbn = isbn;
  199.      this.numberOfPages = numberOfPages; 
  200.     }
  201. }
  202.  
  203. class Software extends Product
  204. {
  205.  private int ram;
  206.  private int processor;
  207.  
  208.  
  209.  
  210.  public Software(int ram,int processor,int productId, String productName, double cost,
  211.                   int yearOfPublication, String publishingHouse)
  212.      {
  213.          super(productId,productName,cost,yearOfPublication,publishingHouse);      
  214.          this.ram = ram;
  215.          this.processor = processor; 
  216.      }
  217.  
  218. }
  219. }
  220.  
  221.  
the section is marked in the attachment
Attached Files
File Type: docx Doc1.docx (258.0 KB, 219 views)
Jul 13 '15 #1

✓ answered by chaarmann

first, rename method in line 19 from "Product" to "showProduct". 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!

Share this Question
Share on Google+
1 Reply


Expert 100+
P: 785
first, rename method in line 19 from "Product" to "showProduct". 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!
Jul 14 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.