|
| 1 | +package com.basicsstrong.reactive.section1; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | + |
| 6 | +public class Book implements SubjectLibrary { |
| 7 | + |
| 8 | + private String name; |
| 9 | + private String type; |
| 10 | + private String author; |
| 11 | + private double price; |
| 12 | + private String inStock; |
| 13 | + private ArrayList<Observer> obsList = new ArrayList<Observer>(); |
| 14 | + |
| 15 | + public Book(String name, String type, String author, double price, String inStock) { |
| 16 | + this.name = name; |
| 17 | + this.type = type; |
| 18 | + this.author = author; |
| 19 | + this.price = price; |
| 20 | + this.inStock = inStock; |
| 21 | + } |
| 22 | + |
| 23 | + public String getName() { |
| 24 | + return name; |
| 25 | + } |
| 26 | + |
| 27 | + public void setName(String name) { |
| 28 | + this.name = name; |
| 29 | + } |
| 30 | + |
| 31 | + public String getType() { |
| 32 | + return type; |
| 33 | + } |
| 34 | + |
| 35 | + public void setType(String type) { |
| 36 | + this.type = type; |
| 37 | + } |
| 38 | + |
| 39 | + public String getAuthor() { |
| 40 | + return author; |
| 41 | + } |
| 42 | + |
| 43 | + public void setAuthor(String author) { |
| 44 | + this.author = author; |
| 45 | + } |
| 46 | + |
| 47 | + public double getPrice() { |
| 48 | + return price; |
| 49 | + } |
| 50 | + |
| 51 | + public void setPrice(double price) { |
| 52 | + this.price = price; |
| 53 | + } |
| 54 | + |
| 55 | + public String getInStock() { |
| 56 | + return inStock; |
| 57 | + } |
| 58 | + |
| 59 | + public void setInStock(String inStock) { |
| 60 | + this.inStock = inStock; |
| 61 | + System.out.println("Availability changed from Sold out to Back in stock \n"); |
| 62 | + notifyObserver(); |
| 63 | + } |
| 64 | + |
| 65 | + public ArrayList<Observer> getObsList() { |
| 66 | + return obsList; |
| 67 | + } |
| 68 | + |
| 69 | + public void setObsList(ArrayList<Observer> obsList) { |
| 70 | + this.obsList = obsList; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void subscribeObserver(Observer ob) { |
| 75 | + |
| 76 | + obsList.add(ob); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void unsubscribeObserver(Observer ob) { |
| 81 | + obsList.remove(ob); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void notifyObserver() { |
| 87 | + System.out.println( |
| 88 | + "Book name : " +this.name + |
| 89 | + ",Book Type : "+ this.type + |
| 90 | + ",Price : " + this.price+ |
| 91 | + ",Author : "+ this.author+ |
| 92 | + ", is now "+this.inStock+". So, please notify all users.\n" |
| 93 | + ); |
| 94 | + |
| 95 | + for(Observer o : obsList) |
| 96 | + { |
| 97 | + o.update(this.inStock); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments