473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sorry I think am too new to comprehend this coding ones

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, 273 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!

1 1212
chaarmann
785 Expert 512MB
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

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

Similar topics

15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
7
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear)....
22
by: petermichaux | last post by:
Hi, I'm curious about server load and download time if I use one big javascript file or break it into several smaller ones. Which is better? (Please think of this as the first time the scripts...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
3
by: ct-86 | last post by:
http://www.cdbook.cn/book.asp?id=2393 Organizational and Policy Issues 1 0. Don't sweat the small stuff. (Or: Know what not to standardize.) 2 1. Compile cleanly at high warning levels. 4 2....
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
7
by: masterjuan | last post by:
http://masterjuan0101.googlepages.com
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.