0% found this document useful (0 votes)
9 views10 pages

ABDULKARIM

Assignment

Uploaded by

patrickemil16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

ABDULKARIM

Assignment

Uploaded by

patrickemil16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIVERSITY OF DAR ES SALAAM

COLLEGE OF INFORMATION AND COMMUNICATION TECHNOLOGY


(CoICT)

STUDENT’S NAME: ABDULKARIM M. BANGORI.

REGISTRATION NO. 2023-04-00747

PROGRAMME: BSc. ELECTRONICS ENGINEERING

COURSE CODE: CS 175

COURSE NAME: PROGRAMMING IN JAVA

INSTRUCTOR’S NAME: DR. JUMA LUNGO

TASK: ASSIGNMENT 3

DATE OF SUBMISSION: 27 JUNE 2024

QUESTION: Develop a Java Swing program to perform simple

registration.
PART A: CODES

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class RegistrationForm extends JFrame {

private final JTextField fullNameField;


private final JTextField birthYearField;
private final JPasswordField passwordField;
private final JPasswordField confirmPasswordField;
private final JComboBox<String> birthMonthCombo;
private final JComboBox<String> genderCombo;
private final JComboBox<String> countryCombo;
private final JButton saveButton;
private final JButton resetButton;
private final JButton exitButton;

public RegistrationForm() {
setTitle("Registration Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 350);
setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();


gbc.insets = new Insets(5, 5, 5, 5);

JLabel titleLabel = new JLabel("Registration Form");

1
titleLabel.setFont(new Font("Serif", Font.BOLD, 20));

gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
gbc.anchor = GridBagConstraints.CENTER;
add(titleLabel, gbc);

gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.gridx = 0;
gbc.gridy = 1;
add(new JLabel("Full Name:"), gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.LINE_START;
add(fullNameField = new JTextField(30), gbc);

gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Password:"), gbc);

gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.LINE_START;
add(passwordField = new JPasswordField(15), gbc);

gbc.gridx = 2;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Confirm Password:"), gbc);

gbc.gridx = 3;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.LINE_START;
add(confirmPasswordField = new JPasswordField(15), gbc);

gbc.gridx = 0;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Birth Month:"), gbc);

gbc.gridx = 1;

2
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.LINE_START;
add(birthMonthCombo = new JComboBox<>(new String[]{"NotSelected",
"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"}), gbc);

gbc.gridx = 2;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Birth Year:"), gbc);

gbc.gridx = 3;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.LINE_START;
add(birthYearField = new JTextField(6), gbc);

gbc.gridx = 0;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Gender:"), gbc);

gbc.gridx = 1;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_START;
add(genderCombo = new JComboBox<>(new String[]{"NotSelected",
"Male", "Female"}), gbc);

gbc.gridx = 2;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Country:"), gbc);

gbc.gridx = 3;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_START;
add(countryCombo = new JComboBox<>(new String[]{"NotSelected",
"Kenya", "Tanzania", "Uganda", "Rwanda", "Burundi", "South Sudan"}),
gbc);

gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.CENTER;
add(saveButton = new JButton("Save"), gbc);

3
gbc.gridx = 1;
gbc.gridy = 5;
add(resetButton = new JButton("Reset"), gbc);

gbc.gridx = 2;
gbc.gridy = 5;
add(exitButton = new JButton("Exit"), gbc);

saveButton.addActionListener(new SaveButtonListener());
resetButton.addActionListener(e -> resetFields());
exitButton.addActionListener(e -> System.exit(0));

setVisible(true);
}

private void resetFields() {


fullNameField.setText("");
passwordField.setText("");
confirmPasswordField.setText("");
birthYearField.setText("");
birthMonthCombo.setSelectedIndex(0);
genderCombo.setSelectedIndex(0);
countryCombo.setSelectedIndex(0);
}

private class SaveButtonListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
String fullName = fullNameField.getText();
String password = new String(passwordField.getPassword());
String confirmPassword = new
String(confirmPasswordField.getPassword());
String birthYearStr = birthYearField.getText();
String birthMonth = (String)
birthMonthCombo.getSelectedItem();
String gender = (String) genderCombo.getSelectedItem();
String country = (String) countryCombo.getSelectedItem();

if (!fullName.matches("\\w+ \\w+ \\w+")) {


showError("Full name must be in 'FirstName Middlename
Surname' format.");
return;
}
if (password.length() < 8) {

4
showError("Password must be at least 8 characters
long.");
return;
}
if (!password.equals(confirmPassword)) {
showError("Passwords do not match.");
return;
}
int birthYear;
try {
birthYear = Integer.parseInt(birthYearStr);
if (birthYear < 2005 || birthYear > 2010) {
showError("Birth Year must be between 2005 and
2010.");
return;
}
} catch (NumberFormatException ex) {
showError("Birth Year must be a valid number.");
return;
}
if (birthMonth.equals("NotSelected") ||
gender.equals("NotSelected") || country.equals("NotSelected")) {
showError("Please make valid selections in all combo
boxes.");
return;
}

int age = 2024 - birthYear;


try (BufferedWriter writer = new BufferedWriter(new
FileWriter("RegInfo.txt"))) {
writer.write("Fullname: " + fullName + "\n");
writer.write("Password: " + password + "\n");
writer.write("Birth Month: " + birthMonth + "\n");
writer.write("Birth Year: " + birthYear + "\n");
writer.write("Age (yrs): " + age + "\n");
writer.write("Gender: " + gender + "\n");
writer.write("Country: " + country + "\n");
} catch (IOException ex) {
showError("Error saving registration information.");
return;
}

showInfo("Fullname: " + fullName + "\nPassword: " + password


+ "\nBirth Month: " + birthMonth + "\nBirth Year: " + birthYear + "\nAge
(yrs): " + age + "\nGender: " + gender + "\nCountry: " + country);

5
}

private void showError(String message) {


JOptionPane.showMessageDialog(RegistrationForm.this, message,
"Error", JOptionPane.ERROR_MESSAGE);
}

private void showInfo(String message) {


JTextArea textArea = new JTextArea(message);
textArea.setFont(new Font("Serif", Font.BOLD, 28));
JOptionPane.showMessageDialog(RegistrationForm.this,
textArea, "Registered Information", JOptionPane.INFORMATION_MESSAGE);
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(RegistrationForm::new);
}
}

PART B: OUTPUT

6
FILLING THE FORM

7
THE RESULTED OUTPUT

WHEN CREDENTIALS ARE WRONG

8
9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy