diff --git a/src/Name/Name.class b/src/Name/Name.class index 9589939..799518e 100644 Binary files a/src/Name/Name.class and b/src/Name/Name.class differ diff --git a/src/Name/Name.java b/src/Name/Name.java index 66723aa..2b5b412 100644 --- a/src/Name/Name.java +++ b/src/Name/Name.java @@ -1,19 +1,10 @@ -import java.io.PrintStream; +import java.io.PrintStream; +import java.util.Scanner; /** This class manages objects characterised by a name, a surname and a title. */ public class Name{ - /** - This constructor uses a default title, name and surname. - */ - public Name(){ - name=""; - sname=""; - title=""; - } - - /** This constructor sets the values of name and surname, and uses a default title. @param name The value of the name. @@ -28,11 +19,11 @@ public class Name{ /** This constructor sets the values of name, surname and title. + @param title The value of the title @param name The value of the name. @param sname The value of the surname. - @param title The value of the title */ - public Name(String name, String sname, String title){ + public Name(String title, String name, String sname){ this.name=name; this.sname=sname; this.title=title; @@ -75,12 +66,45 @@ public class Name{ } + /** + 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); + } + + /** This method prints the value of the istance variables. @param ps The object which must print the values. **/ public void print(PrintStream ps){ - ps.println(getTitleNameSurname()); + 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; } diff --git a/src/Name/NameTester.class b/src/Name/NameTester.class index 49f28b5..a889bab 100644 Binary files a/src/Name/NameTester.class and b/src/Name/NameTester.class differ diff --git a/src/Name/NameTester.java b/src/Name/NameTester.java index 888f979..50ca72f 100644 --- a/src/Name/NameTester.java +++ b/src/Name/NameTester.java @@ -1,18 +1,30 @@ import java.io.*; +import java.util.Scanner; public class NameTester{ - public static void main(String [] args){ - - Name persona = new Name(); - persona.setTitle("Sig."); - persona.print(System.out); //We expect Sig. + public static void main(String [] args) throws Exception{ - persona = new Name("Mario", "Rossi"); - System.out.println(persona.getInitials()); //We expect M. R. + Scanner sc = new Scanner(new File("src")); + Name persona = Name.read(sc); + Name persona1; - persona = new Name("Jon", "Snow", "King in the North"); - System.out.println(persona.getNameSurname()); //We expect Jon Snow - System.out.println(persona.getTitleNameSurname()); //We expect a very big spoiler + if(persona!=null) + persona.print(System.out); + else + System.out.println("ERROR"); + + persona.setTitle("Hand of the Queen"); + persona.print(System.out); + System.out.println(persona.getInitials()); //We expect T. L. + + persona1 = new Name("King in the North", "Jon", "Snow"); + System.out.println(persona1.getNameSurname()); //We expect Jon Snow + System.out.println(persona1.getTitleNameSurname()); //We expect a spoiler + + if(persona.equals(persona1)) //We expect Different + System.out.println("Equal"); + else + System.out.println("Different"); } } diff --git a/src/Name/src b/src/Name/src new file mode 100644 index 0000000..d502ddb --- /dev/null +++ b/src/Name/src @@ -0,0 +1,3 @@ +Ser +Tyrion +Lannister