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

NewAccount Java main

The document is a Java class named 'NewAccount' that creates a GUI for adding new student accounts in a management system. It includes fields for student information, input validation, and functionality to save the data to a file. The class handles user actions such as saving or canceling the entry and ensures that the input adheres to specific formats and constraints.

Uploaded by

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

NewAccount Java main

The document is a Java class named 'NewAccount' that creates a GUI for adding new student accounts in a management system. It includes fields for student information, input validation, and functionality to save the data to a file. The class handles user actions such as saving or canceling the entry and ensures that the input adheres to specific formats and constraints.

Uploaded by

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

import java.awt.

Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class NewAccount extends JInternalFrame implements ActionListener {

private JPanel jpInfo = new JPanel();

private JLabel lbAccountNo, lbStudName, lbStudAdd, lbStudCity, lbStudState,


lbStudContNo, lbDeposit, lbDeposit2, lbDeposit3, lbNotes;
private JTextField txAccountNo, txStudName, txStudAdd, txStudCity,
txStudState, txStudContNo, txDeposit, txDeposit2, txDeposit3, txNotes;
private JButton btnSave, btnCancel;

private int count = 0;


private int rows = 0;
private int total = 0;

// String Type Array use to Load Records From File.


private String records[][] = new String[500][10];

// String Type Array use to Save Records into File.


private String saves[][] = new String[500][10];

private FileInputStream fis;


private DataInputStream dis;

NewAccount() {

// super(Title, Resizable, Closable, Maximizable, Iconifiable)


super("Add New Student", true, true, true, true);
setSize(500, 500);

jpInfo.setBounds(0, 0, 500, 115);


jpInfo.setLayout(null);
lbAccountNo = new JLabel("Student Number: ");
lbAccountNo.setForeground(Color.black);
lbAccountNo.setBounds(15, 20, 100, 25);

lbStudName = new JLabel("Student Name:");


lbStudName.setForeground(Color.black);
lbStudName.setBounds(15, 55, 100, 25);

lbStudAdd = new JLabel("Address:");


lbStudAdd.setForeground(Color.black);
lbStudAdd.setBounds(15, 90, 100, 25);

lbStudCity = new JLabel("City:");


lbStudCity.setForeground(Color.black);
lbStudCity.setBounds(15, 125, 100, 25);

lbStudState = new JLabel("State:");


lbStudState.setForeground(Color.black);
lbStudState.setBounds(15, 160, 100, 25);

lbStudContNo = new JLabel("Mobile No.:");


lbStudContNo.setForeground(Color.black);
lbStudContNo.setBounds(15, 195, 100, 25);

lbDeposit = new JLabel("Enter Creativity:");


lbDeposit.setForeground(Color.black);
lbDeposit.setBounds(15, 265, 100, 25);

lbDeposit2 = new JLabel("Enter Action:");


lbDeposit2.setForeground(Color.black);
lbDeposit2.setBounds(15, 295, 100, 25);

lbDeposit3 = new JLabel("Enter Service:");


lbDeposit3.setForeground(Color.black);
lbDeposit3.setBounds(15, 325, 100, 25);

lbNotes = new JLabel("Enter Remarks:");


lbNotes.setForeground(Color.black);
lbNotes.setBounds(15, 355, 100, 25);

txAccountNo = new JTextField();


txAccountNo.setHorizontalAlignment(JTextField.RIGHT);
txAccountNo.setBounds(125, 20, 205, 25);
txStudName = new JTextField();
txStudName.setBounds(125, 55, 205, 25);

txStudAdd = new JTextField();


txStudAdd.setBounds(125, 90, 205, 25);
txStudCity = new JTextField();
txStudCity.setBounds(125, 125, 205, 25);

txStudState = new JTextField();


txStudState.setBounds(125, 160, 205, 25);
txStudContNo = new JTextField();
txStudContNo.setBounds(125, 195, 205, 25);

txDeposit = new JTextField();


txDeposit.setHorizontalAlignment(JTextField.RIGHT);
txDeposit.setBounds(125, 265, 205, 25);

txDeposit2 = new JTextField();


txDeposit2.setHorizontalAlignment(JTextField.RIGHT);
txDeposit2.setBounds(125, 295, 205, 25);

txDeposit3 = new JTextField();


txDeposit3.setHorizontalAlignment(JTextField.RIGHT);
txDeposit3.setBounds(125, 325, 205, 25);

txNotes = new JTextField();


txNotes.setBounds(125, 355, 205, 25);

// Restricting The User Input to only Numerics in Student Number Box


// by using method if (!((Character.isDigit(c)

txAccountNo.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE)))) {
getToolkit().beep();
ke.consume();
}
}
});

