Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- public class Main {
- public static void main(String[] args) {
- System.out.println("1. Add a new task");
- System.out.println("2. See actual tasks");
- System.out.println("3. Delete a task");
- System.out.print("Your choice: ");
- Scanner input = new Scanner(System.in);
- int choice = input.nextInt();
- if(choice==1)
- {
- try {
- File file = new File("tasks.txt");
- if(!file.exists()) {
- file.createNewFile();
- }
- System.out.print("Name of the task: ");
- Scanner nameInput = new Scanner(System.in);
- String name = nameInput.nextLine();
- System.out.print("Type of the task: ");
- Scanner typeInput = new Scanner(System.in);
- String type = typeInput.nextLine();
- System.out.print("Importance of the task(1-10): ");
- Scanner importanceInput = new Scanner(System.in);
- int importance = importanceInput.nextInt();
- System.out.print("Beggining of the task(dd-mm-yyyy): ");
- Scanner begginingInput = new Scanner(System.in);
- String beggining = begginingInput.nextLine();
- System.out.print("End of the task(dd-mm-yyyy): ");
- Scanner endInput = new Scanner(System.in);
- String end = endInput.nextLine();
- FileWriter fw = new FileWriter(file,true);
- fw.write("Name of the task: "+name+", Type of the task: "+type+", Importance of the task (1-10): "+importance+", Beggining of the task: "+beggining+", End of the task: "+end+",\n");
- fw.close();
- nameInput.close();
- input.close();
- typeInput.close();
- importanceInput.close();
- begginingInput.close();
- endInput.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- } else if(choice==2){
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader("tasks.txt"));
- String line;
- while((line=br.readLine()) != null)
- {
- System.out.println(line);
- }
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- else if(choice==3) {
- String filepath = "tasks.txt";
- System.out.print("Name of the task you want to delete: ");
- Scanner deleteInput = new Scanner (System.in);
- String delete = deleteInput.nextLine();
- String removeTerm = delete;
- removeRecord(filepath,removeTerm);
- }
- }
- public static void removeRecord(String filepath,String removeTerm){
- String tempFile = "temp.txt";
- File oldFile = new File(filepath);
- File newFile = new File(tempFile);
- String name=""; String type=""; String importance=""; String beggining=""; String end="";
- try {
- FileWriter fw = new FileWriter(tempFile,true);
- BufferedWriter bw = new BufferedWriter(fw);
- PrintWriter pw = new PrintWriter(bw);
- Scanner x;
- x = new Scanner(new File(filepath));
- x.useDelimiter(",/n");
- while(x.hasNext()) {
- name = x.next();
- type = x.next();
- importance = x.next();
- beggining = x.next();
- end = x.next();
- if(!name.equals(removeTerm)){
- pw.println("Name of the task: "+name+", Type of the task: "+type+", Importance of the task (1-10): "+importance+", Beggining of the task: "+beggining+", End of the task: "+end+",\n");
- }
- }
- x.close();
- pw.flush();
- pw.close();
- oldFile.delete();
- File dump = new File(filepath);
- newFile.renameTo(dump);
- }
- catch (Exception e) {
- JOptionPane.showMessageDialog(null,"error");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement