Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.time.LocalDate;
- import java.util.*;
- import java.nio.file.FileSystems;
- import Manager.*;
- public class Main {
- static Vector<Person> nameList = new Vector< >();
- static Vector<Task> taskList = new Vector< >();
- static Vector<Project> projectList = new Vector< >();
- public static void main(String[] args) throws IOException {
- Scanner in = new Scanner(System.in);
- String[] splitedArray = null;
- int indexOfPerson;
- int indexOfTask;
- int indexOfProject;
- String name;
- boolean flag;
- LocalDate date;
- while(true){
- System.out.print(">> ");
- String cmd = in.nextLine();
- flag = false;
- try {
- if (cmd.startsWith("add person")) { //Dodaj osobę :->add person nazwa_osoby
- AddPerson(cmd);
- }
- else if (cmd.startsWith("export")) {
- splitedArray = cmd.split(" ");
- name = "test.txt";
- if (splitedArray.length >= 2) {
- if (splitedArray[1].length() != 0)
- name = splitedArray[1];
- else {
- System.out.println("Nazwa nieprawidłowa");
- continue;
- }
- //Main.Export(name);
- Main.Export(name+ ".txt"); // zmienić potem na name ...
- } else {
- throw new BaseExeption( "noFile");
- }
- }
- else if (cmd.startsWith("import")) {
- splitedArray = cmd.split(" ");
- //name = splitedArray[1];
- if (splitedArray.length >= 2) {
- if (splitedArray[1].length() != 0)
- name = splitedArray[1];
- else {
- System.out.println("Nazwa nieprawidłowa");
- continue;
- }
- nameList.clear();
- taskList.clear();
- projectList.clear();
- Main.Import(name);
- } else {
- throw new BaseExeption( "noFile");
- }
- }
- else if (cmd.startsWith("modify person")) { //Modyfikuj osobę :-> modify person indeks_osoby nazwa
- ModifyPerson(cmd, splitedArray);
- }
- else if (cmd.startsWith("delete person")) { //Usuń osobę :-> delete person indeks_osoby
- DeletePerson(cmd, splitedArray);
- }
- else if (cmd.startsWith("list person")) { //Wypisz osoby :-> list person
- ListPerson();
- }
- else if (cmd.startsWith("add task")) { //Dodaj zadanie :-> add task nazwa indeks_osoby
- AddTask(cmd, splitedArray);
- }
- else if (cmd.startsWith("modify task")) { //Modyfikuj zadanie :-> modify task indeks_zadania nazwa
- ModifyTask(cmd, splitedArray);
- }
- else if (cmd.startsWith("delete task")) { //Usuń zadanie :-> delete person indeks_zadania
- DeleteTask(cmd, splitedArray);
- }
- else if (cmd.startsWith("add tperson")) { //Przypisanie osoby do zadania :-> add tperson indeks_zadania indeks_osoby
- AddTperson(cmd, splitedArray);
- }
- else if (cmd.startsWith("list task")) { //Wypisz zadania :-> list task
- ListTask();
- }
- else if (cmd.startsWith("add deadline")) { //Dodanie daty do zadania :-> add deadline indeks_zadania RRRR MM DD
- AddDeadline(cmd, splitedArray);
- }
- else if (cmd.startsWith("order task asc")) { //Posortwanie według daty rosnąco :-> order task asc
- Collections.sort(taskList);
- }
- else if (cmd.startsWith("order task desc")) { //Tu zmienić na komparator
- Collections.sort(taskList);
- Collections.reverse(taskList);
- }
- else if (cmd.startsWith("late")) { //Wypisanie zadań spóźnionych :-> late
- Late();
- }
- else if (cmd.startsWith("add project")) { //Dodanie projektu :-> add project nazwa
- AddProject(cmd);
- }
- else if (cmd.startsWith("modify project")) { //Zmiana nazwy projektu
- ModifyProject(cmd, splitedArray);
- }
- else if (cmd.startsWith("delete project")) { //Usuwanie projektu
- DeleteProject(cmd, splitedArray);
- }
- else if (cmd.startsWith("add pperson")) { //Dodawanie osoby do projektu
- AddPperson(cmd, splitedArray);
- }
- else if (cmd.startsWith("delete pperson")) { //Usuwanie osoby z projektu
- DeletePperson(cmd, splitedArray);
- }
- else if (cmd.startsWith("add ptask")) { //Dodanie zadania do projektu
- AddPtask(cmd, splitedArray);
- }
- else if (cmd.startsWith("delete ptask")) { //Usuwanie zadania z projektu
- DeletePtask(cmd, splitedArray);
- }
- else if (cmd.startsWith("list project")) { //Wyświetl wszystkie projekty
- ListProject();
- }
- else if (cmd.startsWith("list ptask")) { //Wyświetl task'i danego projektu
- ListPtask(cmd, splitedArray);
- }
- else if (cmd.startsWith("list pperson")) { //Wyświetl person'y danego projektu
- ListPperson(cmd, splitedArray);
- }
- else if (cmd.startsWith("quit")) break;
- else {
- System.out.println("Niepoprawna komenda. ---");
- }
- }
- catch(BaseExeption e){
- if(e.getMessage().equals("noParam"))
- e.Parmeters();
- else if(e.getMessage().equals("noMember"))
- e.Member();
- else if(e.getMessage().equals("noTask"))
- e.Task();
- else if(e.getMessage().equals("noProject"))
- e.Project();
- else if(e.getMessage().equals("noFile"))
- e.File();
- else
- System.out.println("Błąd. Klasa" + e.toString() + "nie obejmuje błędu.");
- }
- }
- }
- public static void AddPerson(String cmd){
- Person newPerson = new Person(cmd.substring(11));
- nameList.add(newPerson);
- }
- public static void ModifyPerson(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- String name;
- int indexOfPerson;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- indexOfPerson = Integer.parseInt(splitedArray[2]);
- name = splitedArray[3];
- }
- else {
- throw new BaseExeption( "noParam");
- }
- for (Person somePerson : nameList) {
- if (somePerson.getID() == indexOfPerson) {
- somePerson.setName(name);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noMember");
- }
- }
- public static void DeletePerson(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfPerson;
- boolean flag = false;
- if (splitedArray.length >= 3) {
- indexOfPerson = Integer.parseInt(splitedArray[2]);
- }
- else {
- throw new BaseExeption( "noParam");
- }
- for (Person somePerson : nameList) {
- if (somePerson.getID() == indexOfPerson) {
- for (Task someTask : taskList) {
- if (someTask.getPerson().equals(somePerson)) someTask.setPerson(null);
- }
- for (Project someProject : projectList) {
- someProject.deletePerson(somePerson);
- }
- nameList.remove(somePerson);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noMember");
- }
- }
- public static void ListPerson(){ //Można dodać żeby więcej info wypisywało
- System.out.println("PERSONS\nID | Name");
- for (Person somePerson : nameList) {
- System.out.println(somePerson.getID() + " | " + somePerson.getName());
- }
- }
- public static void AddTask(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfPerson;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- Task newTask = new Task(splitedArray[2]);
- indexOfPerson = Integer.parseInt(splitedArray[3]);
- for (Person somePerson : nameList) {
- if (somePerson.getID() == indexOfPerson) {
- newTask.setPerson(somePerson);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noMember");
- }
- else {
- taskList.add(newTask);
- }
- }
- else {
- throw new BaseExeption( "noParam");
- }
- }
- public static void ModifyTask(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfTask;
- String name;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- indexOfTask = Integer.parseInt(splitedArray[2]);
- name = splitedArray[3];
- for (Task someTask : taskList) {
- if (someTask.getID() == indexOfTask) {
- someTask.setName(name);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noTask");
- }
- }
- else {
- throw new BaseExeption( "noParam");
- }
- }
- public static void DeleteTask(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfTask;
- String name;
- boolean flag = false;
- if (splitedArray.length >= 3) {
- indexOfTask = Integer.parseInt(splitedArray[2]);
- for (Task someTask : taskList) {
- if (someTask.getID() == indexOfTask) {
- taskList.remove(someTask);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noTask");
- }
- } else {
- throw new BaseExeption( "noParam");
- }
- }
- public static void AddTperson(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfTask;
- int indexOfPerson;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- indexOfPerson = Integer.parseInt(splitedArray[3]);
- indexOfTask = Integer.parseInt(splitedArray[2]);
- for (Person somePerson : nameList) {
- if (somePerson.getID() == indexOfPerson) {
- indexOfPerson = nameList.indexOf(somePerson);
- flag = true;
- break;
- }
- }
- if (flag) {
- flag = false;
- for (Task someTask : taskList) {
- if (someTask.getID() == indexOfTask) {
- someTask.setPerson(nameList.elementAt(indexOfPerson));
- flag = true;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noTask");
- }
- }
- else {
- throw new BaseExeption( "noMember");
- }
- }
- else {
- throw new BaseExeption( "noParam");
- }
- }
- public static void AddDeadline(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfTask;
- LocalDate date;
- if (splitedArray.length >= 6) {
- indexOfTask = Integer.parseInt(splitedArray[2]);
- date = LocalDate.of(Integer.parseInt(splitedArray[3]), Integer.parseInt(splitedArray[4]), Integer.parseInt(splitedArray[5]));
- } else {
- throw new BaseExeption( "noParam");
- }
- for (Task someTask : taskList) {
- if (someTask.getID() == indexOfTask) {
- someTask.setDeadline(date);
- break;
- }
- }
- }
- public static void Late(){
- String text;
- System.out.println("LATE TASKS\nID | Name\t|Person\t|Date");
- for (Task someTask : taskList) {
- if (LocalDate.now().isAfter(someTask.getDeadLine())) {
- text = someTask.getID() + " | " + someTask.getName() + "\t| ";
- if (someTask.getPerson() == null) {
- text = text + "No person set\t| ";
- }
- else {
- text = text + someTask.getPerson().getName() + "\t| ";
- }
- if (someTask.getDeadLine() == null) {
- text += "non";
- }
- else {
- text = text + someTask.getDeadLine();
- }
- System.out.println(text);
- }
- }
- }
- public static void ListTask(){ //Można dodać żeby więcej info wypisywało
- String text;
- System.out.println("TASKS\nID | Name\t|Person\t|Date");
- for (Task someTask : taskList) {
- text = someTask.getID() + " | " + someTask.getName() + "\t| ";
- if (someTask.getPerson() == null) {
- text = text + "No person set\t| ";
- }
- else {
- text = text + someTask.getPerson().getName() + "\t| ";
- }
- if (someTask.getDeadLine() == null) {
- text += "non";
- }
- else {
- text = text + someTask.getDeadLine();
- }
- System.out.println(text);
- }
- }
- public static void AddProject(String cmd){
- Project newProject = new Project(cmd.substring(12));
- projectList.add(newProject);
- }
- public static void ModifyProject(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfProject;
- String name;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- indexOfProject = Integer.parseInt(splitedArray[2]);
- name = splitedArray[3];
- } else {
- throw new BaseExeption( "noParam");
- }
- for (Project someProject : projectList) {
- if (someProject.getID() == indexOfProject) {
- someProject.setName(name);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noProject");
- }
- }
- public static void DeleteProject(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfProject;
- boolean flag = false;
- if (splitedArray.length >= 3) {
- indexOfProject = Integer.parseInt(splitedArray[2]);
- } else {
- throw new BaseExeption( "noParam");
- }
- for (Project someProject : projectList) {
- if (someProject.getID() == indexOfProject) {
- projectList.removeElement(someProject);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noProject");
- }
- }
- public static void AddPperson(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfProject;
- int indexOfPerson;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- indexOfProject = Integer.parseInt(splitedArray[2]);
- indexOfPerson = Integer.parseInt(splitedArray[3]);
- }
- else {
- throw new BaseExeption( "noParam");
- }
- for (Project someProject : projectList) {
- if (someProject.getID() == indexOfProject) {
- for (Person somePerson : nameList) {
- if (somePerson.getID() == indexOfPerson) {
- someProject.addPerson(somePerson);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noMember");
- }
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noProject");
- }
- }
- public static void DeletePperson(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfProject;
- int indexOfPerson;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- indexOfProject = Integer.parseInt(splitedArray[2]);
- indexOfPerson = Integer.parseInt(splitedArray[3]);
- } else {
- throw new BaseExeption( "noParam");
- }
- for (Project someProject : projectList) {
- if (someProject.getID() == indexOfProject) {
- for (Person somePerson : nameList) {
- if (somePerson.getID() == indexOfPerson) {
- someProject.deletePerson(somePerson);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noMember");
- }
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noProject");
- }
- }
- public static void AddPtask(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfProject;
- int indexOfTask;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- indexOfProject = Integer.parseInt(splitedArray[2]);
- indexOfTask = Integer.parseInt(splitedArray[3]);
- } else {
- throw new BaseExeption( "noParam");
- }
- for (Project someProject : projectList) {
- if (someProject.getID() == indexOfProject) {
- for (Task someTask : taskList) {
- if (someTask.getID() == indexOfTask) {
- someProject.addTask(someTask);
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noTask");
- }
- flag = true;
- break;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noProject");
- }
- }
- public static void DeletePtask(String cmd, String splitedArray[]) throws BaseExeption{
- splitedArray = cmd.split(" ");
- int indexOfProject;
- int indexOfTask;
- boolean flag = false;
- if (splitedArray.length >= 4) {
- indexOfProject = Integer.parseInt(splitedArray[2]);
- indexOfTask = Integer.parseInt(splitedArray[3]);
- } else {
- throw new BaseExeption( "noParam");
- }
- for (Project someProject : projectList) {
- if (someProject.getID() == indexOfProject) {
- for (Task someTask : taskList) {
- if (someTask.getID() == indexOfTask) {
- someProject.deleteTask(someTask);
- flag = true;
- }
- }
- if (!flag) {
- throw new BaseExeption( "noTask");
- }
- }
- }
- if (!flag) {
- throw new BaseExeption( "noProject");
- }
- }
- public static void ListProject(){ //Można dodać żeby więcej info wypisywało
- System.out.println("PROJECTS\nID | Name");
- for (Project someP : projectList)
- System.out.println(someP.getID() + " | " + someP.getName());
- }
- public static void ListPperson(String cmd, String splitedArray[]){ //Można dodać żeby więcej info wypisywało
- int indexOfProject;
- splitedArray = cmd.split(" ");
- indexOfProject = 0;
- if (splitedArray.length >= 3) indexOfProject = Integer.parseInt(splitedArray[2]);
- for (Project someP : projectList)
- if (someP.getID() == indexOfProject) someP.showPersons();
- }
- public static void ListPtask(String cmd, String splitedArray[]){ //Można dodać żeby więcej info wypisywało
- int indexOfProject;
- splitedArray = cmd.split(" ");
- indexOfProject = 0;
- if (splitedArray.length >= 2) indexOfProject = Integer.parseInt(splitedArray[2]);
- for (Project someP : projectList)
- if (someP.getID() == indexOfProject) someP.showTasks();
- }
- public static void Export(String fileName) throws FileNotFoundException, IOException {
- try ( FileWriter file= new FileWriter(".//" + fileName)){
- BufferedWriter buffor = new BufferedWriter(file);
- if(!nameList.isEmpty()) {
- buffor.write("PERSONS: ");
- buffor.newLine();
- for (Person somePerson : nameList) {
- buffor.write(somePerson.getID() + ":" + somePerson.getName());
- if (!somePerson.equals(nameList.lastElement())) {
- buffor.write(",");
- }
- }
- buffor.newLine();
- }
- else buffor.newLine();
- if(!taskList.isEmpty()) {
- buffor.write("TASKS: ");
- buffor.newLine();
- for (Task someTask : taskList) {
- buffor.write(someTask.getID() + ":" + someTask.getName() + ":" + someTask.getPerson().getID() + ":" + someTask.getDeadLine());
- if (!someTask.equals(taskList.lastElement())) {
- buffor.write(",");
- }
- }
- buffor.newLine();
- }
- else buffor.newLine();
- if(!projectList.isEmpty()) {
- buffor.write("PROJECTS: ");
- buffor.newLine();
- for (Project someProject : projectList) {
- buffor.write(someProject.getID() + ":" + someProject.getName() + ":");
- someProject.exportPersons(buffor);
- buffor.write(":");
- someProject.exportTasks(buffor);
- buffor.write(";");
- }
- }
- else buffor.newLine();
- buffor.close();
- System.out.println("Zapisano do pliku");
- }
- catch (FileNotFoundException a){
- System.out.println("Nie ma takiego pliku");
- }
- }
- public static void Import(String fileName) throws IOException {
- BufferedReader fileReader = null;
- String filePath = ".//" + fileName;
- String[] splitedArray1;
- String[] splitedArray2;
- try {
- fileReader = new BufferedReader(new FileReader(filePath));
- String text;
- text = fileReader.readLine();
- if(text.equals("PERSONS: ")){
- text = fileReader.readLine();
- splitedArray1 = text.split(",");
- for(int i=0; i<splitedArray1.length; i++){
- splitedArray2 = splitedArray1[i].toString().split(":");
- Person newPerson = new Person(splitedArray2[1]);
- newPerson.setID(Integer.parseInt(splitedArray2[0]));
- nameList.add(newPerson);
- }
- }
- text = fileReader.readLine();
- if(text.equals("TASKS: ")){
- text = fileReader.readLine();
- splitedArray1 = text.split(",");
- for(int i=0; i<splitedArray1.length; i++){
- splitedArray2 = splitedArray1[i].toString().split(":");
- Task newTask = new Task(splitedArray2[1]);
- newTask.setID(Integer.parseInt(splitedArray2[0]));
- String[] splitedArray3;
- splitedArray3 = splitedArray2[3].toString().split("-");
- LocalDate date = LocalDate.of(Integer.parseInt(splitedArray3[0]), Integer.parseInt(splitedArray3[1]), Integer.parseInt(splitedArray3[2]));
- newTask.setDeadline(date);
- for(Person somePerson: nameList) {
- if (somePerson.getID() == Integer.parseInt(splitedArray2[2])) {
- newTask.setPerson(somePerson);
- break;
- }
- }
- taskList.add(newTask);
- }
- }
- text = fileReader.readLine();
- if(text.equals("PROJECTS: ")){
- text = fileReader.readLine();
- splitedArray1 = text.split(";");
- for(int i=0; i<splitedArray1.length; i++){
- splitedArray2 = splitedArray1[i].split(":");
- Project newProject = new Project(splitedArray2[1]);
- newProject.setID(Integer.parseInt(splitedArray2[0]));
- String[] splitedArray3;
- if(!splitedArray2[2].equals(" ")) {
- splitedArray3 = splitedArray2[2].split(",");
- for (int j = 0; j < splitedArray3.length; j++) {
- for (Person somePerson : nameList) {
- if (somePerson.getID() == Integer.parseInt(splitedArray3[j])) {
- newProject.addPerson(somePerson);
- break;
- }
- }
- }
- }
- if(!splitedArray2[3].equals(" ")) {
- splitedArray3 = splitedArray2[3].split(",");
- for (int j = 0; j < splitedArray3.length; j++) {
- for (Task someTask : taskList) {
- if (someTask.getID() == Integer.parseInt(splitedArray3[j])) {
- newProject.addTask(someTask);
- break;
- }
- }
- }
- }
- projectList.add(newProject);
- }
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- if (fileReader != null) {
- fileReader.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement