Skip to content

Commit

Permalink
RentItAll Item Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalsk committed Feb 2, 2020
1 parent 20847ab commit 3b12b97
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 21 deletions.
6 changes: 6 additions & 0 deletions RentItAll/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-11-openjdk-amd64">
<attributes>
<attribute name="module" value="true"/>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/home/sonal/Downloads/Apache/apache-tomcat-9.0.30/lib/servlet-api.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v9.0">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
Expand Down
96 changes: 96 additions & 0 deletions RentItAll/WebContent/ItemForm.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Rent It All Items</title>
</head>

<body>
<h1 style="text-align:center;">Items Management</h1>
<h2 style="text-align:center;">
<a href="/new">Add New Item</a>
&nbsp;&nbsp;&nbsp;
<a href="/list">List All Items</a>

</h2>

<div align="center">
<c:if test="${ITEMS != null}">
<form action="update" method="post">
</c:if>
<c:if test="${ITEMS == null}">
<form action="insert" method="post">
</c:if>

<table border="1">
<caption>
<h2>
<c:if test="${ITEMS != null}">
Edit Item
</c:if>
<c:if test="${ITEMS == null}">
Add New Item
</c:if>
</h2>
</caption>
<c:if test="${ITEMS != null}">
<input type="hidden" name="id" value="<c:out value='${ITEMS.ID}' />" />
</c:if>
<tr>
<th>Name: </th>
<td>
<input type="text" name="name" size="45"
value="<c:out value='${ITEMS.Name}' />"
/>
</td>
</tr>
<tr>
<th>Category: </th>
<td>
<input type="text" name="category" size="45"
value="<c:out value='${ITEMS.Category}' />"
/>
</td>
</tr>
<tr>
<th>Price: </th>
<td>
<input type="text" name="price" size="5"
value="<c:out value='${ITEMS.PricePerDay}' />"
/>
</td>
</tr>

<tr>
<th>Duration: </th>
<td>
<input type="text" name="duration" size="5"
value="<c:out value='${ITEMS.Duration}' />"
/>
</td>
</tr>

<tr>
<th>Security Deposit: </th>
<td>
<input type="text" name="deposit" size="5"
value="<c:out value='${ITEMS.SecurityDepo}' />"
/>
</td>
</tr>

<tr>
<td colspan="2" align="center">
<input type="submit" value="Save" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
56 changes: 56 additions & 0 deletions RentItAll/WebContent/ItemList.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Rent It All Items</title>
</head>

<body>

<h1 style="text-align:center;"> ITEMS FOR RENTING </h1>
<h2 style="text-align:center;">
<a href="/new">Add New Item</a>
&nbsp;&nbsp;&nbsp;
<a href="/list">List All Items</a>
</h2>

<div align="center">
<table border="1">
<caption>
List of Items
</caption>
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Duration</th>
<th>Security Deposit </th>
<th>Actions</th>
</tr>

<c:forEach var="ITEMS" items="${listItem}">
<tr>
<td><c:out value="${ITEMS.ID}" /></td>
<td><c:out value="${ITEMS.Name}" /></td>
<td><c:out value="${ITEMS.Category}" /></td>
<td><c:out value="${ITEMS.PricePerDay}" /></td>
<td><c:out value="${ITEMS.Duration}" /></td>
<td><c:out value="${ITEMS.SecurityDepo}" /></td>
<td>
<a href="/edit?ID=<c:out value='${ITEMS.ID}' />">Edit</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="/delete?id=<c:out value='${ITEMS.ID}' />">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</div>

</body>
</html>
Binary file not shown.
Binary file not shown.
Binary file added RentItAll/WebContent/WEB-INF/lib/jstl.jar
Binary file not shown.
28 changes: 28 additions & 0 deletions RentItAll/WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">

<display-name>RentItAll</display-name>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
Expand All @@ -9,4 +11,30 @@
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>jdbcURL</param-name>
<param-value>jdbc:mysql://localhost:3306/RentItAll</param-value>
</context-param>

