如何比较类中的两个对象



我开始编写自己的类,到目前为止,我有一个作者类,它有两个字符串(名字和姓氏)和两个整数(出生和死亡年份)。现在我写了另一个类,书籍,它有一个名为bookAuthor的作者对象。所以,这是我的代码,你能帮助修复我的空指针异常。

public class Author{
      String authorFirstName;
      String authorLastName;
      int authorBirth;
      int authorDeath;
      //creates a new author object with nothing set
      public Author(){
      }
      //String l=last name, String f=frist name
      //creates new author object by setting the last and first name
      public Author(String l, String f){
        authorLastName=l;
        authroFirstName=f;
      }
      //returns a string that is the first name of the author
      public String getFirstName(){
        return authorFirstName;
      }
      //return a string that is the last name of the author
      public String getLastName(){
        return authorLastName;
      }
      //int b=year of birth for author
      //sets the birth year of author and returns nothing
      public void setDates(int b){
        if (b<-2000){
          authorBirth=-40000;
        }else{
          authorBirth=b;
        }
      }
      //int b,d represent the authors year of birth and death respectively
      //returns nothing but sets the authors year of death and birth
      public void setDates(int b, int d){
        if(d>=b||birth<-2000){
          authorBirth=-4000;
          authorDeath==-4000;
        }else{
          authorBirth=b;
          authorDeath=d;
        }
      }
      //returns an int that represents the birth year of the author
      public int getBirth(){
        return authorBirth;
      }
      //returns an int that represents the death year of the author
      public int getDeath(){
        return authorDeath;
      }
      //compares two authors and if they have the same first and last name then it returns 0.
      //it also returns 0 if one of the authors appreviates his or her name by the first letter of their first name.
      public int compareTo(Author a1){
        int compareValue=1;
        if(a1.getFirstName().equals(authorFirstName)&&a1.getLastName().equals(authorLastName)){
          compareValue=0;
        }
        if(a1.charAt(0)==authorFirstName.charAt(0)&&a1.getLastName().equals(authorLastName)){
          compareValue=0;
        }
        return compareValue;
      }
      //returns a String in the form of the authors name by last name, first name
      public String toString(){
        String name=authorLastName+", "+authorFirstName;
        return name;
      }
      //returns a string of the last name, first name of the author
      //if the birth year is known it returns last name, first name of the author but input birth by (b.____)
      //if birth and death are known then returns last name, first name of the author with (birth-death)
      public String printAuthorInfo(){
        String name=authorLastName+", "+authorFirstName;
        if(authorBirth=this.getBirth()){
          name=name+" (b. "+authorBirth+")";
        }
        if(authorDeath=this.getDeath()){
          name=authorLastName+", "+authorFirstName+"("+authorBirth+"-"+authorDeath+")";
        }
        return name;
      }
      //end of Author class
    }
    public class Book{
      int bookYearPublish;
      Author bookAuthor;
      String bookNumberISBN;
      String bookTitle;
      public Book(){
      }
      //String t is the title of the book
      //creats a Book object with a title
      public Book(String t){
        bookTitle=t;
      }
      //String t is the title of the book
      //Author a is the author of the book
      //creats a Book object with a title and author
      public Book(String t, Author a){
        bookTitle=t;
        bookAuthor=a;
      }
      //String t is the title of the book
      //creates a title for a Book object
      //returns nothing but sets the title of the Book object
      //if no title was set then the title is "TITLE NOT SET"
      public void setTitle(String t){
        if (t.length()==0){
          bookTitle="TITLE NOT SET";
        }else{
          bookTitle=t;
        }
      }
      //returns a String that represents the title of the Book
      public String getTitle(){
        return bookTitle;
      }
      //Author a is the author that will be set for the Book
      //sets the author of the book
      public void setAuthor(Author a){
        bookAuthor=a;
      }
      //returns the Author of the Book
      public Author getAuthor(){
        return bookAuthor;
      }
      //int p is the year of publication of the Book
      //returns nothing
      //sets the year of publication for the Book
      public void setYear(int p){
        if (p<-2000||p>2018){
          bookYearPublish=-4000;
        }else{
          bookYearPublish=p;
        }
      }
      //returns an int that represents the year it was published
      public int getYear(){
        return bookYearPublish;
      }
      //String i is the ISBN number
      //returns nothing but sets the ISBN for the book
      //if the ISBN number is not length 13 or 10 the ISBN is "ISBN NOT SET"
      public void setISBN(String i){
        if(i.length()==13||i.length()==10){
          bookNumberISBN=i;
        }else{
          bookNumberISBN="ISBN NOT SET";
        }
      }
      //returns a String that represents the ISBN number of the book
      public String getISBN(){
        return bookNumberISBN;
      }
      //Book b is the book that is being checked if it has the same author
      //returns a boolean true if a book has two authors who are the same
      public boolean sameAuthor(Book b){
        boolean compareValue=false;
        if(b.getAuthor().compareTo(bookAuthor)==0){
          compareValue=true;
        }
      return compareValue;
      }
      //Book b is the book that is being compared to by the ISBN number
      //compares two books by relating thier ISBN numbers and returns 0 is they are the same
      public int compareTo(Book b){
        int compareValue=1;
        if(bookNumberISBN.equals(b.getISBN())){
          compareValue=0;
        }
        return compareValue;
      }
      //returns the title of Book
      //if the author is known returns a String in the form of title. last name, first name of author
      //if the year is known returns a String in the form title (year). last name, first name of author
      public String toString(){
        String title=bookTitle;
        if(bookAuthor.equals(this.getAuthor())){
          title=title+". "+bookAuthor;
        }
        if(bookYearPublish.equals(this.getYear())){
          title=bookTitle+" ("+bookYearPublish+"). "+bookAuthor;
        }
        return title;
      }
      //ends class Book
    }

.getAuthor() 方法是 Book 类中的一个方法,它返回该书的作者我真正的问题是如何告诉我的程序"如果已经设置了某些东西,那就这样做",例如在我的代码中,我试图告诉我的程序是否为这本书设置了标题然后返回标题......此外,如果已设置出版年份,则返回上述标题。

public String toString(){
      String title= (bookTitle==null) ? "" : bookTitle;
      if (getYear() != null && this.getYear().equals(bookYearPublish)) {
           title +=" ("+bookYearPublish+")";
      }
      title += ".";
      if (getAuthor() != null && this.getAuthor().equals(bookAuthor)) {
          title +=" "+bookAuthor;
      }
      return title;
}

相关内容

  • 没有找到相关文章

最新更新