txDeposit.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE)))) {
getToolkit().beep();
ke.consume();
}
}
});
txDeposit2.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE)))) {
getToolkit().beep();
ke.consume();
}
}
});

txDeposit3.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE)))) {
getToolkit().beep();
ke.consume();
}
}
});

// input validation that restricts user to enter specific number

txDeposit.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char keyChar = ke.getKeyChar();
if(!(ke.getKeyChar() >= '5' && ke.getKeyChar() <= '9')) {
getToolkit().beep();
ke.consume();
}
}
});

txStudContNo.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE)))) {
getToolkit().beep();
ke.consume();
}
}
});

// Restricting The User Input to only Letters/Digits/White spaces in Input Boxes

// accept only Characters, White spaces, BACK_SPACE, or DELETE


// by these methods: if (!(Character.isLetter(c) || Character.isWhitespace(c)
txStudName.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!(Character.isLetter(c) || Character.isWhitespace(c) ||
c == KeyEvent.VK_BACK_SPACE ||
c == KeyEvent.VK_DELETE)){
getToolkit().beep();
ke.consume();
}
}
});

// accept only UPPER CASE LETTERS, White spaces, BACK_SPACE, or DELETE


// by these methods: if (!(Character.isUpperCase(c)

txStudName.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!(Character.isUpperCase(c) || Character.isWhitespace(c) ||
c == KeyEvent.VK_BACK_SPACE ||
c == KeyEvent.VK_DELETE)){
getToolkit().beep();
ke.consume();
}
}
});

txStudCity.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!(Character.isLetter(c) || Character.isWhitespace(c) ||
c == KeyEvent.VK_BACK_SPACE ||
c == KeyEvent.VK_DELETE)){
getToolkit().beep();
ke.consume();
}
}
});

txStudState.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!(Character.isLetter(c) || Character.isWhitespace(c) ||
c == KeyEvent.VK_BACK_SPACE ||
c == KeyEvent.VK_DELETE)){
getToolkit().beep();
ke.consume();
}
}
});

// accept both Characters & Digits, BACK_SPACE, or DELETE


// by using method if (!(Character.isLetterOrDigit(c) ||

txNotes.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!(Character.isLetterOrDigit(c) || Character.isWhitespace(c) ||
c == KeyEvent.VK_BACK_SPACE ||
c == KeyEvent.VK_DELETE)){
getToolkit().beep();
ke.consume();
}
}
});

// Aligning The Buttons.


btnSave = new JButton("Save");
btnSave.setBounds(20, 400, 120, 25);
btnSave.addActionListener(this);
btnCancel = new JButton("Cancel");
btnCancel.setBounds(185, 400, 120, 25);
btnCancel.addActionListener(this);

jpInfo.add(lbAccountNo);
jpInfo.add(txAccountNo);
jpInfo.add(lbStudName);
jpInfo.add(txStudName);
jpInfo.add(lbStudAdd);
jpInfo.add(txStudAdd);
jpInfo.add(lbStudCity);
jpInfo.add(txStudCity);
jpInfo.add(lbStudState);
jpInfo.add(txStudState);
jpInfo.add(lbStudContNo);
jpInfo.add(txStudContNo);

jpInfo.add(lbDeposit);
jpInfo.add(txDeposit);
jpInfo.add(lbDeposit2);
jpInfo.add(txDeposit2);
jpInfo.add(lbDeposit3);
jpInfo.add(txDeposit3);
jpInfo.add(lbNotes);
jpInfo.add(txNotes);
jpInfo.add(btnSave);
jpInfo.add(btnCancel);

