class Person { private String lastName; private String firstName; private int cardNumber; code example
Example: class Person { private String lastName; private String firstName; private int cardNumber;
import java.io.*;
/**
* Class Person
* This class holds a person's information, such as first name, last name and birthdate,
* And methods to set and get a person's information.
* @author Li Jin
* @version 1.0
* This is a sample for CISC370. Any suggestions are welcome!
*/
public class Person{
private String firstName;
private String lastName;
private MyDate birthDate;
public Person(){
firstName=null;
lastName=null;
birthDate=new MyDate();
}
public Person(String lasName, String firstName){
this.firstName=firstName;
this.lastName=lastName;
birthDate=new MyDate();
}
public Person(String lastName, String firstName, MyDate birthDate){
this.firstName=firstName;
this.lastName=lastName;
this.birthDate=birthDate;
}
/**
* getFirstName returns the first name of the person.
*/
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public MyDate getBirthDate(){
return birthDate;
}
/**
* getName returns the full name of the person, last name first,
* with a comma between the last and first name.
*/
public String getName(){
return lastName+","+firstName;
}
public void setFirstName(String firstName){
this.firstName=firstName;
}
public void setLastName(String lastName){
this.lastName=lastName;
}
public void setBirthDate(MyDate date){
birthDate=date;
}
public void setBirthDate(int month, int day, int year){
this.birthDate.setYear(year);
this.birthDate.setMonth(month);
this.birthDate.setDay(day);
}
public boolean equalBirthDate(Person person){
if(this.birthDate.equals(person.getBirthDate()))
return true;
else return false;
}
public String toString(){
return lastName+","+firstName+" "+birthDate.toString();
}
/**
* input a person's name from screen.
* I just put all methods related to person in this class. But it is not necessary. You can put it in Menu class or ...
* There are many diffirent ways to manage classes and methods.
*/
public void setNameFromScreen(BufferedReader reader){
String[] name;
try{
System.out.println("Plase input name (Last, First):");
name=reader.readLine().split(",");
this.setLastName(name[0]);
this.setFirstName(name[1]);
}
catch (Exception e){
e.printStackTrace();
}
return;
}
/**
*check if this person's name appears in a file
*/
public boolean existInFile(String fileName){
BufferedReader reader;
String line;
int index=-1;
String[] inputDate;
try{
reader=new BufferedReader(new FileReader(fileName));
while((line=reader.readLine())!=null){
index=line.indexOf(" ");
if(index!=-1){
if(line.substring(0,index).equalsIgnoreCase(this.getLastName()+","+this.getFirstName())){
inputDate=line.substring(index+1).split("/");
this.setBirthDate(Integer.parseInt(inputDate[0]), Integer.parseInt(inputDate[1]), Integer.parseInt(inputDate[2]));
return true;
}
}
}
reader.close();
}
catch(Exception e){
e.printStackTrace();
}
return false;
}
}