Created
July 2, 2020 06:17
-
-
Save olegrewko/5fb89cae508bac73010954552efb59d7 to your computer and use it in GitHub Desktop.
WR02
This file contains 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.shapeBinding; | |
import java.time.LocalDate; | |
import java.util.ArrayList; | |
import java.util.Calendar; | |
import java.util.Random; | |
import java.util.Scanner; | |
public class MainBind { | |
public static void main(String[] args) { | |
Company[] companies = new Company[4]; | |
companies[0] = new Company(); | |
companies[1] = new BookCatalog(); | |
companies[2] = new CD(); | |
companies[3] = new Cat(); | |
for (Company e : companies) { | |
e.show(); | |
} | |
Scanner scanner = new Scanner(System.in); | |
int i = scanner.nextInt(); | |
System.out.println(i); | |
switch (i) { | |
case 0: | |
companies[0].go(); | |
break; | |
case 1: | |
companies[1].go(); | |
break; | |
case 2: | |
companies[2].go(); | |
break; | |
case 3: | |
companies[3].go(); | |
break; | |
} | |
} | |
public static class Company { | |
public void show() { | |
System.out.println("[0] Моя компания: izba 500 для просмотра введите [0]"); | |
} | |
void go() { | |
System.out.println("MyCompany: izba 500"); | |
ArrayList<Employee> staff = new ArrayList<>(); | |
Employee staff0 = new Employee(1, "Carl Cracker", 75000, 1987, 12, 15); | |
Employee staff1 = new Employee(2, "Harry Hacker", 50000, 1989, 10, 1); | |
Employee staff2 = new Employee(3, "Tony Tester", 40000, 1990, 3, 15); | |
staff.add(staff0); | |
staff.add(staff1); | |
staff.add(staff2); | |
System.out.printf("Сотрудников в компании %10s%n", staff.size()); | |
int n = Employee.getNextId(); // calls static method | |
System.out.println("Next available id= " + n); | |
for (Employee e : staff) | |
System.out.printf("id= %2d name= %-16s, salary= %7.2f hireDay= %tF%n", e.id, e.getName(), e.getSalary(), | |
e.getHireDay()); | |
System.out.println("[0]-Удалить сотрудника"); | |
System.out.println("[1]-Добавить сотрудника"); | |
System.out.println("[2]-Добавить[+] или уменьшить[-] зарплату в % [-+][1-100]"); | |
System.out.println("[3]-Удалить всех сотрудников"); | |
System.out.println("[4]-Exit"); | |
Scanner scanner = new Scanner(System.in); | |
int i = scanner.nextInt(); | |
System.out.println(i); | |
switch (i) { | |
case 0: | |
System.out.println("Введите id удаляемого сотрудника"); | |
int id = scanner.nextInt(); | |
System.out.println(id); | |
staff.remove(id - 1); | |
show(staff); | |
System.out.println("Сотрудник " + id + " удален "); | |
break; | |
case 1: | |
System.out.println("Введите имя сотрудника"); | |
String str = scanner.nextLine(); | |
scanner.next(); | |
System.out.println(str); | |
System.out.println("Введите зарплату"); | |
double d = scanner.nextDouble(); | |
System.out.println(d); | |
staff.add(n - 1, new Employee("" + str, d)); | |
show(staff); | |
System.out.println("Сотрудник id= " + n + " добавлен "); | |
Calendar c = Calendar.getInstance(); | |
System.out.format("Today is %tA, %tB %te, %tY, %tl:%tM %tp%n", c, c, c, c, c, c, c); | |
break; | |
case 2: | |
System.out.println("Введите: добавить[+] или уменьшить зарплату[-] в % [-+][1.00 - 100.00]"); | |
double dp = scanner.nextDouble(); | |
// Employee.raiseSalary(dp); | |
System.out.println(dp); | |
for (Employee e : staff) | |
e.raiseSalary(dp); | |
show(staff); | |
System.out.println("Зарплата изменилась на " + dp + "%"); | |
break; | |
case 3: | |
staff.clear(); | |
break; | |
case 4: | |
System.exit(0); | |
break; | |
} | |
} | |
private void show(ArrayList<Employee> staff) { | |
for (Employee e : staff) { | |
System.out.printf("id= %2d name= %-16s, salary= %7.2f hireDay= %tF%n", e.id, e.getName(), e.getSalary(), | |
e.getHireDay()); | |
} | |
} | |
static class Employee { | |
private static int nextId = 1; | |
public static double salary; | |
public int id; | |
private String name = ""; // instance field initialization | |
private LocalDate hireDay; | |
{ | |
id = nextId; | |
nextId++; | |
} | |
public Employee(int id, String n, double s, int year, int month, int day) { | |
this.id = id; | |
name = n; | |
salary = s; | |
hireDay = LocalDate.of(year, month, day); | |
} | |
public Employee(String n, double s) { | |
this(Employee.getNextId(), n, s, 1111, 11, 11); | |
name = n; | |
salary = s; | |
} | |
public String getName() { | |
return name; | |
} | |
public double getSalary() { | |
return salary; | |
} | |
public static int getNextId() { | |
return nextId; | |
} | |
public LocalDate getHireDay() { | |
return hireDay; | |
} | |
public static double raiseSalary(double dp) { | |
double raise = salary * dp / 100; | |
salary += raise; | |
return salary; | |
} | |
} | |
} | |
public static class BookCatalog extends Company { | |
public void show() { | |
System.out.println("[1] Моя библиотека введите [1] для просмотра"); | |
} | |
public void go() { | |
Reader reader = new Reader("Oleg"); | |
Catalog catalog = new Catalog(); | |
Book book1 = new Book("Почтамт", "Буковски"); | |
Book book2 = new Book("Женщины", "Буковски"); | |
Book book3 = new Book("Фактотум", "Буковски"); | |
Book book4 = new Book("Голливуд", "Буковски"); | |
catalog.addBook(book1); | |
catalog.addBook(book2); | |
catalog.addBook(book3); | |
catalog.addBook(book4); | |
catalog.getBookInfoById(0); | |
catalog.getBookInfoById(1); | |
catalog.getBookInfoById(2); | |
catalog.getBookInfoById(3); | |
System.out.println("[1] Моя библиотека"); | |
show(catalog); | |
int what; | |
for (int i = 0; i < 10; i++) { | |
System.out.println("Что будем делать: 0-Добавить книгу; 1-Найти книгу; 2-Удалить книгу; 3-Показать список книг; 4-Завершить работу программы"); | |
what = whatDo(reader); | |
if (what == 0) { | |
addCatalogBook(reader, catalog); | |
show(catalog); | |
} | |
if (what == 1) { | |
searchBook(reader, catalog); | |
} | |
if (what == 2) { | |
if (catalog.getCountBook() > 0) { | |
removeCatalogBook(reader, catalog); | |
System.out.println("В каталоге осталось книг - " + catalog.getCountBook()); | |
} else { | |
System.out.println("В каталоге пусто. Нет книг для удаления"); | |
} | |
show(catalog); | |
} | |
if (what == 3) { | |
show(catalog); | |
} | |
if (what == 4) { | |
System.out.println("Работа программы завершена "); | |
break; | |
} | |
} | |
} | |
public void show(Catalog catalog) { | |
System.out.println("Список моих книг"); | |
for (int u = 0; u < catalog.getCountBook(); u++) | |
System.out.println(catalog.getBookInfoById(u)); | |
System.out.println("Сейчас в каталоге книг - " + catalog.getCountBook()); | |
} | |
private 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); | |
} | |
} | |
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 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 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; | |
} | |
static class Book { | |
private static int nextId = 1; | |
private static int nextISBN; | |
String name; | |
String writer; | |
int id; | |
int ISBN; | |
static { | |
var generator = new Random(); | |
// set nextId to a random number between 0 and 9 | |
nextISBN = generator.nextInt(9999); | |
} | |
{ | |
ISBN = nextISBN; | |
nextISBN++; | |
} | |
{ | |
id = nextId; // set id to next available id | |
nextId++; | |
} | |
public Book(int id, String name, String writer, int ISBN) { | |
this.id = id; | |
this.name = name; | |
this.writer = writer; | |
this.ISBN = ISBN; | |
} | |
public Book(String name, String writer) { | |
this.name = name; | |
this.writer = writer; | |
} | |
public String getBookInfo() { | |
return ("id= " + id + " \"" + name + "\" Автор: " + writer + " ISBN: " + ISBN); | |
} | |
} | |
static class Catalog { | |
static int count; | |
int id; | |
ArrayList<Book> catalog = new ArrayList<>(); | |
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; | |
} | |
} | |
static 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; | |
} | |
} | |
} | |
public static class CD extends Company { | |
public void show() { | |
System.out.println("[2] Моя коллекция CD для просмотра введите [2]"); | |
} | |
void go() { | |
ArrayList<CatalogCD> catalogCD = new ArrayList<>(); | |
CatalogCD cd1 = new CatalogCD(1, "Tutu", "Miles Davis", 1000); | |
CatalogCD cd2 = new CatalogCD(2, "Black Beauty", "Miles Davis", 1001); | |
CatalogCD cd3 = new CatalogCD(3, "Silent Way", "Miles Davis", 1002); | |
CatalogCD cd4 = new CatalogCD(4, "Kind of Blue", "Miles Davis", 1003); | |
catalogCD.add(cd1); | |
catalogCD.add(cd2); | |
catalogCD.add(cd3); | |
catalogCD.add(cd4); | |
for (CatalogCD e : catalogCD) | |
System.out.printf("id= %2d name= %-16s, artist= %-15s ISBN= %4d%n", e.id, e.nameCD, e.artist, e.ISBN); | |
System.out.println("Сейчас в каталоге CD - " + catalogCD.size()); | |
} | |
static class CatalogCD { | |
public static CatalogCD catalogCD; | |
static int count; | |
private final String nameCD; | |
private final String artist; | |
int id; | |
public CatalogCD(int id, String name, String artist, int ISBN) { | |
this.id = id; | |
this.ISBN = ISBN; | |
this.nameCD = name; | |
this.artist = artist; | |
} | |
public void playCD(int k, CatalogCD catalogCD) { | |
System.out.println("id= " + k + "Воспроизводится"); | |
} | |
private static int nextId = 1; | |
private static int nextISBN; | |
int ISBN; | |
static { | |
var generator = new Random(); | |
// set nextId to a random number between 0 and 9 | |
nextISBN = generator.nextInt(9999); | |
} | |
{ | |
ISBN = nextISBN; | |
nextISBN++; | |
} | |
{ | |
id = nextId; // set id to next available id | |
nextId++; | |
} | |
} | |
} | |
static class Cat extends Company { | |
public void show() { | |
System.out.println("[3] Моя кошки для просмотра введите [3]"); | |
} | |
private static int nextCount = 1; | |
private static int nextId; | |
public int id; | |
public int count; | |
public String name = ""; | |
public void go() { | |
ArrayList<Cat> myCatsAL = new ArrayList<Cat>(); | |
myCatsAL.add(new Cat("Земфира", 1)); | |
myCatsAL.add(new Cat("Стелла", 2)); | |
myCatsAL.add(new Cat("Лора", 3)); | |
System.out.println("[3] Мои кошки"); | |
for (Cat e : myCatsAL) | |
System.out.printf("name = %-10s, count = %2d, idPassport = %4d%n", e.getName(), e.getCount(), e.getId()); | |
} | |
static | |
// static initialization block | |
{ | |
var generator = new Random(); | |
// set nextId to a random number between 0 and 999 | |
nextId = generator.nextInt(9999); | |
} | |
// count initialization block | |
{ | |
count = nextCount; | |
nextCount++; | |
} | |
// object initialization block | |
{ | |
id = nextId; | |
nextId++; | |
} | |
// 4 overloaded constructors | |
public Cat(String n, int c) { | |
count++; | |
name = n; | |
count = c; | |
} | |
public Cat(int c) { | |
// calls the Cat(String, int) constructor | |
this("Cat #" + nextId, c); | |
} | |
// the default constructor | |
Cat() { | |
// name initialized to ""--see above | |
// count not explicitly set--initialized to 0 | |
// id initialized in initialization block | |
count++; | |
} | |
public int getCount() { | |
return count; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment