-1

im pretty new to OOP in java and im having trouble of changing the output of the email from as it should run from osan@gmail.com to cuscus@gmail.com to Osan.oro@gmail.com,i also did an argument on the setEmail parameter if relations wether if the input email is wrong then it will be Invalid email! Set to unknown and then the EMAIL : Uknown here is my current code:

    public class BookTest 
{
    public static void main(String[] args) 
    {
        Author author1 = new Author("Josanne Oro", "osan@gmail.com", "Male");
        
        author1.print();
        
        
        Book bk1 = new Book("Java Book", author1, 1499, 10);
        
        bk1.print();
        
        
        author1.setEmail("cuscus@gmail.com");
        
        author1.print();
        bk1.print();
        
        bk1.getAuthor().setEmail("Josanne.oro@gmail.com");
        bk1.print();
    }
}

    public class Author 
{
    private String author;
    private String email;
    private String gender;
    
    Author(String author, String email, String gender) 
    {
        this.author=author;
        this.email=email;
        this.gender=gender;
    }
    
    String getAuthor() 
    {
        return author;      
    }
    
    void setAuthor(String author) 
    {
        this.author=author;
    }
    
    String getEmail() 
    {
        return email;       
    }
    
    
    void setEmail(String email) 
    {
        if
(email=="osan@gmail.com"||email=="cuscus@gmail.com"||email=="Josanne.oro@gmail.com")
        {
            this.email=email;
        }
        else
            System.out.println("Invalid email! Set to unknown");
            this.email="Uknown";
    }
    
    void print() 
    {
        System.out.println("AUTHOR : " + author);
        System.out.println("GENDER : " + gender);
        System.out.println("E-MAIL : " + email);
    }
    
}

    public class Book 
{
    private String bookTitle;
    private Author author;
    private double price;
    private int stock=0;
    
    public Book(String bookTitle, Author author, double price, int stock) 
    {
        this.bookTitle=bookTitle;
        this.author=author;
        this.price=price;
        this.stock=stock;
    }
    
    public Author getAuthor() 
    {
        return this.author;
    }
    
    public void print() 
    {
        System.out.println("Book Title : " + bookTitle);
        author.print();
    } 
} ```
  • 1
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OH GOD SPIDERS Jul 15 '22 at 13:29

1 Answers1

-1

The problem looks to be in setEmail:

void setEmail(String email) 
    {
        if
(email=="osan@gmail.com"||email=="cuscus@gmail.com"||email=="Josanne.oro@gmail.com")
        {
            this.email=email;
        }
        else
            System.out.println("Invalid email! Set to unknown");
            this.email="Uknown"; //This line is not part of "else" because its not in a block
    }

So that this.email line gets called at the end of the function every time, to fix do this:

else {
    System.out.println("Invalid email! Set to unknown");
    this.email="Uknown";
}
Zachary
  • 324
  • 1
  • 13