Created
July 12, 2016 11:56
-
-
Save sunmeat/b077eb0d111f1f34d4354bb9c15d7a4e to your computer and use it in GitHub Desktop.
большой пример
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 files; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.Random; | |
import java.util.Vector; | |
class Human { | |
private String surname, name, middleName; | |
public Date dateOfBirth; | |
public Address homeAddress; | |
public PhoneNumber phoneNumber; | |
// ---------------------------------- Сеттеры ---------------------------------- | |
public void setSurname(String surname) { | |
this.surname = toNameFormat(surname); | |
} | |
public void setName(String name) { | |
this.name = toNameFormat(name); | |
} | |
public void setMiddleName(String middleName) { | |
this.middleName = toNameFormat(middleName); | |
} | |
public void setFullName(String surname, String name, String middleName) { | |
setSurname(surname); | |
setName(name); | |
setMiddleName(middleName); | |
} | |
public void setDateOfBirth(int dd, int mm, int yyyy) { | |
if (dateOfBirth == null) { | |
dateOfBirth = new Date(dd, mm, yyyy); | |
} else { | |
dateOfBirth.setDate(dd, mm, yyyy); | |
} | |
} | |
public void setHomeAddress(String streetname, int number, String index, int apartment_room, String city_town_village, String region, String country, int postalCode) { | |
if (homeAddress == null) { | |
homeAddress = new Address(streetname, number, index, apartment_room, city_town_village, region, country, postalCode); | |
} else { | |
homeAddress.setAddress(streetname, number, index, apartment_room, city_town_village, region, country, postalCode); | |
} | |
} | |
public void setHomeAddress(String streetname, int number, int index, int apartment_room, String city_town_village, String region, String country, int postalCode) { | |
setHomeAddress(streetname, number, String.valueOf(index), apartment_room, city_town_village, region, country, postalCode); | |
} | |
public void setHomeAddress(String streetname, int number, int apartment_room, String city_town_village, String region, String country, int postalCode) { | |
setHomeAddress(streetname, number, "", apartment_room, city_town_village, region, country, postalCode); | |
} | |
public void setPhoneNumber(String phoneNumber) { | |
if (this.phoneNumber == null) { | |
this.phoneNumber = new PhoneNumber(phoneNumber); | |
} else { | |
this.phoneNumber.setPhoneNumber(phoneNumber); | |
} | |
} | |
// ---------------------------------- Геттеры ---------------------------------- | |
public String getSurname() { | |
return surname; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getMiddleName() { | |
return middleName; | |
} | |
public String getFullName() { | |
return surname + " " + name + " " + middleName; | |
} | |
// --------------------------- Дополнительные методы --------------------------- | |
private String toNameFormat(String name) { | |
if (name.length() > 1) { | |
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase(); | |
} else { | |
return ""; | |
} | |
} | |
@Override | |
public String toString() { | |
String result = new String(); | |
if (surname != null) { | |
if (result.length() != 0) { | |
result += " "; | |
} | |
result += surname; | |
} | |
if (name != null) { | |
if (result.length() != 0) { | |
result += " "; | |
} | |
result += name; | |
} | |
if (middleName != null) { | |
if (result.length() != 0) { | |
result += " "; | |
} | |
result += middleName; | |
} | |
if (dateOfBirth != null) { | |
result += String.format("\nдата рождения: %s", dateOfBirth); | |
} | |
if (homeAddress != null) { | |
result += String.format("\nдомашний адрес: %s", homeAddress); | |
} | |
if (phoneNumber != null) { | |
result += String.format("\nномер телефона: %s", phoneNumber); | |
} | |
return result; | |
} | |
} | |
class Student extends Human { | |
Collection credit = new Collection(); | |
Collection courseWork = new Collection(); | |
Collection exam = new Collection(); | |
@Override | |
public String toString() { | |
String result = super.toString(); | |
if (credit.getCount() != 0) { | |
result += String.format("\nоценки по зачетам: %s", credit); | |
} | |
if (courseWork.getCount() != 0) { | |
result += String.format("\nоценки по курсовым работам: %s", courseWork); | |
} | |
if (exam.getCount() != 0) { | |
result += String.format("\nоценки по экзаменам: %s", exam); | |
} | |
return result; | |
} | |
public class Collection { | |
private int[] data; | |
public boolean add(int... x) { | |
boolean correct = true; | |
for (int xi : x) { | |
if (xi > 12 || xi <= 0) { | |
correct &= false; | |
continue; | |
} | |
if (data == null) { | |
data = new int[0]; | |
} | |
int[] temp = data; | |
data = new int[temp.length + 1]; | |
System.arraycopy(temp, 0, data, 0, temp.length); | |
data[temp.length] = xi; | |
} | |
return correct; | |
} | |
public boolean delete(int index) { | |
if (index > data.length || index <= 0) { | |
return false; | |
} | |
int[] temp = data; | |
data = new int[temp.length - 1]; | |
System.arraycopy(temp, 0, data, 0, index); | |
for (int i = index; i < data.length; i++) { | |
data[i] = temp[i + 1]; | |
} | |
return true; | |
} | |
public boolean deleteLast() { | |
if (data.length == 0) { | |
return false; | |
} | |
int[] temp = data; | |
data = new int[temp.length - 1]; | |
System.arraycopy(temp, 0, data, 0, data.length); | |
return true; | |
} | |
public int getCount() { | |
if (data == null) { | |
return 0; | |
} | |
return data.length; | |
} | |
@Override | |
public String toString() { | |
if (data.length == 0) { | |
return "нет оценок"; | |
} | |
String result = "[ " + data[0]; | |
for (int i = 1; i < data.length; i++) { | |
result += ", " + data[i]; | |
} | |
return result + " ]"; | |
} | |
} | |
} | |
class PhoneNumber { | |
private int countryCode; | |
private int cityCode; | |
private int number; | |
private boolean correct; | |
// private int DEFAULT_CITYCODE = 48; | |
// private int DEFAULT_COUNTRYCODE = 380; | |
// ------------------------------ Конструкторы ------------------------------ | |
public PhoneNumber() { | |
} | |
public PhoneNumber(String phoneNumber) { | |
setPhoneNumber(phoneNumber); | |
} | |
// -------------------------------- Сеттеры -------------------------------- | |
public boolean setNumber(int number) { | |
boolean correctInput = false; | |
if (number < 9_999_999 && number > 0) { | |
this.number = number; | |
correctInput = true; | |
} else { | |
this.number = 0; | |
} | |
this.correct = (this.number * this.cityCode * this.countryCode != 0); | |
return correctInput; | |
} | |
public boolean setCityCode(int cityCode) { | |
boolean correctInput = false; | |
if (cityCode < 99 && cityCode > 0) { | |
this.cityCode = cityCode; | |
correctInput = true; | |
} else { | |
this.cityCode = 0; | |
} | |
this.correct = (this.number * this.cityCode * this.countryCode != 0); | |
return correctInput; | |
} | |
public boolean setCountryCode(int countryCode) { | |
boolean correctInput = false; | |
if (countryCode < 9_999 && countryCode > 0) { | |
this.countryCode = countryCode; | |
correctInput = true; | |
} else { | |
this.countryCode = 0; | |
} | |
this.correct = (this.number * this.cityCode * this.countryCode != 0); | |
return correctInput; | |
} | |
public boolean setNumber(String number) { | |
try { | |
return setNumber(Integer.parseInt(number)); | |
} catch (Exception e) { | |
this.number = 0; | |
return false; | |
} | |
} | |
public boolean setCityCode(String cityCode) { | |
try { | |
return setCityCode(Integer.parseInt(cityCode)); | |
} catch (Exception e) { | |
this.cityCode = 0; | |
return false; | |
} | |
} | |
public boolean setCountryCode(String countryCode) { | |
try { | |
return setCountryCode(Integer.parseInt(countryCode)); | |
} catch (Exception e) { | |
this.countryCode = 0; | |
return false; | |
} | |
} | |
public boolean setPhoneNumber(int countryCode, int cityCode, int number) { | |
return setCountryCode(countryCode) && setCityCode(cityCode) && setNumber(number); | |
} | |
public boolean setPhoneNumber(String countryCode, String cityCode, String number) { | |
return setCountryCode(countryCode) && setCityCode(cityCode) && setNumber(number); | |
} | |
public boolean setPhoneNumber(String phoneNumber) { | |
phoneNumber = phoneNumber.replaceAll("\\ ", ""); | |
phoneNumber = phoneNumber.replaceAll("\\-", ""); | |
phoneNumber = phoneNumber.replaceAll("\\_", ""); | |
phoneNumber = phoneNumber.replaceAll("\\(", ""); | |
phoneNumber = phoneNumber.replaceAll("\\)", ""); | |
phoneNumber = phoneNumber.replaceAll("\\+", ""); | |
correct = false; | |
try { | |
number = Integer.parseInt(phoneNumber.substring(phoneNumber.length() - 7)); | |
cityCode = Integer.parseInt(phoneNumber.substring(phoneNumber.length() - 7 - 2, phoneNumber.length() - 7)); | |
countryCode = Integer.parseInt(phoneNumber.substring(0, phoneNumber.length() - 7 - 2)); | |
} catch (Exception e) { | |
return false; | |
} | |
if (countryCode > 4000) { | |
return false; | |
} | |
return correct = true; | |
} | |
// -------------------------------- Геттеры -------------------------------- | |
public int getNumber() { | |
return number; | |
} | |
public int getCityCode() { | |
return cityCode; | |
} | |
public int getCountryCode() { | |
return countryCode; | |
} | |
public long getPhoneNumber() { | |
return (correct) ? (long) countryCode * 1_000_000_000 + cityCode * 10_000_000 + number : 0; | |
} | |
// ------------------------- Дополнительные методы ------------------------- | |
public boolean isCorrect() { | |
return correct; | |
} | |
@Override | |
public String toString() { | |
return (correct) ? String.format("(+%d)%02d-%03d-%04d", countryCode, cityCode, number / 10000, number % 10000) : "не указан"; | |
} | |
} | |
class Group { | |
private Vector<Student> Students = new Vector<>(0); | |
private String name; | |
private String specialization; | |
private byte course; | |
public Group() { | |
generateRandomStudents(30); | |
} | |
public byte getCountOfStudents() { | |
return (byte) Students.size(); | |
} | |
public Student getStudent(int index) { | |
return Students.get(index); | |
} | |
public String getName() { | |
return name; | |
} | |
public String getSpecialization() { | |
return specialization; | |
} | |
public byte getCourse() { | |
return course; | |
} | |
public void generateRandomStudents(int count) { | |
Random random = new Random(); | |
Vector<String> lines; | |
lines = (new FileWorker("src/ВУЗы/Список специальностей.txt")).readLines(); | |
specialization = lines.get(random.nextInt(lines.size() - 1)).replaceAll("\n", ""); | |
course = (byte) (random.nextInt(5) + 1); | |
name = specialization.charAt(0) + "-" + course + random.nextInt(9); | |
for (int i = 0; i < count; i++) { | |
Student student = new Student(); | |
if (random.nextBoolean()) { | |
lines = (new FileWorker("src/Имена/Список фамилий.txt")).readLines(); | |
student.setSurname(lines.get(random.nextInt(lines.size() - 1)).replaceAll(" ", "").replaceAll("\n", "")); | |
lines = (new FileWorker("src/Имена/Список мужских имен.txt")).readLines(); | |
student.setName(lines.get(random.nextInt(lines.size() - 1)).replaceAll(" ", "").replaceAll("\n", "")); | |
lines = (new FileWorker("src/Имена/Список мужских отчеств.txt")).readLines(); | |
student.setMiddleName(lines.get(random.nextInt(lines.size() - 1)).replaceAll("\n", "")); | |
} else { | |
lines = (new FileWorker("src/Имена/Список Фамилий.txt")).readLines(); | |
student.setSurname(lines.get(random.nextInt(lines.size() - 1)).replaceAll(" ", "").replaceAll("\n", "") + "а"); | |
lines = (new FileWorker("src/Имена/Список женских имен.txt")).readLines(); | |
student.setName(lines.get(random.nextInt(lines.size() - 1)).replaceAll(" ", "").replaceAll("\n", "")); | |
lines = (new FileWorker("src/Имена/Список женских отчеств.txt")).readLines(); | |
student.setMiddleName(lines.get(random.nextInt(lines.size() - 1)).replaceAll("\n", "")); | |
} | |
FileWorker file; | |
file = new FileWorker("src/Адреса"); | |
String country = file.list()[random.nextInt(file.list().length)]; | |
file = new FileWorker("src/Адреса/" + country); | |
String region = file.list()[random.nextInt(file.list().length)]; | |
file = new FileWorker("src/Адреса/" + country + "/" + region); | |
lines = file.readLines(); | |
String city_posalCode = lines.get(random.nextInt(lines.size())); | |
region = region.replaceAll(".txt", ""); | |
String city = city_posalCode.substring(0, city_posalCode.indexOf(";")); | |
int postalCode = Integer.parseInt(city_posalCode.substring(city_posalCode.indexOf(";") + 1)); | |
student.homeAddress = new Address(); | |
student.homeAddress.setCountry(country); | |
student.homeAddress.setRegion(region); | |
student.homeAddress.setCity_Town_Village(city); | |
student.homeAddress.setPostalCode(postalCode); | |
try { | |
file = new FileWorker("src/Телефоны/" + country); | |
String countryCode = file.list()[random.nextInt(file.list().length)]; | |
file = new FileWorker("src/Телефоны/" + country + "/" + countryCode + "/Список кодов мобильных операторов.txt"); | |
lines = file.readLines(); | |
String line = lines.get(random.nextInt(lines.size())); | |
String cityCode = line.substring(0, line.indexOf(';')); | |
student.phoneNumber = new PhoneNumber(); | |
student.phoneNumber.setPhoneNumber(countryCode, cityCode, String.valueOf(random.nextInt((int) Math.pow(10, 7)))); | |
} catch (Exception e) { | |
} | |
student.dateOfBirth = new Date(); | |
@SuppressWarnings("deprecation") | |
int currentYear = java.util.Calendar.getInstance().getTime().getYear() + 1900; | |
while (!student.dateOfBirth.setDate(random.nextInt(31) + 1, random.nextInt(12) + 1, currentYear - course - 17 + random.nextInt(2))); | |
if (Students.isEmpty()) { | |
Students.add(student); | |
} else { | |
int j = 0; | |
for (; j < Students.size(); j++) { | |
if (Students.get(j).getFullName().compareTo(student.getFullName()) > 0) { | |
Students.add(j, student); | |
break; | |
} | |
} | |
if (j == Students.size()) { | |
Students.add(student); | |
} | |
} | |
} | |
} | |
@Override | |
public String toString() { | |
StringBuilder result = new StringBuilder(); | |
result.append("Группа ").append(getName()).append("\n"); | |
result.append("Специальность: ").append(getSpecialization()).append("\n"); | |
result.append("Курс: ").append(getCourse()).append("\n"); | |
for (int i = 0; i < getCountOfStudents(); i++) { | |
result.append("\n ").append(i + 1).append(". ").append(getStudent(i)).append("\n"); | |
} | |
return result.toString(); | |
} | |
} | |
class FileWorker extends File { | |
private static final long serialVersionUID = 1L; | |
public FileWorker(String name) { | |
super(name); | |
} | |
public void erase() { | |
try { | |
try (FileWriter file = new FileWriter(this, false)) { | |
file.write(""); | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public boolean write(String string) { | |
try { | |
try (FileWriter file = new FileWriter(this, true)) { | |
file.write(string); | |
} | |
} catch (IOException e) { | |
return false; | |
} | |
return true; | |
} | |
public boolean writeln(String string) { | |
return write(string + "\r\n"); | |
} | |
public Vector<String> readLines() { | |
Vector<String> result = new Vector<>(); | |
try { | |
try (BufferedReader file = new BufferedReader(new FileReader(this))) { | |
String line; | |
while ((line = file.readLine()) != null) { | |
result.add(line); | |
} | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
return result; | |
} | |
public StringBuilder readAll() { | |
StringBuilder result = new StringBuilder(); | |
try { | |
try (BufferedReader file = new BufferedReader(new FileReader(this))) { | |
String line; | |
while ((line = file.readLine()) != null) { | |
result.append(line).append("\r\n"); | |
} | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
return result; | |
} | |
@Override | |
public String toString() { | |
return readAll().toString(); | |
} | |
} | |
class Date { | |
private int year; | |
private int month; | |
private int day; | |
public boolean textFormat = true; | |
private boolean correct; | |
private final String[] nameOfMonth = {"", "января", "февраля", "марта", | |
"апреля", "майя", "июня", "июля", "августа", | |
"сентября", "октября", "ноября", "декабря"}; | |
// ------------------------------ онструкторы ------------------------------ | |
public Date() { | |
} | |
public Date(int dd, int mm, int yyyy) { | |
setDate(dd, mm, yyyy); | |
} | |
// -------------------------------- —еттеры -------------------------------- | |
public boolean setDate(int dd, int mm, int yyyy) { | |
boolean leapYear = yyyy % 4 == 0 && yyyy % 100 != 0 || yyyy % 400 == 0; | |
final byte[] daysInMonth = new byte[]{0, 31, (byte) (leapYear ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
try { | |
correct = (mm > 0 && mm <= 12 && dd > 0 && dd <= daysInMonth[mm]); | |
if (!correct) { | |
return false; | |
} | |
year = yyyy; | |
month = mm; | |
day = dd; | |
return true; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
// -------------------------------- √еттеры -------------------------------- | |
public int getDay() { | |
return day; | |
} | |
public int getMonth() { | |
return month; | |
} | |
public int getYear() { | |
return year; | |
} | |
// ------------------------- ƒополнительные методы ------------------------- | |
public boolean isCorrect() { | |
return correct; | |
} | |
@Override | |
public String toString() { | |
if (textFormat) { | |
return (correct) ? day + " " + nameOfMonth[month] + " " + year + " г." : "не указана"; | |
} else { | |
return (correct) ? String.format("%d.%02d.%d", day, month, year) : "не указана"; | |
} | |
} | |
} | |
/** | |
* ------------------------ CLASS ADDRESS ------------------------ | |
*/ | |
class Address { | |
private String streetname; | |
private int number; | |
private String index; | |
private int apartment_room; | |
private String city_town_village; | |
private String region; | |
private String country; | |
private int postalCode; | |
// ------------------------------ Конструкторы ------------------------------ | |
public Address() { | |
} | |
public Address(String streetname, int number, int apartment_room, | |
String city_town_village, String region, String country, int postalCode) { | |
setAddress(streetname, number, apartment_room, | |
city_town_village, region, country, postalCode); | |
} | |
public Address(String streetname, int number, String index, int apartment_room, | |
String city_town_village, String region, String country, int postalCode) { | |
setAddress(streetname, number, index, apartment_room, | |
city_town_village, region, country, postalCode); | |
} | |
public Address(String streetname, int number, int index, int apartment_room, | |
String city_town_village, String region, String country, int postalCode) { | |
setAddress(streetname, number, index, apartment_room, | |
city_town_village, region, country, postalCode); | |
} | |
// -------------------------------- Сеттеры -------------------------------- | |
public void setStreetname(String streetname) { | |
streetname = streetname.toLowerCase().replaceFirst("ул. ", "").replaceFirst("улица ", ""); | |
this.streetname = toNameFormat(streetname); | |
} | |
public void setNumber(int number) { | |
if (number > 0) { | |
this.number = number; | |
} else { | |
this.number = 0; | |
} | |
} | |
public void setIndex(String index) { | |
this.index = index; | |
} | |
public void setApartment_Room(int apartment_room) { | |
if (apartment_room > 0) { | |
this.apartment_room = apartment_room; | |
} else { | |
this.apartment_room = 0; | |
} | |
} | |
public void setCity_Town_Village(String city_town_village) { | |
this.city_town_village = city_town_village; | |
} | |
public void setRegion(String region) { | |
this.region = region; | |
} | |
public void setCountry(String country) { | |
this.country = country; | |
} | |
public void setPostalCode(int postalCode) { | |
if (postalCode > 0) { | |
this.postalCode = postalCode; | |
} else { | |
this.postalCode = 0; | |
} | |
} | |
public void setAddress(String streetname, int number, int apartment_room, | |
String city_town_village, String region, String country, int postalCode) { | |
setStreetname(streetname); | |
setNumber(number); | |
setApartment_Room(apartment_room); | |
setCity_Town_Village(city_town_village); | |
setRegion(region); | |
setCountry(country); | |
setPostalCode(postalCode); | |
} | |
public void setAddress(String streetname, int number, String index, int apartment_room, | |
String city_town_village, String region, String country, int postalCode) { | |
setAddress(streetname, number, apartment_room, | |
city_town_village, region, country, postalCode); | |
setIndex(index); | |
} | |
public void setAddress(String streetname, int number, int index, int apartment_room, | |
String city_town_village, String region, String country, int postalCode) { | |
setAddress(streetname, number, apartment_room, | |
city_town_village, region, country, postalCode); | |
setIndex(String.valueOf(index)); | |
} | |
// -------------------------------- Геттеры -------------------------------- | |
public String getStreetname() { | |
return (streetname.length() != 0) ? "ул. " + streetname : ""; | |
} | |
public String getNumber() { | |
return (number != 0) ? "д. " + number : ""; | |
} | |
public String getIndex() { | |
return (index.length() != 0) ? "/" + index : ""; | |
} | |
public String getApartment_Room() { | |
return (apartment_room != 0) ? "кв. " + apartment_room : ""; | |
} | |
public String getCity_Town_Village() { | |
return (city_town_village.length() != 0) ? "г. " + city_town_village : ""; | |
} | |
public String getRegion() { | |
return (region.length() != 0) ? region : ""; | |
} | |
public String getCountry() { | |
return (country.length() != 0) ? country : ""; | |
} | |
public String getPostalCode() { | |
return (postalCode != 0) ? String.format("%05d", postalCode) : ""; | |
} | |
// ------------------------- Дополнительные методы ------------------------- | |
private String toNameFormat(String name) { | |
if (name.length() > 1) { | |
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase(); | |
} else { | |
return ""; | |
} | |
} | |
public String toString() { | |
String result = new String(); | |
if (streetname != null && !"".equals(streetname)) { | |
result += getStreetname(); | |
if (number != 0) { | |
result += ", " + getNumber(); | |
if (index != null && !"".equals(index)) { | |
result += getIndex(); | |
} | |
if (apartment_room != 0) { | |
result += ", " + getApartment_Room(); | |
} | |
} | |
} | |
if (city_town_village != null && !"".equals(city_town_village)) { | |
if (result.length() != 0) { | |
result += ", "; | |
} | |
result += getCity_Town_Village(); | |
} | |
if (region != null && !"".equals(region)) { | |
if (result.length() != 0) { | |
result += ", "; | |
} | |
result += getRegion(); | |
} | |
if (country != null && !"".equals(country)) { | |
if (result.length() != 0) { | |
result += ", "; | |
} | |
result += getCountry(); | |
} | |
if (postalCode != 0) { | |
if (result.length() != 0) { | |
result += ", "; | |
} | |
result += getPostalCode(); | |
} | |
return (result.length() != 0) ? result : "не указан"; | |
} | |
} | |
public class Program { | |
public static void main(String[] args) { | |
Group group = new Group(); | |
System.out.println(group); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment