You have to make a new menu which show first menu like that 1.Enter 1 for using Doubly linkedList 2.Enter 2 for using arrayList
Course:Data Structure and
I am providing the two menu base programs in java for address book by using doubly LinkedList and arrayList separate codes i am proving in question:
You have to make a new menu which show first menu like that
1.Enter 1 for using Doubly linkedList
2.Enter 2 for using arrayList
AddressBook by using doubly LinkedList Code Are:
public class Person
{
private String name;
private String address;
private long phoneNumber;
public Person(String name, String address, long phoneNumber){
this.name=name;
this.address=address;
this.phoneNumber=phoneNumber;
}
public String getName(){
return this.name;
}
public String getAddress(){
return this.address;
}
public long getPhoneNumber()
{
return this.phoneNumber;
}
public String toString(){
return "Name: "+this.name+" Address: "+this.address+" Phone Number: "+this.phoneNumber;
}
}
import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
List<Person> AddressBook=new LinkedList<Person>();
Scanner sc=new Scanner(System.in);
System.out.println("1.Add");
System.out.println("2.Delete");
System.out.println("3.Search");
System.out.println("4.Exit");
while(true)
{
System.out.print("Enter the choice: ");
int choice=Integer.parseInt(sc.nextLine());
if(choice==1)
{
System.out.print("Enter Name: ");
String name=sc.nextLine();
System.out.print("Enter Address: ");
String address=sc.nextLine();
System.out.print("Enter Phone Number: ");
long phoneNumber=Long.parseLong(sc.nextLine());
Person p=new Person(name,address,phoneNumber);
AddressBook.add(p);
System.out.println("Person added succesfully");
}
else if(choice==2)
{
System.out.print("Enter Name: ");
String inputName=sc.nextLine();
for(Person p:AddressBook)
{
if(p.getName().equals(inputName))
{
AddressBook.remove(p);
System.out.println("Person deleted succesfully");
}
}
}
else if(choice==3)
{
System.out.print("Enter Name: ");
String inputName=sc.nextLine();
for(Person p:AddressBook)
{
if(p.getName().equals(inputName))
{
System.out.println(p);
}
}
}
else if(choice==4)
{
System.exit(0);
}
else
{
System.out.println("Invalid Choice!Try again");
}
}
}
}
Address book by using arrayList Code Are:
public class Person
{
public String name;
public String address;
public long phoneNumber;
public Person(String name, String address, long phoneNumber)
{
this.name=name;
this.address=address;
this.phoneNumber=phoneNumber;
}
public String toString()
{
return "Name: "+this.name+" Address: "+this.address+" Phone Number: "+this.phoneNumber;
}
}
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
List<Person> AddressBook=new ArrayList<Person>();
Scanner sc=new Scanner(System.in);
System.out.println("1.Add");
System.out.println("2.Delete");
System.out.println("3.Search");
System.out.println("4.Exit");
while(true)
{
System.out.print("Enter the choice: ");
int choice=Integer.parseInt(sc.nextLine());
if(choice==1)
{
System.out.print("Enter Name: ");
String name=sc.nextLine();
System.out.print("Enter Address: ");
String address=sc.nextLine();
System.out.print("Enter Phone Number: ");
long phoneNumber=Long.parseLong(sc.nextLine());
Person p=new Person(name,address,phoneNumber);
AddressBook.add(p);
System.out.println("Person added succesfully");
}
else if(choice==2)
{
System.out.print("Enter Name: ");
String inputName=sc.nextLine();
for(Person p:AddressBook)
{
if(p.name.equals(inputName))
{
AddressBook.remove(p);
System.out.println("Person deleted succesfully");
}
}
}
else if(choice==3)
{
System.out.print("Enter Name: ");
String inputName=sc.nextLine();
for(Person p:AddressBook)
{
if(p.name.equals(inputName))
{
System.out.println(p);
}
}
}
else if(choice==4)
{
System.exit(0);
}
else
{
System.out.println("Invalid Choice!Try again");
}
}
}
}
Step by step
Solved in 3 steps with 1 images