Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.alpotzh.controller;
- import com.example.alpotzh.model.Bejglibolt;
- import com.example.alpotzh.model.Megrendeles;
- import jakarta.servlet.*;
- import jakarta.servlet.http.*;
- import jakarta.servlet.annotation.*;
- import java.io.IOException;
- import static java.lang.Integer.parseInt;
- @WebServlet(name = "RendelesAdatokServlet", value = "/RendelesAdatokServlet")
- public class RendelesAdatokServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- int inputId = parseInt(request.getParameter("id"));
- Megrendeles megrendeles = Bejglibolt.getInstance().findRendelesId(inputId);
- if(megrendeles!=null){
- request.setAttribute("megrendeles",megrendeles);
- request.getRequestDispatcher("index.jsp").forward(request,response);
- } else {
- request.setAttribute("message", "Megrendelés nem található");
- request.getRequestDispatcher("index.jsp").forward(request,response);
- }
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- }
- }
- package com.example.alpotzh.controller;
- import com.example.alpotzh.model.Bejgli;
- import com.example.alpotzh.model.BejgliTipus;
- import com.example.alpotzh.model.Bejglibolt;
- import com.example.alpotzh.model.Megrendeles;
- import jakarta.servlet.*;
- import jakarta.servlet.http.*;
- import jakarta.servlet.annotation.*;
- import java.io.IOException;
- import static java.lang.Integer.parseInt;
- @WebServlet(name = "RendelesServlet", value = "/RendelesServlet")
- public class RendelesServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- BejgliTipus tipus = BejgliTipus.valueOf(request.getParameter("tipus"));
- Bejglibolt bolt = Bejglibolt.getInstance();
- int mennyiseg = parseInt(request.getParameter("db"));
- boolean rumos = request.getParameter("rumos")!=null;
- boolean vegan = request.getParameter("vegan")!=null;
- if(bolt.getBejglik().stream().anyMatch(b->b.getTipus().equals(tipus)&&b.isRumosE()==rumos&&b.isVeganE()==vegan&&b.getMennyiseg()>=mennyiseg)) {
- //if(bolt.getBejglik().stream().anyMatch(b->b.equals(new Bejgli(tipus,mennyiseg,rumos,vegan)))){
- Bejgli rendelendo = new Bejgli(tipus,mennyiseg,rumos,vegan);
- rendelendo.decreaseDarabszam(mennyiseg);
- bolt.addMegrendeles(new Megrendeles(rendelendo,mennyiseg));
- request.setAttribute("message","sikeres rendelés");
- }
- else{
- request.setAttribute("message","rendelés nem teljesíthető, mert nincs elegendő mennyiség a kért bejgliből");
- }
- request.getRequestDispatcher("rendeles.jsp").forward(request,response);
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- }
- }
- package com.example.alpotzh.model;
- public class Bejgli {
- private BejgliTipus tipus;
- public void setMennyiseg(int mennyiseg) {
- this.mennyiseg = mennyiseg;
- }
- private int mennyiseg;
- private boolean veganE;
- public BejgliTipus getTipus() {
- return tipus;
- }
- public int getMennyiseg() {
- return mennyiseg;
- }
- public boolean isVeganE() {
- return veganE;
- }
- public boolean isRumosE() {
- return rumosE;
- }
- private boolean rumosE;
- public Bejgli(BejgliTipus tipus, int mennyiseg, boolean veganE, boolean rumosE) {
- this.tipus = tipus;
- this.mennyiseg = mennyiseg;
- this.veganE = veganE;
- this.rumosE = rumosE;
- }
- @Override
- public String toString() {
- return "Íz: " + tipus +
- ", veganE=" + veganE +
- ", rumosE=" + rumosE;
- }
- public void decreaseDarabszam(int input) {
- this.mennyiseg = mennyiseg-input;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Bejgli bejgli = (Bejgli) o;
- if (veganE != bejgli.veganE) return false;
- if (rumosE != bejgli.rumosE) return false;
- return tipus == bejgli.tipus;
- }
- @Override
- public int hashCode() {
- int result = tipus.hashCode();
- result = 31 * result + (veganE ? 1 : 0);
- result = 31 * result + (rumosE ? 1 : 0);
- return result;
- }
- }
- package com.example.alpotzh.model;
- import java.util.ArrayList;
- public class Bejglibolt {
- private static Bejglibolt instance;
- public ArrayList<Bejgli> getBejglik() {
- return bejglik;
- }
- public ArrayList<Megrendeles> getMegrendelesek() {
- return megrendelesek;
- }
- private ArrayList<Bejgli> bejglik;
- private ArrayList<Megrendeles> megrendelesek;
- public static Bejglibolt getInstance() {
- if (instance == null) {
- instance = new Bejglibolt();
- }
- return instance;
- }
- private Bejglibolt() {
- this.bejglik = new ArrayList();
- this.megrendelesek = new ArrayList<>();
- Bejgli a1= new Bejgli(BejgliTipus.Dios,40,true,true);
- Bejgli a2= new Bejgli(BejgliTipus.Makos,30,false,true);
- Bejgli a3= new Bejgli(BejgliTipus.Gesztenyes,15,true,false);
- bejglik.add(a1);
- bejglik.add(a2);
- bejglik.add(a3);
- megrendelesek.add(new Megrendeles(a1,5));
- megrendelesek.add(new Megrendeles(a1,12));
- megrendelesek.add(new Megrendeles(a2,5));
- megrendelesek.add(new Megrendeles(a3,5));
- }
- public Megrendeles findRendelesId(int id){
- return megrendelesek.stream().filter(a->a.getId()==id).findFirst().orElse(null);
- }
- public void addMegrendeles(Megrendeles m){
- //darabszám csökkentése nem sikerült, voltak nullpointerexceptionok teszt közben
- //bejglik.stream().filter(b->b.getTipus().equals(m.getRendelendo().getTipus())&&b.isRumosE()==m.getRendelendo().isRumosE()&&b.isVeganE()==m.getRendelendo().isVeganE()&&b.getMennyiseg()>=m.getRendelendo().getMennyiseg()).findFirst().get().decreaseDarabszam(m.getDb());
- //m.getRendelendo().setMennyiseg(m.getRendelendo().getMennyiseg()-m.getDb());
- findBejgli(m.getRendelendo()).decreaseDarabszam(m.getDb());
- megrendelesek.add(m);
- }
- public Bejgli findBejgli(Bejgli bejgli){
- return bejglik.stream().filter(b->b.isVeganE()== bejgli.isVeganE()).filter(b->b.isRumosE()==bejgli.isRumosE()).filter(b->b.getTipus().equals(bejgli.getTipus())).findFirst().orElse(null);
- }
- }
- package com.example.alpotzh.model;
- public enum BejgliTipus {
- Makos,
- Dios,
- Gesztenyes
- }
- package com.example.alpotzh.model;
- public class Megrendeles {
- public int getId() {
- return id;
- }
- public int getDb() {
- return db;
- }
- private int id;
- static int nextId = 0;
- public Bejgli getRendelendo() {
- return rendelendo;
- }
- private Bejgli rendelendo;
- private int db;
- public Megrendeles(Bejgli rendelendo, int db) {
- this.id = nextId++;
- this.rendelendo = rendelendo;
- this.db = db;
- }
- }
- <%@ page import="com.example.alpotzh.model.Megrendeles" %>
- <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
- <!DOCTYPE html>
- <html>
- <head>
- <title>JSP - Hello World</title>
- </head>
- <body>
- <h1><%= "Hello World!" %>
- </h1>
- <br/>
- <form method="get" action="RendelesAdatokServlet">
- Rendelésszám: <input type="number" name="id">
- <input type="submit" value="Küldés">
- </form>
- <a href="rendeles.jsp">Rendelés</a>
- <%=
- request.getAttribute("message") == null ? "" : request.getAttribute("message")
- %>
- <%
- //if(request.getAttribute("megrendeles")!=null)
- Megrendeles megrendeles = (Megrendeles)request.getAttribute("megrendeles");
- if(megrendeles!=null){
- %>
- <table border="1">
- <tr>
- <td>
- Id: <%=megrendeles.getId()%>
- </td>
- <td>
- Bejgli: <%=megrendeles.getRendelendo()%>
- </td>
- <td>
- Mennyiség: <%=megrendeles.getDb()%>
- </td>
- </tr>
- </table>
- <%
- };
- %>
- </body>
- </html>
- <%@ page import="com.example.alpotzh.model.Bejglibolt" %>
- <%@ page import="com.example.alpotzh.model.BejgliTipus" %>
- <%@ page import="com.example.alpotzh.model.Bejgli" %><%--
- Created by IntelliJ IDEA.
- User: kklev
- Date: 2022. 01. 03.
- Time: 14:48
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Rendelés</title>
- </head>
- <body>
- <h1>
- Rendelj bejglit :)
- </h1>
- <form method="get" action="RendelesServlet">
- Ízesítés: <select name="tipus">
- <option value="<%=BejgliTipus.Dios%>">Diós</option>
- <option value="<%=BejgliTipus.Makos%>">Mákos</option>
- <option value="<%=BejgliTipus.Gesztenyes%>">Gesztenyés</option>
- </select><br/>
- Rumos? <input type="checkbox" name="rumos"><br/>
- Vegán? <input type="checkbox" name="vegan"><br/>
- Mennyiség: <input type="number" name="db"><br/>
- <input type="submit" value="Rendelés">
- </form><br/>
- <%=
- request.getAttribute("message") == null ? "" : request.getAttribute("message")
- %>
- </body>
- </html>
Add Comment
Please, Sign In to add comment