Easy Steps:
1. Create
a file Main.java and type the following code:
class Main
{
public static void main(String[] args)
{
ContactsModel
model = new ContactsModel();
ContactsView
view = new ContactsView(model);
ContactsController controller = new
ContactsController(view, model);
}
}
2. Create
a file ContactsModel.java and type the following code:
public class
ContactsModel{
private String id;
private String name;
private String address;
private String contactNo;
ContactsModel contacts[];
private int ndx;
private int ctr;
public ContactsModel(){
contacts=new ContactsModel[10];
ndx=0;
ctr=0;
}
public void
incrementNdx(){
ndx++;
}
public void
decrementNdx(){
ndx--;
}
public int getNdx(){
return ndx;
}
public void
setId(String id){
this.id=id;
}
public
String getId(){
return this.id;
}
public
void setName(String name){
this.name=name;
}
public
String getName(){
return this.name;
}
public
void setAddress(String address){
this.address=address;
}
public
String getAddress(){
return this.address;
}
public
void setContactNo(String contactNo){
this.contactNo=contactNo;
}
public String getContactNo(){
return
this.contactNo;
}
public void save(ContactsModel contact){
if(contacts[ctr]==null)
contacts[ctr]=contact;
ctr++;
}
public ContactsModel
getContact(int ndx){
return contacts[ndx];
}
public int
countContacts(){
return ctr;
}
}
3. Create
a file ContactsView.java and type the following code:
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
class ContactsView
extends Frame{
ContactsModel
model;
public ContactsView(ContactsModel model){
super("ContactUI");
this.model=model;
}
public ContactsView(){
}
TextField
txtId=new TextField(30);
TextField
txtName=new TextField(30);
TextField
txtAddress=new TextField(30);
TextField
txtContactNo=new TextField(30);
Button
btnSave=new Button("Save");
Button
btnReset=new Button("Reset");
Button
btnExit=new Button("Exit");
Button
btnPrevious=new Button("Previous");
Button
btnNext=new Button("Next");
public void UI(){
Panel
bodyPanel=new Panel();
bodyPanel.setLayout(new GridLayout(5,1));
Panel
idPanel=new Panel();
idPanel.setLayout(new FlowLayout());
idPanel.add(new Label("ID:"));
idPanel.add(txtId);
Panel
namePanel=new Panel();
namePanel.setLayout(new FlowLayout());
namePanel.add(new Label("Name:"));
namePanel.add(txtName);
Panel
addressPanel=new Panel();
addressPanel.setLayout(new FlowLayout());
addressPanel.add(new Label("Address:"));
addressPanel.add(txtAddress);
Panel
salaryPanel=new Panel();
salaryPanel.setLayout(new FlowLayout());
salaryPanel.add(new Label("ContactNo:"));
salaryPanel.add(txtContactNo);
Panel
btnPanel=new Panel();
btnPanel.setLayout(new FlowLayout());
btnPanel.add(btnSave);
btnPanel.add(btnReset);
btnPanel.add(btnExit);
btnPanel.add(btnPrevious);
btnPanel.add(btnNext);
bodyPanel.add(idPanel);
bodyPanel.add(namePanel);
bodyPanel.add(addressPanel);
bodyPanel.add(salaryPanel);
bodyPanel.add(btnPanel);
add(bodyPanel);
pack();
setSize(500,200);
setVisible(true);
}
public String getIdUserInput(){
return txtId.getText();
}
public String getNameUserInput(){
return txtName.getText();
}
public String getAddressUserInput(){
return txtAddress.getText();
}
public String getContactNoUserInput(){
return txtContactNo.getText();
}
public void
addBtnSaveActionListener(ActionListener save){
btnSave.addActionListener(save);
}
public void
addBtnResetActionListener(ActionListener reset){
btnReset.addActionListener(reset);
}
public void
addBtnExitActionListener(ActionListener exit){
btnExit.addActionListener(exit);
}
public void
addBtnPreviousActionListener(ActionListener previous){
btnPrevious.addActionListener(previous);
}
public void
addBtnNextActionListener(ActionListener next){
btnNext.addActionListener(next);
}
public void
getRecord(int ndx){
ContactsModel
contact=new ContactsModel();
contact=model.getContact(ndx);
txtId.setText(contact.getId());
txtName.setText(contact.getName());
txtAddress.setText(contact.getAddress());
txtContactNo.setText(contact.getContactNo());
}
public void reset(){
txtId.setText("");
txtName.setText("");
txtAddress.setText("");
txtContactNo.setText("");
}
public void
closeUI(){
System.exit(0);
}
}
4. Create
a file ContactsController.java and type the following code:
import java.awt.event.*;
class ContactsController{
ContactsModel
model;
ContactsView view;
public ContactsController(){
}
public ContactsController(ContactsView
view,ContactsModel model){
this.model=model;
this.view=view;
view.UI();
view.addBtnSaveActionListener(new saveActionListener());
view.addBtnResetActionListener(new resetActionListener());
view.addBtnExitActionListener(new exitActionListener());
view.addBtnPreviousActionListener(new previousActionListener());
view.addBtnNextActionListener(new nextActionListener());
}
class saveActionListener
implements ActionListener{
public void
actionPerformed(ActionEvent e){
ContactsModel
contact=new ContactsModel();
contact.setId(view.getIdUserInput());
contact.setName(view.getNameUserInput());
contact.setAddress(view.getAddressUserInput());
contact.setContactNo(view.getContactNoUserInput());
model.save(contact);
view.reset();
}
}
class resetActionListener
implements ActionListener{
public void
actionPerformed(ActionEvent e){
view.reset();
}
}
class exitActionListener
implements ActionListener{
public void
actionPerformed(ActionEvent e){
view.closeUI();
}
}
class previousActionListener
implements ActionListener{
public void
actionPerformed(ActionEvent e){
int ndx=model.getNdx();
if(ndx>0)
model.decrementNdx();
view.getRecord(ndx);
}
}
class nextActionListener
implements ActionListener{
public void
actionPerformed(ActionEvent e){
int ndx=model.getNdx();
view.getRecord(ndx);
if(ndx<model.countContacts()-1)
model.incrementNdx();
}
}
private ContactsModel getContact(){
ContactsModel
contact=new ContactsModel();
return contact;
}
}
5. Put
your code to the test, perfect!, nice jobJ

No comments:
Post a Comment