(Define a Person class, generate a target, the request has name, age, and achievement attributes, and later keep this target in the file, you can display the information of the person on the screen, and you can repair the information, but the request has a complete menu presentation .
How many classes are required to terminate this program?
1. Person class
2. File operation class
3. Type of input data
4. Detailed operation of the Person class
Develop with Eclipse
First define the following classes:
Person class
redking.blog.51cto/attachment/200903/3/27212_1236090918Qbgr.png
redking.blog.51cto/attachment/200903/3/27212_1236090923alT4.png
Define Person class properties
redking.blog.51cto/attachment/200903/3/27212_12360909272E5e.png
PersonOperate class
redking.blog.51cto/attachment/200903/3/27212_123609093157j8.png
redking.blog.51cto/attachment/200903/3/27212_1236090936J1FD.png
InputData input data class
redking.blog.51cto/attachment/200903/3/27212_12360909401hog.png
Define three methods:
redking.blog.51cto/attachment/200903/3/27212_1236090945OKx1.png
Menu menu display class:
redking.blog.51cto/attachment/200903/3/27212_1236090950A7RJ.png
redking.blog.51cto/attachment/200903/3/27212_1236090954v0sf.png
FileOperate file operation class
redking.blog.51cto/attachment/200903/3/27212_1236090959KBvW.png
redking.blog.51cto/attachment/200903/3/27212_12360909637WGZ.png
Main main class test client:
redking.blog.51cto/attachment/200903/3/27212_12360909685PHV.png
redking.blog.51cto/attachment/200903/3/27212_1236090973PaVU.png
After defining these five classes, we have to write the code separately~
First is the Person class
We're going to write Setter and Getter methods
redking.blog.51cto/attachment/200903/3/27212_12360909802KLB.png
redking.blog.51cto/attachment/200903/3/27212_12360909878eYR.png
redking.blog.51cto/attachment/200903/3/27212_1236090993K3CS.png
Then write two structure methods
redking.blog.51cto/attachment/200903/3/27212_1236091001FJR5.png
redking.blog.51cto/attachment/200903/3/27212_1236091005ePJP.png
redking.blog.51cto/attachment/200903/3/27212_1236091012aP76.png
Remove yellow warning CTRL 1
redking.blog.51cto/attachment/200903/3/27212_12360910185k8q.png
redking.blog.51cto/attachment/200903/3/27212_1236091026JU7y.png
In this way, the Person class is finished.~~~
InputData class to write code
The code of all input data is actually very similar, so design an InputData class here, which is specially used to obtain various data types of input.
redking.blog.51cto/attachment/200903/3/27212_1236091033HtRK.png
This ends the function of entering numbers, and we verify it in Test
redking.blog.51cto/attachment/200903/3/27212_1236091035ulC2.png
redking.blog.51cto/attachment/200903/3/27212_1236091038nQSY.png
The complete code of the Person class:
packageorg.redking.demo.vo;
importjava.io.Serializable;
@SuppressWarnings(serial)
publicclassPersonimplementsSerializable{
privateStringname;
privateintage;
privatefloatscore;
publicPerson(){}
publicPerson(Stringname,intage,floatscore){
super();
this.name=name;
this.age=age;
this.score=score;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicfloatgetScore(){
returnscore;
}
publicvoidsetScore(floatscore){
this.score=score;
}
publicStringtoString(){
return name: this.name , age: this.age , achievement: this.score;
}
}
The complete code of the PersonOperate class:
packageorg.redking.demo.op;
importorg.redking.demo.util.FileOperate;
importorg.redking.demo.util.InputData;
importorg.redking.demo.vo.Person;
publicclassPersonOperate{
privateInputDatainput=null;
publicPersonOperate(){
this.input=newInputData();
}
//End the detailed Person target operation
publicvoidadd(){
//class to use input data
Stringname=null;
intage=0;
floatscore=0.0f;
System.out.print(Enter name:);
name=this.input.getString();
System.out.print(Enter age:);
age=this.input.getInt();
System.out.print(input result:);
score=this.input.getFloat();
//Generate the Person target and keep the target in the file
Personp=newPerson(name,age,score);
try{
newFileOperate().save(p);
System.out.println(Data retention successful!);
}catch(Exceptione){
System.out.println(Data retention failed!);
}
}
publicvoidshow(){
// read the content from the file
Personp=null;
try{
p=(Person)newFileOperate().read();
}catch(Exceptione){
System.out.println(content display failed, please determine whether the data exists!);
}
if(p!=null){
System.out.println(p);
}
}
publicvoidupdate(){
abbr_266ac41fdcf35af9f41c6793b6acffb8.doc)