EldiraSesto

CashRegister

Jun 9th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.07 KB | None | 0 0
  1. package cashregister;
  2.  
  3. import java.util.Collection;
  4.  
  5. import cashregister.ui.ICashRegisterUI;
  6. import container.Container;
  7. import managementserver.ISubjectManagementServer;
  8. import paymentprovider.IPayment;
  9. import rbvs.product.IShoppingCartElement;
  10. import rbvs.product.Product;
  11. import rbvs.record.IInvoice;
  12. import rbvs.record.Invoice;
  13. import tree.ITree;
  14. import tree.ProductTree;
  15. import util.Tuple;
  16. import util.searchable.ProductNameFilter;
  17.  
  18. public class CashRegister extends Object
  19. implements ICashRegister, IObserver {
  20.  
  21.     private long id;
  22.     private Collection<IInvoice> records;
  23.     private Collection<ICashRegisterUI> uis;
  24.     private ITree<rbvs.product.IProduct> products = new ProductTree();
  25.     private Collection<Tuple<ISubjectManagementServer,Boolean>> subjects;
  26.     private Collection<IShoppingCart> shoppingCarts; //this is nowhere to be seen on diagram but i cant do anything without it
  27.    
  28.     public CashRegister(long id){
  29.         this.id = id;
  30.         this.shoppingCarts = new Container<>();
  31.         this.records = new Container<>();
  32.         this.uis = new Container<>();
  33.         this.subjects = new Container<>();
  34.     }
  35.    
  36.    
  37.     @Override
  38.     public void activateNotifications(ISubjectManagementServer subject) {
  39.         // TODO Auto-generated method stub
  40.         for (Tuple<ISubjectManagementServer, Boolean> t : subjects) {
  41.             if(t.getValueA().equals(subject) && t.getValueB() == false)
  42.                 t.setValueB(true);
  43.         }
  44.         this.subjects.add(new Tuple<ISubjectManagementServer, Boolean>(subject, true));
  45.         }
  46.        
  47.        
  48.    
  49.  
  50.     @Override
  51.     public void deactivateNotifications(ISubjectManagementServer subject) {
  52.         // TODO Auto-generated method stub
  53.         for(Tuple<ISubjectManagementServer, Boolean> t : subjects) {
  54.             if(t.valueA.equals(subject) && t.valueB.equals(true)) {
  55.                 t.setValueB(false);
  56.             }
  57.         }
  58.        
  59.     }
  60.  
  61.     @Override
  62.     public void notifyChange(ISubjectManagementServer subject) {
  63.         // TODO Auto-generated method stub
  64.         for (Tuple<ISubjectManagementServer, Boolean> t : subjects) {
  65.             if (t.getValueA().equals(subject) && t.getValueB() == true){
  66.                 this.products = subject.getChanges();
  67.                
  68.             }
  69.         }
  70.        
  71.     }
  72.  
  73.     public boolean addProductToShoppingCart​(long id, IShoppingCartElement element) {
  74.         // TODO Auto-generated method stub
  75.         if(element != null && id > 0) {
  76.             for(IShoppingCart s : this.shoppingCarts) {
  77.                 if(s.getShoppingCartID() == id) {
  78.                     s.addElement(element.deepCopy());
  79.                     return true;
  80.                 }
  81.             }
  82.         }
  83.         return false;
  84.        
  85.     }
  86.  
  87.     @Override
  88.     public Long addShoppingCart() {
  89.         // TODO Auto-generated method stub
  90.         IShoppingCart cart = ShoppingCartFactory.createShoppingCart();
  91.         this.shoppingCarts.add(cart);
  92.         return cart.getShoppingCartID();
  93.     }
  94.  
  95.     @Override
  96.     public void displayProducts() {
  97.         // TODO Auto-generated method stub
  98.          for(ICashRegisterUI u : uis){
  99.                 u.displayProducts(products);
  100.             }
  101.          }
  102.  
  103.     @Override
  104.     public void displayRecords() {
  105.         // TODO Auto-generated method stub
  106.          for(ICashRegisterUI u : uis){
  107.                 u.displayRecords(records);
  108.             }
  109.     }
  110.  
  111.     @Override
  112.     public void displayShoppingCart(long id) {
  113.         // TODO Auto-generated method stub
  114.         for(ICashRegisterUI i : uis){
  115.             i.displayShoppingCart(this.findShoppingCartById(id));
  116.         }
  117.        
  118.     }
  119.  
  120.     @Override
  121.     public void displayShoppingCarts() {
  122.         // TODO Auto-generated method stub
  123.          for(ICashRegisterUI i : uis){
  124.                 i.displayShoppingCarts(shoppingCarts);
  125.             }
  126.  
  127.        
  128.     }
  129.  
  130.     @Override
  131.     public long getID() {
  132.         // TODO Auto-generated method stub
  133.         return this.id;
  134.     }
  135.  
  136.     @Override
  137.     public Collection<IShoppingCart> getShoppingCarts() {
  138.         // TODO Auto-generated method stub
  139.         return this.shoppingCarts;
  140.        
  141.     }
  142.  
  143.     @Override
  144.     public IInvoice payShoppingCart(long id, IPayment provider) throws ShoppingCartNotFoundException {
  145.         Collection<IShoppingCartElement> elements = new Container<>();
  146.         float sum = 0;
  147.  
  148.         IShoppingCart cart = findShoppingCartById(id);
  149.         if(cart == null) {
  150.             throw new ShoppingCartNotFoundException(this.id, id);
  151.         }
  152.         for(IShoppingCartElement element : cart.currentElements()) {
  153.             sum += element.getPrice();
  154.         }
  155.         rbvs.record.PaymentTransaction payment = provider.pay(sum);
  156.         IInvoice invoice = new Invoice();
  157.         for(IShoppingCartElement element : cart.currentElements()) {
  158.             elements.add(element.deepCopy());
  159.         }
  160.         invoice.setInvoiceProducts(elements);
  161.         invoice.addPaymentTransaction(payment);
  162.         records.add(invoice);
  163.         cart.currentElements().clear();
  164.         return invoice;
  165.     }
  166.  
  167.     protected IShoppingCart findShoppingCartById(Long id) {
  168.         for(IShoppingCart cart : this.getShoppingCarts()) {
  169.             if(cart.getShoppingCartID().equals(id)) {
  170.                 return cart;
  171.             }
  172.         }
  173.         return null;
  174.        
  175.         }
  176.  
  177.     @Override
  178.     public void registerCashRegisterUI(ICashRegisterUI arg0) {
  179.         // TODO Auto-generated method stub
  180.         if(!uis.contains(arg0)) {
  181.             uis.add(arg0);
  182.         }
  183.          
  184.     }
  185.  
  186.     @Override
  187.     public IShoppingCartElement selectProduct(String product) {
  188.         // TODO Auto-generated method stub
  189.           return (IShoppingCartElement) this.products.searchByFilter(new ProductNameFilter(), product);
  190.        
  191.     }
  192.  
  193.     @Override
  194.     public IShoppingCartElement selectProduct(Product arg0) {
  195.         // TODO Auto-generated method stub
  196.         return (IShoppingCartElement) this.products.findNode(arg0);
  197.  
  198.     }
  199.  
  200.     @Override
  201.     public void unregisterCashRegisterUI(ICashRegisterUI arg0) {
  202.         // TODO Auto-generated method stub
  203.          if(uis.contains(arg0)){
  204.                 uis.remove(arg0);
  205.             }
  206.     }
  207.  
  208.  
  209.     @Override
  210.     public boolean addProductToShoppingCart(long id, IShoppingCartElement element) {
  211.         // TODO Auto-generated method stub
  212.          if(element != null && id > 0) {
  213.                 for(IShoppingCart s : this.shoppingCarts) {
  214.                     if(s.getShoppingCartID() == id) {
  215.                         s.addElement(element.deepCopy());
  216.                         return true;
  217.                     }
  218.                 }
  219.             }
  220.             return false;
  221.         //return false;
  222.     }
  223.  
  224.     //konsturktor
  225.     /*public CashRegister(long id) {
  226.         this.id=id;
  227.         this.records = new Container<>();
  228.         this.uis = new Container<>();
  229.         this.subjects = new Container<>();
  230.         //this.products = new Container<>();
  231.        
  232.     }
  233. */
  234.  
  235.  
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment