Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Stuck on a Java project. Having issues with setting up button listeners for a li

ID: 3828037 • Letter: S

Question

Stuck on a Java project. Having issues with setting up button listeners for a list and my panes seem to overlap. Any help / suggestions would be appreciated!

You are a proud owner of your shop. Identify what kind of shop it is: a book shop, flower shop, automotive repair shop, a fast-food joint, a health/fitness equipment shop

Now that you have settled in with what your shop does, identify a list of things that you would like to sell in the shop. In case of a book shop, this would a list of books that you are selling. In order to sell, you would need to know the title of the book, the cost of each book, the number of books currently in stock.

You will need to create a static file (a text file in notepad), where each line includes the name of item, its price and number in stock. The entries in each line are separated by a “,”. Such files are typically referred to as comma separated value (csv) files.

Here is an example of how entries can be stored in a File (Book.txt)

Jungle Book, 10, 6

Harry Potter, 15, 5

Java Programming, 50, 10

Now that you have the file about what you are selling in the shop for the day, you can start with your application.

Your application should have:

a title.

Menu with the following entries‘File’.

Entries under ‘File’ should be ‘Save’, ‘Exit’

‘Report’

Inventory

Re-order

Sales

A JList to show the List of Items in the Shop.

4 buttons – Add, Remove, Edit, CheckOut.

JList to show a Shopping Cart.

Functionality

When the application is started, the items in the shop should be listed in the JList.

When Add Button is clicked the Item selected in the ListBox should be added to the Cart, a dialog box should ask for the number of items needed and the Style of that item. (Example – paperback/hardbound, Small/Large)

At any point, the user should be able to select an item from the cart and

remove it - By Clicking the Remove Button

Edit it – By Clicking the Edit Button.

When Checkout Button is Clicked:

it should print the total cost and ask if the Customer wants to continue (Yes/No)

If Yes is Clicked, the transaction is complete. It should update the inventory, and clear the Shopping cart.

If No is clicked, then you return to the original screen to Edit the cart.

Menu functionalities

When the user clicks on File+ Save, it should save the file with the current inventory count. The filename can be the same as that of the input file.

When the user clicks on File + Exit, it should Exit the application.

When the user clicks on “Report + Inventory”, it should display the current inventory for each item in the shop. Each item should be listed in a separate line.

When the user clicks on “Report + Re-order”, it should display the items to be re-ordered i.e. those whose available count is less than 5.

When the user clicks on “Sales”, it should display the items sold and the total quantity sold for each. The last line should display the total dollar amount of the sales.

Explanation / Answer

index.html:-
---------------
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="getDetails.js"></script>
<script src="getDetails2.js"></script>

</head>
  
<body bgcolor="voilet">>
<center>
<fieldset>
<h3> Select Category </h3>
<form>
<select name="category">
<option value="">Select</option>
<option value="BOOK1">Jungle Book</option>
<option value="BOOK2">Harry Potter</option>
<option value="BOOK3">Java Programming</option>
</select>
  
<div id="DetailsDiv">
</div>
<div id="DetailsDiv2">
</div>
</center>
</fieldset>

</form>
</body>
</html>

getdetails.js:-
------------------

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var xmlHttp;
function showDetails(str)
{
xmlHttp=GetXmlHttpObject();//this is userdefined method and will return
if (xmlHttp===null)
{
alert ("Browser is not supporting AJAX");
return;
}
var url="cart"+"?qparam="+str;
xmlHttp.onreadystatechange=stateChanged;//this is userdefined method which will fired
xmlHttp.open("Get",url,true);//it will call 'Search' servlet
xmlHttp.send();

}

function stateChanged()
{
  
//check whether the response is completely loaded or not?
if (xmlHttp.readyState===4)
{
if (xmlHttp.readyState===4 || xmlHttp.readyState==="complete")
{
document.getElementById("DetailsDiv").innerHTML=xmlHttp.responseText;
}
}

}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for Firefox, Opera 8.0+, Safari, IE7+, Chrome
return new XMLHttpRequest();
}
else
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}

cart1:-
----------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.chegg;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author chegg
*/
public class cart extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String str = "";
String OptionValue = (String) request.getParameter("qparam");

//str = "<table><tr><td><b></b></td><td><b></b></td></tr>";
switch (OptionValue) {
case "MOBILES":
out.print("<h3> Select Item </h3> " +
" <select name="BOOK1"> " +
" <option value="">Select</option> " +
" <option value="price">10</option> " +
" <option value="count remaining">2</option> " +
" </select>");
break;
//str += "</table>";
//out.print(str);
case "Laptops":
out.print(" <h3> Select Item </h3> " +
" <select name="BOOK2"> " +
" <option value="">Select</option> " +
" <option value="">Select</option> " +
" <option value="price">15</option> " +
" <option value="count remaining">5</option> " +
" </select>");
break;
case "TV":
out.print(" <h3> Select Item </h3> " +
" <select name="BOOK3"> " +
" <option value="">Select</option> " +
" <option value="">Select</option> " +
" <option value="price">50</option> " +
" <option value="count remaining">10</option> " +
" </select>");
break;
default:
str += "<tr><td>N/A</td><td>N/A</td><td>N/A</td></tr>";
break;
}
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

cart2:-
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.chegg;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author chegg
*/
public class cart2 extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String str = "";
String OptionValue2 = (String) request.getParameter("qparam2");
  
  
  
if (OptionValue2.equals("BOOK1")) {
  
Out.print("Jungle Book ADDED to cart")
}
           else if (OptionValue2.equals("BOOK1")) {
  
Out.print("Harry Potter ADDED to cart")
}
           else if (OptionValue2.equals("BOOK1")) {
  
Out.print("Java Programming ADDED to cart")
}
  
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet
(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost
(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo
  
() {
return "Short description";
}// </editor-fold>

}