Advertisement
Egor_Vakar

(Java) lab 4.1 MainClass

Feb 22nd, 2022
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.53 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.reflect.Array;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class MainClass {
  7.     public static Scanner scan = new Scanner(System.in);
  8.  
  9.     public static void main(String[] args) {
  10.         final String SHOW_TEAMS = "1";
  11.         final String ADD_NEW_TEAM = "2";
  12.         final String LOAD_TEAMS_FROM_FILE = "3";
  13.         final String SAVE_TEAMS_TO_FILE = "4";
  14.         final String SHOW_TOURNAMENT_TOP = "5";
  15.         final String CHANGE_TEAM_INFO = "6";
  16.         final String DELETE_TEAM_INFO = "7";
  17.         ArrayList<Teams> teams = new ArrayList<>();
  18.         String inStr;
  19.         do {
  20.             showMenu();
  21.             inStr = scan.nextLine();
  22.             switch (inStr.toLowerCase()){
  23.                 case SHOW_TEAMS:{
  24.                     showTeams(teams);
  25.                     break;
  26.                 }
  27.                 case ADD_NEW_TEAM:{
  28.                     Teams newTeam =  addNewTeam();
  29.                     boolean isExist = false;
  30.                     for (Teams team : teams){
  31.                         if (newTeam.isAlreadyExists(team)){
  32.                             isExist = true;
  33.                             break;
  34.                         }
  35.                     }
  36.                     if (isExist){
  37.                         System.out.println("Team is already in list");
  38.                     } else {
  39.                         teams.add(newTeam);
  40.                         System.out.println("||Team adding was successfully done||\n");
  41.                     }
  42.                     break;
  43.                 }
  44.                 case LOAD_TEAMS_FROM_FILE:{
  45.                     String inPath = takeInPath();
  46.                     teams = loadTeamsFromFile(inPath);
  47.                     break;
  48.                 }
  49.                 case SAVE_TEAMS_TO_FILE:{
  50.                     String outPath = takeOutPath();
  51.                     saveToFile(teams, outPath);
  52.                     break;
  53.                 }
  54.                 case SHOW_TOURNAMENT_TOP:{
  55.                     showTournamentTop(teams);
  56.                     break;
  57.                 }
  58.                 case CHANGE_TEAM_INFO:{
  59.                     showTeams(teams);
  60.                     changeTeamInfo(teams);
  61.                     System.out.println("||Team changing was successfully done||\n");
  62.                     break;
  63.  
  64.                 }
  65.                 case DELETE_TEAM_INFO:{
  66.                     showTeams(teams);
  67.                     System.out.print("Enter number of team that you want to delete");
  68.                     int num = takeInt(1, Array.getLength(teams.toArray()));
  69.                     teams.remove(teams.get(num - 1));
  70.                     System.out.println("||Team deleting was successfully done||\n");
  71.                     break;
  72.                 }
  73.                 case "x":{
  74.                     break;
  75.                 }
  76.                 default:{
  77.                     System.out.println("Incorrect choice, try again!");
  78.                 }
  79.             }
  80.         } while (!inStr.equalsIgnoreCase("x"));
  81.         scan.close();
  82.     }
  83.  
  84.     static String takeOutPath() {
  85.         String path;
  86.         boolean isIncorrect;
  87.         System.out.print("Enter file path: ");
  88.         do {
  89.             isIncorrect = false;
  90.             path = scan.nextLine();
  91.             if (!path.endsWith(".txt")) {
  92.                 System.out.print("It should be a \".txt\" file\nEnter file path: ");
  93.                 isIncorrect = true;
  94.             }
  95.         } while (isIncorrect);
  96.         return path;
  97.     }
  98.  
  99.     private static void saveToFile(ArrayList<Teams> staff, String path) {
  100.         if (staff.isEmpty()){
  101.             System.out.println("There is no data, saving is stopped");
  102.         } else {
  103.             try (FileWriter fw = new FileWriter(path)) {
  104.                 fw.write("Teams list");
  105.                 for (Teams teams : staff) {
  106.                     fw.write(teams.getTeamName() + " ");
  107.                     fw.write(teams.getTeamPoints() + " ");
  108.                     fw.write(teams.getTeamCountry() + " ");
  109.                     fw.write(teams.getCoachName() + " ");
  110.                     fw.write("\n");
  111.                 }
  112.                 System.out.println("||Saved||");
  113.             } catch (IOException e) {
  114.                 e.printStackTrace();
  115.             }
  116.         }
  117.     }
  118.  
  119.     static String takeInPath() {
  120.         String path;
  121.         boolean isIncorrect;
  122.         System.out.print("Enter file path: ");
  123.         do {
  124.             isIncorrect = false;
  125.             path = scan.nextLine();
  126.             File file = new File(path);
  127.             if (!file.exists()) {
  128.                 System.out.print("File is not found\nEnter file path: ");
  129.                 isIncorrect = true;
  130.             }
  131.             if (!isIncorrect && (!path.endsWith(".txt"))) {
  132.                 System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
  133.                 isIncorrect = true;
  134.             }
  135.         } while (isIncorrect);
  136.         return path;
  137.     }
  138.  
  139.     private static ArrayList<Teams> loadTeamsFromFile(String filePath) {
  140.         ArrayList<Teams> staff = new ArrayList<>();
  141.         try(Scanner fileScan = new Scanner(new FileReader(filePath))){
  142.             while(fileScan.hasNext()) {
  143.                 String[] newTeamInfo = fileScan.nextLine().split(" ");
  144.                 Teams newTeam = new Teams();
  145.                 newTeam.setTeamName(newTeamInfo[0]);
  146.                 newTeam.setTeamPoints(Integer.parseInt(newTeamInfo[1]));
  147.                 newTeam.setTeamCountry(newTeamInfo[2]);
  148.                 newTeam.setCoachName(newTeamInfo[3]);
  149.                 staff.add(newTeam);
  150.             }
  151.             System.out.println("||Teams loading successfully done||");
  152.         } catch (Exception e){
  153.             e.printStackTrace();
  154.         }
  155.         return staff;
  156.     }
  157.  
  158.     private static void showTournamentTop(ArrayList<Teams> teams) {
  159.         Teams[] tournamentTop = new Teams [Array.getLength(teams.toArray())];
  160.         int i = 0;
  161.         for (Teams person : teams){
  162.             tournamentTop[i++] = person;
  163.         }
  164.         for (int left = 0; left < tournamentTop.length; left++) {
  165.             Teams current = tournamentTop[left];
  166.             i = left - 1;
  167.             for (; i >= 0; i--) {
  168.                 if (current.getTeamPoints() > tournamentTop[i].getTeamPoints()) {
  169.                     tournamentTop[i + 1] = tournamentTop[i];
  170.                 } else {
  171.                     break;
  172.                 }
  173.             }
  174.             tournamentTop[i + 1] = current;
  175.         }
  176.         printTop(tournamentTop);
  177.     }
  178.  
  179.     private static void changeTeamInfo(ArrayList<Teams> teams) {
  180.         System.out.print("Enter number of team that you want to change");
  181.         int num = takeInt(1, Array.getLength(teams.toArray()));
  182.         System.out.println("Enter new team data");
  183.         Teams chosenTeam = addNewTeam();
  184.         teams.remove(teams.get(num - 1));
  185.         teams.add(chosenTeam);
  186.     }
  187.  
  188.     private static Teams addNewTeam() {
  189.         Teams newTeam = new Teams();
  190.         System.out.print("\nEnter team name: ");
  191.         newTeam.setTeamName(scan.nextLine());
  192.         System.out.print("Enter team tournament points");
  193.         newTeam.setTeamPoints(takeInt(0, 999));
  194.         System.out.print("Enter team's country: ");
  195.         newTeam.setTeamCountry(scan.nextLine());
  196.         System.out.print("Enter coach's name: ");
  197.         newTeam.setCoachName(scan.nextLine());
  198.         return newTeam;
  199.     }
  200.  
  201.     private static int takeInt(final int MIN, final int MAX) {
  202.         boolean isIncorrect;
  203.         int num = -1;
  204.         do {
  205.             isIncorrect = false;
  206.             System.out.printf(" in range (%s, %s): ", MIN, MAX);
  207.             try {
  208.                 num = Integer.parseInt(scan.nextLine());
  209.             } catch (NumberFormatException e) {
  210.                 isIncorrect = true;
  211.             }
  212.             if (!isIncorrect) {
  213.                 isIncorrect = num > MAX | num < MIN;
  214.             }
  215.             if (isIncorrect){
  216.                 System.out.print("Incorrect input, try again!\nEnter number ");
  217.             }
  218.         } while (isIncorrect);
  219.         return num;
  220.     }
  221.  
  222.     private static void showMenu() {
  223.         System.out.println("______________________________________");
  224.         System.out.println("                MENU");
  225.         System.out.println("______________________________________");
  226.         System.out.println("1. Show all teams");
  227.         System.out.println("2. Add new team");
  228.         System.out.println("3. Load list of teams from the file");
  229.         System.out.println("4. Save list of teams to the file");
  230.         System.out.println("5. Show tournament top");
  231.         System.out.println("6. Change team info in system");
  232.         System.out.println("7. Delete team info in system");
  233.         System.out.println("X. Close project");
  234.     }
  235.  
  236.     private static void printTop(Teams[] teams) {
  237.         int i = 0;
  238.         String numStr;
  239.         StringBuilder nameSep;
  240.         if (1 > Array.getLength(teams)){
  241.             System.out.println("There is no teams");
  242.         } else{
  243.             System.out.println("\n-------------------------");
  244.             System.out.println("|     TOURNAMENT TOP     |");
  245.             System.out.println("-------------------------");
  246.             System.out.println("№  |Team name |Points");
  247.             System.out.println("-------------------------");
  248.             for(Teams team : teams){
  249.                 numStr = ". |";
  250.                 nameSep = new StringBuilder("|");
  251.                 if (i++ > 8){
  252.                     numStr = ".|";
  253.                 }
  254.                 for (int j = 0; j < (10 - team.getTeamName().length()); j++) {
  255.                     nameSep.insert(0, " ");
  256.                 }
  257.                 System.out.println(i + numStr + team.getTeamName() + nameSep +team.getTeamPoints());
  258.             }
  259.             System.out.println();
  260.         }
  261.     }
  262.  
  263.  
  264.  
  265.     private static void showTeams(ArrayList<Teams> teams) {
  266.         int i = 0;
  267.         String numStr;
  268.         StringBuilder nameSep;
  269.         StringBuilder pointsSep;
  270.         StringBuilder countrySep;
  271.         if (teams.isEmpty()){
  272.             System.out.println("There is no teams");
  273.         } else{
  274.             System.out.println("\n-------------------------------------------");
  275.             System.out.println("|                 TEAM LIST                |");
  276.             System.out.println("-------------------------------------------");
  277.             System.out.println("№  |Team name |Points| Country  | Coach");
  278.             System.out.println("-------------------------------------------");
  279.             for(Teams team : teams){
  280.                 numStr = ". |";
  281.                 nameSep = new StringBuilder("|");
  282.                 pointsSep = new StringBuilder(nameSep.toString());
  283.                 countrySep = new StringBuilder(nameSep.toString());
  284.                 if (i++ > 8){
  285.                     numStr = ".|";
  286.                 }
  287.                 for (int j = 0; j < (10 - team.getTeamName().length()); j++) {
  288.                     nameSep.insert(0, " ");
  289.                 }
  290.                 for (int j = 0; j < (10 - team.getTeamCountry().length()); j++) {
  291.                     countrySep.insert(0, " ");
  292.                 }
  293.                 for (int j = 0; j < (6 - Integer.toString(team.getTeamPoints()).length()); j++) {
  294.                     pointsSep.insert(0, " ");
  295.                 }
  296.                 System.out.println(i + numStr + team.getTeamName() + nameSep +team.getTeamPoints() + pointsSep + team.getTeamCountry() + countrySep + team.getCoachName());
  297.             }
  298.             System.out.println();
  299.         }
  300.     }
  301. }
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement