Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. class Product {
  2.     private int catId;
  3.     private int channelPrice;
  4.  
  5.     public Product(int catId) {
  6.         this.catId = catId;
  7.         this.channelPrice = -1;
  8.     }
  9.  
  10.     public Product(int catId, int channelPrice) {
  11.         this.catId = catId;
  12.         this.channelPrice = channelPrice;
  13.     }
  14.  
  15.     public int getCatId() {
  16.         return this.catId
  17.     }
  18.  
  19.     public int getChannelPrice() {
  20.         return this.channelPrice;
  21.     }
  22. }
  23.  
  24. class Category {
  25.     private int id;
  26.     public Category parent;
  27.     private int price;
  28.  
  29.     public Category(int id, Category parent) {
  30.         this.id = id;
  31.         this.parent = parent;
  32.         this.price = -1;
  33.     }
  34.  
  35.     public Category(int id, Category parent, int price) {
  36.         this.id = id;
  37.         this.parent = parent;
  38.         this.price = price;
  39.     }
  40.  
  41.     public int getId() {
  42.         return this.id;
  43.     }
  44.  
  45.     public int getPrice() {
  46.         return this.price;
  47.     }
  48.  
  49.     public void setPrice(int price) {
  50.         this.price = price;
  51.     }
  52. }
  53.  
  54. class FindPrice {
  55.     public int findFinalPrice (List<Category> categories, Product item) {
  56.         Category currentCat = null;
  57.         for (Category cat : categories) {
  58.             if (cat.getId() == item.getCatId()) {
  59.                 currentCat = cat;
  60.                 break;
  61.             }
  62.         }
  63.  
  64.         if (currentCat != null) {
  65.             Category tmpCat = currentCat;
  66.             while (currentCat.getPrice() == -1) {
  67.                 tmpCat = tmpCat.parent;
  68.             }
  69.         }
  70.         return tmpCat.getPrice() > Product.getChannelPrice() ? tmpCat.getPrice() : Product.getChannelPrice();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement