JAVA programming - I am creating a data driven application for a pizza ordering
ID: 3815832 • Letter: J
Question
JAVA programming - I am creating a data driven application for a pizza ordering system. This project must include:
A class that encapsulates the database access logic in to a reusable component must be used. An example: from my data entry form, I need to encapsulate a line item from the order as a class with attributes, like name, address, pizza size, toppings etc.
I need to be able to add records, delete records, update an existing record, search for record and display a single record.
Another interface must be a summary report showing the summary of orders for the week output to a file.
The data entry form must write to one or more files, using file handling classes. The report interface must read from one or more files created by the data entry form. The report must be based on JFrame like the data entry form.
Here is the code for my data entry form. I don't know how to link to a database. Any help would be appreciated.
import java.awt.*;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*; //imports classes
import javax.swing.event.*;
import javax.swing.event.ListSelectionListener;
public class Orders extends JFrame implements ActionListener, ListSelectionListener
{
//declares labels, text fields, menu bar, menu items, button
private JLabel nameL, addressL, cityL, phoneL;
private JTextField nameTF, addressTF, cityTF, phoneTF;
private JMenuBar menuMB = new JMenuBar();
private JMenu fileM, helpM; //
private JMenuItem exitI, aboutI;
JButton submitB;
private JLabel sizeJL;
private JLabel toppingJL;
private JList pizzasizeJL;
private JList availtoppingsJL;
private JScrollPane selectionJS1;
private JScrollPane selectionJS2;
//creates arrays
private String[] pizzaSize = {"Personal", "Small", "Medium", "Large"};
private String[] availableToppings = {"Cheese", "Extra Cheese", "Pepperoni", "Mushroom", "Ham", "Bacon", "Sausage"};
public Orders()
{
//title of program
setTitle("Pizza Palace Ordering System");
// creates labels
nameL = new JLabel("Name: " , SwingConstants.LEFT);
addressL = new JLabel("Street Address: ", SwingConstants.LEFT);
cityL = new JLabel("City: ", SwingConstants.LEFT);
phoneL = new JLabel("Phone: ", SwingConstants.LEFT);
submitB = new JButton("Submit Order"); //creates button
sizeJL = new JLabel("Pizza Size", SwingConstants.LEFT);
sizeJL.setSize(350, 20);
sizeJL.setLocation(10,0);
//creates JList for pizza sizes
pizzasizeJL = new JList(pizzaSize);
pizzasizeJL.setVisibleRowCount(1);
pizzasizeJL.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
pizzasizeJL.addListSelectionListener(this);
selectionJS1 = new JScrollPane(pizzasizeJL);
selectionJS1.setSize(350,60);
selectionJS1.setLocation(10,20);
//creates JList of available toppings
toppingJL = new JLabel("Pizza Toppings", SwingConstants.RIGHT);
toppingJL.setSize(250, 20);
toppingJL.setLocation(20,0);
availtoppingsJL = new JList(availableToppings);
availtoppingsJL.setVisibleRowCount(1);
availtoppingsJL.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
availtoppingsJL.addListSelectionListener(this);
selectionJS2 = new JScrollPane(availtoppingsJL);
selectionJS2.setSize(350, 60);
selectionJS2.setLocation(30, 0);
//creates text fields
nameTF = new JTextField(20);
addressTF = new JTextField(50);
cityTF = new JTextField(20);
phoneTF = new JTextField(12);
//set container layout
Container pane = getContentPane();
pane.setLayout(new GridLayout(7,2));
// add items to content pane
pane.add(nameL);
pane.add(nameTF);
pane.add(addressL);
pane.add(addressTF);
pane.add(cityL);
pane.add(cityTF);
pane.add(phoneL);
pane.add(phoneTF);
pane.add(sizeJL);
pane.add(toppingJL);
pane.add(selectionJS1);
pane.add(selectionJS2);
pane.add(submitB);
//set menu bar
setJMenuBar(menuMB);
setFileMenu();
setHelpMenu();
//content pane settings
setSize(800,800);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//method to create file menu
private void setFileMenu()
{
fileM = new JMenu("File");
menuMB.add(fileM);
exitI = new JMenuItem("Exit");
fileM.add(exitI);
exitI.addActionListener(this);
}
//method to create help menu
private void setHelpMenu()
{
helpM = new JMenu("Help");
menuMB.add(helpM);
aboutI = new JMenuItem("About");
helpM.add(aboutI);
aboutI.addActionListener(this);
}
public static void main(String[]args)
{
Orders orders = new Orders();
}
//closes window when exit menu option is chosen
//shows about pane when about menu option is chosen
@Override
public void actionPerformed(java.awt.event.ActionEvent e)
{
JMenuItem mItem = (JMenuItem) e.getSource();
if(mItem == exitI)
{
System.exit(0);
}
else if (mItem == aboutI)
{
JOptionPane.showMessageDialog( null, "Version 1.0 Copyright 2017. Created by Paula Baker.", "About", JOptionPane.INFORMATION_MESSAGE);
}
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
}
Explanation / Answer
I have used derby and created code to link to the database, created Order class through which you can set values from UI and insert into the db. Created another class to connect to DB.
Order.java
import java.sql.Date;
import java.util.List;
public class Order {
private int orderId;
private String name;
private String address;
private String city;
private String phoneNumber;
private String pizzaSize;
private List<String> toppings;
private Date orderDate;
private boolean delivered;
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPizzaSize() {
return pizzaSize;
}
public void setPizzaSize(String pizzaSize) {
this.pizzaSize = pizzaSize;
}
public List<String> getToppings() {
return toppings;
}
public void setToppings(List<String> toppings) {
this.toppings = toppings;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public boolean isDelivered() {
return delivered;
}
public void setDelivered(boolean delivered) {
this.delivered = delivered;
}
}
ConnectionDB.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectDB {
private static Connection conn = null;
public Connection getConnection() {
String dbURL = "jdbc:derby:testdb;create=true";
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection(dbURL);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public void dbShutDown() {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException sqlExcept) {
sqlExcept.printStackTrace();
}
}
}
main method to connect to database and insert the values into database
public static void main(String[] args) {
// OrdersUI orders = new OrdersUI();
try {
ConnectDB connect = new ConnectDB();
Connection conn = connect.getConnection();
stmt = conn.createStatement();
/* Create Table to store order details
String createString = "CREATE TABLE ORDER_Test (ORDER_ID int not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), name varchar(50), address varchar(100), city varchar(50), phone varchar(12), pizzaSize varchar(15), toppings varchar(100), orderDate DATE, delivered BOOLEAN)";
stmt.executeUpdate(createString);
*/
//sql statement to insert order details
psInsert = conn.prepareStatement("insert into ORDER_Test(name, address, city, phone, pizzasize, toppings, orderdate, delivered) values (?,?,?,?,?,?,?,?)");
//set the values from the UI to the order object
Order order =new Order();
order.setName("Name");
order.setAddress("Street No1");
order.setCity("Hyderabad");
order.setPhoneNumber("1235482332");
order.setPizzaSize("Medium");
ArrayList<String> toppings =new ArrayList<String>();
toppings.add("Cheese");
toppings.add("Ham");
toppings.add("Sausage");
order.setToppings(toppings);
Calendar calendar = Calendar.getInstance();
order.setOrderDate(new Date(calendar.getTime().getTime()));
order.setDelivered(true);
//Set order values to update into the database
psInsert.setString(1, order.getName());
psInsert.setString(2, order.getAddress());
psInsert.setString(3, order.getCity());
psInsert.setString(4, order.getPhoneNumber());
psInsert.setString(5, order.getPizzaSize());
psInsert.setString(6, order.getToppings().toString());
psInsert.setDate(7, order.getOrderDate());
psInsert.setBoolean(8, order.isDelivered());
psInsert.executeUpdate();
//Code to update orders
psInsert = conn.prepareStatement("Update ORDER_Test set name=? where order_id=1");
psInsert.setString(1, "Name2");
psInsert.executeUpdate();
//Code to display orders
rs = stmt.executeQuery("select * from ORDER_Test");
while (rs.next()) {
System.out.println(" " + rs.getString(2) + " " + rs.getString(3) +" " + rs.getString(4) + " " + rs.getString(5)+" " + rs.getString(6) + " " + rs.getString(7)+" " + rs.getString(8) +" ");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.