V
V
Vitaly2021-07-22 13:58:16
Java
Vitaly, 2021-07-22 13:58:16

How to implement a role system in Java?

I have a Reader class - it's a normal reader in a bookstore, but there also needs to be a premium reader in my "system". How can I implement this reader correctly? How to turn a reader into a premium and vice versa? And each has its own additional methods that the other does not have. I hope I wrote clearly.
Here is the code:

public class Reader {
    private String name;
    private String number;
    private int countBooks;
    private int readerTicket;

    private List<Book> book = new LinkedList<>();

    public Reader(String name, String number, int readerTicket) {
        this.name = name;
        this.number = number;
        this.readerTicket = readerTicket;
    }

    public Reader(Reader a) {
        name = a.name;
        book = a.book;
        countBooks = a.countBooks;
    }

    public void takeBook(Book nameBook) {
        for (Book b : book) {
            if (nameBook.equals(b)) {
                System.out.println("Такая книга уже есть");
                return;
            }
        }
        System.out.println("Читатель: " + name + " взял книгу: " + nameBook.getName());
        book.add(nameBook);
        countBooks++;
    }

    public void takeBook(Book[] nameBook) {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < nameBook.length; i++) {
            countBooks++;
            book.add(nameBook[i]);
            if (i == nameBook.length - 1) {
                builder.append(nameBook[i].getName());
                break;
            }
            builder.append(nameBook[i].getName()).append(", ");
        }
        System.out.println("Читатель: " + name + " взял книги: " + builder);
    }

    public void cBooks() {
        System.out.println("Пользователь: " + name + " Взял вот столько книг: " + countBooks);
    }

    public String getName() {
        return name;
    }

    public List<Book> getBook() {
        return book;
    }

    protected int getCountBooks() {
        return countBooks;
    }
}


public class Reader {
    private String name;
    private String number;
    private int countBooks;
    private int readerTicket;

    private List<Book> book = new LinkedList<>();

    public Reader(String name, String number, int readerTicket) {
        this.name = name;
        this.number = number;
        this.readerTicket = readerTicket;
    }

    public Reader(Reader a) {
        name = a.name;
        book = a.book;
        countBooks = a.countBooks;
    }

    public void takeBook(Book nameBook) {
        for (Book b : book) {
            if (nameBook.equals(b)) {
                System.out.println("Такая книга уже есть");
                return;
            }
        }
        System.out.println("Читатель: " + name + " взял книгу: " + nameBook.getName());
        book.add(nameBook);
        countBooks++;
    }

    public void takeBook(Book[] nameBook) {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < nameBook.length; i++) {
            countBooks++;
            book.add(nameBook[i]);
            if (i == nameBook.length - 1) {
                builder.append(nameBook[i].getName());
                break;
            }
            builder.append(nameBook[i].getName()).append(", ");
        }
        System.out.println("Читатель: " + name + " взял книги: " + builder);
    }

    public void cBooks() {
        System.out.println("Пользователь: " + name + " Взял вот столько книг: " + countBooks);
    }

    public String getName() {
        return name;
    }

    public List<Book> getBook() {
        return book;
    }

    protected int getCountBooks() {
        return countBooks;
    }
}


public class Reader {
    private String name;
    private String number;
    private int countBooks;
    private int readerTicket;

    private List<Book> book = new LinkedList<>();

    public Reader(String name, String number, int readerTicket) {
        this.name = name;
        this.number = number;
        this.readerTicket = readerTicket;
    }

    public Reader(Reader a) {
        name = a.name;
        book = a.book;
        countBooks = a.countBooks;
    }

    public void takeBook(Book nameBook) {
        for (Book b : book) {
            if (nameBook.equals(b)) {
                System.out.println("Такая книга уже есть");
                return;
            }
        }
        System.out.println("Читатель: " + name + " взял книгу: " + nameBook.getName());
        book.add(nameBook);
        countBooks++;
    }

    public void takeBook(Book[] nameBook) {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < nameBook.length; i++) {
            countBooks++;
            book.add(nameBook[i]);
            if (i == nameBook.length - 1) {
                builder.append(nameBook[i].getName());
                break;
            }
            builder.append(nameBook[i].getName()).append(", ");
        }
        System.out.println("Читатель: " + name + " взял книги: " + builder);
    }

    public void cBooks() {
        System.out.println("Пользователь: " + name + " Взял вот столько книг: " + countBooks);
    }

    public String getName() {
        return name;
    }

    public List<Book> getBook() {
        return book;
    }

    protected int getCountBooks() {
        return countBooks;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2021-07-22
@Maksclub

Make a separate entity of the Subscribe or Order type and link it to your role by id,
then it will be clear whether there is an order (and its appropriate status) or not.
Then you will have all the readers, but some have an active subscription / order

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question