A proper answer could take one or more complete books, so this is just a snippet:
in a C style and used classes to make a new data type
In java you use classes to provide services - data is usually hidden inside the class, and the public interface consists of methods you can call to get things done.
When you're building the class you concentrate on how you will get those things done. You then test it by hard-coding some calls to the public methods.
Once it's working you can just include that class in any application that needs those services, and you don't worry about how they are provided, you just use the public interface.
Eg (semi-pseudo code)
You have some user info and it needs to be validated.
public class UserInfo {
public UserInfo(...) { set up a new user object}
public boolean isValid(some data) {
if (somedata .... logical tests) return true; // valid
else return false;
}
}
then in the gui:
UserInfo info = new UserInfo(...);
String input = someUIcomponent.getText();
if (info.isValid(input) ...
JamesCherrill
... trying to help
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30
I have one method that accepts user input and I need the method that checks it to know the string that the user entered, the validation string, JtextField to display the cleaned up version of the entry (toUpper and such), and a specific error message that says why a string was rejected.
Should I be passing all of this information to another method? Should I do all of this in the original method? Should I make an array in the method to store the possible error messages and validation strings?
Try to separate the things that are intrinsic to the application from those that happen to belong to a particular UI. It's always a good test to consider what you could re-use and how if you had to produce a version of the same app where the UI was done via web pages.
So,validation take social security number as an eg. The valid formats for a SSN are independent of how the user happened to enter them. JTextField is definitely NOT relevant. Error messages are controversial, real apps often have to work in >1 local language. I vere towards returning an error number, then providing a lookup of texts which can be localised. It looks like:
// DataModel...
public int validateSSN(String ssn) {
// returns 0 if valid, error number if not
...
}
// UI...
String s = myJLabel.getText();
int errorCode = myDataModel.validateSSN(s);
if (errorCode != 0) {
// display error message using String DataModel.getErrorText(errorCode);
Check out MVC (model - view - controller) on the web.
JamesCherrill
... trying to help
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30