<context-param>
<param-name>jdbcUsername</param-name>
<param-value>root</param-value>
</context-param>

<context-param>
<param-name>jdbcPassword</param-name>
<param-value>P@ssw0rd</param-value>
</context-param>

<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>net.codejava.javaee.RentItAll.ControllerServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>
Binary file removed RentItAll/build/classes/Item.class
Binary file not shown.
Binary file removed RentItAll/build/classes/ItemDBC.class
Binary file not shown.
Binary file not shown.
Binary file added RentItAll/build/classes/items/Item.class
Binary file not shown.
Binary file added RentItAll/build/classes/items/ItemDBC.class
Binary file not shown.
120 changes: 120 additions & 0 deletions RentItAll/src/items/ControllerServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package items;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.GenericServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ItemDBC itemDBC;

public void init() {
String jdbcURL = getServletContext().getInitParameter("jdbcURL");
String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");

itemDBC = new ItemDBC(jdbcURL, jdbcUsername, jdbcPassword);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();

try {
switch (action) {
case "/new":
showNewForm(request, response);
break;
case "/insert":
insertItem(request, response);
break;
case "/delete":
deleteItem(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/update":
updateItem(request, response);
break;
default:
listItem(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}

private void listItem(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List<Item> listItem = ItemDBC.listAllItems();
request.setAttribute("listItem", listItem);
RequestDispatcher dispatcher = request.getRequestDispatcher("ItemList.jsp");
dispatcher.forward(request, response);
}

private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("ItemForm.jsp");
dispatcher.forward(request, response);
}

private void showEditForm(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
int id = Integer.parseInt(request.getParameter("ID"));
Item existingItem = ItemDBC.getItem(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("ItemForm.jsp");
request.setAttribute("item", existingItem);
dispatcher.forward(request, response);
}

private void insertItem(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
String name = request.getParameter("Name");
String category = request.getParameter("category");
int price = Integer.parseInt(request.getParameter("PricePerDay"));
int duration = Integer.parseInt(request.getParameter("Duration"));
int deposit = Integer.parseInt(request.getParameter("SecurityDepo"));

Item newItem = new Item(name, category, price, duration, deposit);
ItemDBC.insertItem(newItem);
response.sendRedirect("list");
}

private void updateItem(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("ID"));
String name = request.getParameter("Name");
String category = request.getParameter("Category");
int price = Integer.parseInt(request.getParameter("PricePerDay"));
int duration = Integer.parseInt(request.getParameter("Duration"));
int deposit = Integer.parseInt(request.getParameter("SecurityDepo"));

Item item = new Item(id, name, category, price, duration, deposit);
ItemDBC.updateItem(item);
response.sendRedirect("list");
}

private void deleteItem(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("ID"));

Item item = new Item(id);
ItemDBC.deleteItem(item);
response.sendRedirect("list");
}
}
18 changes: 15 additions & 3 deletions RentItAll/src/Item.java → RentItAll/src/items/Item.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

package items;
//model class for item table
public class Item {
protected int id;
protected String name;
protected String category;
protected int price;
protected int duration;
protected int deposit;

public Item() {

Expand All @@ -14,20 +16,22 @@ public Item(int id) {
this.id = id;
}

public Item(int id, String name, String category, int price, int duration) {
public Item(int id, String name, String category, int price, int duration, int deposit) {
this.name = name;
this.category = category;
this.price = price;
this.duration = duration;
this.id = id;
this.deposit = deposit;
}


public Item(String name, String category, int price, int duration) {
public Item(String name, String category, int price, int duration, int deposit) {
this.name = name;
this.category = category;
this.price = price;
this.duration = duration;
this.deposit = deposit;
}

public int getID() {
Expand Down Expand Up @@ -69,6 +73,14 @@ public int getDuration() {
public void setDuration(int duration) {
this.duration = duration;
}

public int getDeposit() {
return price;
}

public void setDeposit(int price) {
this.price = price;
}
}


Expand Down
Loading

0 comments on commit 3b12b97

Please sign in to comment.