Java Certification, Programming, JavaBean and Object Oriented Reference Books
Exercise to simulate a safe

exercisetosimulateasafe.java

// excercise to simulate a safe

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 

public class Mysafe extends Applet implements ActionListener
{
 private Button button[];
 private TextField text;
 private Font f;
 private Label label;
 private String LockCode;
 private String buffer="    ";//4 spaces
 private String CodeSelection[];
 private Panel panel1, panel2;
 private static int resetCount=0;
 private int numberTries=6; // to permanetley lock the safe if this is exceeded
 
 public void init()
 {
  // setBackground(Color.lightGray);
  this.setLayout(new BorderLayout());
 
  CodeSelection = new String[]{"5567","1234","2727","0123"};
  //initialise the Lock Code of the safe to the first code selection
  LockCode = new String(CodeSelection[0]);
 
  panel1 = new Panel();
  add("North", panel1);
  label = new Label("    CLOSED");
  f=new Font("Courier",Font.BOLD,18);
  label.setFont(f);
 
  panel1.add(label); // add label to pane
  label.setAlignment(Label.LEFT);// set label to the left
 
  //text field
  text=new TextField(25);
  panel1.add(text);
 
  panel2 = new Panel();
  add("Center",panel2);
 
  //set gridLayout for panel2(int rows,int col,int hgap,int vgap)
  panel2.setLayout(new GridLayout(4,2,5,5));
 
  button = new Button[13];
 
  for (int i=0;i<10;i+=1)
  {
   // array buttons
   button[i] = new Button(Integer.toString(i));
   panel2.add(button[i]);
   button[i].addActionListener(this);
  }
 
  button[10] = new Button("CLOSE");
  button[10].setForeground(Color.red);
  button[10].addActionListener(this);
 
  button[11] = new Button("OPEN");
  button[11].setForeground(Color.red);
  button[11].addActionListener(this);
 
  button[12] = new Button("NEW CODE");
  button[12].setForeground(Color.red);
  button[12].addActionListener(this);
 
  // add buttons to the layout
  panel2.add(button[10]);
  panel2.add(button[11]);
  panel2.add(button[12]);
 }
 
 //action
 public void actionPerformed(ActionEvent e)
 {
  //checks if the button is pressed
  String a,currentNumber;
  // create a String buffer to manipulate the string
  StringBuffer found = new StringBuffer(buffer);
  int j,k;
  for(j=0;j<10;j+=1)
  {
   if(e.getSource()==button[j])
   {
    // get the string representationof the pressed button number
    currentNumber=button[j].getLabel(); // JApplet uses getText() method
 
    for(k=0;k<4;k+=1) // cycle through the four numbers in the code
    {
     // check to see if the button pressed matches a number in the code
 
     if(currentNumber.equals(String.valueOf(LockCode.charAt(k))))
     {
      //replace method to insert the matched number
      found.replace(k,k+1,currentNumber);
     }
    }
    numberTries-=1; // decrement the number of tries
    buffer = new String(found);
    text.setText(buffer);
 
    if(numberTries<1) // no more tries
    {
     label.setText("Alarm!Locked!");
     text.setText("LOCKSMITH - RESET!!");
    }
   }
  }
 
  if(e.getSource()==button[10]) //close
  {
   label.setText("CLOSED");
   buffer="    ";
   text.setText(buffer); // set text to buffer
  }
 
  if(e.getSource()==button[11])//open
  {
   buffer="    ";
   if(LockCode.equals(text.getText()))
   {
    label.setText("SAFE OPEN");
    text.setText(buffer);
    numberTries=6; // new tries
   }
   else
   {
    numberTries-=1;
    text.setText(buffer);
    if(numberTries<1) // no more tries
    {
     label.setText("ALARM!LOCKED!");
     text.setText("LOCKSMITH- RESET");
    }
   }
  }
 
  if(e.getSource()==button[12]) // new code
  {
   resetCount+=1; // moves to the next safe code
   //cycling through the available safe combinatoins in the code selection array
   LockCode=CodeSelection[resetCount%4];
   numberTries=6;
   buffer="    ";
   text.setText(buffer);
   label.setText("CLOSED");
  }
 }
}//end

Return to : Java Programming Hints and Tips