Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cashregister;
- import java.util.Collection;
- import cashregister.ui.ICashRegisterUI;
- import container.Container;
- import managementserver.ISubjectManagementServer;
- import paymentprovider.IPayment;
- import rbvs.product.IShoppingCartElement;
- import rbvs.product.Product;
- import rbvs.record.IInvoice;
- import rbvs.record.Invoice;
- import tree.ITree;
- import tree.ProductTree;
- import util.Tuple;
- import util.searchable.ProductNameFilter;
- public class CashRegister extends Object
- implements ICashRegister, IObserver {
- private long id;
- private Collection<IInvoice> records;
- private Collection<ICashRegisterUI> uis;
- private ITree<rbvs.product.IProduct> products = new ProductTree();
- private Collection<Tuple<ISubjectManagementServer,Boolean>> subjects;
- private Collection<IShoppingCart> shoppingCarts; //this is nowhere to be seen on diagram but i cant do anything without it
- public CashRegister(long id){
- this.id = id;
- this.shoppingCarts = new Container<>();
- this.records = new Container<>();
- this.uis = new Container<>();
- this.subjects = new Container<>();
- }
- @Override
- public void activateNotifications(ISubjectManagementServer subject) {
- // TODO Auto-generated method stub
- for (Tuple<ISubjectManagementServer, Boolean> t : subjects) {
- if(t.getValueA().equals(subject) && t.getValueB() == false)
- t.setValueB(true);
- }
- this.subjects.add(new Tuple<ISubjectManagementServer, Boolean>(subject, true));
- }
- @Override
- public void deactivateNotifications(ISubjectManagementServer subject) {
- // TODO Auto-generated method stub
- for(Tuple<ISubjectManagementServer, Boolean> t : subjects) {
- if(t.valueA.equals(subject) && t.valueB.equals(true)) {
- t.setValueB(false);
- }
- }
- }
- @Override
- public void notifyChange(ISubjectManagementServer subject) {
- // TODO Auto-generated method stub
- for (Tuple<ISubjectManagementServer, Boolean> t : subjects) {
- if (t.getValueA().equals(subject) && t.getValueB() == true){
- this.products = subject.getChanges();
- }
- }
- }
- public boolean addProductToShoppingCart​(long id, IShoppingCartElement element) {
- // TODO Auto-generated method stub
- if(element != null && id > 0) {
- for(IShoppingCart s : this.shoppingCarts) {
- if(s.getShoppingCartID() == id) {
- s.addElement(element.deepCopy());
- return true;
- }
- }
- }
- return false;
- }
- @Override
- public Long addShoppingCart() {
- // TODO Auto-generated method stub
- IShoppingCart cart = ShoppingCartFactory.createShoppingCart();
- this.shoppingCarts.add(cart);
- return cart.getShoppingCartID();
- }
- @Override
- public void displayProducts() {
- // TODO Auto-generated method stub
- for(ICashRegisterUI u : uis){
- u.displayProducts(products);
- }
- }
- @Override
- public void displayRecords() {
- // TODO Auto-generated method stub
- for(ICashRegisterUI u : uis){
- u.displayRecords(records);
- }
- }
- @Override
- public void displayShoppingCart(long id) {
- // TODO Auto-generated method stub
- for(ICashRegisterUI i : uis){
- i.displayShoppingCart(this.findShoppingCartById(id));
- }
- }
- @Override
- public void displayShoppingCarts() {
- // TODO Auto-generated method stub
- for(ICashRegisterUI i : uis){
- i.displayShoppingCarts(shoppingCarts);
- }
- }
- @Override
- public long getID() {
- // TODO Auto-generated method stub
- return this.id;
- }
- @Override
- public Collection<IShoppingCart> getShoppingCarts() {
- // TODO Auto-generated method stub
- return this.shoppingCarts;
- }
- @Override
- public IInvoice payShoppingCart(long id, IPayment provider) throws ShoppingCartNotFoundException {
- Collection<IShoppingCartElement> elements = new Container<>();
- float sum = 0;
- IShoppingCart cart = findShoppingCartById(id);
- if(cart == null) {
- throw new ShoppingCartNotFoundException(this.id, id);
- }
- for(IShoppingCartElement element : cart.currentElements()) {
- sum += element.getPrice();
- }
- rbvs.record.PaymentTransaction payment = provider.pay(sum);
- IInvoice invoice = new Invoice();
- for(IShoppingCartElement element : cart.currentElements()) {
- elements.add(element.deepCopy());
- }
- invoice.setInvoiceProducts(elements);
- invoice.addPaymentTransaction(payment);
- records.add(invoice);
- cart.currentElements().clear();
- return invoice;
- }
- protected IShoppingCart findShoppingCartById(Long id) {
- for(IShoppingCart cart : this.getShoppingCarts()) {
- if(cart.getShoppingCartID().equals(id)) {
- return cart;
- }
- }
- return null;
- }
- @Override
- public void registerCashRegisterUI(ICashRegisterUI arg0) {
- // TODO Auto-generated method stub
- if(!uis.contains(arg0)) {
- uis.add(arg0);
- }
- }
- @Override
- public IShoppingCartElement selectProduct(String product) {
- // TODO Auto-generated method stub
- return (IShoppingCartElement) this.products.searchByFilter(new ProductNameFilter(), product);
- }
- @Override
- public IShoppingCartElement selectProduct(Product arg0) {
- // TODO Auto-generated method stub
- return (IShoppingCartElement) this.products.findNode(arg0);
- }
- @Override
- public void unregisterCashRegisterUI(ICashRegisterUI arg0) {
- // TODO Auto-generated method stub
- if(uis.contains(arg0)){
- uis.remove(arg0);
- }
- }
- @Override
- public boolean addProductToShoppingCart(long id, IShoppingCartElement element) {
- // TODO Auto-generated method stub
- if(element != null && id > 0) {
- for(IShoppingCart s : this.shoppingCarts) {
- if(s.getShoppingCartID() == id) {
- s.addElement(element.deepCopy());
- return true;
- }
- }
- }
- return false;
- //return false;
- }
- //konsturktor
- /*public CashRegister(long id) {
- this.id=id;
- this.records = new Container<>();
- this.uis = new Container<>();
- this.subjects = new Container<>();
- //this.products = new Container<>();
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment