public class CustomerActivity extends Activity implements ICustomerView, OnClickListener {
private EditText mFirstNameEditText, mLastNameEditText, mIdEditText;
private Button mSaveButton, mLoadButton;
private CustomerPresenter mCustomerPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
mFirstNameEditText = (EditText) findViewById(R.id.firstNameEditText);
mLastNameEditText = (EditText) findViewById(R.id.lastNameEditText);
mIdEditText = (EditText) findViewById(R.id.et_id);
mSaveButton = (Button) findViewById(R.id.saveButton);
mLoadButton = (Button) findViewById(R.id.loadButton);
mCustomerPresenter = new CustomerPresenter(this);
mSaveButton.setOnClickListener(this);
mLoadButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.saveButton:
mCustomerPresenter.saveCustomer(mFirstNameEditText.getText().toString(), mLastNameEditText.getText().toString());
break;
case R.id.loadButton:
mCustomerPresenter.loadCustomer(Integer.parseInt(mIdEditText.getText().toString()));
break;
}
}
@Override
public void setLastName(String lastName) {
mLastNameEditText.setText(lastName);
}
@Override
public void setFirstName(String firstName) {
mFirstNameEditText.setText(firstName);
}
@Override
public void setId(int id) {
mIdEditText.setText(String.valueOf(id));
}
}
public class CustomerPresenter {
private ICustomerView mCustomerView;
private ICustomerModel mCustomerModel;
public CustomerPresenter(ICustomerView view) {
mCustomerView = view;
mCustomerModel = new CustomerModel();
}
public void saveCustomer(String firstName, String lastName) {
mCustomerModel.setFirstName(firstName);
mCustomerModel.setLastName(lastName);
}
public void loadCustomer(int id) {
mCustomerView.setId(mCustomerModel.getId());
mCustomerView.setFirstName(mCustomerModel.getFirstName());
mCustomerView.setLastName(mCustomerModel.getLastName());
}
}
public interface ICustomerView {
void setLastName (String lastName);
void setFirstName (String firstName);
void setId(int id);
}
public interface ICustomerModel {
void setFirstName(String firstName);
void setLastName(String lastName);
void setId(int id);
int getId();
String getFirstName();
String getLastName();
}
public class CustomerModel implements ICustomerModel {
int id;
String firstName;
String lastName;
@Override
public int getId() {
return id;
}
@Override
public void setId(int id) {
this.id = id;
}
@Override
public String getFirstName() {
return firstName;
}
@Override
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Override
public String getLastName() {
return lastName;
}
@Override
public void setLastName(String lastName) {
this.lastName = lastName;
}
}