Encapsulation in JAVA, a Brief explanation

It is a process of wrapping up the data members along with related data handlers method is called Encapsulation.

Wrapping up the data members and data handlers method means we are grouping both as a single unit. In Java, we group or wrap both inside the Class, so Class is a single unit that consists of data members as well as methods. As shown in the figure.

So, the class having both data members and related data handler method is called Encapsulated Class.

Here in the above definition, two important terms have been used i.e, data members, and data handlers method.

  • Data members: The variables which are declared inside the class.
  • Data handlers Method: It is just a usual method, whose only purpose is to initialized or return the value of data members present inside the Class. Such types of methods do not use for writing any business logic or functionality.
 #knowledgestreams.blogspot.com

Types of Data Handler Method
  1. Getter Method: It is used to return the value of private data members present in class.
  2. Setter Method: The purpose of this method to initialize or override private data member’s value. With the setter method, you can modify the value of data members.

Don’t worry I explained in detail below.

Let us understand in detail,

In Java we have the concept of Class and Object, so inside the class, we declare some variables and methods, that variable or data members can be accessed easily which the help Object Reference of that class.

We can modify, initialize, and do all different types of operation on that data member.

Since we can directly do any type of changes or modifications with the value of data members, in short, we have the access to data members without any restrictions or without doing any validation of the user, this will give birth to a very big security breach. And you will land up in a horrible situation.

If your application contains confidential data, then Security is a very essential part, so to avoid security breach, it is highly recommended to declare your data members with private access modifiers.

And create a getter and setter method for each data member. So that no one can access directly instead they need to use a getter and setter method to access it.

Once the getter or setter method invoked or called then the very first line of code will perform validation, to identify the right user.

In the above paragraph, one new term has been used that is a private access modifier. Let’s have a quick look into the Access Modifier.

Access Modifier

It is a keyword in java which is used to decide the scope of classes, methods, or data members. Scope refers to visibility or from where you can able to access any of the members of Class which has some access modifier.

In Java, We have in total Four Access Modifiers:

  1. public
  2. private
  3. default
  4. protected

 #knowledgestreams.blogspot.com

1. Public

  • It is the highest visible access modifier in Java.
  • It can be accessed globally, that is from within the class or outside the class or within the package or outside the packages.
  • All the data members in Java can be declared with public access modifier.

2. Private

  • It is the least visible access modifier in Java.
  • The scope of private members is up to class level only that is private members can only be accessed from within the class and cannot be accessed outside the class.
  • Private members cannot even be inherited.
  • The outer class cannot be private but constructor, method, variables can be private.

3. Protected

  • The scope of protected members is the package level.
  • Protected variables and methods can be accessed outside of the package with the help of Inheritance.

4. Default

  • Default is the automatic access modifier in Java, i.e, if any member is not declared as public, private, or protected then it automatically becomes the default.
  • The scope of default member is package level, i.e, default members can only be accessed within the package and cannot be accessed outside the package.
  • All the members in Java can have a default access modifier.

I hope you got some idea about Access Modifiers. Let’s come back to Encapsulation. I was talking about how to prevent the data members from accessing directly.

 So for that, we need to make it private and provide getter and setter methods to access them.

Main Purpose of Encapsulation

The idea of making a data member as private, is that to provide security, so before accessing the data, there must be validation should perform for the login user.

So for this, we created a getter and a setter method, once either of the methods gets called or invoked, the very first step application will validate whether the login user is the right person to access this data, once the validation completed then the user can able to access the data.

In short, we can say, one more layer or wall is added named Validation before data members. Anyone who want to do modification or changes in the data member value, first they have to go through this validation process.

Encapsulation in Java with realtime example

When you input the PIN and press the Enter button, then a method got triggered or invoked, that method first validates your PIN, so once the validation completed.

 It will redirect to the next screen where you have lots of options, such as Balance Enquiry, Amount Withdrawal and many more. Suppose you wanted to withdraw money, once you input your amount and press enter.

 It will trigger one more method, that is setter method, where the first thing again it will validate to proceed further. Similarly for checking the balance, if you press the enter button, the getter method will get invoked and firstly it will also do the validation.

