Program to
create a safe with 4 digit codes
programtocreateasafewith4digitcodes.java
// program to create a safe with 4 digit codes
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class Safe extends JApplet implements ActionListener
{
private JButton button[];
private TextField code;
private Label label;
private int comb;
private int number;
private Panel panel1, panel2;
private String status;
private String entered;
public void init()
{
Container cp = getContentPane();
panel1 = new Panel();
cp.add(panel1,"North");
label = new Label("CLOSED");
panel1.add(label); // add the label to the pane
label.setAlignment(Label.LEFT); // set label label to
the left
//text field
code = new TextField(20);
//code.setEditable(false);
panel1.add(code);
panel2 = new Panel();
cp.add(panel2,"Center");
// set grid layout for panel2
panel2.setLayout(new GridLayout(4,3,5,5));
button = new JButton[13];
int i=0;
for (i=0;i<10;i+=1)
{
// array buttons
button[i] = new JButton(Integer.toString(i));
panel2.add(button[i]);
button[i].addActionListener(this);
}
button[10] = new JButton("CLOSE");
button[10] .setForeground(Color.red);
button[10].addActionListener(this);
button[11] = new JButton("OPEN");
button[11] .setForeground(Color.red);
button[11].addActionListener(this);
button[12] = new JButton("NEW CODE");
button[12] .setForeground(Color.red);
button[12].addActionListener(this);
// add remaining buttons to the layout
panel2.add(button[10]);
panel2.add(button[11]);
panel2.add(button[12]);
} // end the naming and positioning of the buttons on the screen
public void actionPerformed(ActionEvent e)
{
// checks if the buttons are pressed
String a;
int num1,num2,num3,num4;
int j;
int tries = 6;
int total = 0;
String passcode = "1234";
String entered="";
int n;
num2 = 1000;
num3=0;
if (total<4)
{
for (j=0;j<10;j+=1)
{
if (e.getSource()==button[10])
{
num1=j;
num3=num1*num2+num3;
num2=num2/10;
total+=1;
a=Integer.toString(num3);
code.setText(a);
}
}
entered=Integer.toString(num3);
code.setText(entered);
}
else
{
label = new Label("LOCKED AND ALARMED");
panel1.add(label); // add the label to the pane
tries = 4;
code.setText(entered);
}
if(e.getSource()==button[10])
{
label = new Label("CLOSED");
panel1.add(label); // add the label to the pane
passcode = passcode;
total =0;
}
if(e.getSource()==button[11])
{
if (tries>0)
{
if ( entered == passcode )
{
label = new Label("OPEN");
panel1.add(label); // add the label to the
pane
tries = 6;
total = 0;
}
else
{
tries = tries - 1;
code.setText("Incorrect code");
label = new Label("CLOSED");
panel1.add(label); // add the label to the
pane
total = 0;
}
}
else
{
label = new Label("ALARMED AND LOCKED");
panel1.add(label); // add the label to the pane
code.setText("Enter new code");
}
}
if(e.getSource()==button[12]);
{
entered="";
total = 0;
while (total<4)
{
for (j=0;j<10;j+=1)
{
if (e.getSource()==button[10])
{
num1=j;
num3=num1*num2+num3;
num2=num2/10;
total+=1;
}
}
entered=Integer.toString(num3);
code.setText(entered);
}
passcode = entered;
tries = 6;
total = 0;
label = new Label("CLOSED");
panel1.add(label); // add the label to the pane
}
}//ends action performed
}// ends
Related:
Do you have a Java Problem?
Ask It in The Java
Forum
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|