Tuesday, October 12, 2010

code for water billing system

import javax .swing.*;import java.awt.event.*;import java.awt.*;import javax.swing.border.*;
public class calcBills4 extends JFrame implements ActionListener
{
                       
           
public static void main(String [] args)
               {
               
new calcBills4();
               }
                 
            CardLayout cl =
new CardLayout();  
            JButton calcButton =
new JButton("Calculate");
            JButton clearButton =
new JButton("Clear");
            JButton viewButton =
new JButton("View");
            JButton quit =
new JButton("Quit");
            JButton back =
new JButton("Back");
            JButton save =
new JButton("Save");
            JTextField nameField =
new JTextField(20);
            JTextField mtField =
new JTextField(10); 
            JTextField rdField =
new JTextField(10); 
            JTextField prField =
new JTextField(10);
            JTextField rtField =
new JTextField("2",10);
            JLabel bill =
new JLabel();  
            JTextArea ta=
new JTextArea(18,25);
            JScrollPane scroll=
new JScrollPane(ta);
           
double name,rd,pr,rt,value;
            String mm;
           
            
public calcBills4()
             {
         
this.setLocation(300,250);
   
          JPanel myPanel=
new JPanel();
          JPanel conPanel=
new JPanel();
          JPanel viewPanel=
new JPanel();                                      
          JPanel panel1 =
new JPanel();         
          JPanel panel2 =
new JPanel();                     
          JPanel panel3 =
new JPanel();
          myPanel.setLayout(
new BorderLayout());
          panel2.setLayout(
new GridLayout(2,0));                                                
          panel3.setLayout(
new GridBagLayout());
          panel3.setBackground(Color.lightGray);
          viewPanel.setLayout(
new BorderLayout());    
                  
                  
         
this.setContentPane(conPanel);
          conPanel.setLayout(cl);
         
          conPanel.add(
"opt1",myPanel);
          conPanel.add(
"opt2",viewPanel);                   
         
                                
          JLabel label1 =
new JLabel("WATER BILLING SYSTEM");
          label1.setForeground(Color.blue);
          label1.setFont(
new Font("algerian",Font.BOLD | Font.ITALIC, 18));  
        
          Border b1 =BorderFactory.createTitledBorder(
"PROVIDE DETAILS BELOW");
          panel3.setBorder(b1);
         
          addItem(panel3,
new JLabel("Customer account"),0,0,1, 1, GridBagConstraints.EAST);                       
          addItem(panel3,nameField,1,0,1, 1, GridBagConstraints.WEST);                   
          addItem(panel3,
new JLabel("Customer name"),0,1,1, 1, GridBagConstraints.EAST);                       
          addItem(panel3,mtField,1,1,1, 1, GridBagConstraints.WEST);                      
          addItem(panel3,
new JLabel("Metre reading"),0,2,1, 1, GridBagConstraints.EAST);                     
          addItem(panel3,rdField,1,2,1, 1, GridBagConstraints.WEST);                       
          addItem(panel3,
new JLabel("previous reading"),0,3,1, 1, GridBagConstraints.EAST);                      
          addItem(panel3,prField,1,3,1, 1, GridBagConstraints.WEST);
          addItem(panel3,
new JLabel("Rate"),0,4,1, 1, GridBagConstraints.EAST);
          addItem(panel3,rtField,1,4,1, 1, GridBagConstraints.WEST);
          save.setEnabled(
false);        
          panel1.add(label1);
                                             
         myPanel.add(panel1,BorderLayout.NORTH);
          myPanel.add(panel2,BorderLayout.SOUTH);
          myPanel.add(panel3,BorderLayout.CENTER);
         
          Box box = Box.createHorizontalBox();
          box.add(
new JLabel("Customer Database"));
          box.add(Box.createHorizontalStrut(450)); 
          box.add(back);
          viewPanel.add(
new JLabel("water Billing Sytem"),BorderLayout.NORTH);
          viewPanel.add(scroll,BorderLayout.CENTER);
          viewPanel.add(box,BorderLayout.SOUTH);
          ta.setEditable(
false);
  
           Box box1 = Box.createHorizontalBox();   
           box1.add(calcButton);
           box1.add(Box.createHorizontalStrut(5));                       
           box1.add(clearButton);               
        
//  box1.add(Box.createHorizontalStrut(250));                                   //  box1.add(viewButton);         //  box1.add(Box.createHorizontalStrut(5));               // box1.add(save);           box1.add(Box.createHorizontalStrut(5));
           box1.add(quit);        
          
           Box box2 = Box.createHorizontalBox();
           box2.add(bill);        
                      
          panel2.add(box1);
         panel2.add(box2);                                               
          calcButton.addActionListener(
this);
          clearButton.addActionListener(
this);
          viewButton.addActionListener(
this);
          quit.addActionListener(
this);
          back.addActionListener(
this);
          save.addActionListener(
this);
         
     
this.setTitle("water billing system");             
     
this.pack();
     
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
this.setVisible(true);
     
this.setResizable(false);
                          
                 
   }                   


   
public void actionPerformed(ActionEvent e){
      
       
if (e.getSource() == calcButton){
              
                 mm=mtField.getText();
              

              
try{
                  name = Double.parseDouble(nameField.getText());
               }
                  
catch(NumberFormatException evt){
                     nameField.setText(
"Illegal Data Entered");
                     JOptionPane.showMessageDialog(
this,"Enter a valid Account Number please!",
                    
"Bill System",JOptionPane.WARNING_MESSAGE);
                     nameField.setText(
"");
                     nameField.requestFocus();
                    
return; 
                  }             

              
if((mm.length()==0)||(mm.startsWith(" ")==true)){                            
               JOptionPane.showMessageDialog(
this,"Enter a valid Name please!",
              
"Bill System",JOptionPane.WARNING_MESSAGE);          
               mtField.requestFocus();
return;

               }    
            
              
               
try{
               rd = Double.parseDouble(rdField.getText());
            } 
               
catch(NumberFormatException evt)   {
                  rdField.setText(
"Illegal Data Entered");
                  JOptionPane.showMessageDialog(
this,"Enter a valid value for metre reading !",
                 
"Bill System",JOptionPane.WARNING_MESSAGE);
                  rdField.setText(
"");
                  rdField.requestFocus();
                 
return;
               }

              
try{
               pr = Double.parseDouble(prField.getText());
            } 
               
catch(NumberFormatException evt)   {
                  prField.setText(
"Illegal Data Entered");
                  JOptionPane.showMessageDialog(
this,"Enter a valid value for previous reding !",
                 
"Bill System",JOptionPane.WARNING_MESSAGE);
                  prField.setText(
"");
                  prField.requestFocus();                                       
                 
return;
               }

                
try{
                  rt = Double.parseDouble(rtField.getText());
               }
                    
catch(NumberFormatException evt){
                     rtField.setText(
"Illegal Data Entered");
                     JOptionPane.showMessageDialog(
this,"Enter a valid rate!",
                    
"Bill System",JOptionPane.WARNING_MESSAGE);
                     rtField.setText(
"");
                     rtField.requestFocus();
                    
return; 
                  }
                 
                 
if((rd-pr)<0){
                  JOptionPane.showMessageDialog(
this,"Current reading less than previous Re-enter readings!",
                
"Bill System",JOptionPane.ERROR_MESSAGE);
                  rdField.setText(
"");
                  prField.setText(
"");
                  } 
                 
else {
                 
                  value = calc(rd,pr,rt);                                                                                 
                  JOptionPane.showMessageDialog(
null,"BILL IS MK "+value+"\n\n THANK YOU");                
                  save.setEnabled(
true);
                  }                           
               }
       
              
        
else if (e.getSource() == clearButton){
                           nameField.setText(
"");
                           mtField.setText(
"");
                           rdField.setText(
"");
                           prField.setText(
"");
                           rtField.setText(
"");
                           bill.setText(
"");
                           save.setEnabled(
false);                                                                        
                                      
               }
           
  
         
else
               { 
               System.exit(0);              
               } 
              
                       
      } 
  
/*    */                              //GridBag method                                        public static void addItem(JPanel p, JComponent c,
 
int x, int y, int width, int height,
 
int align)
    {
        GridBagConstraints gc =
new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets =
new Insets(5, 5, 5, 5);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }
   
public static double calc(double rd, double pr, double rt)
{

     
double value;                               
      value = ((rd-pr)*rt);                                                                                   
     
return value;

}  
   
            
}

1 comment:

  1. very nice program but how to run online this program

    ReplyDelete