Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package domain;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.json.JSONObject;
- import com.fasterxml.jackson.core.JsonParseException;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.JsonMappingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.google.gson.Gson;
- import com.sun.jersey.api.client.Client;
- import com.sun.jersey.api.client.ClientResponse;
- import com.sun.jersey.api.client.WebResource;
- import exceptions.DomainException;
- import ui.Menu;
- public class App {
- //private String baseURL = "http://localhost:9099/";
- private String orderID = "";
- private Menu menu = new Menu();
- protected void showWelcomeScreen() throws DomainException, JsonParseException, JsonMappingException, IOException{
- String choice = menu.showWelcomeScreen();
- createOrder();
- requestHandler(choice);
- }
- private void requestHandler(String choice) throws DomainException, JsonParseException, JsonMappingException, IOException{
- if(choice.equals(PizzaEnum.addPizza))
- addPizza();
- if(choice.equals(PizzaEnum.cancelOrder))
- cancelOrder();
- if(choice.equals(PizzaEnum.cheaperPizzas))
- cheaperPizzas();
- if(choice.equals(PizzaEnum.checkMenu))
- checkMenu();
- //if(choice.equals(PizzaEnum.createOrder))
- //createOrder();
- if(choice.equals(PizzaEnum.getPizzas))
- getPizzas();
- if(choice.equals(PizzaEnum.orderState))
- orderState();
- if(choice.equals(PizzaEnum.pizzaIngredients))
- pizzaIngredients();
- if(choice.equals(PizzaEnum.price))
- price();
- if(choice.equals(PizzaEnum.submitOrder))
- submitOrder();
- if(choice.equals(PizzaEnum.vegetarian))
- vegetarian();
- else
- throw new DomainException("A wrong choice was made.");
- }
- private void addPizza() {
- List<String> ingredients = menu.addPizza();
- String json = new Gson().toJson(ingredients);
- String response = executeManageOrderPost(generateUrlPartManageOrder(), "addPizza", json);
- menu.showResponse(response);
- }
- private void cancelOrder() {
- String response = executeManageOrderPost(generateUrlPartManageOrder(), "cancelOrder", null);
- menu.showResponse(response);
- }
- private void cheaperPizzas() throws JsonParseException, JsonMappingException, IOException {
- ObjectMapper mapper = new ObjectMapper();
- String response = executeManageOrderGet(generateUrlPartCheckMenu(), "cheaperPizzas");
- List<String> cheaperPizzas = mapper.readValue(response, new TypeReference<List<String>>(){});
- menu.showResponseList(cheaperPizzas);
- }
- private void checkMenu() {
- //TODO
- //Onvoldoende gedocumenteerd in de blueprint.
- menu.showWelcomeScreen();
- }
- private void createOrder() {
- this.orderID = executeManageOrderGet(generateUrlPartManageOrder(), "createorder");
- }
- private void getPizzas() {
- String response = executeManageOrderGet(generateUrlPartCheckMenu(), "getPizzas");
- JSONObject jsonObject = new JSONObject(response);
- List<Pizza> pizzas = new ArrayList<Pizza>();
- for(int i = 0; i < jsonObject.length(); i++){
- JSONObject jO = jsonObject.getJSONObject("Pizza");
- Pizza pizza = new Pizza(jO.getString("Name"), Double.parseDouble(jO.getString("Price")));
- JSONObject ingredients = jO.getJSONObject("Ingredients");
- for(int j = 0; i < ingredients.length(); i++){
- pizza.addIngredient(new Ingredient(ingredients.getString("Name"), ingredients.getString("Description"), Double.parseDouble(ingredients.getString("Price")), Integer.parseInt(ingredients.getString("maxAmountInStock"))));
- }
- pizzas.add(pizza);
- }
- menu.showResponseListAllPizzas(pizzas);
- }
- private void orderState() {
- String response = executeManageOrderGetOrderID(generateUrlPartManageOrder(), "orderState");
- menu.showResponse(response);
- }
- private void pizzaIngredients() {
- String input = menu.askPizzaName();
- //TODO
- //Blueprint was hier verkeerd over. je kan beter een naam vragen en dan de
- //lijst geven van ingredienten.
- menu.showWelcomeScreen();
- }
- private void price() {
- String response = executeManageOrderGetOrderID(generateUrlPartManageOrder(),"price");
- menu.showResponse(response);
- }
- private void submitOrder() {
- String response = executeManageOrderPost(generateUrlPartManageOrder(), "submitOrder", null);
- menu.showResponse(response);
- }
- private void vegetarian() {
- String response = executeManageOrderGet(generateUrlPartCheckMenu(), "vegetarian");
- JSONObject jsonObject = new JSONObject(response);
- List<Pizza> pizzas = new ArrayList<Pizza>();
- for(int i = 0; i < jsonObject.length(); i++){
- JSONObject jO = jsonObject.getJSONObject("Pizza");
- Pizza pizza = new Pizza(jO.getString("Name"), Double.parseDouble(jO.getString("Price")));
- JSONObject ingredients = jO.getJSONObject("Ingredients");
- for(int j = 0; i < ingredients.length(); i++){
- pizza.addIngredient(new Ingredient(ingredients.getString("Name"), ingredients.getString("Description"), Double.parseDouble(ingredients.getString("Price")), Integer.parseInt(ingredients.getString("maxAmountInStock"))));
- }
- pizzas.add(pizza);
- }
- menu.showResponseListAllPizzas(pizzas);
- }
- private String generateUrlPartManageOrder(){
- return "manageOrder";
- }
- private String generateUrlPartCheckMenu(){
- return "checkMenu";
- }
- private String executeManageOrderGet(String urlPathPiece, String urlPart){
- Client client = Client.create();
- WebResource webResource = client.resource("http://localhost:9099/" + urlPathPiece + "/" + urlPart);
- ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
- if(response.getStatus() != 200)
- throw new RuntimeException("Failed: HTTP error code: " + response.getStatus());
- return response.getEntity(String.class);
- }
- private String executeManageOrderGetOrderID(String urlPathPiece, String urlPart){
- Client client = Client.create();
- WebResource webResource = client.resource("http://localhost:9099/" + urlPathPiece + "/" + urlPart + "/" + orderID);
- ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
- if(response.getStatus() != 200)
- throw new RuntimeException("Failed: HTTP error code: " + response.getStatus());
- return response.getEntity(String.class);
- }
- private String executeManageOrderPost(String urlPathPiece, String urlPart, String json){
- Client client = Client.create();
- WebResource webResource = client.resource("http://localhost:9099/" + urlPathPiece + "/" + urlPart + "/" +orderID);
- ClientResponse response = webResource.type("application/json").post(ClientResponse.class, json);
- return response.getEntity(String.class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement