Skip to content

Instantly share code, notes, and snippets.

@olegrewko
Created June 30, 2020 13:46
Show Gist options
  • Select an option

  • Save olegrewko/d019c2b80769bd36f74c8d603750e5b7 to your computer and use it in GitHub Desktop.

Select an option

Save olegrewko/d019c2b80769bd36f74c8d603750e5b7 to your computer and use it in GitHub Desktop.
WareHouse01
package IgorDolgov.wareHome;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Thing[] things = new Thing[5];
things[0] = new Thing();
things[1] = new MyCompany();
things[2] = new MainBook();
things[3] = new CD();
things[4] = new Cat();
for (Thing e : things) {
e.show();
}
}
}
class Thing {
protected static Thing[] things;
void show() {
System.out.println("Показать весь каталог склада: [9]");
System.out.println("Удалить всё : [8]");
System.out.println("0-MyCompany/1-Библиотека/2-Музыкальная коллекция /3-Мои кошки/4-Exit");
Scanner scanner = new Scanner(System.in);
int userInt = scanner.nextInt();
switch (userInt) {
case 9:
System.out.println("Список склада");
Thing.things[0].show();
break;
case 8:
System.out.println("Очистить все каталоги склада");
break;
case 0:
System.out.println("Список сотрудников MyCompany");
Thing.things[1].show();
break;
case 1:
System.out.println("Моя библиотека");
Thing.things[2].show();
break;
case 2:
System.out.println("Моя Музыкальная коллекция");
Thing.things[3].show();
break;
case 3:
System.out.println("Мои кошки");
Thing.things[4].show();
break;
case 4:
System.exit(0);
break;
}
}
}
class Cat extends Thing {
private static int nextCount = 1;
private static int nextId = 1;
private static int idPassport;
public int id;
public int count;
public String name = "";
void show() {
ArrayList<Cat> myCatsAL = new ArrayList<Cat>();
myCatsAL.add(new Cat("Земфира", 1));
myCatsAL.add(new Cat("Стелла", 2));
myCatsAL.add(new Cat("Лора", 3));
for (Cat e : myCatsAL)
System.out.printf("name = %-10s, count = %2d, id = %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;
}
}
class MyCompany extends Thing {
public void show() {
ArrayList<Staff> staff = new ArrayList<>();
staff.add(0, new Staff("Dick", 60));
staff.add(1, new Staff("Mike", 100));
staff.add(2, new Staff(200));
staff.add(3, new Staff(400));
staff.add(4, new Staff());
staff.add(5, new Staff("John"));
System.out.printf("Сотрудников в компании %10s%n", staff.size());
int n = Staff.getNextId(); // calls static method
System.out.println("Next available id= " + n);
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);
Thing.things[1].show();
System.out.println("Сотрудник " + id + " удален ");
Thing.things[1].show();
break;
case 1:
System.out.println("Введите имя сотрудника");
String str = scanner.nextLine();
scanner.next();
System.out.println(str);
System.out.println("Введите зарплату");
int d = scanner.nextInt();
System.out.println(d);
staff.add(n - 1, new Staff(str, d));
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();
raiseSalary(dp);
System.out.println(dp);
System.out.println("Зарплата изменилась на " + dp + "%");
break;
case 3:
staff.clear();
break;
case 4:
System.exit(0);
break;
}
}
private static void raiseSalary(double byPercent) {
Staff staff;
double raise = Staff.salary * byPercent / 100;
Staff.salary += raise;
}
private static void deleteStaff(ArrayList<Staff> staff) {
for (int i = 0; i < staff.size(); i++) {
staff.remove(i);
}
}
}
class Staff {
public static int nextId = 1;
public static double salary;
private String name = " ";
private int id;
public Staff(String n, double s) {
this.name = n;
salary = s;
id = nextId; // set id to next available id
nextId++;
}
public Staff() {
id = nextId; // set id to next available id
nextId++;
}
public Staff(String n) {
this.name = n;
id = nextId; // set id to next available id
nextId++;
}
public Staff(double s) {
id = nextId; // set id to next available id
nextId++;
salary = s;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public int getId() {
return id;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
public static int getNextId() {
return nextId; // returns static field
}
public void setName(String name) {
this.name = name;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setId(int id) {
this.id = id;
}
}
class Book {
private static int nextISBN;
public int nextId = 1;
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);
}
// object initialization block
{
ISBN = nextISBN;
nextISBN++;
}
public Book(String name, String writer) {
id = nextId; // set id to next available id
nextId++;
this.name = name;
this.writer = writer;
}
public void getBookInfo() {
System.out.printf("id= %d %-10s, Автор: %-10s, ISBN: %4d%n", id, name, writer, ISBN);
}
static class CD {
public static int nextId;
String nameCD;
String writerCD;
int id;
public CD(String nameCD, String writerCD) {
id = nextId; // set id to next available id
nextId++;
this.nameCD = nameCD;
this.writerCD = writerCD;
}
public CD() {
id = nextId; // set id to next available id
nextId++;
}
public String getInfoCD() {
return (id + " " + "\"" + nameCD + "\" Автор: " + writerCD);
}
}
}
class CatalogBook {
static int count;
int id;
ArrayList<Book> catalogB = new ArrayList<Book>();
public void addBook(Book book) {
catalogB.add(book);
count++;
id = count;
}
public void removeBook(int id) {
catalogB.remove(id);
System.out.println("Книга " + id + " удалена");
}
public int getCountBook() {
return catalogB.size();
}
public void getBookInfoById(int id) {
Book book = catalogB.get(id);
book.getBookInfo();
}
public int searchBookByName(String text) {
int rez = 0;
String nameBook;
for (int i = 0; i < getCountBook(); i++) {
Book book = catalogB.get(i);
nameBook = book.name.toLowerCase();
if (nameBook.contains(text.toLowerCase())) {
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 = catalogB.get(i);
nameWriter = book.writer.toLowerCase();
if (nameWriter.contains(text.toLowerCase())) {
book.getBookInfo();
rez++;
}
}
return rez;
}
static void removeCatalogBook(Reader reader, CatalogBook catalogB) {
int numBook;
System.out.println("В каталоге книг - " + catalogB.getCountBook());
System.out.println("Укажите номер удаляемой книги");
do {
numBook = reader.getAnswer();
} while (numBook < 1 || numBook > catalogB.getCountBook());
--numBook;
System.out.println("Вы уверены что хотите удалить книгу: ?");
System.out.println("0-нет; 1-да");
int ans;
do {
ans = reader.getAnswer();
} while (ans != 0 & ans != 1);
if (ans == 1) {
catalogB.removeBook(numBook);
}
}
static void addCatalogBook(Reader reader, CatalogBook catalogB) {
String name;
String writer;
System.out.println("Укажите название книги");
name = reader.getString();
System.out.println("Укажите автора книги");
writer = reader.getString();
catalogB.addBook(new Book(name, writer));
System.out.println("Книга добавлена в каталог");
}
static void searchBook(Reader reader, CatalogBook catalogB) {
System.out.println("Что ищем?");
String str = reader.getString();
int rez = catalogB.searchBookByName(str);
if (rez == 0) {
System.out.println("Книга \"" + str + "\" не найдена");
}
}
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;
}
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;
}
}
}
class MainBook extends Thing {
private static CatalogBook.Reader reader;
public void show() {
CatalogBook catalogB = new CatalogBook();
Book book1 = new Book("Почтамт", "Буковски");
Book book2 = new Book("Женщины", "Буковски");
Book book3 = new Book("Фактотум", "Буковски");
Book book4 = new Book("Голливуд", "Буковски");
catalogB.addBook(book1);
catalogB.addBook(book2);
catalogB.addBook(book3);
catalogB.addBook(book4);
catalogB.getBookInfoById(0);
catalogB.getBookInfoById(1);
catalogB.getBookInfoById(2);
catalogB.getBookInfoById(3);
System.out.println("Сейчас в каталоге книг - " + catalogB.getCountBook());
int what;
for (int i = 0; i < 10; i++) {
System.out.println("Что будем делать: 0-Добавить книгу; 1-Найти книгу; 2-Удалить книгу; 3-Показать список книг;4-Завершить работу программы;5-Удалить все книги");
what = CatalogBook.whatDo(reader);
if (what == 1) {
CatalogBook.searchBook(reader, catalogB);
}
if (what == 0) {
CatalogBook.addCatalogBook(reader, catalogB);
System.out.println("В каталоге книг - " + catalogB.getCountBook());
}
if (what == 3) {
for (int u = 0; u < catalogB.getCountBook(); u++)
catalogB.getBookInfoById(u);
System.out.println("Сейчас в каталоге книг - " + catalogB.getCountBook());
}
if (what == 4) {
System.out.println("Работа программы завершена ");
break;
}
if (what == 5) {
CatalogBook.removeCatalogBook(reader, catalogB);
System.out.println("Все книги удалены ");
break;
}
if (what == 2) {
if (catalogB.getCountBook() > 0) {
CatalogBook.removeCatalogBook(reader, catalogB);
System.out.println("В каталоге осталось книг - " + catalogB.getCountBook());
} else {
System.out.println("В каталоге пусто. Нет книг для удаления");
}
}
}
}
}
class CD extends Thing {
private static CatalogCD catalogCD;
private static Object ArrayList;
private static Object CatalogCD;
public void show() {
ArrayList<CatalogCD> catalogCD = new ArrayList<>();
CatalogCD cd1 = new CatalogCD("Tutu", "Miles Davis");
CatalogCD cd2 = new CatalogCD("Black Beauty", "Miles Davis");
CatalogCD cd3 = new CatalogCD("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);
// for (CatalogCD e : catalogCD)
// System.out.println("id= " + e.getId() + " name= " + e.getName() + ", salary= " + e.getSalary());
System.out.println("Сейчас в каталоге CD - " + catalogCD.size());
System.out.println("Что будем делать: 0-Воспроизводить cd; 1-Найти cd; 2-Удалить cd; 3-Показать список cd;4-Завершить работу программы");
int w;
int numCD;
Scanner scanner = new Scanner(System.in);
w = scanner.nextInt();
System.out.println(w);
if (w == 0) {
System.out.println("Введите id воспроизводимого CD");
numCD = scanner.nextInt();
System.out.println(numCD);
// CatalogCD.playCD(numCD - 1, catalogCD);
}
if (w == 4) {
System.exit(0);
}
}
static class CatalogCD {
public static CatalogCD catalogCD;
static int count;
private final String nameCD;
private final String artist;
int id;
public CatalogCD(String name, String artist) {
this.nameCD = name;
this.artist = artist;
}
public void playCD(int k, CatalogCD catalogCD) {
System.out.println("id= " + k + "Воспроизводится");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment