Open House Password

Open House Password

This applet is the starter code designed for Open House at St Francis. A guest types their name and a password based upon their name for a prize. The guest can inspect the code as a hint.

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

import javax.swing.*;
public class OpenHouse extends Applet implements ActionListener{
    TextArea t;
    Button clearButton, level1Button,level2Button,level3Button,level4Button;
    String nm,pw;
    JTextField n=new JTextField("");
    JTextField p=new JTextField("");

    public void init(){
        t=new TextArea();
        t.setFont(new Font("Courier", Font.BOLD,20));
        t.setForeground(new Color(251,182,0));
        t.setBackground(new Color(44,23,0));
        t.setEditable(true);

        level1Button=new Button("Level 1");
        level1Button.addActionListener(this);
        level2Button=new Button("Level 2");
        level2Button.addActionListener(this);
        level3Button=new Button("Level 3");
        level3Button.addActionListener(this);
        level4Button=new Button("Level 4");
        level4Button.addActionListener(this);
        clearButton=new Button("Clear");
        clearButton.addActionListener(this);
        this.setLayout(new BorderLayout());
        this.add(t, BorderLayout.CENTER);
        Panel buttonPanel=new Panel(new FlowLayout());
        buttonPanel.add(level1Button);
        buttonPanel.add(level2Button);
        buttonPanel.add(level3Button);
        buttonPanel.add(level4Button);
        buttonPanel.add(clearButton);

        nm="";
        pw="";

        this.add(buttonPanel, BorderLayout.NORTH);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        Object buttonPressed=e.getSource();
        if (buttonPressed==clearButton){

            t.setText("");

        } else if (buttonPressed==level1Button) {

            t.append("Level 1:\n");
            doDialog("Level 1");
            if (level1(nm).equals(pw) ){
                t.append("\nCorrect! Level 1 Unlocked!\n");
            } else {
                t.append("\nSorry, try again.\n");
            }

        } else if (buttonPressed==level2Button) {
            doDialog("Level 2");
            if (level2(nm).equals(pw) ){
                t.append("\nCorrect! Level 2 Unlocked!\n");
            } else {
                t.append("\nSorry, try again.\n");
            }

        }else if (buttonPressed==level3Button) {
            doDialog("Level Patrick");
            if (levelPatrick(nm).equals(pw) ){
                t.append("\nCorrect! Level Patrick Unlocked!\n");
            } else {
                t.append("\nSorry, try again.\n");
            }

        }else if (buttonPressed==level4Button) {
            doDialog("Level James");
            if (level4(nm).equals(pw) ){
                t.append("\nCorrect! Level James Unlocked!\n");
            } else {
                t.append("\nSorry, try again.\n");
            }

        }

    }

    public void doDialog(String prompt){

        final JComponent[] order = new JComponent[] {
                new JLabel("name: "),
                n,
                new JLabel("password: "),
                p,

        };
        JOptionPane.showMessageDialog(this, order, prompt, JOptionPane.PLAIN_MESSAGE);
        try{
         nm=  n.getText().toUpperCase();
         pw = p.getText().toUpperCase();
         t.append("\nName: "+nm+"\nPassword: "+pw);
        }catch( Exception exception){
          t.append("\nHuh? try again");
        }
    }

    public String level1(String name)
    {
        return 2*name.length()+"";
    }
    public String level2(String name)
    {
        String alpha="ABCDEFGHIJKLMNOPQURSTUVWXYZA";
        String result="";
        for (int i=0;i<name.length(); i++)
        {
            int x= alpha.indexOf(name.substring(i,i+1));
            result+=alpha.substring(x+1,x+2);
        }
        return result;
    }
    public String levelPatrick(String name)
    {
        //what do you thint % means in java code?
        int answer= name.length() % 4;    
        return answer + "";
        //hint 1: it does not mean percent
        //hint 2: Patrick=3 and Chris=1
    }
    public String level4(String name)
    {
        return "GO_KNIGHTS";
    }
}