// Adding Panel to Window.


getContentPane().add(jpInfo);

// In the End Showing the New Account Window.


setVisible(true);

// Function use By Buttons of Window to Perform Action as User Click Them.


public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if (obj == btnSave) {
if (txAccountNo.getText().equals("")
|| txStudName.getText().equals("")
|| txStudAdd.getText().equals("")
|| txStudCity.getText().equals("")
|| txStudState.getText().equals("")
|| txStudContNo.getText().equals("")
|| txDeposit.getText().equals("")
|| txDeposit2.getText().equals("")
|| txDeposit3.getText().equals("")
|| txNotes.getText().equals("")) {
JOptionPane
.showMessageDialog(
null,
"Entries Empty: Fill in the required entries to Add a new Student!!!!!",
"CAS Management System",
JOptionPane.INFORMATION_MESSAGE);
} else {
populateArray(); // Load All Existing Records in Memory.
findRec(); // Finding if Student No. Already Exists or Not.
}
}
if (obj == btnCancel) {
txtClear();
setVisible(false);
dispose();
}

// Function use to load all Records from File when Application Execute.
void populateArray() {

try {
fis = new FileInputStream("CAS.dat");
dis = new DataInputStream(fis);
// Loop to Populate the Array.
while (true) {
for (int i = 0; i < 10; i++) {
records[rows][i] = dis.readUTF();
}
rows++;
}
} catch (Exception ex) {
total = rows;
if (total == 0) {
} else {
try {
dis.close();
fis.close();
} catch (Exception exp) {
}
}
}

// Function use to Find Record by Matching the Contents of Records Array


// with ID TextBox.
void findRec() {

boolean found = false;


for (int x = 0; x < total; x++) {
if (records[x][0].equals(txAccountNo.getText())) {
found = true;
JOptionPane.showMessageDialog(this,
"Invalid Entries: Student Number " +
txAccountNo.getText()
+ "already exists!!!!!",
"CAS Management System", JOptionPane.PLAIN_MESSAGE);
txtClear();
break;
}
}
if (found == false) {
saveArray();
}

// Function use to add new Element to Array.


void saveArray() {
saves[count][0] = txAccountNo.getText();
saves[count][1] = txStudName.getText();
saves[count][2] = txStudAdd.getText();
saves[count][3] = txStudCity.getText();
saves[count][4] = txStudState.getText();
saves[count][5] = txStudContNo.getText();
saves[count][6] = txDeposit.getText();
saves[count][7] = txDeposit2.getText();
saves[count][8] = txDeposit3.getText();
saves[count][9] = txNotes.getText();
saveFile(); // Save This Array to File.
count++;

// Function use to Save new Record to the File.


void saveFile() {
try {
FileOutputStream fos = new FileOutputStream("CAS.dat", true);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF(saves[count][0]);
dos.writeUTF(saves[count][1]);
dos.writeUTF(saves[count][2]);
dos.writeUTF(saves[count][3]);
dos.writeUTF(saves[count][4]);
dos.writeUTF(saves[count][5]);
dos.writeUTF(saves[count][6]);
dos.writeUTF(saves[count][7]);
dos.writeUTF(saves[count][8]);
dos.writeUTF(saves[count][9]);

JOptionPane.showMessageDialog(this,
"Record Saved: New Entry saved Successfully!!!!!",
"CAS Management System", JOptionPane.PLAIN_MESSAGE);
txtClear();
dos.close();
fos.close();
} catch (IOException ioe) {
}

// Function use to Clear all TextFields of Window.


void txtClear() {
txAccountNo.setText("");
txStudName.setText("");
txStudAdd.setText("");
txStudCity.setText("");
txStudState.setText("");
txStudContNo.setText("");
txDeposit.setText("");
txDeposit2.setText("");
txDeposit3.setText("");
txNotes.setText("");
txAccountNo.requestFocus();

}
}

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