Sei sulla pagina 1di 6

/** Juliet Akinkugbe

* JAVA Project- SmartPhone Database


*
*/
package COMMON_SMPHONEDB;
import java.awt.EventQueue;
import
import
import
import
import

javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JOptionPane;
javax.swing.JTextField;
javax.swing.JButton;

import org.apache.commons.lang3.Validate;
import
import
import
import

java.awt.event.MouseAdapter;
java.awt.event.MouseEvent;
java.io.File;
java.io.IOException;

public class CreateSmphoneDb {


private
private
private
private
private
private
private
private

JFrame frmCreateSmartPhone;
JTextField textFieldSerialNumber;
JTextField textFieldManufacturerModel;
JTextField textFieldScreen;
JTextField textFieldSize;
JTextField textFieldCPU;
JTextField textFieldMemory;
JTextField textFieldWireless;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CreateSmphoneDb window = new CreateSmpho
neDb();
window.frmCreateSmartPhone.setVisible(tr
ue);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CreateSmphoneDb() {
initialize();
}
/**

* Initialize the contents of the frame.


*/
private void initialize() {
frmCreateSmartPhone = new JFrame();
frmCreateSmartPhone.setTitle("Create Smart Phone Database");
frmCreateSmartPhone.setBounds(100, 100, 414, 462);
frmCreateSmartPhone.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
E);
frmCreateSmartPhone.getContentPane().setLayout(null);
JLabel lblSerialNumber = new JLabel("Serial Number");
lblSerialNumber.setBounds(42, 28, 83, 31);
frmCreateSmartPhone.getContentPane().add(lblSerialNumber);
JLabel lblManufactureModel = new JLabel("Manufacturer & Model");
lblManufactureModel.setBounds(42, 75, 178, 14);
frmCreateSmartPhone.getContentPane().add(lblManufactureModel);
JLabel lblScreen = new JLabel("Screen");
lblScreen.setBounds(42, 115, 46, 14);
frmCreateSmartPhone.getContentPane().add(lblScreen);
JLabel lblSize = new JLabel("Size");
lblSize.setBounds(42, 155, 46, 14);
frmCreateSmartPhone.getContentPane().add(lblSize);
JLabel lblCpu = new JLabel("CPU");
lblCpu.setBounds(42, 196, 46, 14);
frmCreateSmartPhone.getContentPane().add(lblCpu);
JLabel lblMemory = new JLabel("Memory");
lblMemory.setBounds(42, 235, 98, 14);
frmCreateSmartPhone.getContentPane().add(lblMemory);
JLabel lblWireless = new JLabel("Wireless");
lblWireless.setBounds(42, 278, 98, 14);
frmCreateSmartPhone.getContentPane().add(lblWireless);
textFieldSerialNumber = new JTextField();
textFieldSerialNumber.setBounds(230, 33, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldSerialNumber);
textFieldSerialNumber.setColumns(10);
textFieldManufacturerModel = new JTextField();
textFieldManufacturerModel.setColumns(10);
textFieldManufacturerModel.setBounds(230, 72, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldManufacturerMo
del);
textFieldScreen = new JTextField();
textFieldScreen.setColumns(10);
textFieldScreen.setBounds(230, 112, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldScreen);
textFieldSize = new JTextField();
textFieldSize.setColumns(10);
textFieldSize.setBounds(230, 152, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldSize);
textFieldCPU = new JTextField();

textFieldCPU.setColumns(10);
textFieldCPU.setBounds(230, 193, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldCPU);
textFieldMemory = new JTextField();
textFieldMemory.setColumns(10);
textFieldMemory.setBounds(230, 232, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldMemory);
textFieldWireless = new JTextField();
textFieldWireless.setColumns(10);
textFieldWireless.setBounds(230, 275, 143, 20);
frmCreateSmartPhone.getContentPane().add(textFieldWireless);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
btnSubmit_CLICK();
}
catch (Exception arg0){
System.out.println(arg0.toString());
}
}
});
btnSubmit.setBounds(107, 339, 138, 31);
frmCreateSmartPhone.getContentPane().add(btnSubmit);
}
private void btnSubmit_CLICK() {
//-------------- First, validate all the text fields
// If any problem, a dialog warning pops up to stop the program
boolean isValidated = validateTextFields();
if (! isValidated) return;
//-------------- All the text fields have been validated
FileIO fileIOHandler = new FileIO();
// Declare output file database: bookDatabase.txt
// MUST use \ to qualify '\' in the path of the file
File outputFile = new File("H:\\\\JAVA\\OUTPUTS\\SMPhoneDatabase
.txt");
String strSerialNumber = textFieldSerialNumber.getText();
long SerialNumber = Long.parseLong(strSerialNumber);
String strManufacturerModel = textFieldManufacturerModel.getText
();
String
String
String
String
String

strScreen = textFieldScreen.getText();
strSize = textFieldSize.getText();
strCPU = textFieldCPU.getText();
strMemory = textFieldMemory.getText();
strWireless = textFieldWireless.getText();

// Create a Book object


SMPHONE aSMPHONE = new SMPHONE (SerialNumber, strManufacturerMod
el);

aSMPHONE.setScreen(strScreen);
aSMPHONE.setSize(strSize);
aSMPHONE.setCPU(strCPU);
aSMPHONE.setMemory(strMemory);
aSMPHONE.setWireless(strWireless);
// Get the string of book data
String strSMPHONEInfo = aSMPHONE.toString();
try {
// Write the string to the book database file
// by adding a line to the file
fileIOHandler.appendOneLineToFile(outputFile, strSMPHONE
Info);
}
catch (IOException ex){
ex.printStackTrace();
}
// After successfully inserting a new book record to the databas
e
// refresh all the text fields to prepare for the next record
textFieldSerialNumber.setText("");
textFieldManufacturerModel.setText("");
textFieldScreen.setText("");
textFieldSize.setText("");
textFieldCPU.setText("");
textFieldMemory.setText("");
textFieldWireless.setText("");
} // End of btnSubmit_CLICK
private boolean validateTextFields() {
boolean isValidated = true;
//----------- Validate ISBN text field
try{
Validate.notBlank(textFieldSerialNumber.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Serial Number must have a Numeric Value of 10 D
igits.");
textFieldSerialNumber.requestFocusInWindow(); // make it ready t
o enter the value
textFieldSerialNumber.selectAll(); // select all text in the tex
t field to delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
// For ISBN, also need to verify the entered value is a valid numeric
try{
long tempLong = Long.parseLong(textFieldSerialNumber.getText());
}catch(Exception e){

JOptionPane.showMessageDialog(frmCreateSmartPhone, "Serial Numbe


r must have a Numeric Value.");
textFieldSerialNumber.requestFocusInWindow(); // make it ready t
o enter the value
textFieldSerialNumber.selectAll(); // select all text in the tex
t field to delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
//----------- Validate Title text field
try{
Validate.notBlank(textFieldManufacturerModel.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Manufacturer and Model cannot be blank !!!.");
textFieldManufacturerModel.requestFocusInWindow(); // make it re
ady to enter the value
textFieldManufacturerModel.selectAll(); // select all text in th
e text field to delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
//----------- Validate Author text field
try{
Validate.notBlank(textFieldScreen.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Screen cannot be blank !!!.");
textFieldScreen.requestFocusInWindow(); // make it ready to ente
r the value
textFieldScreen.selectAll(); // select all text in the text fiel
d to delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
//----------- Validate Publisher text field
try{
Validate.notBlank(textFieldSize.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Size cannot be blank !!!.");
textFieldSize.requestFocusInWindow(); // make it ready to enter
the value
textFieldSize.selectAll(); // select all text in the text field
to delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
//----------- Validate MonthYear text field

try{
Validate.notBlank(textFieldCPU.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - CPU cannot be blank !!!.");
textFieldCPU.requestFocusInWindow(); // make it ready to enter t
he value
textFieldCPU.selectAll(); // select all text in the text field t
o delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
//----------- Validate Edition text field
try{
Validate.notBlank(textFieldMemory.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Memory cannot be blank !!!.");
textFieldMemory.requestFocusInWindow(); // make it ready to ente
r the value
textFieldMemory.selectAll(); // select all text in the text fiel
d to delete it or to replace it
isValidated = false;
}
if (! isValidated) return (isValidated);
//----------- Validate Edition text field
try{
Validate.notBlank(textFieldWireless.getText());
}catch(Exception e){
JOptionPane.showMessageDialog(frmCreateSmartPhone, "All the text
fields must have valid values - Wireless cannot be blank !!!.");
textFieldWireless.requestFocusInWindow(); // make it ready to en
ter the value
textFieldWireless.selectAll(); // select all text in the text fi
eld to delete it or to replace it
isValidated = false;
}

return (isValidated);
} // validateTextFields
}

Potrebbero piacerti anche