Skip to content
Snippets Groups Projects
Commit 055fd8ef authored by Sara Prifti's avatar Sara Prifti
Browse files

Added check methods for Age of Customer and Ticket Type

parent 6ff9f030
Branches Ying
No related tags found
No related merge requests found
......@@ -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;
}
}
......@@ -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);
......
......@@ -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);
......
......@@ -136,6 +136,13 @@ public class CustomerTest {
}
}
......
......@@ -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//信息一致,且有效期内
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment