Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. import javax.swing.JOptionPane;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         System.out.println("1. Add a new task");
  9.         System.out.println("2. See actual tasks");
  10.         System.out.println("3. Delete a task");
  11.         System.out.print("Your choice: ");
  12.        
  13.         Scanner input = new Scanner(System.in);
  14.         int choice = input.nextInt();
  15.        
  16.         if(choice==1)
  17.         {
  18.         try {          
  19.             File file = new File("tasks.txt");
  20.            
  21.             if(!file.exists()) {
  22.                 file.createNewFile();
  23.             }
  24.            
  25.             System.out.print("Name of the task: ");
  26.             Scanner nameInput = new Scanner(System.in);
  27.             String name = nameInput.nextLine();
  28.            
  29.             System.out.print("Type of the task: ");
  30.             Scanner typeInput = new Scanner(System.in);
  31.             String type = typeInput.nextLine();
  32.            
  33.             System.out.print("Importance of the task(1-10): ");
  34.             Scanner importanceInput = new Scanner(System.in);
  35.             int importance = importanceInput.nextInt();
  36.            
  37.             System.out.print("Beggining of the task(dd-mm-yyyy): ");
  38.             Scanner begginingInput = new Scanner(System.in);
  39.             String beggining = begginingInput.nextLine();
  40.            
  41.             System.out.print("End of the task(dd-mm-yyyy): ");
  42.             Scanner endInput = new Scanner(System.in);
  43.             String end = endInput.nextLine();
  44.            
  45.             FileWriter fw = new FileWriter(file,true);
  46.            
  47.             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");
  48.            
  49.             fw.close();
  50.             nameInput.close();
  51.             input.close();
  52.             typeInput.close();
  53.             importanceInput.close();
  54.             begginingInput.close();
  55.             endInput.close();
  56.            
  57.  
  58.         } catch(IOException e) {
  59.             e.printStackTrace();
  60.         }
  61.            
  62.            
  63.            
  64.         } else if(choice==2){
  65.             BufferedReader br = null;
  66.             try {
  67.                 br = new BufferedReader(new FileReader("tasks.txt"));
  68.                 String line;
  69.                
  70.                 while((line=br.readLine()) != null)
  71.                 {
  72.                     System.out.println(line);
  73.                 }
  74.             } catch(IOException e) {
  75.                 e.printStackTrace();
  76.             }
  77.         }
  78.        
  79.         else if(choice==3) {
  80.            
  81.             String filepath = "tasks.txt";
  82.             System.out.print("Name of the task you want to delete: ");
  83.             Scanner deleteInput = new Scanner (System.in);
  84.             String delete = deleteInput.nextLine();
  85.             String removeTerm = delete;
  86.            
  87.             removeRecord(filepath,removeTerm);
  88.            
  89.         }
  90.     }
  91.     public static void removeRecord(String filepath,String removeTerm){
  92.         String tempFile = "temp.txt";
  93.         File oldFile = new File(filepath);
  94.         File newFile = new File(tempFile);
  95.         String name=""; String type=""; String importance=""; String beggining=""; String end="";
  96.        
  97.         try {
  98.             FileWriter fw = new FileWriter(tempFile,true);
  99.             BufferedWriter bw = new BufferedWriter(fw);
  100.             PrintWriter pw = new PrintWriter(bw);
  101.            
  102.             Scanner x;
  103.             x = new Scanner(new File(filepath));
  104.             x.useDelimiter(",/n");
  105.            
  106.             while(x.hasNext()) {
  107.                 name = x.next();
  108.                 type = x.next();
  109.                 importance = x.next();
  110.                 beggining = x.next();
  111.                 end = x.next();
  112.                 if(!name.equals(removeTerm)){
  113.                     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");
  114.                 }
  115.             }
  116.             x.close();
  117.             pw.flush();
  118.             pw.close();
  119.             oldFile.delete();
  120.             File dump = new File(filepath);
  121.             newFile.renameTo(dump);
  122.         }
  123.         catch (Exception e) {
  124.             JOptionPane.showMessageDialog(null,"error");
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement