Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CreateForm class #26

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions src/Pages/CreateForm/CreateForm.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Pages.CreateForm">
<grid id="27dc6" binding="createPanel" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="4" left="4" bottom="4" right="4"/>
<constraints>
<xy x="20" y="20" width="587" height="390"/>
</constraints>
<properties>
<background color="-16744320"/>
</properties>
<border type="none"/>
<children>
<grid id="2f880" layout-manager="GridLayoutManager" row-count="8" column-count="9" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="4" left="4" bottom="4" right="4"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="bevel-lowered"/>
<children>
<component id="e79d8" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="First Name"/>
</properties>
</component>
<component id="c5443" class="javax.swing.JTextField" binding="tfFirstName">
<constraints>
<grid row="0" column="2" row-span="1" col-span="6" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="75f18" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Last Name"/>
</properties>
</component>
<component id="d7133" class="javax.swing.JTextField" binding="tfLastName">
<constraints>
<grid row="1" column="2" row-span="1" col-span="6" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="429f6" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Email"/>
</properties>
</component>
<component id="4a03d" class="javax.swing.JTextField" binding="tfEmail">
<constraints>
<grid row="2" column="2" row-span="1" col-span="6" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="bbb75" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Phone Number"/>
</properties>
</component>
<component id="16e02" class="javax.swing.JTextField" binding="tfPhoneNumber">
<constraints>
<grid row="3" column="2" row-span="1" col-span="6" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="155fd" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Birthday"/>
</properties>
</component>
<component id="87f89" class="javax.swing.JLabel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Membership Duration"/>
</properties>
</component>
<component id="44b46" class="javax.swing.JTextField" binding="MembershipDuration">
<constraints>
<grid row="5" column="4" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<hspacer id="e140">
<constraints>
<grid row="1" column="8" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="15feb" class="javax.swing.JLabel">
<constraints>
<grid row="5" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Months"/>
</properties>
</component>
<component id="ed843" class="javax.swing.JTextField" binding="bdYear">
<constraints>
<grid row="4" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="2403b" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Year"/>
</properties>
</component>
<component id="c4c8d" class="javax.swing.JTextField" binding="bdDay">
<constraints>
<grid row="4" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="f4a2d" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Day"/>
</properties>
</component>
<component id="d6f5b" class="javax.swing.JTextField" binding="bdMonth">
<constraints>
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="8e8ba" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Month"/>
</properties>
</component>
<component id="388bb" class="javax.swing.JButton" binding="createButton" default-binding="true">
<constraints>
<grid row="6" column="0" row-span="1" col-span="9" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Create"/>
</properties>
</component>
<component id="55cf" class="javax.swing.JButton" binding="cancelButton" default-binding="true">
<constraints>
<grid row="7" column="0" row-span="1" col-span="9" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
</children>
</grid>
<grid id="3469d" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="bevel-raised"/>
<children>
<component id="ad325" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font name="Droid Sans Mono Slashed" size="18" style="1"/>
<text value="Create Membership Form"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
124 changes: 124 additions & 0 deletions src/Pages/CreateForm/CreateForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

public class CreateForm extends JDialog implements jdbcValues{
private JPanel createPanel;
private JTextField tfFirstName;
private JTextField tfLastName;
private JTextField tfEmail;
private JTextField tfPhoneNumber;
private JTextField bdMonth;
private JTextField bdDay;
private JTextField bdYear;
private JButton createButton;
private JTextField MembershipDuration;
private JButton cancelButton;

public CreateForm(JFrame parent) {
super(parent);
setTitle("Create Member");
setContentPane(createPanel);
setMinimumSize(new Dimension(600,500));
setModal(true);
setLocationRelativeTo(parent);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
createButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
long newMemberId = -1;
try {
String fn = String.valueOf(tfFirstName.getText());
String ln = String.valueOf(tfLastName.getText());
String em = String.valueOf(tfEmail.getText());
Long pn = Long.parseLong(tfPhoneNumber.getText());
int md = Integer.parseInt(String.valueOf(MembershipDuration.getText()));
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-M-d");
LocalDate bd = LocalDate.parse(Integer.parseInt(bdYear.getText()) + "-" + Integer.parseInt(bdMonth.getText()) + "-" + Integer.parseInt(bdDay.getText()),df);

Member m = new Member(fn, ln, em, bd, pn);
newMemberId = createMember(m, md);

if(newMemberId == -1) throw new Exception();

dispose();
JOptionPane.showMessageDialog(CreateForm.this, "New Gym Member\n\tName: " + m.getName() + "\n\tID: " + m.id_number, "Member has been added", JOptionPane.INFORMATION_MESSAGE);
}catch (Exception o) {
JOptionPane.showMessageDialog(CreateForm.this, "Input Is INVALID", "Try Again", JOptionPane.ERROR_MESSAGE);
}
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {dispose();}
});
setVisible(true);
}

public long createMember(Member m, int md) {
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
switch (checkIfMemberExists(m, conn)) {
case 1 -> throw new Exception("Email Already Exists In System");
case 2 -> throw new Exception("Phone Number Already Exists In System");
}
m.id_number = createUniqueID(conn);
String query = "INSERT INTO members " +
"(id_number, first_name, last_name, birth_date, email_address, phone_number, creation_date, expiration_date, last_checked_in_date) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setLong(1, m.id_number);
preparedStatement.setString(2, m.first_name.substring(0,1).toUpperCase() + m.first_name.substring(1).toLowerCase());
preparedStatement.setString(3, m.last_name.substring(0,1).toUpperCase() + m.last_name.substring(1).toLowerCase());
preparedStatement.setDate(4, Date.valueOf(m.date_of_birth));
preparedStatement.setString(5, m.email_address.toLowerCase());
preparedStatement.setLong(6, m.phone_number);
preparedStatement.setDate(7, Date.valueOf(LocalDate.now()));
preparedStatement.setDate(8, Date.valueOf(LocalDate.now().plusMonths(md)));
preparedStatement.setDate(9, Date.valueOf(LocalDate.now()));
preparedStatement.executeUpdate();
conn.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(CreateForm.this, e, "Try Again", JOptionPane.ERROR_MESSAGE);
return -1;
}
return m.id_number;
}

public int checkIfMemberExists(Member member, Connection conn) {
try {
String query = "SELECT * FROM members WHERE email_address=? OR phone_number=?";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, member.email_address);
preparedStatement.setLong(2, member.phone_number);
ResultSet rs = preparedStatement.executeQuery();
if(rs.next()) {return Objects.equals(rs.getString(5), member.email_address) ? 1 : 2;}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

public Long createUniqueID(Connection conn) {
long id = 10000000000L;
try{
while(true){
String query = "SELECT * FROM members WHERE id_number=?";
PreparedStatement preparedStatement = conn.prepareStatement(query);
preparedStatement.setLong(1, id);
ResultSet rs = preparedStatement.executeQuery();
if(!rs.next()) break;
else id = (long)(Math.random() * (99999999999L - 10000000000L)) + 10000000000L;
}
} catch (Exception e) {
e.printStackTrace();
}
return id;
}
}
Loading