Advertisement
Guest User

Untitled

a guest
Feb 13th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. ```public class Item {
  2.  
  3.     private String person;
  4.     private String name;
  5.     private Integer quantity;
  6.     private Integer price;
  7.    
  8.  
  9.      public Item(String name, int price) {
  10.  
  11.         this.person = "";
  12.         this.quantity = 0;
  13.         this.name = name;
  14.         this.price = price;
  15.       }
  16.  
  17.     public Item(String person, String name, int quantity, int price) {
  18.  
  19.         this.name = name;
  20.         this.person = person;
  21.         this.quantity = quantity;
  22.         this.price = price;
  23.     }
  24.  
  25.     @JsonProperty
  26.     public String getPerson() {
  27.         return this.person;
  28.     }
  29.  
  30.  
  31.     @JsonProperty
  32.     public String getName(){
  33.         return this.name;
  34.     }
  35.  
  36.     @JsonProperty
  37.     public Integer getQuantity(){
  38.         return this.quantity;
  39.     }
  40.  
  41.     @JsonProperty
  42.     public Integer getPrice(){
  43.         return this.price;
  44.     }
  45.  
  46. }
  47. ____________________________
  48. public class Menu {
  49.  
  50.     private Integer id;
  51.     private String name;
  52.     private List<Item> items;
  53.  
  54.     public Menu() { }
  55.  
  56.     public Menu(int id, String name, List<Item> items) {
  57.         this.id = id;
  58.         this.name = name;
  59.         this.items = items;
  60.     }
  61.  
  62.  
  63.     @JsonProperty
  64.     public List<Item> getItems() {
  65.         return this.items;
  66.     }
  67.  
  68.     @JsonProperty
  69.     public void setItems(List<Item> items){
  70.         this.items = items;
  71.     }
  72.  
  73.     @JsonProperty
  74.     public Integer getId() {
  75.  
  76.         return this.id;
  77.     }
  78.  
  79.     @JsonProperty
  80.     public String getName() {
  81.         return this.name;
  82.     }
  83.  
  84.     @JsonProperty
  85.     public void setId(final int id) {
  86.         this.id = id;
  87.     }
  88.  
  89.     @JsonProperty
  90.     public void setName(final String name) {
  91.         this.name = name;
  92.     }
  93. }
  94. _____________________________
  95.  
  96. ``` public MenuRepository() {
  97.         this.menus = new HashMap<>();
  98.         for (int i=1; i<9; i++) {
  99.             Item item = new Item("Item " + i, i+200);
  100.             this.items.add(item);
  101.         }
  102.  
  103.         addNewMenu(new Menu(1, "First Menu", this.items));
  104.         addNewMenu(new Menu(2, "Second Menu", this.items));
  105.         addNewMenu(new Menu(3, "Third Menu", this.items));
  106.         addNewMenu(new Menu(4, "Fourth Menu", this.items));
  107.         addNewMenu(new Menu(5, "Fifth Menu", this.items));
  108.         addNewMenu(new Menu(6, "Sixth Menu", this.items));
  109.         addNewMenu(new Menu(7, "Seventh Menu", this.items));
  110.        
  111.     }
  112.  
  113.     private int getNewId() {
  114.         return counter++;
  115.     }
  116.  public Collection<Menu> getAllMenus() {
  117.         return this.menus.values();
  118.     }
  119.  
  120.     public Menu get(final int id) {
  121.         return this.menus.get(id);
  122.     }
  123.  
  124.     public Collection<Menu> addNewMenu(final Menu menu) {
  125.  
  126.         menu.setId(this.getNewId());
  127.         this.menus.put(menu.getId(), menu);
  128.         return this.menus.values();
  129.  
  130.     }
  131.  
  132.     public Collection<Menu> removeMenu(final int id) {
  133.         this.menus.remove(id);
  134.         return this.menus.values();
  135.     }```
  136.  
  137. ```@Path("/menu")
  138. @Produces(MediaType.APPLICATION_JSON)
  139.  
  140. public class MenuResource {
  141.  
  142.     private MenuRepository menuRepository;
  143.  
  144.     public MenuResource(final MenuRepository menuRepository) {
  145.         this.menuRepository = menuRepository;
  146.     }
  147.  
  148.     @GET
  149.     @Timed
  150.     public Collection<Menu> getAll() {
  151.         return this.menuRepository.getAllMenus();
  152.     }
  153.  
  154.     @GET
  155.     @Path("/{id}")
  156.     @Timed
  157.     public Menu get(@PathParam("id") final int id) {
  158.  
  159.  
  160.         return this.menuRepository.get(id);
  161.     }
  162.  
  163.     @POST
  164.     @Timed
  165.     public Collection<Menu> post(final Menu menu) {
  166.         return this.menuRepository.addNewMenu(menu);
  167.     }
  168.  
  169.     @DELETE
  170.     @Path("/{id}")
  171.     @Timed
  172.     public Collection<Menu> delete(@PathParam("id") final int id) {
  173.         return this.menuRepository.removeMenu(id);
  174.     }```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement