Create a program that keeps track of the following informati
Create a program that keeps track of the following information input by the user:
First Name, Last Name, Phone Number, Age
Now - let\'s store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns.
You should be able to add, display and remove contacts in the array. Look down here.
******************************************************************************************************************************************************************************************************************
this code comes close to what I am looking for except that - it stores more than 10 contacts. you can input 100 contacts if you like. I NEED IT TO STORE <= 10 CONTACTS
import java.util.*;
public class contact {
private String fname;// define fname
private String lname; // define lname
private String phone; // define phone
private int age; // define age
public contact()
{
}
public static void main(String args[])
{
ArrayList<contact> list1 = new ArrayList<contact>(); // Create a array of list
contact c1=new contact(); // create new contact
int choice=0;
do
{
Scanner sc1=new Scanner(System.in); // create scanner object for console input
System.out.println(\"Contact Information Database:\");
System.out.println(\"-----------------Menu--------------:\");
System.out.println(\"1. Add a New Contact\");
System.out.println(\"2. Display Contacts\");
System.out.println(\"3. Remove a Contact\");
System.out.println(\"4. Exit\");
choice=sc1.nextInt();
if(choice==1)
{
contact c=new contact();
Scanner sc=new Scanner(System.in);
System.out.println(\"Enter First Name\");
c.fname=sc.nextLine(); // First name input
System.out.println(\"Enter Last Name\");
c.lname=sc.nextLine(); // Last Name
System.out.println(\"Enter Phone No\");
c.phone=sc.nextLine(); // Phone number
System.out.println(\"Enter Age\");
c.age=sc.nextInt(); // Enter Age
list1.add(c);
}
if(choice==2)
{
System.out.println(\"------------------------------List of Contact------------------------------\");
for(int i=0;i<list1.size();i++)
{
System.out.println(\"\"+(i+1)+\"\\t\\t\"+((contact)list1.get(i)).lname+\"\\t\\t\\t\"+((contact)list1.get(i)).fname+\"\\t\\t\\t\"+((contact)list1.get(i)).phone+\"\\t\\t\\t\"+((contact)list1.get(i)).age); // Printed as SNO, LAST NAME , FIRST NAME, PHONE, AGE
}
System.out.println(\"---------------------------------------------------------------------------\");
}
if(choice==3)
{
String dlt_name;
int dlt_sn=-1;
Scanner sc2=new Scanner(System.in);
System.out.println(\"Enter First Name of the person that you want to remove\");
dlt_name=sc2.nextLine(); // Name to delete
int flag=0;
for(int i=0;i<list1.size();i++)
{
contact c2=new contact();
c2=(contact)list1.get(i); // get all the component of list
if(c2.fname.equals(dlt_name)) // check whether the name is exists in list or not
{
System.out.println(\"\"+(i+1)+\"\\t\\t\"+c2.lname+\"\\t\\t\\t\"+c2.fname+\"\\t\\t\\t\"+c2.phone+\"\\t\\t\\t\"+c2.age);
flag=1;
}
}
if(flag==1)
{
System.out.println(\"Type Serial No of The Record To Delete\");
dlt_sn=sc2.nextInt(); // get input to delete the record
if(dlt_sn>=1 && dlt_sn<=list1.size())
{
list1.remove(dlt_sn-1); // remove record from a particulat location
}
else
{
System.out.println(\"Wrong Index\");
}
}
else
{
System.out.println(\"No Record Found\");
}
}
if(choice==4)
{
break;
}
}while(true);
}
}
Solution
See,I had modified the add case. I hopes it will work for 10 contacts.The modified statements are written in bold format
import java.util.*;
public class contact {
private String fname;// define fname
private String lname; // define lname
private String phone; // define phone
private int age; // define age
public contact()
{
}
public static void main(String args[])
{
ArrayList<contact> list1 = new ArrayList<contact>(); // Create a array of list
contact c1=new contact(); // create new contact
int choice=0;
do
{
Scanner sc1=new Scanner(System.in); // create scanner object for console input
System.out.println(\"Contact Information Database:\");
System.out.println(\"-----------------Menu--------------:\");
System.out.println(\"1. Add a New Contact\");
System.out.println(\"2. Display Contacts\");
System.out.println(\"3. Remove a Contact\");
System.out.println(\"4. Exit\");
choice=sc1.nextInt();
if(choice==1)
{
if(list1.size() < 10){ // only adds when the less than 10 contacts are there.Otherwise,it won\'t add and will show a message
contact c=new contact();
Scanner sc=new Scanner(System.in);
System.out.println(\"Enter First Name\");
c.fname=sc.nextLine(); // First name input
System.out.println(\"Enter Last Name\");
c.lname=sc.nextLine(); // Last Name
System.out.println(\"Enter Phone No\");
c.phone=sc.nextLine(); // Phone number
System.out.println(\"Enter Age\");
c.age=sc.nextInt(); // Enter Age
list1.add(c);
}
else{
System.out.println(\"The list is full . Max = 10 contacts \");
}
}
if(choice==2)
{
System.out.println(\"------------------------------List of Contact------------------------------\");
for(int i=0;i<list1.size();i++)
{
System.out.println(\"\"+(i+1)+\"\\t\\t\"+((contact)list1.get(i)).lname+\"\\t\\t\\t\"+((contact)list1.get(i)).fname+\"\\t\\t\\t\"+((contact)list1.get(i)).phone+\"\\t\\t\\t\"+((contact)list1.get(i)).age); // Printed as SNO, LAST NAME , FIRST NAME, PHONE, AGE
}
System.out.println(\"---------------------------------------------------------------------------\");
}
if(choice==3)
{
String dlt_name;
int dlt_sn=-1;
Scanner sc2=new Scanner(System.in);
System.out.println(\"Enter First Name of the person that you want to remove\");
dlt_name=sc2.nextLine(); // Name to delete
int flag=0;
for(int i=0;i<list1.size();i++)
{
contact c2=new contact();
c2=(contact)list1.get(i); // get all the component of list
if(c2.fname.equals(dlt_name)) // check whether the name is exists in list or not
{
System.out.println(\"\"+(i+1)+\"\\t\\t\"+c2.lname+\"\\t\\t\\t\"+c2.fname+\"\\t\\t\\t\"+c2.phone+\"\\t\\t\\t\"+c2.age);
flag=1;
}
}
if(flag==1)
{
System.out.println(\"Type Serial No of The Record To Delete\");
dlt_sn=sc2.nextInt(); // get input to delete the record
if(dlt_sn>=1 && dlt_sn<=list1.size())
{
list1.remove(dlt_sn-1); // remove record from a particulat location
}
else
{
System.out.println(\"Wrong Index\");
}
}
else
{
System.out.println(\"No Record Found\");
}
}
if(choice==4)
{
break;
}
}while(true);
}
}



