/*Imports*/ import java.applet.Applet; import java.util.*; import java.awt.*; public class reg extends Applet { /*Declaring Variables*/ TextField name, SSN, c1, c2, c3, c4, c5, course; String courseText; int recNum, z, x, y, matches, paintRecord; Button accept, search; Graphics g; Panel p, p1; Font font; /*Initalizing Variables*/ String[][] record = new String[20][7]; String[][] roll = new String[20][2]; int numberOfRecords = 0; int process = 5; public void init () { /*GUI setup*/ setLayout(new BorderLayout()); Panel p = new Panel(); Panel p1 = new Panel(); p.setLayout(new GridLayout(15, 1)); p.add(new Label("Your Name")); name = new TextField(20); p.add(name); p.add(new Label("SS Number (No dashes!)")); SSN = new TextField(20); p.add(SSN); p.add(new Label("First Class")); c1 = new TextField(20); p.add(c1); p.add(new Label("Second Class")); c2 = new TextField(20); p.add(c2); p.add(new Label("Third Class")); c3 = new TextField(20); p.add(c3); p.add(new Label("Fourth Class")); c4 = new TextField(20); p.add(c4); p.add(new Label("Fifth Class")); c5 = new TextField(20); p.add(c5); Button accept = new Button("Accept"); p.add(accept); p1.setLayout(new GridLayout(1,3)); p1.add(new Label("List students in class")); course = new TextField(20); p1.add(course); Button search = new Button("Search"); p1.add(search); add("West", p); add("South", p1); /*Intialzing of record fields*/ for (x = 0; x <= 19; x++) { for (y = 0; y <= 6; y++) { record[x][y] = ""; } } process = 5; repaint(); } public boolean action(Event e, Object arg) { /*If the accept button is pressed...*/ if (arg == "Accept") { checkEntry(); } /*If the search button is pressed...*/ if (arg == "Search") { searchRoll(); } return super.action(e, arg); } public void checkEntry() { /*Intialzing local variables*/ String nameCompare, SSNCompare; boolean foundRecord = false; /*Pulls the name that is entered in the GUI and checks to make sure it's valid*/ String compare = name.getText(); if (compare.length() == 0) { process = 1; repaint(); } else { /*Pulls the SSN that is entered in the GUI and checks to make sure it's valid*/ compare = SSN.getText(); if (compare.length() != 9 || intFromTextField(SSN) == 0) { process = 2; repaint(); } else { /*Pulls the first class that is entered in the GUI and checks to make sure it's valid*/ compare = c1.getText(); if (compare.length() == 0) { process = 3; repaint(); } else { /*Enters record if this is the first student to register.*/ if (numberOfRecords == 0) { addRecord(); } else { /*Sets local variables to check for occurance of SSN and name in previous records*/ nameCompare = name.getText(); SSNCompare = SSN.getText(); foundRecord = false; /*Looks through all previous records to find occurance of SSN and name*/ for (z = 0; z <= (numberOfRecords - 1); z++) { if (nameCompare.equalsIgnoreCase(record[z][0]) && SSNCompare.equalsIgnoreCase(record[z][1])) { recNum = z; foundRecord = true; replaceRecord(); /*If match is found, goes to replace record with new classes*/ } } /*If no match is found, new record added*/ if (foundRecord == false) { addRecord(); } } /*Prints out confrimation screen*/ process = 4; repaint(); } } } } public void addRecord() { /*Adds new record to table*/ record[numberOfRecords][0] = name.getText(); record[numberOfRecords][1] = SSN.getText(); record[numberOfRecords][2] = c1.getText(); /*Makes sure there is a class entered before entering the field into the record*/ String compare = c2.getText(); if (compare.length() != 0) { record[numberOfRecords][3] = c2.getText(); compare = c3.getText(); if (compare.length() != 0) { record[numberOfRecords][4] = c3.getText(); compare = c4.getText(); if (compare.length() != 0) { record[numberOfRecords][5] = c4.getText(); compare = c5.getText(); if (compare.length() != 0) { record[numberOfRecords][6] = c5.getText(); } } } } /*Sets a variable for the printing of the screen and increases the number of records talley*/ paintRecord = numberOfRecords; numberOfRecords++; } public void replaceRecord() { /*Replaces the duplicate record with the new information*/ record[recNum][0] = name.getText(); record[recNum][1] = SSN.getText(); record[recNum][2] = c1.getText(); /*If field is empty, makes sure that record field is empty also*/ String compare = c2.getText(); if (compare.length() != 0) { record[recNum][3] = c2.getText(); } else { record[recNum][3] = ""; } compare = c3.getText(); if (compare.length() != 0) { record[recNum][4] = c3.getText(); } else { record[recNum][4] = ""; } compare = c4.getText(); if (compare.length() != 0) { record[recNum][5] = c4.getText(); } else { record[recNum][5] = ""; } compare = c5.getText(); if (compare.length() != 0) { record[recNum][6] = c5.getText(); } else { record[recNum][6] = ""; } /*Set variable for the paint routine*/ paintRecord = recNum; } public void searchRoll() { /*Intializes local varialbe*/ matches = 0; /*Pulls class from GUI to search for*/ courseText = course.getText(); /*Begins search in all the records for the class*/ for (x = 0; x <= numberOfRecords - 1; x++) { /*Searches all the class fields in each record for the class*/ for (y = 2; y <= 6; y++) { if (courseText.equalsIgnoreCase(record[x][y])) { /*If match is found, pulls student name and SSN and stores it to an array*/ roll[matches][0] = record[x][0]; roll[matches][1] = record[x][1]; matches++; } } } /*Prints search results*/ process = 6; repaint(); } public void paint(Graphics g) { g.setFont(new Font("TimesRoman", Font.BOLD, 20)); if (process == 1) { /*Error message for no name*/ g.drawString("You must give a name.", 200, 50); } if (process == 2) { /*Error message for no SSN*/ g.drawString("You must give a valid SSN.", 200, 50); } if (process == 3) { /*Error message for no classes entered*/ g.drawString("You must enter at least one class.", 200, 50); } if (process == 4) { /*Confrimation message for entering in classes*/ g.drawString("Thank you " + record[numberOfRecords - 1][0], 200, 50); g.drawString("You are registered for:", 200, 80); g.drawString(record[paintRecord][2], 200, 100); /*Checks class fields to make sure they are not empty before they print out*/ if (record[paintRecord][3] != null) { g.drawString(record[paintRecord][3], 200, 120); if (record[paintRecord][4] != null) { g.drawString(record[paintRecord][4], 200, 140); if (record[paintRecord][5] != null) { g.drawString(record[paintRecord][5], 200, 160); if (record[paintRecord][6] != null) { g.drawString(record[paintRecord][6], 200, 180); } } } } } if (process == 5) { /*Welcome screen*/ g.drawString("Enter your name, SSN, ", 250, 200); g.drawString("and your class choices.", 250, 220); } if (process == 6) { /*List of students in a class*/ g.drawString("Students in class " + courseText + " :", 250, 60); for (x = 0; x <= matches - 1; x++) { g.drawString(roll[x][0] + " " + roll[x][1], 250, (100 + (x * 20))); } } } /*Subroutine for pulling intergers from textfields*/ int intFromTextField(TextField tf) { String TextforConversion; int IntegerReturned; TextforConversion = tf.getText(); try { IntegerReturned = Integer.parseInt(TextforConversion); } catch (Exception e) { IntegerReturned = 0; } return IntegerReturned; } }