From 055fd8ef5943e1c0c03936245808cd1e1692a803 Mon Sep 17 00:00:00 2001 From: Sara Prifti <72574638+sarapr97@users.noreply.github.com> Date: Thu, 26 May 2022 21:45:02 +0200 Subject: [PATCH] Added check methods for Age of Customer and Ticket Type --- .../java/de/rwth/swc/sqa/DataService.java | 97 +++++++++++++++++++ .../rwth/swc/sqa/api/CustomerController.java | 6 +- .../de/rwth/swc/sqa/api/TicketController.java | 14 ++- .../java/de/rwth/swc/sqa/CustomerTest.java | 7 ++ src/test/java/de/rwth/swc/sqa/TicketTest.java | 4 +- 5 files changed, 122 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/rwth/swc/sqa/DataService.java b/src/main/java/de/rwth/swc/sqa/DataService.java index e1f5cdc..38b029f 100644 --- a/src/main/java/de/rwth/swc/sqa/DataService.java +++ b/src/main/java/de/rwth/swc/sqa/DataService.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonSetter; import de.rwth.swc.sqa.model.Customer; import de.rwth.swc.sqa.model.DiscountCard; import de.rwth.swc.sqa.model.Ticket; +import de.rwth.swc.sqa.model.TicketValidationRequest; import lombok.Data; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; @@ -28,4 +29,100 @@ public class DataService { public void saveDiscountCardToCustomer(Long customer_id, List<DiscountCard> discountCard){ discountCardMap.put(customer_id,discountCard); } + + //check restirctions given in the pdf file + + //1.Check if the bought ticket can be applied for the customer + public static boolean ticketIsValidForTheCustomerAge(Customer customer, Ticket ticket){ + boolean isValid=false; + //if customer is a senior + if(DataService.customerIsSenior(customer)){ + if(ticket.getValidFor().getValue().equals("1d") || ticket.getValidFor().getValue().equals("30d") || ticket.getValidFor().getValue().equals("1y")){ + isValid=true; + return isValid; + } + //if customer is a student + }else if (DataService.customerIsStudent(customer)){ + if(ticket.getValidFor().getValue().equals("30d") || ticket.getValidFor().getValue().equals("1y")){ + isValid=true; + return isValid; + } + + }else if (DataService.customerIsChild(customer)){ + if(ticket.getValidFor().getValue().equals("1h") || ticket.getValidFor().getValue().equals("30d") || ticket.getValidFor().getValue().equals("1y")){ + isValid=true; + return isValid; + } + + }else if (DataService.customerIsAdult(customer)){ + isValid=true; + return isValid; + } + return isValid; + } + + //check if the discountCard can be applied for the requested ticket + public static boolean discountCanBeApplied(DiscountCard discountCard, TicketValidationRequest ticketValidationRequest){ + boolean applyDiscount=false; + if(discountCard.getId().equals(ticketValidationRequest.getDiscountCardId())){ + if(ticketValidationRequest.getZone().getValue().equals("30d")|| ticketValidationRequest.getZone().getValue().equals("1y")){ + //apply type 25 and 50 + applyDiscount=true; + } + } + + return applyDiscount; + } + + + //customer is senior + //Senior + //born before 1962 + public static boolean customerIsSenior(Customer customer){ + boolean isSenior= false; + //senior customer + if(Integer.valueOf(customer.getBirthdate().substring(0,4)) <= 1962 ){ + isSenior=true; + } + + return isSenior; + } + + + //customer is a child + //born before 2008 + public static boolean customerIsChild(Customer customer){ + boolean isChild= false; + if(Integer.valueOf(customer.getBirthdate().substring(0,4)) <= 2008 ){ + isChild=true; + } + + return isChild; + } + + + //Student + //born after 1994 + public static boolean customerIsStudent(Customer customer){ + boolean isStudent= false; + if(Integer.valueOf(customer.getBirthdate().substring(0,4)) <= 1994 && !customerIsChild(customer) ){ + isStudent=true; + } + + return isStudent; + } + + //Adult + //after 2008 + public static boolean customerIsAdult(Customer customer){ + boolean isAdult = false; + if(customerIsChild(customer)==false){ + isAdult=true; + } + return isAdult; + } + + + + } diff --git a/src/main/java/de/rwth/swc/sqa/api/CustomerController.java b/src/main/java/de/rwth/swc/sqa/api/CustomerController.java index 773dc10..5bb6d04 100644 --- a/src/main/java/de/rwth/swc/sqa/api/CustomerController.java +++ b/src/main/java/de/rwth/swc/sqa/api/CustomerController.java @@ -73,8 +73,10 @@ public class CustomerController implements CustomersApi{ }else { discountCardList.add(body); } - }else { - //if the discount card does not exists for the customer id + + + }else { + List<DiscountCard> discountCardList = new ArrayList<DiscountCard>(); discountCardList.add(body); DataService.discountCardMap.put(customerId, discountCardList); diff --git a/src/main/java/de/rwth/swc/sqa/api/TicketController.java b/src/main/java/de/rwth/swc/sqa/api/TicketController.java index e2e9f39..4ee10aa 100644 --- a/src/main/java/de/rwth/swc/sqa/api/TicketController.java +++ b/src/main/java/de/rwth/swc/sqa/api/TicketController.java @@ -5,6 +5,7 @@ import java.util.List; import javax.validation.Valid; +import de.rwth.swc.sqa.model.Customer; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; @@ -24,6 +25,7 @@ public class TicketController implements TicketsApi{ @PostMapping("") public ResponseEntity<Ticket> buyTicket(@ApiParam(value = "TicketRequest object" ,required=true ) @Valid @RequestBody TicketRequest body) { try { + // create Ticket Ticket ticket = new Ticket(); ticket.setId(System.currentTimeMillis()); ticket.setBirthdate(body.getBirthdate()); @@ -34,8 +36,16 @@ public class TicketController implements TicketsApi{ ticket.setValidFor(Ticket.ValidForEnum.fromValue(body.getValidFor().getValue())); ticket.setValidFrom(body.getValidFrom()); ticket.setZone(Ticket.ZoneEnum.fromValue(body.getZone().getValue())); - DataService.ticketList.add(ticket); - return ResponseEntity.ok().body(ticket); + + Customer customer=new Customer(); + customer.setBirthdate(body.getBirthdate()); + + //check if ticket is valid for the given inputs and customer is allowed to buy it + if(DataService.ticketIsValidForTheCustomerAge(customer,ticket)){ + DataService.ticketList.add(ticket); + return ResponseEntity.ok().body(ticket); + } + return ResponseEntity.status(400).body(null); }catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(400).body(null); diff --git a/src/test/java/de/rwth/swc/sqa/CustomerTest.java b/src/test/java/de/rwth/swc/sqa/CustomerTest.java index 049768f..7f2ba1a 100644 --- a/src/test/java/de/rwth/swc/sqa/CustomerTest.java +++ b/src/test/java/de/rwth/swc/sqa/CustomerTest.java @@ -136,6 +136,13 @@ public class CustomerTest { } + + + + + + + } diff --git a/src/test/java/de/rwth/swc/sqa/TicketTest.java b/src/test/java/de/rwth/swc/sqa/TicketTest.java index 4d89801..cc84839 100644 --- a/src/test/java/de/rwth/swc/sqa/TicketTest.java +++ b/src/test/java/de/rwth/swc/sqa/TicketTest.java @@ -53,7 +53,7 @@ public class TicketTest { parms1.put("disabled", true); parms1.put("validFrom", "2022-05-20 10:00:00"); RestAssured.given().header("content-Type", "application/json").and() - .body(parms1).when().post("/tickets").then().statusCode(200); + .body(parms1).when().post("/tickets").then().statusCode(400); parms1.clear(); parms1.put("birthdate", "1992-01-01"); @@ -103,7 +103,7 @@ public class TicketTest { parms.put("disabled", true); parms.put("date", "2022-05-22 10:00:00"); RestAssured.given().header("content-Type", "application/json").and() - .body(parms).when().post(path).then().statusCode(403); + .body(parms).when().post(path).then().statusCode(200); //200//ä¿¡æ¯ä¸€è‡´ï¼Œä¸”有效期内 -- GitLab