In short with the help of the Encapsulation we can achieve High-Level Security.

But here to achieve security, we are paying something. Have you noticed, each time the validation happened because of that the process will be a little bit slower. See the image for a better understanding.

Rules of Encapsulation

  1. Make the Class public.
  2. Make the data members private.
  3. Every data member must have a public getter and setter methods.

Advantages of Encapsulation

  1. We can achieve Data Hiding.
  2. We can perform Data validation.
  3. We can achieve high-level security.

Note: The Class which fulfills all above condition that class in Java we called Bean Class and Object created for that class is called Bean Object.

Explain the Java Bean Specification?

  1.  A Bean Class must be a public non-abstract class.
  2. The data members present inside the bean class must be of a private access modifier.
  3. Every data member must have a public getter and setter method.
  4. A Bean Class must have a public default constructor.

Here one more important thing I would like to mention, Encapsulation is a combination of two things Data hiding and Abstraction.

How Encapsulation is a combination of two Data hiding and Abstraction?

Let us consider the same ATM example, above Since all logics such as once you press enter after putting the PIN, what method will be triggered you don’t know, and all these processes executed behind the door you don’t have any clue.

 At the same time, you cannot access the data member directly, because it is made private. So in order access, you need a getter or setter method, in short, the data member is hidden behind the method. Hence data hiding has been achieved.

Difference between Abstraction and Encapsulation?

In Abstraction we simply hiding the internal implementation details which are not useful for the users, however, security is not a high priority.

You can easily know what is a thing happening behind the scene you are interested in. But in the case of Encapsulation, the highest priority is given to achieve security. No unauthorized person is allowed to access any kind of data.

Let’s understand with some Real World Example.

Consider four-wheeler vehicles, there are so many small parts assembled to form a complete structure of the vehicle. 

Such as axle, engine, gasoline filter, and many more, but all are hidden under the bonnet/hood of the vehicle. The only important things such as steering wheel, seats, etc.

 Here the hidden things are not restricted from users, even if you want you can easily see everything. And the parts which essential for the users that only exposed. This is nothing but in Java, it is called Abstraction

Although in case of Encapsulation,  even if you are interested to know, logic or algorithm wrote for the application which you browse frequently such as Facebook or Google, then you unable to know as an EndUser, for that, you have joined that companies and be a part of the developer team.

Thus, we can say that Encapsulation is the combination of two terms that is Data Hiding as well as Abstraction


#java #encapsulation #post, #blog #knowledge

ELECTRICITY BILLING SYSTEM

ELECTRICITY BILLING SYSTEM

Create a electricity biilling system using JAVA

#knowledge #stream #blog
you can find the full source code for the project below.


A - Below is the source code of the project.

B - Create separate class files for every class.

C - Copy the code of one class (ex: login.java) and paste it in Login class file, do it for every class.

1 - Create your first class with name 'conn' and paste the code


package Electricity;

import java.sql.*;  

//knowledgestreams.blogspot.com

public class conn{
    Connection c;
    Statement s;
    public conn(){  
        try{  
            Class.forName("com.mysql.jdbc.Driver");  
            c =DriverManager.getConnection("jdbc:mysql:///project","root","");    
            s =c.createStatement();  
        }catch(Exception e){ 
            System.out.println(e);
        }  
    }  
}  
//knowledgestreams.blogspot.com
//knowledgestreams.blogspot.com

2 - Create next class with name 'Splash' and paste the code below in it. This is also our Main Frame of the project.

package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class splash { public static void main(String[] args){ fframe f1 = new fframe(); f1.setVisible(true); int i; int x=1; for(i=2; i<=600; i+=4, x+=1){ f1.setLocation(800 - ((i+x)/2), 500 - (i/2)); f1.setSize(i+x,i); try{ Thread.sleep(10); }catch(Exception e){} } } } class fframe extends JFrame implements Runnable{ Thread t1; fframe(){ super("Electricity Billing System"); setLayout(new FlowLayout()); ImageIcon c1 = new ImageIcon(ClassLoader.getSystemResource("icon/elect.jpg")); Image i1 = c1.getImage().getScaledInstance(730, 550,Image.SCALE_DEFAULT); ImageIcon i2 = new ImageIcon(i1); JLabel l1 = new JLabel(i2); add(l1); t1 = new Thread(this); t1.start(); } public void run(){ try{ Thread.sleep(7000); this.setVisible(false); Login l = new Login(); l.setVisible(true); }catch(Exception e){ e.printStackTrace(); } } }

//knowledgestreams.blogspot.com https://knowledgestreams.blogspot.com/

3 - Create next class with name 'Login'.

package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class Login extends JFrame implements ActionListener{ JLabel l1,l2,l3; JTextField tf1; JPasswordField pf2; JButton b1,b2; JPanel p1,p2,p3,p4; Login(){ super("Login Page"); l1 = new JLabel("Username"); l2 = new JLabel("Password"); tf1 = new JTextField(15); pf2 = new JPasswordField(15); ImageIcon ic1 = new ImageIcon(ClassLoader.getSystemResource("icon/login.png")); Image i1 = ic1.getImage().getScaledInstance(16, 16,Image.SCALE_DEFAULT); b1 = new JButton("Login", new ImageIcon(i1)); ImageIcon ic2 = new ImageIcon(ClassLoader.getSystemResource("icon/cancel.jpg")); Image i2 = ic2.getImage().getScaledInstance(16, 16,Image.SCALE_DEFAULT); b2 = new JButton("Cancel",new ImageIcon(i2)); b1.addActionListener(this); b2.addActionListener(this); ImageIcon ic3 = new ImageIcon(ClassLoader.getSystemResource("icon/pop.png")); Image i3 = ic3.getImage().getScaledInstance(128, 128,Image.SCALE_DEFAULT); ImageIcon icc3 = new ImageIcon(i3); l3 = new JLabel(icc3); setLayout(new BorderLayout()); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p4 = new JPanel(); knowledgestreams.blogspot.com add(l3,BorderLayout.WEST); p2.add(l1); p2.add(tf1); p2.add(l2); p2.add(pf2); add(p2,BorderLayout.CENTER); p4.add(b1); p4.add(b2); add(p4,BorderLayout.SOUTH); p2.setBackground(Color.WHITE); p4.setBackground(Color.WHITE); setSize(440,250); setLocation(600,400); setVisible(true); //knowledgestreams.blogspot.com } public void actionPerformed(ActionEvent ae){ try{ conn c1 = new conn(); String a = tf1.getText(); String b = pf2.getText(); String q = "select * from login where username = '"+a+"' and password = '"+b+"'"; ResultSet rs = c1.s.executeQuery(q); if(rs.next()){ new Project().setVisible(true); this.setVisible(false); }else{ JOptionPane.showMessageDialog(null, "Invalid login"); setVisible(false); } }catch(Exception e){ e.printStackTrace(); System.out.println("error: "+e); } } public static void main(String[] args){ new Login().setVisible(true); } }

//knowledgestreams.blogspot.com
4 - Create next class with name 'Project' and paste the code inside the class.

package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Project extends JFrame implements ActionListener{ Project(){ super("Electricity Billing System"); setSize(1920,1030); /* Adding background image */ ImageIcon ic = new ImageIcon(ClassLoader.getSystemResource("icon/elect1.jpg")); Image i3 = ic.getImage().getScaledInstance(1900, 950,Image.SCALE_DEFAULT); ImageIcon icc3 = new ImageIcon(i3); JLabel l1 = new JLabel(icc3); add(l1); /* First Column */ JMenuBar mb = new JMenuBar(); JMenu master = new JMenu("Master"); JMenuItem m1 = new JMenuItem("New Customer"); JMenuItem m2 = new JMenuItem("Customer Details"); JMenuItem m3 = new JMenuItem("Deposit Details"); master.setForeground(Color.BLUE); /* ---- Customer Details ---- */ m1.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon1 = new ImageIcon(ClassLoader.getSystemResource("icon/icon1.png")); Image image1 = icon1.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); m1.setIcon(new ImageIcon(image1)); m1.setMnemonic('D'); m1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK)); m1.setBackground(Color.WHITE); /* ---- Meter Details ---- */ m2.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon2 = new ImageIcon(ClassLoader.getSystemResource("icon/icon2.png")); Image image2 = icon2.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); m2.setIcon(new ImageIcon(image2)); m2.setMnemonic('M'); m2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK)); m2.setBackground(Color.WHITE); /* ---- Deposit Details ----- */ m3.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon3 = new ImageIcon(ClassLoader.getSystemResource("icon/icon3.png")); Image image3 = icon3.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); m3.setIcon(new ImageIcon(image3)); m3.setMnemonic('N'); m3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); m3.setBackground(Color.WHITE); m1.addActionListener(this); m2.addActionListener(this); m3.addActionListener(this); // -------------------------------------------------------------------------------------------- //knowledgestreams.blogspot.com
/* Second Column */ JMenu user = new JMenu("User"); JMenuItem u1 = new JMenuItem("Pay Bill"); JMenuItem u2 = new JMenuItem("Calculate Bill"); JMenuItem u3 = new JMenuItem("Last Bill"); user.setForeground(Color.RED); /* ---- Pay Bill ---- */ u1.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon4 = new ImageIcon(ClassLoader.getSystemResource("icon/icon4.png")); Image image4 = icon4.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); u1.setIcon(new ImageIcon(image4)); u1.setMnemonic('P'); u1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK)); u1.setBackground(Color.WHITE); /* ---- Bill Details ---- */ u2.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon5 = new ImageIcon(ClassLoader.getSystemResource("icon/icon5.png")); Image image5 = icon5.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); u2.setIcon(new ImageIcon(image5)); u2.setMnemonic('B'); u2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK)); u2.setBackground(Color.WHITE); /* ---- Last Bill ----*/ u3.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon6 = new ImageIcon(ClassLoader.getSystemResource("icon/icon6.png")); Image image6 = icon6.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); u3.setIcon(new ImageIcon(image6)); u3.setMnemonic('L'); u3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK)); u3.setBackground(Color.WHITE); u1.addActionListener(this); u2.addActionListener(this); u3.addActionListener(this); // --------------------------------------------------------------------------------------------- /* Third Column*/ JMenu report = new JMenu("Report"); JMenuItem r1 = new JMenuItem("Generate Bill"); report.setForeground(Color.BLUE); /* ---- Report ---- */ r1.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon7 = new ImageIcon(ClassLoader.getSystemResource("icon/icon7.png")); Image image7 = icon7.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); r1.setIcon(new ImageIcon(image7)); r1.setMnemonic('R'); r1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK)); r1.setBackground(Color.WHITE); r1.addActionListener(this); // ----------------------------------------------------------------------------------------------- /* Fourth Column*/ JMenu utility = new JMenu("Utility"); JMenuItem ut1 = new JMenuItem("Notepad"); JMenuItem ut2 = new JMenuItem("Calculator"); JMenuItem ut3 = new JMenuItem("Web Browser"); utility.setForeground(Color.RED); /* ---- Calender ---- */ ut1.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon8 = new ImageIcon(ClassLoader.getSystemResource("icon/icon12.png")); Image image8 = icon8.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); ut1.setIcon(new ImageIcon(image8)); ut1.setMnemonic('C'); ut1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); ut1.setBackground(Color.WHITE); /* ---- Calculator ---- */ ut2.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon9 = new ImageIcon(ClassLoader.getSystemResource("icon/icon9.png")); Image image9 = icon9.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); ut2.setIcon(new ImageIcon(image9)); ut2.setMnemonic('X'); ut2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); ut2.setBackground(Color.WHITE); /* ---- Web Browser ---- */ ut3.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon10 = new ImageIcon(ClassLoader.getSystemResource("icon/icon10.png")); Image image10 = icon10.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); ut3.setIcon(new ImageIcon(image10)); ut3.setMnemonic('W'); ut3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK)); ut3.setBackground(Color.WHITE); ut1.addActionListener(this); ut2.addActionListener(this); ut3.addActionListener(this); // --------------------------------------------------------------------------------------- /*Fifth Column */ JMenu exit = new JMenu("Exit"); JMenuItem ex = new JMenuItem("Exit"); exit.setForeground(Color.BLUE); /* ---- Exit ---- */ ex.setFont(new Font("monospaced",Font.PLAIN,12)); ImageIcon icon11 = new ImageIcon(ClassLoader.getSystemResource("icon/icon11.png")); Image image11 = icon11.getImage().getScaledInstance(20, 20,Image.SCALE_DEFAULT); ex.setIcon(new ImageIcon(image11)); ex.setMnemonic('Z'); ex.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK)); ex.setBackground(Color.WHITE); ex.addActionListener(this); // --------------------------------------------------------------------------------------------- master.add(m1); master.add(m2); master.add(m3); user.add(u1); user.add(u2); user.add(u3); report.add(r1); utility.add(ut1); utility.add(ut2); utility.add(ut3); exit.add(ex); mb.add(master); mb.add(user); mb.add(report); mb.add(utility); mb.add(exit); setJMenuBar(mb); setFont(new Font("Senserif",Font.BOLD,16)); setLayout(new FlowLayout()); setVisible(false); } public void actionPerformed(ActionEvent ae){ String msg = ae.getActionCommand(); if(msg.equals("Customer Details")){ new customer_details().setVisible(true); }else if(msg.equals("New Customer")){ new new_customer().setVisible(true); }else if(msg.equals("Calculate Bill")){ new calculate_bill().setVisible(true); }else if(msg.equals("Pay Bill")){ new pay_bill().setVisible(true); }else if(msg.equals("Notepad")){ try{ Runtime.getRuntime().exec("notepad.exe"); }catch(Exception e){ } }else if(msg.equals("Calculator")){ try{ Runtime.getRuntime().exec("calc.exe"); }catch(Exception e){ } }else if(msg.equals("Web Browser")){ try{ Runtime.getRuntime().exec("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); }catch(Exception e){ } }else if(msg.equals("Exit")){ System.exit(0); }else if(msg.equals("Generate Bill")){ new generate_bill().setVisible(true); } } public static void main(String[] args){ new Project().setVisible(true); } }

