Professional Documents
Culture Documents
STUDENT ID : 2019294714_________________________________________
SIGNATURE : ___________________________________________________
I CERTIFY THAT THIS ASSIGNMENT IS MY OWN ORIGINAL WORK AND MATERIALS HAVE
NOT BEEN COPIED FROM ANY OTHER WORK (PUBLISHED OR OTHERWISE) WITHOUT
ACKNOWLEDGEMENT IN THE TEXT. I AM FULLY AWARE OF THE PENALTY OF
PLAGIARISM.
Note: Submit soft copy of report (.pdf), source code of project folder (.zip) and database file (.sql)
via Google Classroom.
MVC (Model-View-Controller) is a design pattern that separates the business logic, presentation
logic and data. Controller acts as an interface between View and Model and intercepts all the
incoming requests. Model represents the state of the application i.e. data. It can also have
business logic. View represents the presentation i.e. UI (User Interface). Based on Assignment
1, complete each of the following requirements:
Question 1
Design the interface (i.e. web page) that displays the list of all data and allows the user to add,
update, and delete the data. Briefly explain and include the screenshots of the interface.
This is the webpage for List Customer. In this page, we can view, update and delete the respective
customer. We can also add customer or add report at this page by clicking the respective button.
The button Delete will make all the information of the respective customer is deleted permanently
including in the database but if the Customer ID is related to any report, the customer cannot be
deleted unless the report that is related to that Customer ID is deleted first.
This is the page that will be displayed after we click the button View at the respective customer.
All the customer’s information will be displayed. We can also go back to the list of customers page
by clicking the button Customer List.
This is the page that will be directed after we click the button Update from the List Customer page.
This is the update page for the customer. The customer information can be updated but the
Customer ID cannot be updated, it is for displayed only. If we click the submit button, the new
information of the customer can be updated and directly displayed the List Customer page. We
can also click the button List Customer if we want to go back to the List Customer page.
This is the webpage for Add Customer. In this page, a new data for a new customer is added and
it will appear at the list customer page. If we do not want to add a new customer, we can click the
List Customer button and we will be directed back to the list customer page. The customer ID is
auto generated by the system so we do not need to insert the ID.
This is the webpage for List Report. In this page, we can view, update and delete the respective
report. We can also add customer or add report at this page by clicking the respective button.
The button Delete will make all the information of the respective report is deleted permanently
including in the database.
This is the page that will be displayed after we click the button View at the respective report. All
the respective report’s information will be displayed. We can also go back to the list of repots page
by clicking the button List Report.
This is the page that will be directed after we click the button Update from the List Report page.
This is the update page for the respective report. The report information can be updated but the
Report ID cannot be updated, it is for displayed only. If we click the submit button, the new
information of the report can be updated and directly displayed the List Report page. We can also
click the button List Report if we want to go back to the List Report page.
This is the webpage for Add Report. In this page, a new data for a new report is added and it will
appear at the list report page. If we do not want to add a new customer, we can click the List
Report button and we will be directed back to the list report page. The report ID is auto generated
by the system, so we do not need to insert the ID. We also need to choose the customer for which
customer that make the report. The customer is the fk from the table so, the customer ID is
directed to the report table and displayed in the report form.
customerform.jsp
<div class="row">
<div class="col-25">
<label for="name">Name*</label>
</div>
<div class="col-75">
<input type="text" id="name" name="name"
placeholder="Customer Name" required>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="email">Email*</label>
</div>
<div class="col-75">
<input type="email" id="email" name="email"
placeholder="Customer Email" required>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="phonenumber">Phone
Number*</label>
</div>
<div class="col-75">
<input type="text" id="phonenumber"
name="phonenumber" placeholder="Customer Phone Number" required>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="state">State/Province*</label>
</div>
<div class="col-75">
<select id="state" name="state" required>
<option value="" style="display:
none;">--Please Select State--</option>
<option value="Johor">Johor</option>
<option value="Kedah">Kedah</option>
<option
value="Kelantan">Kelantan</option>
<option value="Melaka">Melaka</option>
<option value="Negeri Sembilan">Negeri
Sembilan</option>
<option value="Pahang">Pahang</option>
<option value="Perak">Perak</option>
<option value="Perlis">Perlis</option>
<option value="Pulau Pinang">Pulau
Pinang</option>
<option value="Sabah">Sabah</option>
<option
value="Sarawak">Sarawak</option>
<option
value="Selangor">Selangor</option>
<option
value="Terengganu">Terengganu</option>
<option value="Wilayah Persekutuan
KL">Wilayah Persekutuan Kuala Lumpur</option>
<option value="Wilayah Persekutuan
labuan">Wilayah Persekutuan Labuan</option>
<option value="Wilayah Persekutuan
Putrajaya">Wilayah Persekutuan Putrajaya</option>
</select>
</div>
</div>
<input type="submit" value="Submit"><input type="reset"
value="Reset">
</fieldset>
</form>
</div>
<a href="CustomerController?action=listCustomer" class="w3-btn w3-yellow w3-
border w3-round-large" style="float: right">List Customer</a>
</body>
</html>
reportform.jsp
href="CustomerController?action=viewCustomer&cid=<c:out value="${cus.cid}"/>"
class="w3-btn w3-green w3-round-
large">View</a></td>
<td><a
href="CustomerController?action=updateCustomer&cid=<c:out
value="${cus.cid}"/>"
class="w3-btn w3-yellow w3-
round-large">Update</a></td>
<td><a
href="CustomerController?action=deleteCustomer&cid=<c:out
value="${cus.cid}"/>"
class="w3-btn w3-red w3-round-
large">Delete</a></td>
</tr>
</c:forEach>
</table>
</fieldset>
</form>
</div>
</body>
</html>
listReport.jsp
<br>
<table class="w3-table-all w3-centered">
<tr class="w3-black">
<th>Report ID</th>
<th>Restaurant Number</th>
<th>Date Visit</th>
<th>Price</th>
<th>Customer ID</th>
<th colspan="3">Actions</th>
</tr>
<c:forEach items="${reps}" var="rep">
<tr>
<td><c:out value="${rep.rid}" /></td>
<td><c:out value="${rep.restaurantnum}"
/></td>
<td><c:out value="${rep.datevisit}"
/></td>
<td><c:out value="${rep.price}" /></td>
<td><c:out value="${rep.cid}" /></td>
<td><a
href="ReportController?action=viewReport&rid=<c:out value="${rep.rid}"/>" class="w3-
btn w3-green w3-round-large">View</a></td>
<td><a
href="ReportController?action=updateReport&rid=<c:out value="${rep.rid}"/>"
class="w3-btn w3-yellow w3-round-large">Update</a></td>
<td><a
href="ReportController?action=deleteReport&rid=<c:out value="${rep.rid}"/>"
class="w3-btn w3-red w3-round-large">Delete</a></td>
</tr>
</c:forEach>
</table>
</fieldset>
</form>
</div>
</body>
</html>
updateCustomer.jsp
<div class="row">
<div class="col-25">
<label for="price">Final Total
Price:RM *</label>
</div>
<div class="col-75">
<input type="text" id="price"
name="price" value="<c:out value="${rep.price}"/>">
</div>
</div>
<div class="row">
<div class="col-25">
<label
for="problem">Problem/Feedback*</label>
</div>
<div class="col-75">
<textarea name="problem"
rows="10" cols="47"><c:out value="${rep.problem}"/></textarea>
</div>
</div>
</div>
<input type="submit" value="Submit">
</fieldset>
</form>
</div>
<a href="ReportController?action=listReport" class="w3-btn w3-green w3-border
w3-round-large" style="float: right">List Report</a>
</body>
</html>
viewCustomer.jsp
<fieldset>
<legend>
<h3>Customer Details</h3>
</legend>
<div class="w3-center">
<div>
<label for="cid">Customer ID</label>:<c:out
value="${cus.cid}"/><br>
</div>
<div>
<label for="name">Customer
Name</label>:<c:out value="${cus.name}"/><br>
</div>
<div>
<label for="email">Customer
Email</label>:<c:out value="${cus.email}"/><br>
</div>
<div>
<label for="phonenumber">Phone
Number</label>:<c:out value="${cus.phonenumber}"/><br>
</div>
<div>
<label for="state">State</label>:<c:out
value="${cus.state}"/><br>
</div>
</div>
</fieldset>
<a href="CustomerController?action=listCustomer"
class="w3-btn w3-green w3-border w3-round-large">Customer List</a>
</form>
</div>
</body>
</html>
viewReport.jsp
<fieldset>
<legend>
<h3>Problem/Feedback Details</h3>
</legend>
<div class="w3-center">
<div>
<div>
<label for="rid">Report
ID</label>:<c:out value="${rep.rid}"/><br>
</div>
<div>
<label for="restaurantnum">Restaurant
Number</label>:<c:out value="${rep.restaurantnum}"/><br>
</div>
<div>
<label for="datevisit">Date
Visit</label>:<c:out value="${rep.datevisit}"/><br>
</div>
<div>
<label for="hour">Hour</label>:<c:out
value="${rep.hour}"/><br>
</div>
<div>
<label
for="minute">Minute</label>:<c:out value="${rep.minute}"/><br>
</div>
<div>
<label for="time">Time</label>:<c:out
value="${rep.time}"/><br>
</div>
<div>
<label for="price">Total
Price</label>:RM<c:out value="${rep.price}"/><br>
</div>
<div>
<label
for="problem">Problem</label>:<c:out value="${rep.problem}"/><br>
</div>
<div>
<label
for="cid">Customer</label>:<c:out value="${rep.cid}"/><br>
</div>
</div>
</div>
</fieldset>
<a href="ReportController?action=listReport" class="w3-btn w3-
green w3-border w3-round-large">List Report</a>
</form>
</div>
</body>
</html>
Question 2
CustomerController.java
package customer.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import customer.dao.CustomerDAO;
import system.model.customer;
@WebServlet("/CustomerController")
public class CustomerController extends HttpServlet {
private static final long serialVersionUID = 1L;
private CustomerDAO dao;
private static String LIST = "/listCustomer.jsp";
private static String UPDATE = "/updateCustomer.jsp";
private static String VIEW = "/viewCustomer.jsp";
private static String ADD = "/customerform.jsp";
String forward = "";
public CustomerController() {
super();
dao = new CustomerDAO();
// TODO Auto-generated constructor stub
}
if(action.equalsIgnoreCase("updateCustomer")) {
forward = UPDATE;
int cid = Integer.parseInt(request.getParameter("cid"));
customer cus = dao.getCustomerById(cid);
request.setAttribute("cus", cus);
}
else if(action.equalsIgnoreCase("viewCustomer")) {
forward = VIEW;
int cid = Integer.parseInt(request.getParameter("cid"));
request.setAttribute("cus", dao.getCustomerById(cid));
}
else if(action.equalsIgnoreCase("deleteCustomer")) {
}
else if(action.equalsIgnoreCase("listCustomer")) {
forward = LIST;
request.setAttribute("cust", CustomerDAO.getAllCustomer());
}
else {
forward = ADD;
}
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
else {
cus.setCid(Integer.parseInt(cid));
dao.updateCustomer(cus);
}
forward = LIST;
request.setAttribute("cust", CustomerDAO.getAllCustomer());
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
}
ReportController.java
package report.controller;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import customer.dao.CustomerDAO;
import report.dao.ReportDAO;
import system.model.report;
@WebServlet("/ReportController")
public class ReportController extends HttpServlet {
private static final long serialVersionUID = 1L;
DateFormat formatter;
private ReportDAO dao;
private CustomerDAO daoc;
private static String LIST = "/listReport.jsp";
private static String UPDATE = "/updateReport.jsp";
private static String VIEW = "/viewReport.jsp";
private static String ADD = "/reportform.jsp";
String forward = "";
public ReportController() {
super();
dao = new ReportDAO();
daoc = new CustomerDAO();
}
if(action.equalsIgnoreCase("updateReport")) {
}
else if(action.equalsIgnoreCase("viewReport")) {
}
else if(action.equalsIgnoreCase("deleteReport")) {
}
else if(action.equalsIgnoreCase("listReport")) {
request.setAttribute("reps", ReportDAO.getAllReport());
RequestDispatcher view =
request.getRequestDispatcher("/listReport.jsp");
view.forward(request, response);
request.setAttribute("cust",CustomerDAO.getCustID());
RequestDispatcher view =
request.getRequestDispatcher("/reportform.jsp");
view.forward(request, response);
try {
formatter = new SimpleDateFormat("yyyy-MM-dd");
rep.setRestaurantnum(Integer.parseInt(request.getParameter("restaurantnum")));
rep.setDatevisit(date1);
rep.setHour(request.getParameter("hour"));
rep.setMinute(request.getParameter("minute"));
rep.setTime(request.getParameter("time"));
rep.setPrice(Double.parseDouble(request.getParameter("price")));
rep.setProblem(request.getParameter("problem"));
rep.setCid(Integer.parseInt(request.getParameter("cid")));
request.setAttribute("reps", ReportDAO.getAllReport());
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
catch(Exception e){
System.out.println(e);
}
}
}
AddReportController.java
package report.controller;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import customer.dao.CustomerDAO;
import report.dao.ReportDAO;
import system.model.report;
@WebServlet("/AddReportController")
public class AddReportController extends HttpServlet {
private static final long serialVersionUID = 1L;
DateFormat formatter;
private ReportDAO dao;
private CustomerDAO daoc;
public AddReportController() {
super();
dao = new ReportDAO();
daoc = new CustomerDAO();
}
try {
System.out.println("Shot2");
formatter = new SimpleDateFormat("yyyy-MM-dd");
rep.setRestaurantnum(Integer.parseInt(request.getParameter("restaurantnum")));
rep.setDatevisit(date1);
rep.setHour(request.getParameter("hour"));
System.out.println("Shot4");
rep.setMinute(request.getParameter("minute"));
rep.setTime(request.getParameter("time"));
System.out.println("Shot5");
rep.setPrice(Double.parseDouble(request.getParameter("price")));
rep.setProblem(request.getParameter("problem"));
System.out.println("Shot6");
rep.setCid(Integer.parseInt(request.getParameter("cid")));
System.out.println("Shot7");
request.setAttribute("reps", ReportDAO.getAllReport());
RequestDispatcher view =
request.getRequestDispatcher("/listReport.jsp");
view.forward(request, response);
System.out.println("shot14");
}
catch(Exception e){
System.out.println(e);
}
}
}
Question 3
Write the DAO to add, update, delete, and select the data.
CustomerDAO.java
package customer.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import db.connection.ConnectionManager;
import system.model.customer;
//add customer
public void addCustomer(customer bean) {
name = bean.getName();
email = bean.getEmail();
phonenumber = bean.getPhonenumber();
state = bean.getState();
try {
con = ConnectionManager.getConnection();
ps = con.prepareStatement("insert into customer
(name,email,phonenumber,state) values (?,?,?,?)");
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, phonenumber);
ps.setString(4, state);
ps.executeUpdate();
}
//view customer
public static customer getCustomerById(int cid) {
customer cus = new customer();
try {
con = ConnectionManager.getConnection(); //2. call the method
from ConnectionManager class to establish connection
//String query = "select * from person where id=?";
ps = con.prepareStatement("select * from customer where cid=?");
ps.setInt(1, cid);
ResultSet rs = ps.executeQuery(); //4. process the result
if(rs.next()) {
cus.setCid(rs.getInt("cid"));
cus.setName(rs.getString("name"));
cus.setEmail(rs.getString("email"));
cus.setPhonenumber(rs.getString("phonenumber"));
cus.setState(rs.getString("state"));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return cus;
}
//list customer
public static List<customer> getAllCustomer() {
List<customer> cust = new ArrayList<customer>();
try {
con = ConnectionManager.getConnection();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from customer order by
cid");
while (rs.next()) {
customer cus = new customer();
cus.setCid(rs.getInt("cid"));
cus.setName(rs.getString("name"));
cus.setEmail(rs.getString("email"));
cus.setPhonenumber(rs.getString("phonenumber"));
cus.setState(rs.getString("state"));
cust.add(cus);
}
} catch (SQLException e) {
e.printStackTrace();
}
return cust;
}
//delete customer
public void deleteCustomer(int cid) {
try {
con = ConnectionManager.getConnection(); //2. establish
connection
ps = con.prepareStatement("delete from customer where cid=?");
//3. create statement
ps.setInt(1, cid);
ps.executeUpdate();//3. execute statement
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//update customer
public void updateCustomer(customer bean) {
cid = bean.getCid();
name = bean.getName();
email = bean.getEmail();
phonenumber = bean.getPhonenumber();
state = bean.getState();
try {
con = ConnectionManager.getConnection(); //2. establish
connection
String query = "UPDATE customer SET name= '" + name +
"', email= '" + email +
"', phonenumber= '" + phonenumber +
"', state= '" + state +
"' WHERE cid= '"+ cid +"' ";
stmt = con.createStatement(); //3. create statement
stmt.executeUpdate(query);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
try {
con = ConnectionManager.getConnection(); //2. call the method
from ConnectionManager class to establish connection
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select cid, name from
customer");
while(rs.next()) {
cus.setCid(rs.getInt("cid"));
cus.setName(rs.getString("name"));
cust.add(cus);
}
}
catch (SQLException e) {
e.printStackTrace();
}
return cust;
}
}
}
ReportDAO.java
package report.dao;
import java.sql.Connection;
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import customer.dao.CustomerDAO;
import java.util.Date;
import db.connection.ConnectionManager;
import system.model.customer;
import system.model.report;
//add Report
public void add(report bean) {
restaurantnum = bean.getRestaurantnum();
datevisit = bean.getDatevisit();
hour = bean.getHour();
minute = bean.getMinute();
time = bean.getTime();
price = bean.getPrice();
problem = bean.getProblem();
cid = bean.getCid();
try {
con = ConnectionManager.getConnection();
ps = con.prepareStatement("insert into report
(restaurantnum,datevisit,hour,minute,time,price,problem,cid) values
(?,?,?,?,?,?,?,?)");
ps.setInt(1, restaurantnum);
ps.setDate(2, sdate);
ps.setString(3, hour);
ps.setString(4, minute);
ps.setString(5, time);
ps.setDouble(6, price);
ps.setString(7, problem);
ps.setInt(8, cid);
ps.executeUpdate();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//view report
public static report getReportById(int rid) {
report rep = new report();
try {
con = ConnectionManager.getConnection(); //2. call the method
from ConnectionManager class to establish connection
//String query = "select * from person where id=?";
ps = con.prepareStatement("select * from report where rid=?");
ps.setInt(1, rid);
ResultSet rs = ps.executeQuery(); //4. process the result
if(rs.next()) {
rep.setRid(rs.getInt("rid"));
rep.setRestaurantnum(rs.getInt("restaurantnum"));
rep.setDatevisit(rs.getDate("datevisit"));
rep.setHour(rs.getString("hour"));
rep.setMinute(rs.getString("minute"));
rep.setTime(rs.getString("time"));
rep.setPrice(rs.getDouble("price"));
rep.setProblem(rs.getString("problem"));
rep.setCid(rs.getInt("cid"));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return rep;
}
//list report
public static List<report> getAllReport() {
List<report> reps = new ArrayList<report>();
try {
con = ConnectionManager.getConnection();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from report order by
rid");
while (rs.next()) {
report rep = new report();
rep.setRid(rs.getInt("rid"));
rep.setRestaurantnum(rs.getInt("restaurantnum"));
rep.setDatevisit(rs.getDate("datevisit"));
rep.setHour(rs.getString("hour"));
rep.setMinute(rs.getString("minute"));
rep.setTime(rs.getString("time"));
rep.setPrice(rs.getDouble("price"));
rep.setProblem(rs.getString("problem"));
rep.setCid(rs.getInt("cid"));
reps.add(rep);
}
} catch (SQLException e) {
e.printStackTrace();
}
return reps;
}
//delete report
public void deleteReport(int rid) {
try {
con = ConnectionManager.getConnection(); //2. establish
connection
ps = con.prepareStatement("delete from report where rid=?"); //3.
create statement
ps.setInt(1, rid);
ps.executeUpdate();//3. execute statement
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//update report
public void updateReport(report bean) {
rid = bean.getRid();
restaurantnum = bean.getRestaurantnum();
datevisit = bean.getDatevisit();
hour = bean.getHour();
minute = bean.getMinute();
time = bean.getTime();
price = bean.getPrice();
problem = bean.getProblem();
cid = bean.getCid();
try {
con = ConnectionManager.getConnection(); //2. establish
connection
String query = "UPDATE report SET restaurantnum= '" +
restaurantnum +
"', datevisit= '" + datevisit +
"', hour= '" + hour +
"', minute= '" + minute +
"', time= '" + time +
"', price= '" + price +
"', problem= '" + problem +
"', cid= '" + cid +
"' WHERE rid= '"+ rid +"' ";
stmt = con.createStatement(); //3. create statement
stmt.executeUpdate(query);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
Question 4
The database name for both table customer and table report is customerservice. The pk for table
customer is cid, while the pk for table report is rid. There is a fk in table report which is cid that is
related with the pk of table customer.