CIS355A - Week 7 Course Project Flooring Application - Source Code
CIS355A_StudentName_CourseProject/CIS355A Week 7 Course Project.docx
CIS355A Week 7 Course Project
Source Code
/***********************************************************************
Program Name: Flooring.java
Programmer's Name: Sarabjit Singh
Program Description: This program is a tabbed layout form with flooring inputs
and connects to mysql database to insert and display records.
***********************************************************************/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class Flooring extends JFrame implements ActionListener {
JTextField nameTextField, addressTextField, lengthTextField, widthTextField, areaTextField, costTextField; // Text fields
JButton calculateAreaButton = new JButton("Calculate Area"); // Calculate Area Button
JButton calculateCostButton = new JButton("Calculate Cost"); // Calculate Cost Button
JButton submitButton = new JButton("Submit Order"); // Submit Button
JButton summaryButton = new JButton("Display Order Summary"); // Summary Button
JButton ShowListButton = new JButton("Display Order List"); // Summary Button
ButtonGroup btngroup = new ButtonGroup();
JTextArea summaryTextArea = new JTextArea(5, 25); // dispay summary
JTextArea customerList = new JTextArea(10, 25); // dispay summary
JFrame frmCustomerList;
String floorType = "";
int cost;
Flooring() {
super("Flooring Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(getContentPane());
setSize(350, 200);
//pack();
setVisible(true);
}
public void addComponentsToPane(Container contentPane) {
JTabbedPane tabbedPane = new JTabbedPane();
// Add customer tab to the frame
JPanel customerPanel = customerTab();
tabbedPane.addTab("Customer", customerPanel);
// Add flooring tab to the frame
JPanel floorPanel = floorTab();
tabbedPane.addTab("Flooring", floorPanel);
// Add flooring tab to the frame
JPanel floorAreaPanel = floorDimensionsTab();
tabbedPane.addTab("Floor Area", floorAreaPanel);
// Add calculate tab to the frame
JPanel calculatePanel = calculateTab();
tabbedPane.addTab("Calculate", calculatePanel);
add(tabbedPane);
}
@Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equalsIgnoreCase("Calculate Area")) {
calculateArea();
}
if (action.equalsIgnoreCase("Calculate Cost")) {
calculateCost();
}
if (action.equalsIgnoreCase("Submit Order")) {
submitOrders();
}
if (action.equalsIgnoreCase("Display Order List")) {
viewCustomerList();
}
if(action.equalsIgnoreCase("Display Order Summary")){
// Get type of the floor
ButtonModel b = btngroup.getSelection();
if (b != null) {
floorType = b.getActionCommand();
}
String result = "Customer name: " + nameTextField.getText() + "\nCustomer Address: " + addressTextField.getText() + "\nFlooring Type: " + floorType + "\nFloor Length: " + lengthTextField.getText() + " ft" + "\nFloor Width: " + widthTextField.getText() + " ft" + "\nFlooring Cost: " + costTextField.getText() + "\nFlooring Area: " + areaTextField.getText();
summaryTextArea.setText(result);
JOptionPane.showMessageDialog(null, summaryTextArea,"Summary",1);
}
}
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/cis355?useSSL=false";
/**
* Create a database connection
*/
public static Connection getConnection() {
// Database credentials
Connection conn = null;
try {
conn = DriverManager.getConnection(DB_URL,"root","password");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void submitOrders() {
int length, width, area;
// Get type of the floor
ButtonModel b = btngroup.getSelection();
if (b != null) {
floorType = b.getActionCommand();
}
if (nameTextField.getText().isEmpty() || addressTextField.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Input name and address of the customer");
return;
}
if (floorType.isEmpty()) {
JOptionPane.showMessageDialog(null, "Select type of flooring");
return;
}
try {
length = Integer.parseInt(lengthTextField.getText());
width = Integer.parseInt(widthTextField.getText());
area = length * width;
//areaTextField.setText(String.valueOf(area) + " ft2");
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter valid Length and width of floor");
return;
}
calculateArea();
Connection conn = getConnection();
try {
String sql = "INSERT INTO Flooring "
+ "VALUES ('" + nameTextField.getText() + "', '" + addressTextField.getText() + "', '" + floorType + "','" + area + "','" + cost + "')";
java.sql.PreparedStatement statement = conn.prepareStatement(sql);
int result = statement.executeUpdate();
if(result >0)
JOptionPane.showMessageDialog(null, "Order Added");
} catch (Exception se) {
se.printStackTrace();
}
}
public void viewCustomerList(){
frmCustomerList = new JFrame("Orders List");
frmCustomerList.setSize(new Dimension(700,500));
customerList = new JTextArea(6,2);
customerList.setEditable(false);
try {
Connection conn = DriverManager.getConnection(DB_URL,"root","password");
String strSQl = "select * from flooring";
java.sql.PreparedStatement statement = conn.prepareStatement(strSQl);
ResultSet resultSet = statement.executeQuery();
String text="Customer Name\tCustomer Address\tFlooring Type\tFloor Area\tFloor Cost\n";
while(resultSet.next()){
text += resultSet.getString(1)+"\t\t" + resultSet.getString(2)+"\t\t" +resultSet.getString(3)+"\t" + resultSet.getDouble(4) +"\t" + resultSet.getDouble(5) +"\n";
}
customerList.setText(text);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frmCustomerList.add(customerList);
frmCustomerList.setVisible(true);
}
public void calculateArea() {
// Area = length x width
int length, width, area;
// Get type of the floor
ButtonModel b = btngroup.getSelection();
if (b != null) {
floorType = b.getActionCommand();
}
try {
length = Integer.parseInt(lengthTextField.getText());
width = Integer.parseInt(widthTextField.getText());
area = length * width;
areaTextField.setText(String.valueOf(area) + " ft2");
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter valid Length and width of floor");
return;
}
}
public void calculateCost() {
// Area = length x width
int length, width, area;
// Get type of the floor
ButtonModel b = btngroup.getSelection();
if (b != null) {
floorType = b.getActionCommand();
}
try {
length = Integer.parseInt(lengthTextField.getText());
width = Integer.parseInt(widthTextField.getText());
area = length * width;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter valid Length and width of floor");
return;
}
if (floorType.isEmpty()) {
JOptionPane.showMessageDialog(null, "Select type of flooring");
} else {
if (floorType.equalsIgnoreCase("Wood")) {
cost = area * 20;
costTextField.setText("$" + String.valueOf(area * 20));
//area*20
} else {
cost = area * 10;
costTextField.setText("$" + String.valueOf(area * 10));
//area*10
}
}
}
public JPanel customerTab() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JLabel("Welcome to Flooring Application"));
panel.add(new JLabel("Customer Name: "));
nameTextField = new JTextField(15);
panel.add(nameTextField);
panel.add(new JLabel("Customer Address: "));
addressTextField = new JTextField(15);
panel.add(addressTextField);
panel.add(new JLabel(" "));
panel.add(new JLabel(" "));
return panel;
}
public JPanel floorTab() {
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel.setLayout(new FlowLayout());
panel1.setLayout(new GridLayout(0, 2));
panel2.add(panel, BorderLayout.CENTER);
panel2.add(panel1, BorderLayout.NORTH);
panel.add(new JLabel("Flooring Type? ", JLabel.CENTER));
JRadioButton smallOption = new JRadioButton("Wood");
smallOption.setSelected(true);
smallOption.setActionCommand("Wood");
JRadioButton mediumOption = new JRadioButton("Carpet");
mediumOption.setActionCommand("Carpet");
btngroup.add(smallOption);
btngroup.add(mediumOption);
panel.add(smallOption);
panel.add(mediumOption);
return panel2;
}
public JPanel floorDimensionsTab() {
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel.setLayout(new FlowLayout());
panel1.setLayout(new GridLayout(0, 2));
panel2.add(panel1, BorderLayout.NORTH);
panel1.add(new JLabel("Floor Length: "));
lengthTextField = new JTextField(15);
panel1.add(lengthTextField);
panel1.add(new JLabel("Floor Width: "));
widthTextField = new JTextField(15);
panel1.add(widthTextField);
return panel2;
}
public JPanel calculateTab() {
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel.setLayout(new BorderLayout());
panel1.setLayout(new GridLayout(0, 2));
panel2.setLayout(new GridLayout(2,2));
panel1.add(new JLabel("Floor Area: "));
areaTextField = new JTextField(15);
panel1.add(areaTextField);
panel1.add(new JLabel("Flooring Cost: "));
costTextField = new JTextField(15);
panel1.add(costTextField);
panel2.add(calculateAreaButton);
panel2.add(calculateCostButton);
calculateAreaButton.addActionListener(this);
calculateCostButton.addActionListener(this);
panel2.add(submitButton);
submitButton.addActionListener(this);
summaryButton.addActionListener(this);
panel2.add(summaryButton);
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.WEST);
panel.add(ShowListButton,BorderLayout.SOUTH);
ShowListButton.addActionListener(this);
return panel;
}
public JPanel summaryTab() {
JPanel panel = new JPanel();
panel.add(summaryTextArea);
return panel;
}
public static void main(String[] args) {
@SuppressWarnings("unused")
Flooring floor = new Flooring();
}
}
Screenshots
CIS355A_StudentName_CourseProject/Flooring.class
public synchronized class Flooring extends javax.swing.JFrame implements java.awt.event.ActionListener { javax.swing.JTextField nameTextField; javax.swing.JTextField addressTextField; javax.swing.JTextField lengthTextField; javax.swing.JTextField widthTextField; javax.swing.JTextField areaTextField; javax.swing.JTextField costTextField; javax.swing.JButton calculateAreaButton; javax.swing.JButton calculateCostButton; javax.swing.JButton submitButton; javax.swing.JButton summaryButton; javax.swing.JButton ShowListButton; javax.swing.ButtonGroup btngroup; javax.swing.JTextArea summaryTextArea; javax.swing.JTextArea customerList; javax.swing.JFrame frmCustomerList; String floorType; int cost; static final String JDBC_DRIVER = com.mysql.jdbc.Driver; static final String DB_URL = jdbc:mysql://localhost/cis355?useSSL=false; void Flooring(); public void addComponentsToPane(java.awt.Container); public void actionPerformed(java.awt.event.ActionEvent); public static java.sql.Connection getConnection(); public void submitOrders(); public void viewCustomerList(); public void calculateArea(); public void calculateCost(); public javax.swing.JPanel customerTab(); public javax.swing.JPanel floorTab(); public javax.swing.JPanel floorDimensionsTab(); public javax.swing.JPanel calculateTab(); public javax.swing.JPanel summaryTab(); public static void main(String[]); }
CIS355A_StudentName_CourseProject/Flooring.java
CIS355A_StudentName_CourseProject/Flooring.java
/***********************************************************************
Program Name: Flooring.java
Programmer's Name: Sarabjit Singh
Program Description: This program is a tabbed layout form with flooring inputs
and connects to mysql database to insert and display records.
***********************************************************************/
import
java
.
awt
.
BorderLayout
;
import
java
.
awt
.
Container
;
import
java
.
awt
.
Dimension
;
import
java
.
awt
.
FlowLayout
;
import
java
.
awt
.
GridLayout
;
import
java
.
awt
.
event
.
ActionEvent
;
import
java
.
awt
.
event
.
ActionListener
;
import
java
.
sql
.
Connection
;
import
java
.
sql
.
DriverManager
;
import
java
.
sql
.
ResultSet
;
import
java
.
sql
.
SQLException
;
import
java
.
sql
.
Statement
;
import
java
.
util
.
Properties
;
import
javax
.
swing
.
BoxLayout
;
import
javax
.
swing
.
ButtonGroup
;
import
javax
.
swing
.
ButtonModel
;
import
javax
.
swing
.
JButton
;
import
javax
.
swing
.
JFrame
;
import
javax
.
swing
.
JLabel
;
import
javax
.
swing
.
JOptionPane
;
import
javax
.
swing
.
JPanel
;
import
javax
.
swing
.
JRadioButton
;
import
javax
.
swing
.
JTabbedPane
;
import
javax
.
swing
.
JTextArea
;
import
javax
.
swing
.
JTextField
;
@
SuppressWarnings
(
"serial"
)
public
class
Flooring
extends
JFrame
implements
ActionListener
{
JTextField
nameTextField
,
addressTextField
,
lengthTextField
,
widthTextField
,
areaTextField
,
costTextField
;
// Text fields
JButton
calculateAreaButton
=
new
JButton
(
"Calculate Area"
);
// Calculate Area Button
JButton
calculateCostButton
=
new
JButton
(
"Calculate Cost"
);
// Calculate Cost Button
JButton
submitButton
=
new
JButton
(
"Submit Order"
);
// Submit Button
JButton
summaryButton
=
new
JButton
(
"Display Order Summary"
);
// Summary Button
JButton
ShowListButton
=
new
JButton
(
"Display Order List"
);
// Summary Button
ButtonGroup
btngroup
=
new
ButtonGroup
();
JTextArea
summaryTextArea
=
new
JTextArea
(
5
,
25
);
// dispay summary
JTextArea
customerList
=
new
JTextArea
(
10
,
25
);
// dispay summary
JFrame
frmCustomerList
;
String
floorType
=
""
;
int
cost
;
Flooring
()
{
super
(
"Flooring Application"
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
addComponentsToPane
(
getContentPane
());
setSize
(
350
,
200
);
//pack();
setVisible
(
true
);
}
public
void
addComponentsToPane
(
Container
contentPane
)
{
JTabbedPane
tabbedPane
=
new
JTabbedPane
();
// Add customer tab to the frame
JPanel
customerPanel
=
customerTab
();
tabbedPane
.
addTab
(
"Customer"
,
customerPanel
);
// Add flooring tab to the frame
JPanel
floorPanel
=
floorTab
();
tabbedPane
.
addTab
(
"Flooring"
,
floorPanel
);
// Add flooring tab to the frame
JPanel
floorAreaPanel
=
floorDimensionsTab
();
tabbedPane
.
addTab
(
"Floor Area"
,
floorAreaPanel
);
// Add calculate tab to the frame
JPanel
calculatePanel
=
calculateTab
();
tabbedPane
.
addTab
(
"Calculate"
,
calculatePanel
);
add
(
tabbedPane
);
}
@
Override
public
void
actionPerformed
(
ActionEvent
e
)
{
String
action
=
e
.
getActionCommand
();
if
(
action
.
equalsIgnoreCase
(
"Calculate Area"
))
{
calculateArea
();
}
if
(
action
.
equalsIgnoreCase
(
"Calculate Cost"
))
{
calculateCost
();
}
if
(
action
.
equalsIgnoreCase
(
"Submit Order"
))
{
submitOrders
();
}
if
(
action
.
equalsIgnoreCase
(
"Display Order List"
))
{
viewCustomerList
();
}
if
(
action
.
equalsIgnoreCase
(
"Display Order Summary"
)){
// Get type of the floor
ButtonModel
b
=
btngroup
.
getSelection
();
if
(
b
!=
null
)
{
floorType
=
b
.
getActionCommand
();
}
String
result
=
"Customer name: "
+
nameTextField
.
getText
()
+
"\nCustomer Address: "
+
addressTextField
.
getText
()
+
"\nFlooring Type: "
+
floorType
+
"\nFloor Length: "
+
lengthTextField
.
getText
()
+
" ft"
+
"\nFloor Width: "
+
widthTextField
.
getText
()
+
" ft"
+
"\nFlooring Cost: "
+
costTextField
.
getText
()
+
"\nFlooring Area: "
+
areaTextField
.
getText
();
summaryTextArea
.
setText
(
result
);
JOptionPane
.
showMessageDialog
(
null
,
summaryTextArea
,
"Summary"
,
1
);
}
}
// JDBC driver name and database URL
static
final
String
JDBC_DRIVER
=
"com.mysql.jdbc.Driver"
;
static
final
String
DB_URL
=
"jdbc:mysql://localhost/cis355?useSSL=false"
;
/**
* Create a database connection
*/
public
static
Connection
getConnection
()
{
// Database credentials
Connection
conn
=
null
;
try
{
conn
=
DriverManager
.
getConnection
(
DB_URL
,
"root"
,
"password"
);
}
catch
(
SQLException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
return
conn
;
}
public
void
submitOrders
()
{
int
length
,
width
,
area
;
// Get type of the floor
ButtonModel
b
=
btngroup
.
getSelection
();
if
(
b
!=
null
)
{
floorType
=
b
.
getActionCommand
();
}
if
(
nameTextField
.
getText
().
isEmpty
()
||
addressTextField
.
getText
().
isEmpty
())
{
JOptionPane
.
showMessageDialog
(
null
,
"Input name and address of the customer"
);
return
;
}
if
(
floorType
.
isEmpty
())
{
JOptionPane
.
showMessageDialog
(
null
,
"Select type of flooring"
);
return
;
}
try
{
length
=
Integer
.
parseInt
(
lengthTextField
.
getText
());
width
=
Integer
.
parseInt
(
widthTextField
.
getText
());
area
=
length
*
width
;
//areaTextField.setText(String.valueOf(area) + " ft2");
}
catch
(
NumberFormatException
e
)
{
JOptionPane
.
showMessageDialog
(
null
,
"Please enter valid Length and width of floor"
);
return
;
}
calculateArea
();
Connection
conn
=
getConnection
();
try
{
String
sql
=
"INSERT INTO Flooring "
+
"VALUES ('"
+
nameTextField
.
getText
()
+
"', '"
+
addressTextField
.
getText
()
+
"', '"
+
floorType
+
"','"
+
area
+
"','"
+
cost
+
"')"
;
java
.
sql
.
PreparedStatement
statement
=
conn
.
prepareStatement
(
sql
);
int
result
=
statement
.
executeUpdate
();
if
(
result
>
0
)
JOptionPane
.
showMessageDialog
(
null
,
"Order Added"
);
}
catch
(
Exception
se
)
{
se
.
printStackTrace
();
}
}
public
void
viewCustomerList
(){
frmCustomerList
=
new
JFrame
(
"Orders List"
);
frmCustomerList
.
setSize
(
new
Dimension
(
700
,
500
));
customerList
=
new
JTextArea
(
6
,
2
);
customerList
.
setEditable
(
false
);
try
{
Connection
conn
=
DriverManager
.
getConnection
(
DB_URL
,
"root"
,
"password"
);
String
strSQl
=
"select * from flooring"
;
java
.
sql
.
PreparedStatement
statement
=
conn
.
prepareStatement
(
strSQl
);
ResultSet
resultSet
=
statement
.
executeQuery
();
String
text
=
"Customer Name\tCustomer Address\tFlooring Type\tFloor Area\tFloor Cost\n"
;
while
(
resultSet
.
next
()){
text
+=
resultSet
.
getString
(
1
)
+
"\t\t"
+
resultSet
.
getString
(
2
)
+
"\t\t"
+
resultSet
.
getString
(
3
)
+
"\t"
+
resultSet
.
getDouble
(
4
)
+
"\t"
+
resultSet
.
getDouble
(
5
)
+
"\n"
;
}
customerList
.
setText
(
text
);
}
catch
(
SQLException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
frmCustomerList
.
add
(
customerList
);
frmCustomerList
.
setVisible
(
true
);
}
public
void
calculateArea
()
{
// Area = length x width
int
length
,
width
,
area
;
// Get type of the floor
ButtonModel
b
=
btngroup
.
getSelection
();
if
(
b
!=
null
)
{
floorType
=
b
.
getActionCommand
();
}
try
{
length
=
Integer
.
parseInt
(
lengthTextField
.
getText
());
width
=
Integer
.
parseInt
(
widthTextField
.
getText
());
area
=
length
*
width
;
areaTextField
.
setText
(
String
.
valueOf
(
area
)
+
" ft2"
);
}
catch
(
NumberFormatException
e
)
{
JOptionPane
.
showMessageDialog
(
null
,
"Please enter valid Length and width of floor"
);
return
;
}
}
public
void
calculateCost
()
{
// Area = length x width
int
length
,
width
,
area
;
// Get type of the floor
ButtonModel
b
=
btngroup
.
getSelection
();
if
(
b
!=
null
)
{
floorType
=
b
.
getActionCommand
();
}
try
{
length
=
Integer
.
parseInt
(
lengthTextField
.
getText
());
width
=
Integer
.
parseInt
(
widthTextField
.
getText
());
area
=
length
*
width
;
}
catch
(
NumberFormatException
e
)
{
JOptionPane
.
showMessageDialog
(
null
,
"Please enter valid Length and width of floor"
);
return
;
}
if
(
floorType
.
isEmpty
())
{
JOptionPane
.
showMessageDialog
(
null
,
"Select type of flooring"
);
}
else
{
if
(
floorType
.
equalsIgnoreCase
(
"Wood"
))
{
cost
=
area
*
20
;
costTextField
.
setText
(
"$"
+
String
.
valueOf
(
area
*
20
));
//area*20
}
else
{
cost
=
area
*
10
;
costTextField
.
setText
(
"$"
+
String
.
valueOf
(
area
*
10
));
//area*10
}
}
}
public
JPanel
customerTab
()
{
JPanel
panel
=
new
JPanel
();
panel
.
setLayout
(
new
BoxLayout
(
panel
,
BoxLayout
.
Y_AXIS
));
panel
.
add
(
new
JLabel
(
"Welcome to Flooring Application"
));
panel
.
add
(
new
JLabel
(
"Customer Name: "
));
nameTextField
=
new
JTextField
(
15
);
panel
.
add
(
nameTextField
);
panel
.
add
(
new
JLabel
(
"Customer Address: "
));
addressTextField
=
new
JTextField
(
15
);
panel
.
add
(
addressTextField
);
panel
.
add
(
new
JLabel
(
" "
));
panel
.
add
(
new
JLabel
(
" "
));
return
panel
;
}
public
JPanel
floorTab
()
{
JPanel
panel
=
new
JPanel
();
JPanel
panel1
=
new
JPanel
();
JPanel
panel2
=
new
JPanel
();
panel2
.
setLayout
(
new
BorderLayout
());
panel
.
setLayout
(
new
FlowLayout
());
panel1
.
setLayout
(
new
GridLayout
(
0
,
2
));