Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class TestPerson{
  5.     public static void main(String[] args)throws IOException{
  6.         //check if file exists
  7.         File myFile = new File(args[0]);
  8.         try{
  9.             if(!myFile.exists()){
  10.                 throw new FileNotFoundException();
  11.             }
  12.         }
  13.         catch(FileNotFoundException fnfe){
  14.             System.out.println("\nSorry, the file ["+args[0]+"] cannot be found.");
  15.             System.exit(0);
  16.         }
  17.  
  18.         //csv file reader
  19.         BufferedReader in = new BufferedReader(new FileReader(myFile));
  20.        
  21.         //create person list
  22.         List<Person> personList = new ArrayList<Person>();
  23.        
  24.         //add csv data to person list
  25.         String str;
  26.         while ((str = in.readLine()) != null){
  27.             String[] data = str.split(",");
  28.             personList.add(new Person(data[0],Integer.parseInt(data[1])));
  29.         }
  30.        
  31.         printList(personList);
  32.        
  33.         //ask user what to edit
  34.         Scanner scan = new Scanner(System.in);
  35.         boolean invalidChoice;
  36.         do{
  37.             invalidChoice = false;
  38.             try{
  39.                 System.out.print("\nWhich person would you like to edit? (Enter [-1] to exit): ");
  40.                 int choice = scan.nextInt();
  41.                
  42.                 //exit if choice is -1
  43.                 if(choice == -1){
  44.                     System.exit(0);
  45.                 }
  46.                 editPerson(personList.get(choice));
  47.             }
  48.             catch(IndexOutOfBoundsException exc){
  49.                 System.out.println("\nRecord does not exist, please try again.");
  50.                 invalidChoice = true;
  51.             }
  52.         } while(invalidChoice);
  53.        
  54.         in.close();
  55.         scan.close();
  56.        
  57.         //overwrite csv file
  58.         PrintWriter personWriter = new PrintWriter(new FileWriter(args[0], false));
  59.        
  60.         for(Person p : personList){
  61.             personWriter.println(p.getName()+","+p.getAge());
  62.         }
  63.        
  64.         personWriter.close();
  65.        
  66.         printList(personList);
  67.     }
  68.    
  69.    
  70.     //method to edit
  71.     public static Object editPerson(Person p){
  72.         Scanner scan = new Scanner(System.in);
  73.         System.out.print("Enter New Name for "+p.getName()+": ");
  74.         String newName = scan.next();
  75.         p.setName(newName);
  76.         System.out.print("Enter New Age for "+p.getName()+": ");
  77.         int newAge = scan.nextInt();
  78.         p.setAge(newAge);
  79.         return p;
  80.     }
  81.    
  82.    
  83.     //method to print
  84.     public static void printList(List personList){
  85.         ListIterator it = personList.listIterator();
  86.         while(it.hasNext()){
  87.             System.out.println("\nPerson " + it.nextIndex()+":");
  88.             System.out.println(it.next());
  89.         }
  90.     }
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement