2016-10-19 13:41:21 +00:00
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.util.Scanner;
|
2016-10-13 14:13:36 +00:00
|
|
|
/**
|
2016-10-18 10:04:36 +00:00
|
|
|
This class manages objects characterised by a name, a surname and a title.
|
2016-10-13 14:13:36 +00:00
|
|
|
*/
|
|
|
|
public class Name{
|
|
|
|
|
|
|
|
/**
|
2016-10-18 10:04:36 +00:00
|
|
|
This constructor sets the values of name and surname, and uses a default title.
|
|
|
|
@param name The value of the name.
|
|
|
|
@param sname The value of the surname.
|
2016-10-13 14:13:36 +00:00
|
|
|
*/
|
2016-10-18 10:04:36 +00:00
|
|
|
public Name(String name, String sname){
|
|
|
|
this.name=name;
|
|
|
|
this.sname=sname;
|
|
|
|
title="";
|
2016-10-13 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:04:36 +00:00
|
|
|
|
2016-10-13 14:13:36 +00:00
|
|
|
/**
|
2016-10-18 10:04:36 +00:00
|
|
|
This constructor sets the values of name, surname and title.
|
2016-10-19 13:41:21 +00:00
|
|
|
@param title The value of the title
|
2016-10-18 10:04:36 +00:00
|
|
|
@param name The value of the name.
|
|
|
|
@param sname The value of the surname.
|
2016-10-13 14:13:36 +00:00
|
|
|
*/
|
2016-10-19 13:41:21 +00:00
|
|
|
public Name(String title, String name, String sname){
|
2016-10-18 10:04:36 +00:00
|
|
|
this.name=name;
|
|
|
|
this.sname=sname;
|
|
|
|
this.title=title;
|
2016-10-13 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:04:36 +00:00
|
|
|
|
2016-10-13 14:13:36 +00:00
|
|
|
/**
|
2016-10-18 10:04:36 +00:00
|
|
|
This method returns the initials as a String object.
|
|
|
|
@return the string of the initials.
|
2016-10-13 14:13:36 +00:00
|
|
|
*/
|
2016-10-18 10:04:36 +00:00
|
|
|
public String getInitials(){
|
|
|
|
return name.substring(0, 1).concat(". ").concat(sname.substring(0,1)).concat(".");
|
2016-10-13 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:04:36 +00:00
|
|
|
|
2016-10-13 14:13:36 +00:00
|
|
|
/**
|
2016-10-18 10:04:36 +00:00
|
|
|
This method returns a String object that contains both name and surname, strictly in this order.
|
|
|
|
@return A string object made up by name and surname.
|
2016-10-13 14:13:36 +00:00
|
|
|
*/
|
2016-10-18 10:04:36 +00:00
|
|
|
public String getNameSurname(){
|
|
|
|
return name.concat(" ").concat(sname);
|
2016-10-13 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:04:36 +00:00
|
|
|
|
2016-10-13 14:13:36 +00:00
|
|
|
/**
|
2016-10-18 10:04:36 +00:00
|
|
|
This method returns a String object that contains title, name and surname, strictly in this order.
|
|
|
|
@return A string object made up by title,surname and name.
|
2016-10-13 14:13:36 +00:00
|
|
|
*/
|
2016-10-18 10:04:36 +00:00
|
|
|
public String getTitleNameSurname(){
|
|
|
|
return title.concat(" ").concat(name).concat(" ").concat(sname);
|
2016-10-13 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:04:36 +00:00
|
|
|
|
2016-10-13 14:13:36 +00:00
|
|
|
/**
|
2016-10-18 10:04:36 +00:00
|
|
|
This method set a new value for the title of an object.
|
|
|
|
@param title A new value for the title of the object.
|
|
|
|
**/
|
|
|
|
public void setTitle(String title){
|
|
|
|
this.title=title;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-19 13:41:21 +00:00
|
|
|
/**
|
|
|
|
Indicates whether some other object is "equal to" this one.
|
|
|
|
@param n the reference object with which to compare.
|
|
|
|
@return a boolean.
|
|
|
|
**/
|
|
|
|
public boolean equals(Name n){
|
|
|
|
return this.name.equals(n.name) && this.sname.equals(n.sname) && this.title.equals(n.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-18 10:04:36 +00:00
|
|
|
/**
|
|
|
|
This method prints the value of the istance variables.
|
|
|
|
@param ps The object which must print the values.
|
|
|
|
**/
|
|
|
|
public void print(PrintStream ps){
|
2016-10-19 13:41:21 +00:00
|
|
|
ps.println(title);
|
|
|
|
ps.println(name);
|
|
|
|
ps.println(sname);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
This method reads the value of the istance variables.
|
|
|
|
@param sc The Scanner.
|
|
|
|
@return The new Name
|
|
|
|
**/
|
|
|
|
public static Name read(Scanner sc) throws Exception{
|
|
|
|
String title, name, sname;
|
|
|
|
if(sc.hasNext()){
|
|
|
|
title=sc.nextLine();
|
|
|
|
if(sc.hasNext()){
|
|
|
|
name = sc.nextLine();
|
|
|
|
if(sc.hasNext()){
|
|
|
|
sname=sc.nextLine();
|
|
|
|
return new Name(title, name, sname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2016-10-13 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 10:04:36 +00:00
|
|
|
|
2016-10-13 14:13:36 +00:00
|
|
|
private String name;
|
2016-10-18 10:04:36 +00:00
|
|
|
private String sname;
|
|
|
|
private String title;
|
2016-10-13 14:13:36 +00:00
|
|
|
}
|