package uttara.projects.contactapp;  import java.io.*; import java.util.*;  /**  * @author Soumya  *  */ public class MyContactsBook  { 	public static void main(String[] args) 	{ 	    String newContact1=null; 	    String name=null;	 	    String phno1=null; 	    String email1=null; 	    String personDescription=null; 	    String address=null; 	    String newContact =null; 	    String newContact2=null; 	  		Scanner sc=new Scanner(System.in); 		List<String> contactBook = new ArrayList<String>(); 		int ch1=0, count=0; 	    BufferedReader br= null; 	    BufferedWriter bw=null; 		System.out.println("=======Loading the Contact Book.....================="); 		System.out.println(); 		System.out.println("===================================================="); 		while(ch1!=5) 		{ 			System.out.println("Enter 1 for FRIENDS Contact Book"); 			System.out.println("Enter 2 for FAMILY Contact Book"); 			System.out.println("Enter 3 for OFFICIAL Contact Book"); 			System.out.println("Enter 4 for OTHER Contacts"); 			System.out.println("Enter 5 to exit"); 			System.out.println(); 			System.out.println("Enter your choice: "); 			 			ch1=sc.nextInt(); 			System.out.println("================================================="); 			System.out.println(); 			switch(ch1) 			{ 			     case 1: System.out.println("WELCOME TO FRIENDS CONTACT BOOK"); 			             SwitchCase("C:/MyContacts/friends.txt"); 			             break; 			     case 2: System.out.println("WELCOME TO FAMILY CONTACT BOOK"); 			             SwitchCase("C:/MyContacts/family.txt"); 				         break; 			     case 3: System.out.println("WELCOME TO OFFICIAL CONTACT BOOK"); 			             SwitchCase("C:/MyContacts/official.txt"); 			             break; 			     case 4: System.out.println("WELCOME TO MISC CONTACT BOOK"); 			             SwitchCase("C:/MyContacts/misc.txt"); 			             break; 			     case 5: System.out.println(" THANK YOU DEAR...." + "VISIT AGAIN..... BYE BYE"); 				         break; 			     default: System.out.println(" OOPs !!!!! Enter again "); 				         break; 			} 			 		} 				 		  	}  	public static void SwitchCase(String path) 	{ 		String newContact1=null; 	    String name=null;	 	    String phno1=null; 	    String email1=null; 	    String personDescription=null; 	    String address=null; 	    String newContact =null; 	    String newContact2=null; 	    int index=0; 		Scanner sc=new Scanner(System.in); 		List<String> contactBook = new ArrayList<String>(); 		int ch1=0, count=0; 	    BufferedReader br= null; 	    BufferedWriter bw=null; 		try         {        	 File fp = new File(path);        	 System.out.println("does "+fp.getName()+" exist ? "+fp.exists());             if(fp.exists())             {            	 br=new BufferedReader(new FileReader(path));            	 String input;            	 contactBook.clear();            	 while((input=br.readLine())!=null)            	 {            	                 	//	 String completeContact = null;            		String[] sa = input.split(":");            		System.out.println(""+sa.length);            			 name=sa[0];            		    address=sa[1];            			phno1=sa[2];            			email1=sa[3];            			personDescription=sa[4];            			newContact = name+":"+address+":"+phno1+":"+email1+":"+personDescription;            			System.out.println("== "+newContact);            		    contactBook.add(newContact);            		            	}            	 //System.out.println(contactBook);        	  }             else             {            	 fp.createNewFile();             }         }         catch(IOException e)         {        	e.printStackTrace();          }         finally         {        	 try        	 {        		 if(br!=null)        			 br.close();        		            			         	 }        	 catch(IOException e)        	   {        		   e.printStackTrace();        	   }          }         int ch2=0;         while(ch2!=8)         {        	 System.out.println("===========================================");        	 System.out.println("Sub menu entered");        	 System.out.println();        	 System.out.println("Enter 1 to add new Contact");        	 System.out.println("Enter 2 to edit existing Contact");        	 System.out.println("Enter 3 to remove Contact");        	 System.out.println("Enter 4 to search for a Contact");        	 System.out.println("Enter 5 to list available Contacts");        	 System.out.println("Enter 6 to import Contact");        	 System.out.println("Enter 7 to export Contact");        	 System.out.println("Enter 8 to go back to the last menu");        	 System.out.println(); 			 System.out.println("Enter your choice: "); 			  		   //  while(!sc1.hasNextInt()) 		   //	 { 		   //		System.out.println("Enter only Integer numbers between 1-5"); 		  //	 } 			 ch2=sc.nextInt(); 			 switch(ch2) 			 { 			 case 1:System.out.println("===========================================");  				    System.out.println("ADD CONTACTS"); 				    newContact1 = addContact(); 				    contactBook.add(newContact1); 				    try 				    { 				    	 bw = new BufferedWriter(new FileWriter(path,true)); 				    	 bw.write(newContact1); 				    	 bw.newLine(); 				    } 				    catch(IOException e) 				    { 				    	e.printStackTrace(); 				    } 				    finally 				    { 				    	try 				    	{    		                if(bw!=null)    		                	bw.close(); 				    	} 				    	catch(IOException e) 				    	{ 				    		e.printStackTrace(); 				    	} 				    } 				    System.out.println("==========================================="); 				    break; 			 case 2: System.out.println("Edit the contact"); 			         System.out.println("Enter the contact name to edit"); 			         String name1= sc.next(); 			         			                	  			         for(String n : contactBook) 			         { 			        	if(n.contains(name1)) 			        	 { 			        		System.out.println(n); 			        		String[] sa1= n.split(":"); 			        		name=sa1[0]; 	            		    address=sa1[1]; 	            			phno1=sa1[2]; 	            			email1=sa1[3]; 	            			personDescription=sa1[4];//  count++; 			        	 } 			         } 			         int ch3=0; 			         while(ch3!=6) 			         { 			        	 System.out.println("press 1 to edit name");    			         System.out.println("press 2 to edit address");    			         System.out.println("press 3 to edit phno");    			         System.out.println("press 4 to edit email");    			         System.out.println("press 5 to edit person tag");    			         System.out.println("press 6 to exit");    			         System.out.println("Enter the choice: ");    			         while(!sc.hasNextInt())    			         {    			        	 System.out.println("Enter only numbers [1-6]");    			        	ch3= sc.nextInt();    			         }    			         ch3=sc.nextInt();    			         //String name9="";    			         switch(ch3)    			         {    			         case 1: System.out.println("Edit name");    			                 System.out 										.println("original name: "+name);    			                 	                 	                          String ssse=""; 	                 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 	                          for(String snn : contactBook) 			                  { 	                              String[]  sl= snn.split(":"); 	                	          if(sl[0].equals(name)) 	                	          { 	                		          ssse=snn; 	                		      } 	                	      } 	                 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    			                 index=contactBook.indexOf(ssse);    			                 System.out.println("Enter new name:");    			                 name=sc.next();    			                    			                 newContact2 = name+":"+address+":"+phno1+":"+email1+":"+personDescription;    			              			                 contactBook.set(index,newContact2);    			                 contactBook.remove(ssse);    			                 System.out.println(newContact2);    			                 break;    			         case 2: System.out.println("Edit address");    			                  System.out.println("original address: "+address); 	                              System.out.println("enter new address: "); 	                              address=sc.next(); 	                              	  newContact2 = name+":"+address+":"+phno1+":"+email1+":"+personDescription; 	                              	contactBook.set(index,newContact2); 	                              	                               	                              System.out.println(newContact2); 		                          break;    			         case 3: System.out.println("Edit phno");    			                 System.out.println("original phno: "+phno1);                                 System.out.println("enter new phno: ");                                 phno1=sc.next();                                 newContact2 = name+":"+address+":"+phno1+":"+email1+":"+personDescription;                        	        contactBook.add(newContact2);                                 System.out.println(newContact2);                                 break;    			         case 4: System.out.println("Edit email");    			                 System.out.println("original email: "+email1);                                 System.out.println("enter new address: ");                                 email1=sc.next();                                 newContact2 = name+":"+address+":"+phno1+":"+email1+":"+personDescription;                        	          contactBook.add(newContact2);                                 System.out.println(newContact2);                                 break; 		                 case 5: System.out.println("Edit tag"); 		                         System.out.println("original person description: "+personDescription);                                 System.out.println("enter new person description: ");                                 personDescription=sc.next();                                 newContact2 = name+":"+address+":"+phno1+":"+email1+":"+personDescription;                        	        contactBook.add(newContact2);                                 System.out.println(newContact2);                                 break;    			         case 6: System.out.println("go back");    			         try 	    				    { 	    				    	 bw = new BufferedWriter(new FileWriter(path)); 	    				    	 	    				    	 for(String contact: contactBook) 	    				    	 { 	    				    		 bw.write(contact); 		    				    	 bw.newLine(); 	    				    	 } 	    				    	 	    				    } 	    				    catch(IOException e) 	    				    { 	    				    	e.printStackTrace(); 	    				    } 	    				    finally 	    				    { 	    				    	try 	    				    	{ 		    		                if(bw!=null) 		    		                	bw.close(); 	    				    	} 	    				    	catch(IOException e) 	    				    	{ 	    				    		e.printStackTrace(); 	    				    	} 	    				    } 		                        break;    			         default: System.out.println("Edit name"); 		                         break;    			         } 			         } 			         break; 				     			 case 3: System.out.println("==========================================="); 				     System.out.println("Remove the desired contact"); 				     System.out.println("Enter Name Of Contact"); 	                 String name2=sc.next(); 	                 String sss=""; 	                 for(String sn : contactBook) 			         { 	                	 	                	 String[]  sl= sn.split(":"); 	                	 if(sl[0].equals(name2)) 	                	 { 	                		 sss=sn; 	                		  	                	 } 	                	  	                		  	                	   			         } 	              contactBook.remove(sss); 	                 try    				    {    				    	 bw = new BufferedWriter(new FileWriter(path));    				    	 for(String contact: contactBook)    				    	 {    				    		 bw.write(contact); 	    				    	 bw.newLine();    				    	 }    				    	    				    }    				    catch(IOException e)    				    {    				    	e.printStackTrace();    				    }    				    finally    				    {    				    	try    				    	{ 	    		                if(bw!=null) 	    		                	bw.close();    				    	}    				    	catch(IOException e)    				    	{    				    		e.printStackTrace();    				    	}    				    } 	                  	           //      System.out.println("Is "+name+" Removed From List ? "+contactBook.remove(name)); 	                 break; 			 case 4: System.out.println("Search"); 			         System.out.println("Enter the name to be searched"); 			         String ss2=sc.next(); 			         boolean flag=true; 			         String sss1="";            	          for(String n : contactBook) 		              { 		            	  String[]  sl= n.split(":"); 		                	 if(sl[0].equals(ss2)) 		                	 { 		                		 sss1=n; 		                		 flag=true; 		              	     } 		                	 flag=false; 		               }            	          if(flag=true)            	          {            	        	 System.out.println("The contents of the desired contact are");            	        	 System.out.println();            	        	 System.out.println(sss1);            	          }            	          else            	        	  System.out.println("Desired Contact not found"); 		              				    break; 			 case 5: System.out.println("List"); 			         int ch4=0;  			         while(ch4!=3) 			         { 			        	 System.out.println("press 1 to list based on creation"); 			        	 System.out.println("press 2 to list based on alphabetic order"); 			        	 System.out.println("Press 3 to go back to last menu"); 			        	 while(!sc.hasNextInt())    			         {    			        	 System.out.println("Enter only numbers [1-6]");    			        	ch4= sc.nextInt();    			         }    			         ch4=sc.nextInt();    			         switch(ch4)    			         {    			         case 1: System.out.println("Listing based on creation");    			                 try    			                 {    			                	 br=new BufferedReader(new FileReader(path));    			                	 String input2;    			                	 contactBook.clear();    			                	 while((input2=br.readLine())!=null)    			                	 {    			                	     System.out.println(input2);    			                	     System.out.println();    			                /*	//	 String completeContact = null;    			                		String[] sa = input2.split(":");    			                		System.out.println(""+sa.length);    			                			name=sa[0];    			                		    address=sa[1];    			                			phno1=sa[2];    			                			email1=sa[3];    			                			personDescription=sa[4];    			                			newContact = name+":"+address+":"+phno1+":"+email1+":"+personDescription;    			                			System.out.println("== "+newContact);    			                		    contactBook.add(newContact);   */ 			                	 }    			                }    			                catch(IOException e)    			                {    			                	e.printStackTrace();    			                }    			                 finally    			                 {    			                	try    							    {    			   		                if(br!=null)    			   		                	br.close();    							    }    							    catch(IOException e)    							    {    							    	e.printStackTrace();    							    }    							        			                 }    			                 System.out.println();    			                     			                 break;    			         case 2: System.out.println("Listing according to alphabetics");    			                 try 	                         { 	                	        br=new BufferedReader(new FileReader(path)); 	                	        String input3; 	                	        contactBook.clear(); 	                	        while((input3=br.readLine())!=null) 	                	        { 	                	            String completeContact = null; 	                		        String[] sa5 = input3.split(":"); 	                		        name=sa5[0]; 	                		        address=sa5[1]; 	                			    phno1=sa5[2]; 	                			    email1=sa5[3]; 	                			    personDescription=sa5[4]; 	                			    newContact = name+":"+address+":"+phno1+":"+email1+":"+personDescription; 	                			     	                		        contactBook.add(newContact); 	                		                       		          			                	 } 	                          } 	                          catch(IOException e) 	                          { 	                	           e.printStackTrace(); 	                          } 	                          finally 	                          { 	                	          try 					              { 	   		                          if(br!=null) 	   		                	      br.close(); 					              } 					              catch(IOException e) 					              { 					    	          e.printStackTrace(); 					              } 					     	                          } 	                          System.out.println();    			                 Collections.sort(contactBook);    			                 System.out.println(contactBook);    			                 break;    			         case 3: System.out.println("going to previous menu");    			                 break;    			         default: System.out.println("Enter only between 1-3");    			                 break;    			         } 			        	 			         } 			          				    break; 			 case 6: System.out.println("Import "); 				    break; 			 case 7: System.out.println("Export"); 				    break; 			 case 8: System.out.println("go back to main menu"); 				    break; 			 default: System.out.println("Enter nos between 1-8"); 				    break; 			 } 			          } 	} 	public static String addContact() 	{ 		BufferedWriter bw=null; 		BufferedReader br=null; 		List<String> l=new ArrayList<String>(); 		String c="y"; 		String ph = null; 		String phno1=null; 		String email =null; 		String email1=null; 		String input=null; 		System.out.println("in addContact()"); 		Scanner sc = new Scanner(System.in); 		Scanner sc1=new Scanner(System.in); 		/////////////////////////////////////////////////////////////////////////////////////////// 		System.out.println("Enter the name of the contact person: "); 		String name=sc.next(); 		try 		{ 			bw= new BufferedWriter(new FileWriter("C:/MyContacts/names.txt")); 			br= new BufferedReader(new FileReader("C:/MyContacts/names.txt")); 			while((input=br.readLine())!=null) 			{ 				l.add(input); 			} 			if(l.contains(name)) 			{ 				System.out.println("The name already exists Enter new name"); 				name=sc.next(); 				bw.write(name); 				bw.newLine(); 			} 			l.add(name); 			 		} 		catch(IOException e) 		{ 			e.printStackTrace(); 		} 		finally 		{ 			try 			{ 				if(bw!=null) 					bw.close(); 				if(br!=null) 					br.close(); 			} 			catch(IOException e) 			{ 				e.printStackTrace(); 			} 			 		} 		/////////////////////////////////////////////////////////////////////////////////////////// 		System.out.println("Enter the address: "); 		String address=sc1.nextLine(); 		System.out.println("Enter the phno number(s)"); 			phno1=sc.next(); 			System.out.println("if u want to add another no press y"); 			String c1=sc.next(); 			while(c.equals(c1)) 			{ 				System.out.println("next no: "); 				ph=sc.next(); 				phno1=phno1+","+ph; 				 				System.out.println("if u want to add another no please enter y"); 			    c1=sc.next(); 			} 			 		System.out.println("Enter the email id (s): "); 		email1=sc.next(); 		System.out.println("if u want to add another no press y"); 		c1=sc.next(); 		while(c.equals(c1)) 		{ 			System.out.println("next id: "); 			email=sc.next(); 			email1=email1+","+email; 			 			System.out.println("if u want to add another email please enter y"); 		    c1=sc.next(); 		} 		System.out.println("Give Person Description in few words: "); 		String personDescription = sc1.nextLine(); 				return name+":"+address+":"+phno1+":"+email1+":"+personDescription; 			}  }