Skocz do zawartości

Temat został przeniesiony do archiwum

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

piotrek0493

podstawy programowania, serwlety, java - w Netbeansie

Rekomendowane odpowiedzi

Witam,

mam do zrobienia to zadanie - wariant A:

http://www2.wt.pw.edu.pl/~a.czerepicki/wdis/cw5.pdf

Robię wg instrukcji teoretycznie wszystko fajnie, ale w "statystykach sprzedaży" zawsze wyświetla się jeden bilet mimo że wybieram inną ilość, jak to zmienić? Która formuła za to odpowiada?

 

Kody:

plik html

 

 

<!DOCTYPE html>
<!--
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.
-->
<html>
   <head>
       <title>Wujek Stefan</title>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
   </head>
   <body>
       <form method ="GET" action="/ZTM">
           <h1> <b> Wybierz rodzaj biletu</H1> </b> 
   <br><input type="radio" name="bilet" id="20min" value="3.40"/>Bilet 20 minutowy
   <br><input type="radio" name="bilet" id="jednorazowy" value="4.40"/>Bilet jednorazowy
   <br><input type="radio" name="bilet" id="dobowy" value="15.00"/>Bilet dobowy
   <br><input type="radio" name="bilet" id="grupowy" value="40.00"/>Bilet weekendowy grupowy
   <br><br><input type="checkbox" name="ulga">ulga 50%         
   <br> <br> <b>Liczba sztuk </b>
   <br>
   <br><select name="liczba"> 
           <option value="1">1</option>
           <option value="2">2</option>
           <option value="3">3</option>
           <option value="4">4</option>
           <option value="5">5</option>
       </select>
   <br>
   <br>
   Wpłacono PLN
   <br><input type="text" name="kwota" value="0"/>
   <br><input type ="submit" name="Wyślij">
   <br> <input type ="reset" name="Wyślij">
   </form>
   </body>
</html>

 

 

 

 

Plik java

 

/*
* 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.
*/

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 Piotrek
*/
public class Biletomat 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 {
       int[] bilety = new int[4];
       response.setContentType("text/html;charset=UTF-8");
       PrintWriter out = response.getWriter();
       try {
           /* TODO output your page here. You may use following sample code. */
           out.println("<!DOCTYPE html>");
           out.println("<html>");
           out.println("<head>");
           out.println("<title>Servlet biletomat</title>");            
           out.println("</head>");
           out.println("<body>");

           String bilet = request.getParameter("bilet");
String ulga =  request.getParameter("ulga");
String liczba =  request.getParameter("liczba");
String kwota =  request.getParameter("kwota");

double cenaBiletu = Double.parseDouble(bilet);
           int liczbaSzt = Integer.parseInt(liczba);
           double kwotaWplaty = Double.parseDouble(kwota);
           double wspolczynnik = 1.0;
           if (ulga != null) {
               wspolczynnik = 0.5; }

           double razem = cenaBiletu * liczbaSzt * wspolczynnik;
double reszta = kwotaWplaty -razem;

if( reszta < 0 ) throw new Exception("Zbyt mała kwota wpłaty!");



out.println("<h1>Potwierdzenie zakupu</h1>");
out.println("<br>Cena biletu: " + cenaBiletu + " PLN");
out.println("<br>Liczba sztuk: " + liczbaSzt);
out.println("<br>Do zapłaty: " + razem + " PLN");
out.println("<br>Wpłacono: " + kwotaWplaty + " PLN");
out.println("<br>Reszta: " + reszta + " PLN");

if (cenaBiletu == 3.40) {
               bilety[0]++;
           }
           if (cenaBiletu == 4.40) {
               bilety[1]++;
           }
           if (cenaBiletu == 15.00) {
               bilety[2]++;
           }
           if (cenaBiletu == 40.00) {
               bilety[3]++;
           }

           out.println("<hr><h1>Statystyki sprzedaży</h1>");
           out.println("<br>Bilet 20-minutowy: " + bilety[0] + " szt.");
           out.println("<br>Bilet jednorazowy: " + bilety[1] + " szt.");
           out.println("<br>Bilet dobowy: " + bilety[2] + " szt.");
           out.println("<br>Bilet grupowy: " + bilety[3] + " szt.");




           out.println("</body>");
           out.println("</html>");
       }
       catch (Exception e) {
           out.println("<h2><font color='red'>" + e.getMessage() + "</font></h2>");
   }
   }

   // <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>

}

 

 

web.xml

 

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 version="3.1"
 metadata-complete="true">

 <display-name>Welcome to Tomcat</display-name>
 <description>
    Welcome to Tomcat
 </description>
<servlet>
<servlet-name>ZTM</servlet-name>
<servlet-class>Biletomat</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ZTM</servlet-name>
<url-pattern>/ZTM</url-pattern>
</servlet-mapping>
</web-app>

 

 

Druga rzecz jak biorę większą ilość biletów jednorazowych to kwota do zapłaty wychodzi jakaś np 12,00000000001 dlaczego tak jest? W pozostałych opcjach jest normalnie.

 

Trzecia sprawa chciałem zrobić pod innymi nazwami plików, żeby sprawdzić jak mi wyjdzie i wtedy pojawia się problem w momencie gdy chcę uzyskać podsumowanie, wysyłam zapytanie ale niestety wykasuje błąd i nie uzyskuje odpowiedzi. Która formuła za to odpowiada?

Mógłby ktoś zmienić kody z innymi nazwami - projektu, pliku html, servleta? Bym mógł to ogarnąć.

Udostępnij tę odpowiedź


Odnośnik do odpowiedzi
Udostępnij na innych stronach

  • Ostatnio przeglądający   0 użytkowników

    Brak zarejestrowanych użytkowników przeglądających tę stronę.

×
×
  • Dodaj nową pozycję...