Java (programming language): Difference between revisions

Content deleted Content added
Line 538:
[[bat-smg:Java]]
[[zh:Java]]
<!--
package com.vinod;
 
import java.util.Scanner;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
 
public class Solution04{
 
static int menu(){
while(true){
System.out.println("\n\nContact Management\n");
System.out.print("1. New contact"+
"\n2. Display all"+
"\n3. Search by name"+
"\n4. Search by email"+
"\n5. Exit"+
"\n\nEnter your choice >> ");
Scanner sc=new Scanner(System.in);
try{
int choice=sc.nextInt();
if(choice>=1 && choice<=5) return choice;
 
System.out.println("Invalid choice!!!\nTry again.\n\n");
}
catch(Exception ex){
}
}
}
static void newContact(){
Contact c=new Contact();
c.accpetFromUser();
String str=c.getStringForFile();
 
try{
FileWriter out=new FileWriter("contacts.txt", true);
out.write(str);
out.write("\n");
out.close();
}
catch(Exception ex){
System.out.println("There was an error: ");
System.out.println("--------------------------------");
ex.printStackTrace();
System.out.println("--------------------------------");
}
}
 
static void displayAll(){
try{
FileReader file=new FileReader("contacts.txt");
BufferedReader in=new BufferedReader(file);
 
String str;
while((str=in.readLine())!=null){
Contact c=new Contact();
c.setFromString(str);
c.print();
}
in.close();
file.close();
}
catch(Exception ex){
System.out.println("There was an error: ");
System.out.println("--------------------------------");
ex.printStackTrace();
System.out.println("--------------------------------");
}
}
 
static void searchByName(){
try{
Scanner sc=new Scanner(System.in);
 
System.out.print("Enter the first few letters of the name:");
String name=sc.nextLine();
System.out.println();
 
FileReader file=new FileReader("contacts.txt");
BufferedReader in=new BufferedReader(file);
 
String str;
boolean found=false;
while((str=in.readLine())!=null){
Contact c=new Contact();
c.setFromString(str);
 
if(c.getName().toUpperCase().startsWith(name.toUpperCase())){
found=true;
c.print();
}
}
in.close();
file.close();
if(!found){
System.out.println("No matching data found!");
}
 
}
catch(Exception ex){
System.out.println("There was an error: ");
System.out.println("--------------------------------");
ex.printStackTrace();
System.out.println("--------------------------------");
}
}
 
static void searchByEmail(){
try{
Scanner sc=new Scanner(System.in);
 
System.out.print("Enter the email:");
String email=sc.nextLine();
System.out.println();
 
FileReader file=new FileReader("contacts.txt");
BufferedReader in=new BufferedReader(file);
 
String str;
boolean found=false;
while((str=in.readLine())!=null){
Contact c=new Contact();
c.setFromString(str);
 
if(c.getEmail().toUpperCase().equals(email.toUpperCase())){
c.print();
found=true;
break;
}
}
in.close();
file.close();
 
if(!found){
System.out.println("No matching data found!");
}
}
catch(Exception ex){
System.out.println("There was an error: ");
System.out.println("--------------------------------");
ex.printStackTrace();
System.out.println("--------------------------------");
}
}
 
public static void main(String args[]){
int choice;
 
while((choice=menu())!=5){
switch(choice){
case 1:
newContact();
break;
case 2:
displayAll();
break;
case 3:
searchByName();
break;
case 4:
searchByEmail();
break;
}
}
 
System.out.println("\nThank you for using Content Management!\nHave a nice day!");
}
}
 
 
-->