Created
November 12, 2019 09:59
-
-
Save Daniiarz/15d56ad63e8f1843fbffc822c08aad37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Book { | |
private String name; | |
private Author[] authors; | |
private double price; | |
private int qty; | |
public Book(String name, Author[] authors, double price) { | |
this.name = name; | |
this.authors = authors; | |
this.price = price; | |
this.qty = 0; | |
} | |
public Book(String name, Author[] authors, double price, int qty){ | |
this.name = name; | |
this.authors = authors; | |
this.price = price; | |
this.qty = qty; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public Author[] getAuthors() { | |
return this.authors; | |
} | |
public double getPrice() { | |
return this.price; | |
} | |
public void setPrice(double newPrice) { | |
this.price = newPrice; | |
} | |
public int getQty() { | |
return this.qty; | |
} | |
public void setQty(int newQty) { | |
this.qty = newQty; | |
} | |
@Override | |
public String toString() { | |
String result = "Book[name=" + this.name + "," + "authors={"; | |
StringBuilder authors_str = new StringBuilder(); | |
for (Author i: this.authors) { | |
authors_str.append(i.toString()).append(","); | |
} | |
return result + authors_str.substring(0, authors_str.length() - 1) + "},price=" + this.price + ",qty=" + this.qty + "]"; | |
} | |
public String getAuthorNames() { | |
StringBuilder authors_str = new StringBuilder(); | |
for (Author i: this.authors) { | |
authors_str.append(i.toString()).append(","); | |
} | |
return authors_str.substring(0, authors_str.length() - 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Before; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class BookTest { | |
Author author; | |
Author author2; | |
Book book; | |
Author[] authors; | |
@Before | |
public void setUp() throws Exception { | |
author = new Author("Daniiar", "daniyarflash.m01@gmail.com", 'm'); | |
author2 = new Author("Dastan", "dastan@gmail.com", 'm'); | |
authors = new Author[2]; | |
authors[0] = author; | |
authors[1] = author2; | |
book = new Book("How to Python", authors, 1200.0); | |
} | |
@Test | |
public void TestGetName(){ | |
assertEquals(book.getName(), "How to Python"); | |
} | |
@Test | |
public void TestGetAuthors(){ | |
assertArrayEquals(book.getAuthors(), authors); | |
} | |
@Test | |
public void TestGetPrice(){ | |
assertEquals(book.getPrice(), 1200.0, 0); | |
} | |
@Test | |
public void TestSetPrice(){ | |
double newPrice = 1000; | |
book.setPrice(newPrice); | |
assertEquals(book.getPrice(), newPrice, 0); | |
} | |
@Test | |
public void TestGetQty(){ | |
assertEquals(book.getQty(), 0); | |
} | |
@Test | |
public void TestSetQty(){ | |
int newQty = 1000; | |
book.setQty(newQty); | |
assertEquals(book.getQty(), newQty); | |
} | |
@Test | |
public void TestToString(){ | |
assertEquals(book.toString(), | |
"Book[name=How to Python,authors={Author[name=Daniiar,email=daniyarflash.m01@gmail.com,gender=m],Author[name=Dastan,email=dastan@gmail.com,gender=m]},price=1200.0,qty=0]"); | |
} | |
@Test | |
public void TestGetAuthorNames(){ | |
assertEquals(book.getAuthorNames(), "Author[name=Daniiar,email=daniyarflash.m01@gmail.com,gender=m],Author[name=Dastan,email=dastan@gmail.com,gender=m]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment