Easy Steps:
1. On
your textpad, edit Main.java, with the following code:
class Main{
public static void main(String[] args){
ContactsModel
model = new ContactsModel();
ContactsView
view = new ContactsView(model);
ContactsController controller = new
ContactsController(model, view);
}
}
2. Edit
ContactsModel.java with the following code:
class ContactsModel
{
private int id;
private String name;
private String address;
private String contactNo;
ContactsModel[] contacts;
int ndx;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void
setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void
setAddress(String address)
{
this.address = address;
}
public String getContactNo()
{
return contactNo;
}
public void
setContactNo(String contactNo)
{
this.contactNo = contactNo;
}
public ContactsModel()
{
contacts = new ContactsModel[50];
ndx = 0;
}
public boolean insert(ContactsModel
contact)
{
boolean
inserted = false;
if (contacts[ndx] == null)
{
contacts[ndx] = contact;
ndx++;
inserted = true;
}
return inserted;
}
public ContactsModel
getContact(int ndx)
{
return contacts[ndx];
}
public int
countContacts()
{
return ndx;
}
public boolean isFound(int
id)
{
boolean found =
false;
for (int i = 0; i
< countContacts(); i++)
{
if (contacts[i].getId() == id)
{
found =
true;
break;
}
}
return found;
}
public void edit(ContactsModel contact)
{
int i = getIndex(contact.getId());
contacts[i]=contact;
}
private int getIndex(int id)
{
int i;
for (i = 0; i < countContacts(); i++)
{
if (contacts[i].getId() == id)
{
break;
}
}
return i;
}
}
3. Edit
ContactsView.java with the following code:
class ContactsView
{
ContactsModel
model;
public ContactsView(ContactsModel model)
{
this.model = model;
}
public void id()
{
print("ID:");
}
public void name()
{
print("Name:");
}
public void address()
{
print("Address:");
}
public void
contactNo()
{
print("ContactNo:");
}
private void
print(String str){
System.out.print(str);
}
private void
println(String str){
System.out.println(str);
}
public void menu()
{
println("---Menu---");
println("1. Add Employee");
println("2. Edit Employee");
println("0. Quit");
print("Select Menu[0-3]: ");
}
public void
insertedMsg()
{
println(" Inserted Successfully...");
}
public void
editedMsg()
{
println(" Edited Successfully...");
}
public void
errorMsg()
{
println("Error");
}
public void
notFoundMsg()
{
println("Contact Not found.");
}
public void
displayContacts()
{
println("----------------------------------------");
println("ID\tName\tAddress\tContactNo");
for (int i = 0; i
< model.countContacts(); i++)
{
ContactsModel contact = model.getContact(i);
println(contact.getId() + "\t"
+ contact.getName() + "\t" +
contact.getAddress() + "\t" +
contact.getContactNo());
}
println("----------------------------------------");
}
}
4. Edit
ContactsController.java with the following code:
import java.lang.*;
import java.io.*;
class ContactsController
{
ContactsModel
model;
ContactsView
view;
public ContactsController(ContactsModel model,
ContactsView view)
{
this.model=model;
this.view=view;
viewUI();
}
private void viewUI()
{
int option;
do{
ContactsModel
contact = new ContactsModel();
view.menu();
option=readIntegerInput();
switch(option){
case 1:
view.id();
contact.setId(readIntegerInput());
view.name();
contact.setName(readStringInput());
view.address();
contact.setAddress(readStringInput());
view.contactNo();
contact.setContactNo(readStringInput());
if (model.insert(contact))
{
view.insertedMsg();
view.displayContacts();
}
break;
case 2:
view.id();
contact.setId(readIntegerInput());
if(model.isFound(contact.getId())){
view.name();
contact.setName(readStringInput());
view.address();
contact.setAddress(readStringInput());
view.contactNo();
contact.setContactNo(readStringInput());
model.edit(contact);
view.editedMsg();
view.displayContacts();
}
else{
view.notFoundMsg();
}
break;
}
}while(option!=0);
}
private String readStringInput(){
BufferedReader
reader;
reader=new BufferedReader(new
InputStreamReader(System.in));
String
input="";
try{
input=reader.readLine();
}catch(IOException ioe){
view.errorMsg();
}
return input;
}
private int
readIntegerInput(){
BufferedReader
reader;
reader=new BufferedReader(new
InputStreamReader(System.in));
int input=0;
try{
input=Integer.parseInt(reader.readLine());
}catch(IOException ioe){
view.errorMsg();;
}
return input;
}
}
5. Put
your code to the test, perfect!, nice jobJ

No comments:
Post a Comment