This chess board was created with java.
kizzy
Friday, October 15, 2010
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;
}
}
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;
}
}
Thursday, October 7, 2010
The above page is how the water billing system looks like before any transactions happen. The account names, customers names, current and previous meter readings are all blank. The rate 2 is predefined and appears automatically on the rate’s text field. The border header “PROVIDE DETAILS BELOW” requests the Billing Manager to provide particular customer details in their corresponding text fields.
If no field is filled and the calculate button is clicked the system provides a message dialog box prompting the user to fill the fields. By default the warning is “enter a valid account number” hence the interface looks like the snapshot above.
If the account number is correctly filled, with other fields not filled, the interface looks like below:-
If you incorrectly fill the customer’s name, the system provides a message dialog box warning the user to re-enter the customers name such that the system looks like below:-
If the correct name is filled the interface looks like below:-
The snapshot above shows how the system looks like when the correct name and account number are filled.
If the user mistakenly enters a previous meter reading that is bigger than the current reading, the system does not calculate the customer’s bill but instead a message dialog box prompting the user to re-enter the readings.
If the readings are collect but one of the readings has a wrong format the following dialog box is displayed to tell the user to enter the correct values.
A system that has all fields correctly filled, the billing system looks like the picture below:-
When the calculate button is clicked the bill is displayed as shown below:-
When the clear button is clicked the system returns to the interface that has no filled area.
Subscribe to:
Posts (Atom)