Program functionality description written in a way closest to the written language (with stricter rules):
Create a window with field group data: "Forename", "Surname", "Birthday" in format "YYYY-mm-dd", "City" and "Country". When the userclicks on the "Save" button the program stores the data into "Users" table in the "Listings" database on the "localhost" database server. If $"database query result" was true, the program displays a message "Data has been saved.", and if notthen "There was an error: " and $"last database error".
Simplified program functionality description:
Create window(data)("Forename" "Surname" "Birthday"(format:"YYYY-mm-dd")"City" "Country" )
Userclick button("Save")store (data) database server("localhost")database("Listings") table("Users")
If $"database query result" true display "Data has been saved."
If$"database query result" falsedisplay "There was an error: " + $"last database error"
Identifying various parts for the program design:
Create(action) window(object) Forename(parameter) Surname(parameter) Birthday(parameter,format:"YYYY-mm-dd"), City(parameter), Country(parameter)
User(user) enter(action) Forename(parameter) Surname(parameter) Birthday(parameter), City(parameter), Country(parameter)
User(user) click(action) button(Save)(object)
Store(action) localhost(object) Listings(object) Users(object) Forename(parameter) Surname(parameter) Birthday(parameter), City(parameter), Country(parameter)
$"database query result"(parameter) switch(process)
(
( display(action) "Data has been saved."(parameter) )(case TRUE sequence)
( display(action) "There was an error: "(parameter) $"last database result"(parameter) )(case FALSE sequence)
)
Transformation into a script:
{
1a. Create: window
1b. Add: field "Forename", field "Surname", field "Birthday", field "City", field "Country"
1c. Set: field "Birthday" pattern"YYYY-mm-dd"
2a. Add: button "Save"
2b. Set: button "Save" click event{
2b1. Connect:database server "localhost"
2b2. Use:database "Listings"
2b3. Query: INSERT INTO "Users" VALUES ( NULL, field "Forename" value, field "Surname" value, field "Birthday" value, field "City" value, field "Country" value )
2b5. Disconnect:database server
2b4. return:$"database query result" @resultA
}
3a. Process: switch@resultA {
3a1. Case: TRUE{
3a1a. Create: message= "Data has been saved."
}
3a2. Case: FALSE{
3a2a. Create: message= "There was an error: " + $"last database error"
}
}
}
Script without numbering:
{
Create: window
Add: field "Forename", field "Surname", field "Birthday", field "City", field "Country"
Set: field "Birthday" pattern"YYYY-mm-dd"
Add: button "Save"
Set: button "Save" click event{
Connect:database server "localhost"
Use:database "Listings"
Query: INSERT INTO "Users" VALUES ( NULL, field "Forename" value, field "Surname" value, field "Birthday" value, field "City" value, field "Country" value )
Disconnect:database server
return:$"database query result" @resultA
}
Process: switch@resultA {
Case: TRUE{
Create: message= "Data has been saved."
}
Case: FALSE{
Create: message= "There was an error: " + $"last database error"
}
}
}
Transformation into source code:
package spoken;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
publicclass Spoken extends JFrame {
publicstatic JTextField Forename, Surname, Birthday, City, Country;
publicstatic JButton Save;
public Spoken() {
super();
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
JLabel ForenameLabel = new JLabel("Forename");
Forename = new JTextField();
JLabel SurnameLabel = new JLabel("Surname");
Surname = new JTextField();
JLabel BirthdayLabel = new JLabel("Birthday");
Birthday = new JTextField();
JLabel CityLabel = new JLabel("City");
City = new JTextField();
JLabel CountryLabel = new JLabel("Country");
Country = new JTextField();
Birthday.addCaretListener(new CaretListener() {
@Override
publicvoid caretUpdate(CaretEvent e) {
if ( Birthday.getText().equals("") ) {
Birthday.setBackground(Color.WHITE);
} else {
try {
(new SimpleDateFormat("yyyy-MM-dd")).parse( Birthday.getText() );
System.out.println("Validation passed for " + Birthday.getText());
Birthday.setBackground(Color.WHITE);
} catch (Exception ex) {
System.out.println("Validation failed for " + Birthday.getText());
Birthday.setBackground(Color.RED);
}
}
}
});
p.add(ForenameLabel);
p.add(Forename);
p.add(SurnameLabel);
p.add(Surname);
p.add(BirthdayLabel);
p.add(Birthday);
p.add(CityLabel);
p.add(City);
p.add(CountryLabel);
p.add(Country);
Save = new JButton("Save");
Save.addActionListener(new ActionListener() {
@Override
publicvoid actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/Listings";
Connection con = DriverManager.getConnection(url, "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO `Users` VALUES (NULL, ?, ?, ?, ?, ?)");
ps.setString(1, Forename.getText());
ps.setString(2, Surname.getText());
ps.setString(3, Birthday.getText());
ps.setString(4, City.getText());
ps.setString(5, Country.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data has been saved.");
ps.close();
con.close();
} catch (ClassNotFoundException | SQLException ex) {
JOptionPane.showMessageDialog(null, "There was an error: " + ex.getMessage());
}
}
});
p.add(Save);
add(p);
setVisible(true);
}
publicstaticvoid main(String[] args) {
new Spoken();
}
}