diff --git a/src/main/java/de/rwth/swc/sqa/DataService.java b/src/main/java/de/rwth/swc/sqa/DataService.java index e1f5cdcf6b4c87d90ee9729d322a7d7ab7b261de..38b029fd5f3e2fa5ec2ecb094f03a0062530cea3 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 773dc103ec6b0a15ac0654cc410723c49b874b2c..5bb6d0447822c20d3fb78967d12e4b5014361402 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 e2e9f39eb1799e6e9873cc0b270693820ba3c802..4ee10aa0084a9e36f07e5e33c0e551c74ed05eb5 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 049768fb6e2883ba8411e7060fb8395dfc4775b4..7f2ba1a9e0e72d2cc782ea478bd4148025275409 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 4d898012c02517a5e3c1d93238008c85d630ec19..cc84839f70858b0755a645f7dbaf0ee5d10b67bd 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//信息一致,且有效期内