Created
May 29, 2020 10:31
-
-
Save olegrewko/12d573415fd8f0f8ccdf2a455aba95ab to your computer and use it in GitHub Desktop.
Менеджер книг 01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package IgorDolgov.booklist; | |
| import java.util.ArrayList; | |
| import java.util.Scanner; | |
| class Book { | |
| String name; | |
| String writer; | |
| public Book(String name, String writer) { | |
| this.name = name; | |
| this.writer = writer; | |
| } | |
| public String getBookInfo() { | |
| return ("\"" + name + "\" Автор: " + writer); | |
| } | |
| } | |
| class Catalog { | |
| static int count; | |
| int id; | |
| ArrayList<Book> catalog = new ArrayList<Book>(); | |
| public void addBook(Book book) { | |
| catalog.add(book); | |
| count++; | |
| id = count; | |
| } | |
| public void removeBook(int id) { | |
| catalog.remove(id); | |
| System.out.println("Книга " + id + " удалена"); | |
| } | |
| public int getCountBook() { | |
| return catalog.size(); | |
| } | |
| public String getBookInfoById(int id) { | |
| Book book = catalog.get(id); | |
| return book.getBookInfo(); | |
| } | |
| public int searchBookByName(String text) { | |
| int rez = 0; | |
| String nameBook; | |
| for (int i = 0; i < getCountBook(); i++) { | |
| Book book = catalog.get(i); | |
| nameBook = book.name.toLowerCase(); | |
| if (nameBook.contains(text.toLowerCase())) { | |
| System.out.println(book.getBookInfo()); | |
| rez++; | |
| } | |
| } | |
| if (rez == 0) { | |
| rez = searchBookByWriter(text); | |
| } | |
| return rez; | |
| } | |
| public int searchBookByWriter(String text) { | |
| int rez = 0; | |
| String nameWriter; | |
| for (int i = 0; i < getCountBook(); i++) { | |
| Book book = catalog.get(i); | |
| nameWriter = book.writer.toLowerCase(); | |
| if (nameWriter.contains(text.toLowerCase())) { | |
| System.out.println(book.getBookInfo()); | |
| rez++; | |
| } | |
| } | |
| return rez; | |
| } | |
| } | |
| public class Main { | |
| public static void main(String[] args) { | |
| doWork(); | |
| } | |
| private static void doWork() { | |
| Reader reader = new Reader("Oleg"); | |
| Catalog catalog = new Catalog(); | |
| Book book1 = new Book("Почтамт", "Буковски"); | |
| Book book2 = new Book("Женщины", "Буковски"); | |
| catalog.addBook(book1); | |
| catalog.addBook(book2); | |
| System.out.println(catalog.getBookInfoById(0)); | |
| System.out.println(catalog.getBookInfoById(1)); | |
| System.out.println("Сейчас в каталоге книг - " + catalog.getCountBook()); | |
| int what; | |
| for (int i = 0; i < 10; i++) { | |
| System.out.println("Что будем делать: 0-Добавить книгу; 1-Найти книгу; 2-Удалить книгу; 3-Показать список книг; 4-Завершить работу программы"); | |
| what = whatDo(reader); | |
| if (what == 1) { | |
| searchBook(reader, catalog); | |
| } | |
| if (what == 0) { | |
| addCatalogBook(reader, catalog); | |
| System.out.println("В каталоге книг - " + catalog.getCountBook()); | |
| } | |
| if (what == 3) { | |
| for (int u = 0; u < catalog.getCountBook(); u++) | |
| System.out.println(catalog.getBookInfoById(u)); | |
| System.out.println("Сейчас в каталоге книг - " + catalog.getCountBook()); | |
| } | |
| if (what == 4) { | |
| System.out.println("Работа программы завершена "); | |
| break; | |
| } | |
| if (what == 2) { | |
| if (catalog.getCountBook() > 0) { | |
| removeCatalogBook(reader, catalog); | |
| System.out.println("В каталоге осталось книг - " + catalog.getCountBook()); | |
| } else { | |
| System.out.println("В каталоге пусто. Нет книг для удаления"); | |
| } | |
| } | |
| } | |
| } | |
| private static void removeCatalogBook(Reader reader, Catalog catalog) { | |
| int numBook; | |
| System.out.println("В каталоге книг - " + catalog.getCountBook()); | |
| System.out.println("Укажите номер удаляемой книги"); | |
| do { | |
| numBook = reader.getAnswer(); | |
| } while (numBook < 1 || numBook > catalog.getCountBook()); | |
| --numBook; | |
| System.out.println("Вы уверены что хотите удалить книну: " + catalog.getBookInfoById(numBook) + "?"); | |
| System.out.println("0-нет; 1-да"); | |
| int ans; | |
| do { | |
| ans = reader.getAnswer(); | |
| } while (ans != 0 & ans != 1); | |
| if (ans == 1) { | |
| catalog.removeBook(numBook); | |
| } | |
| } | |
| static void addCatalogBook(Reader reader, Catalog catalog) { | |
| String name; | |
| String writer; | |
| System.out.println("Укажите название книги"); | |
| name = reader.getString(); | |
| System.out.println("Укажите автора книги"); | |
| writer = reader.getString(); | |
| catalog.addBook(new Book(name, writer)); | |
| System.out.println("Книга добавлена в каталог"); | |
| } | |
| private static void searchBook(Reader reader, Catalog catalog) { | |
| System.out.println("Что ищем?"); | |
| String str = reader.getString(); | |
| int rez = catalog.searchBookByName(str); | |
| if (rez == 0) { | |
| System.out.println("Книга \"" + str + "\" не найдена"); | |
| } | |
| } | |
| private static int whatDo(Reader reader) { | |
| int d; | |
| do { | |
| d = reader.getAnswer(); | |
| if (d != 0 & d != 1 & d != 2 & d != 3 & d != 4) { | |
| System.out.println("Укажите что нужно сделать: 0-Добавить книгу; 1-Найти книгу; 2-Удалить книгу; 3-Показать список книг; 4-Завершить работу программы"); | |
| } | |
| } while (d != 0 & d != 1 & d != 2 & d != 3 & d != 4); | |
| return d; | |
| } | |
| } | |
| class Reader { | |
| String name; | |
| public Reader(String name) { | |
| this.name = name; | |
| } | |
| public int getAnswer() { | |
| int d; | |
| Scanner scanner = new Scanner(System.in); | |
| while (true) { | |
| if (scanner.hasNextInt()) { | |
| d = scanner.nextInt(); | |
| break; | |
| } else { | |
| scanner.nextLine(); | |
| } | |
| } | |
| return d; | |
| } | |
| public String getString() { | |
| String str; | |
| Scanner scanner = new Scanner(System.in); | |
| str = scanner.nextLine(); | |
| return str; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment