i am not an expert in java, but i need to solve this problem/activity for my course subject, that's why i really need your help guys. I have a programming problem. thing is i can't figure out what method or java codes should i use for this problem:
Create a class address book that can contain 100 entries of Name, Address, contact number and email address. You should provide the following methods for the address book:
Add entry, Delete entry, View all entries and Update an entry
UPDATE: this is the codes I got so far
I am thinking i could use 2d array for this but, as soon as i start coding i can't really continue further, I don't know if its possible to use array or not in this kind of activity. I tried searching for other java codes but the more I learned new techniques or codes that might be possible, the more i got confused on WHAT codes must I use!
if anyone can help me build the coding for this activity I would really apprecaite and will surely study how the hell will/can YOU do it! because im really interested in learning Java, I just need some help to realize how should i DO this. thanks in advance!
THIS ARE THE CODES I'VE GOT SO FAR: the capability of these program is only for adding editing viewing and deleting NAMES, iam still figuring out how to add dimensions to my array or should I add? or if not array? HoW? how am I supposed to answer this activity prior tto its REQUIREMENTS :(
package javaactivities;
import java.util.*;
import java.util.Scanner;
public class AddressBook {
static List<String> l=new ArrayList<String>();
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
boolean y=true;
do{
System.out.println("Enter \n1 to add an entry\n2 to edit entry");
System.out.println("3 to delete an entry\n4 to view entries\n5 to exit");
System.out.print("enter your choice: ");
int choice=in.nextInt();
switch(choice)
{
case 1:
insert();
break;
case 2:
edit();
break;
case 3:
delete();
break;
case 4:
print();
break;
case 5:
toexit();
break;
default:
System.out.println("bad input");
break;
}
System.out.println("want to process more? y/n");
String x=in.next();
char ch=x.charAt(0);
if( ch=='n')
y=false;
}
while(y!=false);
}
static public void insert(){
Scanner in=new Scanner(System.in);
boolean y=true;
do{
System.out.println("enter name to add in list");
String entry=in.next();
l.add(entry);
System.out.println("want to insert more?y/n");
String x=in.next();
char ch=x.charAt(0);
if( ch=='n')
y=false;
}
while(y!=false);
}
static public void print(){
if(l.isEmpty())
System.out.println("list is empty ");
else
System.out.println("members of lists are:");
for(int i=0 ; i<l.size();i++)
System.out.println("Entry "+i+" : "+ l.get(i)+" ");
}
static public void edit(){
Scanner in=new Scanner(System.in);
String num2;
System.out.println("enter name you want to add");
num2=in.next();
try{
System.out.println("enter entry # of the name you want to edit");
int num1=in.nextInt();
l.set(num1, num2);
}catch(IndexOutOfBoundsException e){
System.err.println("caught IndexOutOfBoundsException: specified position is empty "+e.getMessage());
}
}
static public void delete(){
Scanner in=new Scanner(System.in);
System.out.println("enter entry # you want to delete");
int num=in.nextInt();
l.remove(num);
}
static public void toexit(){
System.exit(0);
}
}
First, implement all the required classes, two in your case:
The skeleton could be something like that
public final class AddressBook {
public static final class Entry {
private String name;
private String address;
private String contactNumber;
private String email;
private Entry(String name, String address, String contactNumber, String email) {
this.name = name;
this.address = address;
this.contactNumber = contactNumber;
this.email = email;
}
public String getName() {return name;}
public String getAddress() {return address;}
public String getContactNumer() {return contactNumber;}
public String getEmail() {return email;}
}
private ArrayList<Entry> entries = new ArrayList<Entry>();
public AddressBook() {;}
public int size() {return entries.size();}
public int get(int index) {return entries.get(index);}
...
public Entry add(String name, String address, String contactNumber, String email) {
Entry entry = new Entry(name, address, contactNumber, email);
entries.add(entry);
return entry;
}
...
}
In order to implement viewAll()
you could choose to override toString()
methods in both classes, to delete()
it seemes that you have to implement find()
as well etc. Then just use these classes
public final class Main {
private static AddressBook book = new AddressBook();
public static void main(String[] args) {
...
switch(choice) {
case 1:
book.add(...);
break;
case 2:
book.delete(...);
break;
...
}
System.out.println(book.toString());
...
}
}