knowledge streams blog spot com

5 - Create next class with name 'new_customer'.


package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class new_customer extends JFrame implements ActionListener{ JLabel l1,l2,l3,l4,l5,l6,l7,l8; JTextField t1,t2,t3,t4,t5,t6,t7; JButton b1,b2; new_customer(){ setLocation(350,200); setSize(650,600); JPanel p = new JPanel(); p.setLayout(new GridLayout(9,2,10,10)); p.setBackground(Color.WHITE); l1 = new JLabel("Name"); t1 = new JTextField(); p.add(l1); p.add(t1); l2 = new JLabel("Meter No"); t2 = new JTextField(); p.add(l2); p.add(t2); l3 = new JLabel("Address"); t3 = new JTextField(); p.add(l3); p.add(t3); l4 = new JLabel("State"); t4 = new JTextField(); p.add(l4); p.add(t4); l5 = new JLabel("City"); t5 = new JTextField(); p.add(l5); p.add(t5); l6 = new JLabel("Email"); t6 = new JTextField(); p.add(l6); p.add(t6); l7 = new JLabel("Phone Number"); t7 = new JTextField(); p.add(l7); p.add(t7); b1 = new JButton("Submit"); b2 = new JButton("Cancel"); b1.setBackground(Color.BLACK); b1.setForeground(Color.WHITE); b2.setBackground(Color.BLACK); b2.setForeground(Color.WHITE); p.add(b1); p.add(b2); setLayout(new BorderLayout()); add(p,"Center"); ImageIcon ic1 = new ImageIcon(ClassLoader.getSystemResource("icon/hicon1.jpg")); Image i3 = ic1.getImage().getScaledInstance(150, 300,Image.SCALE_DEFAULT); ImageIcon ic2 = new ImageIcon(i3); l8 = new JLabel(ic2); add(l8,"West"); //for changing the color of the whole getContentPane().setBackground(Color.WHITE); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent ae){ String a = t1.getText(); String c = t2.getText(); String d = t3.getText(); String e = t4.getText(); String f = t5.getText(); String g = t6.getText(); String h = t7.getText(); String q1 = "insert into emp values('"+a+"','"+c+"','"+d+"','"+e+"','"+f+"','"+g+"','"+h+"')"; try{ conn c1 = new conn(); c1.s.executeUpdate(q1); JOptionPane.showMessageDialog(null,"Employee Created"); this.setVisible(false); }catch(Exception ex){ ex.printStackTrace(); } } public static void main(String[] args){ new new_customer().setVisible(true); } }


6 - Create next class with name 'customer_details'.
#knowledgestreams.blogspot.com

//knowledge streams blog spot

package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class customer_details extends JFrame implements ActionListener{ JTable t1; JButton b1; String x[] = {"Emp Name","Meter No","Address","State","City","Email","Phone"}; String y[][] = new String[20][8]; int i=0, j=0; customer_details(){ super("Customer Details"); setSize(1200,650); setLocation(200,200); try{ conn c1 = new conn(); String s1 = "select * from emp"; ResultSet rs = c1.s.executeQuery(s1); while(rs.next()){ y[i][j++]=rs.getString("name"); y[i][j++]=rs.getString("meter_number"); y[i][j++]=rs.getString("address"); y[i][j++]=rs.getString("state"); y[i][j++]=rs.getString("city"); y[i][j++]=rs.getString("email"); y[i][j++]=rs.getString("phone"); i++; j=0; } t1 = new JTable(y,x); }catch(Exception e){ e.printStackTrace(); } b1 = new JButton("Print"); add(b1,"South"); JScrollPane sp = new JScrollPane(t1); add(sp); b1.addActionListener(this); } public void actionPerformed(ActionEvent ae){ try{ t1.print(); }catch(Exception e){} } public static void main(String[] args){ new customer_details().setVisible(true); } }


7 - Create next class with name 'calculate_bill'.

package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class calculate_bill extends JFrame implements ActionListener{ JLabel l1,l2,l3,l4,l5; JTextField t1; Choice c1,c2; JButton b1,b2; JPanel p; calculate_bill(){ p = new JPanel(); p.setLayout(new GridLayout(4,2,30,30)); p.setBackground(Color.WHITE); l1 = new JLabel("Calculate Electricity Bill"); l2 = new JLabel("Meter No"); l3 = new JLabel("Units Cosumed"); l5 = new JLabel("Month"); t1 = new JTextField(); c1 = new Choice(); c1.add("1001"); c1.add("1002"); c1.add("1003"); c1.add("1004"); c1.add("1005"); c1.add("1006"); c1.add("1007"); c1.add("1008"); c1.add("1009"); c1.add("1010"); c2 = new Choice(); c2.add("January"); c2.add("February"); c2.add("March"); c2.add("April"); c2.add("May"); c2.add("June"); c2.add("July"); c2.add("August"); c2.add("September"); c2.add("October"); c2.add("November"); c2.add("December"); b1 = new JButton("Submit"); b2 = new JButton("Cancel"); b1.setBackground(Color.BLACK); b1.setForeground(Color.WHITE); b2.setBackground(Color.BLACK); b2.setForeground(Color.WHITE); ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icon/hicon2.jpg")); Image i2 = i1.getImage().getScaledInstance(180, 270,Image.SCALE_DEFAULT); ImageIcon i3 = new ImageIcon(i2); l4 = new JLabel(i3); l1.setFont(new Font("Senserif",Font.PLAIN,26)); //Move the label to center l1.setHorizontalAlignment(JLabel.CENTER); p.add(l2); p.add(c1); p.add(l5); p.add(c2); p.add(l3); p.add(t1); p.add(b1); p.add(b2); setLayout(new BorderLayout(30,30)); add(l1,"North"); add(p,"Center"); add(l4,"West"); //knowledgestreams.blogspot.com b1.addActionListener(this); b2.addActionListener(this); getContentPane().setBackground(Color.WHITE); setSize(650,500); setLocation(350,220); } public void actionPerformed(ActionEvent ae){ String a = c1.getSelectedItem(); String b = t1.getText(); String c = c2.getSelectedItem(); int p1 = Integer.parseInt(b); int p2 = p1*7; int p3 = p2+50+12+102+20+50; String q = "insert into bill values('"+a+"','"+c+"','"+b+"','"+p3+"')"; try{ conn c1 = new conn(); c1.s.executeUpdate(q); JOptionPane.showMessageDialog(null,"Bill Updated"); }catch(Exception aee){ aee.printStackTrace(); } } public static void main(String[] args){ new calculate_bill().setVisible(true); } }



8 - Create next class with name 'pay_bill'.

package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class pay_bill extends JFrame{ pay_bill(){ JEditorPane j = new JEditorPane(); j.setEditable(false); try { j.setPage("https://paytm.com/electricity-bill-payment"); }catch (Exception e) { j.setContentType("text/html"); j.setText("<html>Could not load</html>"); } JScrollPane scrollPane = new JScrollPane(j); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(scrollPane); setPreferredSize(new Dimension(800,600)); setSize(800,800); setLocation(250,120); setVisible(true); } public static void main(String[] args){ new pay_bill().setVisible(true); } }
//knowledgestreams.blogspot.com

9 - Create next class with name 'generate_bill'.


package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class generate_bill extends JFrame implements ActionListener{ JLabel l1; JTextArea t1; JButton b1; Choice c1,c2; JPanel p1; generate_bill(){ setSize(500,900); setLayout(new BorderLayout()); p1 = new JPanel(); l1 = new JLabel("Generate Bill"); c1 = new Choice(); c2 = new Choice(); //knowledgestreams.blogspot.com
c1.add("1001"); c1.add("1002"); c1.add("1003"); c1.add("1004"); c1.add("1005"); c1.add("1006"); c1.add("1007"); c1.add("1008"); c1.add("1009"); c1.add("1010"); c2.add("January"); c2.add("February"); c2.add("March"); c2.add("April"); c2.add("May"); c2.add("June"); c2.add("July"); c2.add("August"); c2.add("September"); c2.add("October"); c2.add("November"); c2.add("December"); t1 = new JTextArea(50,15); JScrollPane jsp = new JScrollPane(t1); t1.setFont(new Font("Senserif",Font.ITALIC,18)); b1 = new JButton("Generate Bill"); p1.add(l1); p1.add(c1); p1.add(c2); add(p1,"North"); add(jsp,"Center"); add(b1,"South"); b1.addActionListener(this); //knowledgestreams.blogspot.com setLocation(350,40); } public void actionPerformed(ActionEvent ae){ try{ conn c = new conn(); String month = c2.getSelectedItem(); t1.setText("\abc\nELECTRICITY BILL FOR THE MONTH OF "+month+" ,2018\n\n\n"); ResultSet rs = c.s.executeQuery("select * from emp where meter_number="+c1.getSelectedItem()); if(rs.next()){ t1.append("\n Customer Name:"+rs.getString("name")); t1.append("\n Meter Number: "+rs.getString("meter_number")); t1.append("\n Address: "+rs.getString("address")); t1.append("\n State: "+rs.getString("state")); t1.append("\n City: "+rs.getString("city")); t1.append("\n Email: "+rs.getString("email")); t1.append("\n Phone Number "+rs.getString("phone")); t1.append("\n-------------------------------------------------------------"); t1.append("\n"); } rs = c.s.executeQuery("select * from tax"); if(rs.next()){ t1.append("\n Meter Location:"+rs.getString("meter_location")); t1.append("\n Meter Type: "+rs.getString("meter_type")); t1.append("\n Phase Code: "+rs.getString("phase_code")); t1.append("\n Bill Type: "+rs.getString("bill_type")); t1.append("\n Days: "+rs.getString("days")); t1.append("\n"); t1.append("---------------------------------------------------------------"); t1.append("\n\n"); t1.append("\n Meter Rent:\t\t"+rs.getString("meter_rent")); t1.append("\n MCB Rent: \t\t"+rs.getString("mcb_rent")); t1.append("\n Service Tax:\t"+rs.getString("service_rent")); t1.append("\n GST@9%:\t\t"+rs.getString("gst")); t1.append("\n"); } rs = c.s.executeQuery("select * from bill where meter_number="+c1.getSelectedItem()); if(rs.next()){ t1.append("\n Current Month :\t"+rs.getString("month")); t1.append("\n Units Consumed:\t"+rs.getString("units")); t1.append("\n Total Charges :\t"+rs.getString("amount")); t1.append("\n---------------------------------------------------------------"); t1.append("\n TOTAL PAYABLE :\t"+rs.getString("amount")); } //knowledgestreams.blogspot.com
}catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ new generate_bill().setVisible(true); } }

10- Create next class with name "Last Bill"


package Electricity; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class LastBill extends JFrame implements ActionListener{ JLabel l1; JTextArea t1; JButton b1; Choice c1; JPanel p1; LastBill(){ setSize(500,900); setLayout(new BorderLayout()); p1 = new JPanel(); l1 = new JLabel("Generate Bill"); c1 = new Choice(); c1.add("1001"); c1.add("1002"); c1.add("1003"); c1.add("1004"); c1.add("1005"); c1.add("1006"); c1.add("1007"); c1.add("1008"); c1.add("1009"); c1.add("1010"); t1 = new JTextArea(50,15); JScrollPane jsp = new JScrollPane(t1); t1.setFont(new Font("Senserif",Font.ITALIC,18)); b1 = new JButton("Generate Bill"); p1.add(l1); p1.add(c1); add(p1,"North"); add(jsp,"Center"); add(b1,"South"); b1.addActionListener(this); setLocation(350,40); } public void actionPerformed(ActionEvent ae){ try{ conn c = new conn(); ResultSet rs = c.s.executeQuery("select * from emp where meter_number="+c1.getSelectedItem()); if(rs.next()){ t1.append("\n Customer Name:"+rs.getString("name")); t1.append("\n Meter Number: "+rs.getString("meter_number")); t1.append("\n Address: "+rs.getString("address")); t1.append("\n State: "+rs.getString("state")); t1.append("\n City: "+rs.getString("city")); t1.append("\n Email: "+rs.getString("email")); t1.append("\n Phone Number "+rs.getString("phone")); t1.append("\n-------------------------------------------------------------"); t1.append("\n"); } t1.append("Details of the Last Bills\n\n\n"); rs = c.s.executeQuery("select * from bill where meter_number="+c1.getSelectedItem()); while(rs.next()){ t1.append(" "+ rs.getString("month") + " " +rs.getString("amount") + "\n"); } }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ new LastBill().setVisible(true); } }




#java #electricity #bill #code


Free source code for java project on electricity billing system.

Featured Post

Day 41 — BGP Confederations: Sub-AS Design, External View and Migration

1. Opening Confederations are another way to scale BGP inside a large administrative domain. They divide the domain into member autonomous systems while presenting a single confederation identifier to external peers. They are powerful, but their operational model is more complex than simply 'using private ASNs inside.' The engineering goal is not to memorize another BGP command. It is to understand what information each speaker is allowed to propagate, what path information can be hidden, and what failure domain is created by the chosen control-plane architecture . 2. Concept and standards behavior RFC 5065 defines AS_CONFED_SEQUENCE and AS_CONFED_SET and how member-AS relationships are represented. Confederation external sessions have eBGP-like properties inside the confederation, while the confederation is presented externally as one AS. Modern guidance must also account for the fact that RFC 9774 prohibits new origination of AS_SET/AS_CONFED_SET in ordinary aggregation c...