Created
July 4, 2020 08:35
-
-
Save olegrewko/ff3bf671dc0079e1d5dfdd01ff250329 to your computer and use it in GitHub Desktop.
WR04
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.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
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 Library(); | |
companies[2] = new CD(); | |
companies[3] = new Cat(); | |
System.out.println("Полный список всего склада"); | |
for (Company e : companies) { | |
if (e instanceof Company) { | |
System.out.println(e); | |
} | |
} | |
System.out.println("--------------------------"); | |
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]-Добавить[+] или уменьшить[-] зарплату одному сотруднику в % [-+][1-100]"); | |
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 sc = new Scanner(System.in); | |
str = sc.nextLine(); | |
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 + " добавлен "); | |
System.out.println("----------------------------------"); | |
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
Calendar cal = Calendar.getInstance(); | |
System.out.println(dateFormat.format(cal.getTime())); // 2014/08/06 16:00:22 | |
break; | |
case 2: | |
System.out.println("Введите: добавить[+] или уменьшить зарплату[-] в % [-+][1.00 - 100.00]"); | |
double dp = scanner.nextDouble(); | |
System.out.println(dp); | |
for (Employee e : staff) | |
e.raiseSalary(dp); | |
show(staff); | |
System.out.println("Зарплата изменилась на " + dp + "%"); | |
break; | |
case 3: | |
System.out.println("Введите: добавить[+] или уменьшить зарплату[-] в % [-+][1.00 - 100.00]"); | |
double byPersent; | |
Scanner s = new Scanner(System.in); | |
byPersent = s.nextDouble(); | |
System.out.println(byPersent); | |
System.out.println("Введите id сотрудника"); | |
int idn; | |
idn = s.nextInt(); | |
System.out.println(idn); | |
// staff.set(idn - 1, new Employee(idn, , raiseSalary(byPersent), , , )); | |
show(staff); | |
break; | |
case 4: | |
System.exit(0); | |
break; | |
default: | |
throw new IllegalStateException("Unexpected value: " + i); | |
} | |
} | |
public 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; | |
private double salary; | |
public int id; | |
private String name = ""; // instance field initialization | |
private LocalDate hireDay; | |
{ | |
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 str, double s) { | |
this(getNextId(), str, s, 1111, 11, 11); | |
name = str; | |
salary = s; | |
} | |
public String getName() { | |
return name; | |
} | |
public double getSalary() { | |
return salary; | |
} | |
public static int getNextId() { | |
return nextId; | |
} | |
public LocalDate getHireDay() { | |
return hireDay; | |
} | |
public double raiseSalary(double dp) { | |
double raise = salary * dp / 100; | |
salary += raise; | |
return salary; | |
} | |
} | |
} | |
public static class Library 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 < catalog.getCountBook(); 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 static 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 static 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]"); | |
} | |
public 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"); | |
CatalogCD cd3 = new CatalogCD(3, "Silent Way", "Miles Davis"); | |
CatalogCD cd4 = new CatalogCD("Kind of Blue", "Miles Davis"); | |
catalogCD.add(cd1); | |
catalogCD.add(cd2); | |
catalogCD.add(cd3); | |
catalogCD.add(cd4); | |
System.out.println("[2] Моя музыкальная коллекция"); | |
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()); | |
System.out.println("[1]- Добавить CD, [2]- Bоспроизводить CD, [4]- 4-Завершить работу программы"); | |
int a = Library.Reader.getAnswer(); | |
if (a == 1) { | |
System.out.println("Введите название CD "); | |
String stName = Library.Reader.getString(); | |
System.out.println("Введите исполнителя"); | |
String stArtist = Library.Reader.getString(); | |
catalogCD.add(CatalogCD.nextId - 1, new CatalogCD(stName, stArtist)); | |
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.nextId - 1) + " добавлен в коллекцию"); | |
System.out.println("Сейчас в каталоге CD - " + catalogCD.size()); | |
} else if (a == 2) { | |
System.out.println("Введите номер воспроизводимого CD "); | |
int k = Library.Reader.getAnswer(); | |
CatalogCD.playCD(k); | |
} else if (a == 4) { | |
System.exit(0); | |
} | |
} | |
static class CatalogCD { | |
public String nameCD; | |
int id; | |
private static int nextId = 1; | |
private int ISBN; | |
private static int nextISBN; | |
private final String artist; | |
public CatalogCD(int id, String name, String artist, int ISBN) { | |
this.id = id; | |
this.ISBN = ISBN; | |
this.nameCD = name; | |
this.artist = artist; | |
} | |
public CatalogCD(String nameCD, String artist) { | |
this(nextId, nameCD, artist, nextISBN); | |
} | |
public CatalogCD(int id, String nameCD, String artist) { | |
this(nextId, nameCD, artist, nextISBN); | |
} | |
public static void playCD(int k) { | |
System.out.println("id= " + k + " nameCD Воспроизводится"); | |
} | |
static { | |
var generator = new Random(); | |
// set nextId to a random number between 0 and 9 | |
nextISBN = generator.nextInt(9999); | |
} | |
{ | |
nextISBN++; | |
} | |
{ | |
nextId++; | |
} | |
} | |
} | |
static class Cat extends Company { | |
public void show() { | |
System.out.println("[3] Мои кошки для просмотра введите [3]"); | |
} | |
private static int nextCount; | |
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("Стелла")); | |
myCatsAL.add(new Cat("Лора")); | |
System.out.println("[3] Мои кошки"); | |
for (Cat e : myCatsAL) | |
System.out.printf("name = %-10s, count = %2d, idPassport = %4d%n", e.getName(), e.getCount(), e.getId()); | |
System.out.println("Сейчас кошек - " + myCatsAL.size()); | |
System.out.println("[1]- Добавить [2]- Удалить [4]- Завершить работу программы"); | |
int a = Library.Reader.getAnswer(); | |
if (a == 1) { | |
System.out.println("Введите имя новой кошки "); | |
String stName = Library.Reader.getString(); | |
myCatsAL.add(nextCount - 1, new Cat(stName)); | |
for (Cat e : myCatsAL) | |
System.out.printf("name = %-10s, count = %2d, idPassport = %4d%n", e.getName(), e.getCount(), e.getId()); | |
System.out.println("Сейчас кошек - " + myCatsAL.size()); | |
} else if (a == 2) { | |
System.out.println("Введите номер удаляемой кошки "); | |
int k = Library.Reader.getAnswer(); | |
myCatsAL.remove(k - 1); | |
for (Cat e : myCatsAL) | |
System.out.printf("name = %-10s, count = %2d, idPassport = %4d%n", e.getName(), e.getCount(), e.getId()); | |
System.out.println("Сейчас кошек - " + myCatsAL.size()); | |
} else if (a == 4) { | |
System.exit(0); | |
} | |
} | |
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 | |
{ | |
nextCount++; | |
count = nextCount; | |
} | |
// object initialization block | |
{ | |
id = nextId; | |
nextId++; | |
} | |
// 4 overloaded constructors | |
public Cat(String n, int c) { | |
name = n; | |
count = c; | |
} | |
public Cat(int c) { | |
// calls the Cat(String, int) constructor | |
this("Cat #" + nextId, c); | |
} | |
public Cat(String name) { | |
this("" + name, nextCount); | |
} | |
// 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