Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class MainClass {
- public static Scanner scan = new Scanner(System.in);
- public static void main(String[] args) {
- final String SHOW_TEAMS = "1";
- final String ADD_NEW_TEAM = "2";
- final String LOAD_TEAMS_FROM_FILE = "3";
- final String SAVE_TEAMS_TO_FILE = "4";
- final String SHOW_TOURNAMENT_TOP = "5";
- final String CHANGE_TEAM_INFO = "6";
- final String DELETE_TEAM_INFO = "7";
- ArrayList<Teams> teams = new ArrayList<>();
- String inStr;
- do {
- showMenu();
- inStr = scan.nextLine();
- switch (inStr.toLowerCase()){
- case SHOW_TEAMS:{
- showTeams(teams);
- break;
- }
- case ADD_NEW_TEAM:{
- Teams newTeam = addNewTeam();
- boolean isExist = false;
- for (Teams team : teams){
- if (newTeam.isAlreadyExists(team)){
- isExist = true;
- break;
- }
- }
- if (isExist){
- System.out.println("Team is already in list");
- } else {
- teams.add(newTeam);
- System.out.println("||Team adding was successfully done||\n");
- }
- break;
- }
- case LOAD_TEAMS_FROM_FILE:{
- String inPath = takeInPath();
- teams = loadTeamsFromFile(inPath);
- break;
- }
- case SAVE_TEAMS_TO_FILE:{
- String outPath = takeOutPath();
- saveToFile(teams, outPath);
- break;
- }
- case SHOW_TOURNAMENT_TOP:{
- showTournamentTop(teams);
- break;
- }
- case CHANGE_TEAM_INFO:{
- showTeams(teams);
- changeTeamInfo(teams);
- System.out.println("||Team changing was successfully done||\n");
- break;
- }
- case DELETE_TEAM_INFO:{
- showTeams(teams);
- System.out.print("Enter number of team that you want to delete");
- int num = takeInt(1, Array.getLength(teams.toArray()));
- teams.remove(teams.get(num - 1));
- System.out.println("||Team deleting was successfully done||\n");
- break;
- }
- case "x":{
- break;
- }
- default:{
- System.out.println("Incorrect choice, try again!");
- }
- }
- } while (!inStr.equalsIgnoreCase("x"));
- scan.close();
- }
- static String takeOutPath() {
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (!path.endsWith(".txt")) {
- System.out.print("It should be a \".txt\" file\nEnter file path: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static void saveToFile(ArrayList<Teams> staff, String path) {
- if (staff.isEmpty()){
- System.out.println("There is no data, saving is stopped");
- } else {
- try (FileWriter fw = new FileWriter(path)) {
- fw.write("Teams list");
- for (Teams teams : staff) {
- fw.write(teams.getTeamName() + " ");
- fw.write(teams.getTeamPoints() + " ");
- fw.write(teams.getTeamCountry() + " ");
- fw.write(teams.getCoachName() + " ");
- fw.write("\n");
- }
- System.out.println("||Saved||");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- static String takeInPath() {
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.print("File is not found\nEnter file path: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (!path.endsWith(".txt"))) {
- System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static ArrayList<Teams> loadTeamsFromFile(String filePath) {
- ArrayList<Teams> staff = new ArrayList<>();
- try(Scanner fileScan = new Scanner(new FileReader(filePath))){
- while(fileScan.hasNext()) {
- String[] newTeamInfo = fileScan.nextLine().split(" ");
- Teams newTeam = new Teams();
- newTeam.setTeamName(newTeamInfo[0]);
- newTeam.setTeamPoints(Integer.parseInt(newTeamInfo[1]));
- newTeam.setTeamCountry(newTeamInfo[2]);
- newTeam.setCoachName(newTeamInfo[3]);
- staff.add(newTeam);
- }
- System.out.println("||Teams loading successfully done||");
- } catch (Exception e){
- e.printStackTrace();
- }
- return staff;
- }
- private static void showTournamentTop(ArrayList<Teams> teams) {
- Teams[] tournamentTop = new Teams [Array.getLength(teams.toArray())];
- int i = 0;
- for (Teams person : teams){
- tournamentTop[i++] = person;
- }
- for (int left = 0; left < tournamentTop.length; left++) {
- Teams current = tournamentTop[left];
- i = left - 1;
- for (; i >= 0; i--) {
- if (current.getTeamPoints() > tournamentTop[i].getTeamPoints()) {
- tournamentTop[i + 1] = tournamentTop[i];
- } else {
- break;
- }
- }
- tournamentTop[i + 1] = current;
- }
- printTop(tournamentTop);
- }
- private static void changeTeamInfo(ArrayList<Teams> teams) {
- System.out.print("Enter number of team that you want to change");
- int num = takeInt(1, Array.getLength(teams.toArray()));
- System.out.println("Enter new team data");
- Teams chosenTeam = addNewTeam();
- teams.remove(teams.get(num - 1));
- teams.add(chosenTeam);
- }
- private static Teams addNewTeam() {
- Teams newTeam = new Teams();
- System.out.print("\nEnter team name: ");
- newTeam.setTeamName(scan.nextLine());
- System.out.print("Enter team tournament points");
- newTeam.setTeamPoints(takeInt(0, 999));
- System.out.print("Enter team's country: ");
- newTeam.setTeamCountry(scan.nextLine());
- System.out.print("Enter coach's name: ");
- newTeam.setCoachName(scan.nextLine());
- return newTeam;
- }
- private static int takeInt(final int MIN, final int MAX) {
- boolean isIncorrect;
- int num = -1;
- do {
- isIncorrect = false;
- System.out.printf(" in range (%s, %s): ", MIN, MAX);
- try {
- num = Integer.parseInt(scan.nextLine());
- } catch (NumberFormatException e) {
- isIncorrect = true;
- }
- if (!isIncorrect) {
- isIncorrect = num > MAX | num < MIN;
- }
- if (isIncorrect){
- System.out.print("Incorrect input, try again!\nEnter number ");
- }
- } while (isIncorrect);
- return num;
- }
- private static void showMenu() {
- System.out.println("______________________________________");
- System.out.println(" MENU");
- System.out.println("______________________________________");
- System.out.println("1. Show all teams");
- System.out.println("2. Add new team");
- System.out.println("3. Load list of teams from the file");
- System.out.println("4. Save list of teams to the file");
- System.out.println("5. Show tournament top");
- System.out.println("6. Change team info in system");
- System.out.println("7. Delete team info in system");
- System.out.println("X. Close project");
- }
- private static void printTop(Teams[] teams) {
- int i = 0;
- String numStr;
- StringBuilder nameSep;
- if (1 > Array.getLength(teams)){
- System.out.println("There is no teams");
- } else{
- System.out.println("\n-------------------------");
- System.out.println("| TOURNAMENT TOP |");
- System.out.println("-------------------------");
- System.out.println("№ |Team name |Points");
- System.out.println("-------------------------");
- for(Teams team : teams){
- numStr = ". |";
- nameSep = new StringBuilder("|");
- if (i++ > 8){
- numStr = ".|";
- }
- for (int j = 0; j < (10 - team.getTeamName().length()); j++) {
- nameSep.insert(0, " ");
- }
- System.out.println(i + numStr + team.getTeamName() + nameSep +team.getTeamPoints());
- }
- System.out.println();
- }
- }
- private static void showTeams(ArrayList<Teams> teams) {
- int i = 0;
- String numStr;
- StringBuilder nameSep;
- StringBuilder pointsSep;
- StringBuilder countrySep;
- if (teams.isEmpty()){
- System.out.println("There is no teams");
- } else{
- System.out.println("\n-------------------------------------------");
- System.out.println("| TEAM LIST |");
- System.out.println("-------------------------------------------");
- System.out.println("№ |Team name |Points| Country | Coach");
- System.out.println("-------------------------------------------");
- for(Teams team : teams){
- numStr = ". |";
- nameSep = new StringBuilder("|");
- pointsSep = new StringBuilder(nameSep.toString());
- countrySep = new StringBuilder(nameSep.toString());
- if (i++ > 8){
- numStr = ".|";
- }
- for (int j = 0; j < (10 - team.getTeamName().length()); j++) {
- nameSep.insert(0, " ");
- }
- for (int j = 0; j < (10 - team.getTeamCountry().length()); j++) {
- countrySep.insert(0, " ");
- }
- for (int j = 0; j < (6 - Integer.toString(team.getTeamPoints()).length()); j++) {
- pointsSep.insert(0, " ");
- }
- System.out.println(i + numStr + team.getTeamName() + nameSep +team.getTeamPoints() + pointsSep + team.getTeamCountry() + countrySep + team.getCoachName());
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement