Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- import java.util.Objects;
- import java.util.Scanner;
- record Student (String name, int group, int number, int day, int month, int year, String sex) {}
- public class College {
- static Scanner scanner = new Scanner(System.in);
- public static int n = 0;
- public static boolean exit = false;
- public static Student[] arrayOfRecords = new Student[0];
- // тут костыльно реализовано, потому я дебил и не знал,
- // что динамических массивов в jave, как это устроено в delphi, не существует
- // надо было юзать arraylist, но тогда надо переписывать много кода(
- public static Student[] tempArray = new Student[0];
- public static void main (String[] args) throws IOException {
- System.out.println("The program is a college student database.");
- while (!exit) {
- showInfo();
- chooseAction();
- }
- }
- public static void showInfo () {
- System.out.println("1 - add record");
- System.out.println("2 - show sorted records");
- System.out.println("3 - show all records");
- System.out.println("4 - delete record");
- System.out.println("5 - edit record");
- System.out.println("6 - open record from file");
- System.out.println("7 - save record in file");
- System.out.println("8 - exit");
- }
- public static void chooseAction () throws IOException {
- int chose;
- boolean isIncorrect;
- final int MAX = 8;
- System.out.println("Please, choose:");
- do {
- chose = inputData();
- isIncorrect = false;
- if ((chose > MAX) || (chose < 1)) {
- isIncorrect = true;
- System.out.println("Please enter the correct function number.");
- }
- } while (isIncorrect);
- switch (chose) {
- case 1 -> addRecord();
- case 2 -> sortRecords();
- case 3 -> showRecords();
- case 4 -> deleteRecord();
- case 5 -> editRecord();
- case 6 -> openFile();
- case 7 -> saveData();
- case 8 -> exit = true;
- }
- }
- public static void editRecord () {
- System.out.println("Specify the number of the student you want to edit:");
- int number;
- boolean isIncorrect;
- do {
- number = inputData();
- isIncorrect = false;
- if ((number > n) || (number < 1)){
- isIncorrect = true;
- System.out.println("Please enter the number of an existing student:");
- }
- } while (isIncorrect);
- int data; // что мы хотим редактировать, какую позицию
- final int MAX = 7;
- System.out.println("Select what you want to edit:");
- System.out.println("1 - surname");
- System.out.println("2 - group");
- System.out.println("3 - number in logbook");
- System.out.println("4 - day (of birthday)");
- System.out.println("5 - month (of birthday)");
- System.out.println("6 - year (of birthday)");
- System.out.println("7 - sex");
- do {
- data = inputData();
- isIncorrect = false;
- if ((data > MAX) || (data < 1)){
- isIncorrect = true;
- System.out.println("Please enter the number of an existing position.");
- }
- } while (isIncorrect);
- number--;
- String name = arrayOfRecords[number].name();
- int group = arrayOfRecords[number].group();
- int numberInList = arrayOfRecords[number].number();
- int day = arrayOfRecords[number].day();
- int month = arrayOfRecords[number].month();
- int year = arrayOfRecords[number].year();
- String sex = arrayOfRecords[number].sex();
- switch (data) {
- case 1 -> name = inputName();
- case 2 -> group = inputGroup();
- case 3 -> numberInList = inputNumber();
- case 4 -> day = inputDay();
- case 5 -> month = inputMonth();
- case 6 -> year = inputYear();
- case 7 -> sex = inputSex();
- }
- arrayOfRecords[number] = new Student(name, group, numberInList, day, month, year, sex);
- System.out.println("Ready!");
- System.out.println();
- }
- public static void addRecord () {
- String name, sex;
- int group, number, day, month, year;
- name = inputName();
- sex = inputSex();
- group = inputGroup();
- number = inputNumber();
- day = inputDay();
- month = inputMonth();
- year = inputYear();
- if (checkRepeats(name, group, number, day, month, year, sex)) {
- tempArray = arrayOfRecords;
- arrayOfRecords = new Student[n + 1];
- System.arraycopy(tempArray, 0, arrayOfRecords, 0, tempArray.length);
- arrayOfRecords[n] = new Student(name, group, number, day, month, year, sex);
- n++;
- System.out.println("Ready!");
- System.out.println();
- }
- }
- public static void showRecords () {
- String ANSI_YELLOW = "\u001B[33m";
- String ANSI_RESET = "\u001B[0m";
- if (n > 0) {
- for (int i = 0; i < arrayOfRecords.length; i++) {
- System.out.println(ANSI_YELLOW + "Student " + (i + 1) + ":");
- System.out.println("Surname: " + arrayOfRecords[i].name());
- System.out.println("Sex: " + arrayOfRecords[i].sex());
- System.out.println("Group: " + arrayOfRecords[i].group());
- System.out.println("Number in logbook: " + arrayOfRecords[i].number());
- System.out.println("Birthday: " + arrayOfRecords[i].day() + "." + arrayOfRecords[i].month() + "." + arrayOfRecords[i].year() + ANSI_RESET);
- System.out.println();
- }
- System.out.println();
- }
- else {
- System.out.println(ANSI_YELLOW + "The list of students is empty..." + ANSI_RESET);
- }
- }
- public static void deleteRecord () {
- if (!(arrayOfRecords.length == 0)) {
- String ANSI_RED = "\u001B[31m";
- String ANSI_RESET = "\u001B[0m";
- System.out.println("Specify the number of the student you want to delete:");
- int number;
- boolean isIncorrect;
- do {
- number = inputData();
- isIncorrect = false;
- if ((number > n) || (number < 1)) {
- isIncorrect = true;
- System.out.println("Please enter the number of an existing student");
- }
- } while (isIncorrect);
- number--;
- System.out.println(ANSI_RED + "Do you want to remove the student " + arrayOfRecords[number].name() + " ?");
- System.out.println("Write \"yes\" if yes. Write \"no\" if no." + ANSI_RESET);
- String answer;
- do {
- isIncorrect = false;
- answer = scanner.nextLine();
- if (answer.equals("yes") || answer.equals("Yes")) {
- if (number < (arrayOfRecords.length - 1)) {
- for (int i = number; i < (arrayOfRecords.length - 1); i++) { // вот тут может быть что-то не так
- arrayOfRecords[i] = arrayOfRecords[i + 1];
- }
- }
- tempArray = arrayOfRecords;
- arrayOfRecords = new Student[n - 1];
- System.arraycopy(tempArray, 0, arrayOfRecords, 0, arrayOfRecords.length);
- n--;
- } else if (answer.equals("no") || answer.equals("No")) {
- deleteRecord();
- } else {
- isIncorrect = true;
- System.out.println("Please enter \"yes\" or \"no.\"");
- }
- } while (isIncorrect);
- System.out.println("Ready!" + ANSI_RESET);
- System.out.println();
- } else {
- System.out.println("List is empty, impossible to delete someone.");
- }
- }
- public static boolean isAdult (int i) {
- boolean isYoung = true;
- GregorianCalendar gcalendar = new GregorianCalendar();
- int currYear = gcalendar.get(Calendar.YEAR);
- int currMonth = gcalendar.get(Calendar.MONTH);
- int currDay = gcalendar.get(Calendar.DAY_OF_MONTH);
- if (arrayOfRecords[i].year() < (currYear - 18))
- isYoung = false;
- if (arrayOfRecords[i].year() == (currYear - 18) && (arrayOfRecords[i].month() < (currMonth)))
- isYoung = false;
- if ((arrayOfRecords[i].year() == (currYear - 18)) && (arrayOfRecords[i].month() == currMonth) && (arrayOfRecords[i].day() <= currDay))
- isYoung = false;
- return isYoung;
- }
- public static void sortRecords () {
- Student min;
- if (arrayOfRecords.length > 1) { //сортиро4ка по номерам группы (по возрастанию)
- Student[] arrayForSort = arrayOfRecords;
- for (int i = 0; i < (arrayForSort.length - 1); i++) {
- if (isAdult(i)) {
- for (int j = i + 1; j < (arrayForSort.length); j++) {
- if (arrayForSort[j].group() < arrayForSort[i].group()) {
- min = arrayForSort[j];
- arrayForSort[j] = arrayForSort[i];
- arrayForSort[i] = min;
- }
- }
- }
- }
- for (int i = 0; i < (arrayForSort.length - 1); i++) {
- if (isAdult(i)) {
- for (int j = i + 1; j < (arrayForSort.length); j++) {
- if ((arrayForSort[j].number() < arrayForSort[i].number()) && (arrayForSort[j].group() == arrayForSort[i].group())) {
- min = arrayForSort[j];
- arrayForSort[j] = arrayForSort[i];
- arrayForSort[i] = min;
- }
- }
- }
- }
- String ANSI_YELLOW = "\u001B[33m";
- String ANSI_RESET = "\u001B[0m";
- int k = 0;
- for (int i = 0; i < arrayForSort.length; i++) {
- if (isAdult(i)) {
- System.out.println(ANSI_YELLOW + "Student " + (k + 1) + ":");
- System.out.println("Surname: " + arrayForSort[i].name());
- System.out.println("Sex: " + arrayForSort[i].sex());
- System.out.println("Group: " + arrayForSort[i].group());
- System.out.println("Number in logbook: " + arrayForSort[i].number());
- System.out.println("Birthday: " + arrayForSort[i].day() + "." + arrayForSort[i].month() + "." + arrayForSort[i].year() + ANSI_RESET);
- System.out.println();
- k++;
- }
- }
- System.out.println();
- }else{
- System.out.println("Sorting needs in at least 2 students.");
- }
- }
- public static String inputName () {
- System.out.println("Enter a student's surname:");
- boolean isIncorrect;
- String str;
- do {
- isIncorrect = false;
- str = inputString();
- if (!checkName(str))
- isIncorrect = true;
- }while (isIncorrect);
- return str;
- }
- public static boolean checkName (String str) {
- final int MAX = 30;
- boolean isOK = true;
- if ((str.length() < 1) || (str.length() > MAX)) {
- isOK = false;
- System.out.println("The length of the surname should not exceed " + MAX + " characters");
- }
- return isOK;
- }
- private static String inputSex() {
- int sex = 0;
- boolean isIncorrect;
- final int MIN_NUM = 1;
- final int MAX_NUM = 3;
- System.out.println("Enter 1, if the student is male.");
- System.out.println("Enter 2, if the student is female.");
- System.out.println("Enter 3, if the student is non-binary person.");
- do {
- isIncorrect = false;
- try {
- sex = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a number.");
- }
- if (!isIncorrect && (sex < MIN_NUM || sex > MAX_NUM)) {
- System.out.println("Enter a valid value. Repeat the entry.");
- isIncorrect = true;
- }
- if (!isIncorrect && (sex == 3)) {
- isIncorrect = true;
- System.out.println("Once all the genders came into the bar and said: \"We'll have a table for two\"");
- System.out.println("P.S. There are only two genders at our college. Repeat the entry.");
- }
- } while (isIncorrect);
- switch (sex) {
- case 1 -> {
- return "Male";
- }
- case 2 -> {
- return "Female";
- }
- }
- return "";
- }
- public static boolean checkSex (String str) { // это только для файлов, в консоли - своя проверка
- boolean isCorrect = str.equals("Female") || str.equals("Male") || str.equals("female") || str.equals("male");
- if (!isCorrect) {
- System.out.println("Incorrect data. Field \"sex\" should contain either \"Male\" or \"Female\"");
- }
- return isCorrect;
- }
- public static String inputString () {
- String str;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- str = scanner.nextLine();
- for (int i = 0; i < str.length(); i++) {
- if ((str.charAt(i) < (int)'A') || (str.charAt(i) > (int)'z')) {
- if (str.charAt(i) != (int)' ') {
- isIncorrect = true;
- System.out.println("Please enter a string consisting of Latin characters");
- break;
- }
- }
- }
- } while (isIncorrect);
- return str;
- }
- public static int inputGroup () {
- boolean isIncorrect;
- int group;
- System.out.println("Enter the student's group number. Length - no more than 8 digits.");
- final int MAX = 99999999;
- do {
- isIncorrect = false;
- group = inputData();
- if (group > MAX) {
- System.out.println("Group number - no more than 8 digits.");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return group;
- }
- public static boolean checkGroup (String strGroup) {
- boolean isIncorrect;
- int group = 0;
- final int MIN_SIZE = 1;
- final int MAX_SIZE = 99999999;
- isIncorrect = false;
- try {
- group = Integer.parseInt(strGroup);
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please enter an integer number");
- }
- if ((group < MIN_SIZE || group > MAX_SIZE) && !isIncorrect) {
- System.out.println("Please enter a positive number (no longer than 8) symbols");
- isIncorrect = true;
- }
- return !isIncorrect;
- }
- private static int inputData() {
- int n = 0;
- boolean isIncorrect;
- final int MIN_SIZE = 1;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please enter an integer number");
- }
- if (n < MIN_SIZE && !isIncorrect) {
- System.out.println("Please enter a positive number");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return n;
- }
- public static int inputNumber () {
- boolean isIncorrect;
- int number;
- System.out.println("Enter the number of the student in the log list (not more than 40).");
- final int MAX = 40;
- do {
- isIncorrect = false;
- number = inputData();
- if (number > MAX) {
- System.out.println("Student number - a number not to exceed 40");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return number;
- }
- public static boolean checkNumber (String strNum) {
- boolean isIncorrect;
- int number = 0;
- final int MIN_SIZE = 1;
- final int MAX_SIZE = 40;
- isIncorrect = false;
- try {
- number = Integer.parseInt(strNum);
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please enter an integer number");
- }
- if ((number < MIN_SIZE || number > MAX_SIZE) && !isIncorrect) {
- System.out.println("Please enter a number in range [1..40]");
- isIncorrect = true;
- }
- return !isIncorrect;
- }
- public static int inputDay () {
- boolean isIncorrect;
- int day;
- System.out.println("Enter day of birthday:");
- final int MAX = 31;
- do {
- isIncorrect = false;
- day = inputData();
- if (day > MAX) {
- System.out.println("Range: 1..31");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return day;
- }
- public static boolean checkDay (String strDay) {
- boolean isIncorrect;
- int day = 0;
- final int MIN_SIZE = 1;
- final int MAX_SIZE = 31;
- isIncorrect = false;
- try {
- day = Integer.parseInt(strDay);
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please enter an integer number");
- }
- if ((day < MIN_SIZE || day > MAX_SIZE) && !isIncorrect) {
- System.out.println("Please enter a number in range [1..31]");
- isIncorrect = true;
- }
- return !isIncorrect;
- }
- public static int inputMonth () {
- boolean isIncorrect;
- int month;
- System.out.println("Enter month of birthday:");
- final int MAX = 12;
- do {
- isIncorrect = false;
- month = inputData();
- if (month > MAX) {
- System.out.println("Range: 1..12");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return month;
- }
- public static boolean checkMonth (String strMonth) {
- boolean isIncorrect;
- int month = 0;
- final int MIN_SIZE = 1;
- final int MAX_SIZE = 12;
- isIncorrect = false;
- try {
- month = Integer.parseInt(strMonth);
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please enter an integer number");
- }
- if ((month < MIN_SIZE || month > MAX_SIZE) && !isIncorrect) {
- System.out.println("Please enter a number in range [1..12]");
- isIncorrect = true;
- }
- return !isIncorrect;
- }
- public static int inputYear () {
- boolean isIncorrect;
- int year;
- GregorianCalendar gcalendar = new GregorianCalendar();
- int currYear = gcalendar.get(Calendar.YEAR);
- System.out.println("Enter year of birthday:");
- final int MIN_YEAR = 1900;
- do {
- isIncorrect = false;
- year = inputData();
- if (year > currYear || year < MIN_YEAR) {
- System.out.println("Range: 1900..current year");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return year;
- }
- public static boolean checkYear (String strYear) {
- boolean isIncorrect;
- int year = 0;
- GregorianCalendar gcalendar = new GregorianCalendar();
- int currYear = gcalendar.get(Calendar.YEAR);
- final int MIN_SIZE = 1900;
- isIncorrect = false;
- try {
- year = Integer.parseInt(strYear);
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please enter an integer number");
- }
- if ((year < MIN_SIZE || year > currYear) && !isIncorrect) {
- System.out.println("Please enter a number in range [1900..current year]");
- isIncorrect = true;
- }
- return !isIncorrect;
- }
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scanner.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("Wrong way to file.");
- isIncorrect = true;
- }
- if (!file.canRead() && file.exists()) {
- System.out.println("Impossible to read a file.");
- isIncorrect = true;
- }
- if (!file.canWrite() && file.canRead()) {
- System.out.println("Impossible to write a file.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- public static boolean checkRepeats(String name, int group, int number, int day, int month, int year, String sex) {
- boolean isCorrect = true;
- if (Objects.equals(sex, "male"))
- sex = "Male";
- if (Objects.equals(sex, "female"))
- sex = "Female";
- if(arrayOfRecords.length > 0) {
- for (Student arrayOfRecord : arrayOfRecords) {
- if ((name.equals(arrayOfRecord.name())) && (group == arrayOfRecord.group()) &&
- (number == (arrayOfRecord.number())) && (day == arrayOfRecord.day()) &&
- (month == arrayOfRecord.month()) && (year == arrayOfRecord.year()) &&
- (sex.equals(arrayOfRecord.sex()))) {
- isCorrect = false;
- System.out.println("Such an entry already exists.");
- break;
- }
- }
- }
- return isCorrect;
- }
- private static boolean checkDataCorrectness(String str) {
- boolean isCorrect = true;
- boolean isCorrectName, isCorrectGroup, isCorrectNumber, isCorrectSex, isCorrectDay, isCorrectMonth, isCorrectYear;
- String[] chr;
- chr = str.split(" ");
- if (chr.length > 7) {
- System.out.println("Too many characteristics in string.");
- isCorrect = false;
- } else {
- isCorrectName = checkName(chr[0]);
- if (!isCorrectName) {
- isCorrect = false;
- }
- isCorrectSex = checkSex(chr[6]);
- if (!isCorrectSex) {
- isCorrect = false;
- }
- isCorrectGroup = checkGroup(chr[1]);
- if (!isCorrectGroup) {
- isCorrect = false;
- }
- isCorrectNumber = checkNumber(chr[2]);
- if (!isCorrectNumber) {
- isCorrect = false;
- }
- isCorrectDay = checkDay(chr[3]);
- if (!isCorrectDay) {
- isCorrect = false;
- }
- isCorrectMonth = checkMonth(chr[4]);
- if (!isCorrectMonth) {
- isCorrect = false;
- }
- isCorrectYear = checkYear(chr[5]);
- if (!isCorrectYear) {
- isCorrect = false;
- }
- }
- return isCorrect;
- }
- public static void openFile () {
- System.out.println("Example of string with info about student:");
- System.out.println("Name - Group - Number in list - Day (of birthday) - Month (of birthday) - Year (of birthday) - Sex");
- System.out.println("P.S. after every position expected only space ");
- String path = inputFilePath();
- String str;
- boolean isIncorrect = true;
- String[] mas;
- try {
- Scanner fileReader = new Scanner(new File(path));
- while((fileReader.hasNext()) && isIncorrect) {
- str = fileReader.nextLine();
- isIncorrect = checkDataCorrectness(str);
- if (isIncorrect) {
- mas = str.split(" ");
- int group = Integer.parseInt(mas[1]);
- int number = Integer.parseInt(mas[2]);
- int day = Integer.parseInt(mas[3]);
- int month = Integer.parseInt(mas[4]);
- int year = Integer.parseInt(mas[5]);
- if (Objects.equals(mas[6], "male"))
- mas[6] = "Male";
- if (Objects.equals(mas[6], "female"))
- mas[6] = "Female";
- if (Objects.equals(mas[6], "male"))
- mas[6] = "Male";
- isIncorrect = checkRepeats(mas[0], group, number, day, month, year, mas[6]);
- if (isIncorrect){
- tempArray = arrayOfRecords;
- arrayOfRecords = new Student[n + 1];
- System.arraycopy(tempArray, 0, arrayOfRecords, 0, tempArray.length);
- arrayOfRecords[n] = new Student(mas[0], group, number, day, month, year, mas[6]);
- n++;
- }
- } else {
- System.out.println("Such a record already exists.");
- }
- }
- if (isIncorrect)
- System.out.println("Ready!");
- } catch (Exception q) {
- System.out.println("Something went wrong, check file.");
- }
- }
- private static void saveData() throws IOException {
- if (arrayOfRecords.length > 0) {
- String path = inputFilePath();
- FileWriter writer = new FileWriter(path);
- for (Student arrayOfRecord : arrayOfRecords) {
- writer.write(arrayOfRecord.name() + " " + arrayOfRecord.group() + " " +
- arrayOfRecord.number() + " " + arrayOfRecord.day() + " " + arrayOfRecord.month()
- + " " + arrayOfRecord.year() + " " + arrayOfRecord.sex());
- writer.write("\n");
- }
- writer.close();
- System.out.println("Ready!");
- } else
- System.out.println("There is nothing to save, bro.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement