Skip to content

Instantly share code, notes, and snippets.

@Daniiarz
Created November 12, 2019 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniiarz/15d56ad63e8f1843fbffc822c08aad37 to your computer and use it in GitHub Desktop.
Save Daniiarz/15d56ad63e8f1843fbffc822c08aad37 to your computer and use it in GitHub Desktop.
public class Author {
private String name;
private String email;
private char gender;
public Author(String name, String email, char gender) {
this.name = name;
this.email = email;
this.gender = gender;
}
public String getName() {
return this.name;
}
public String getEmail() {
return this.email;
}
public void setEmail(String newEmail) {
this.email = newEmail;
}
public char getGender() {
return this.gender;
}
@Override
public String toString() {
return "Author[name="+this.name+",email="+this.email+",gender="+this.gender+"]";
}
}
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class AuthorTest {
Author author;
@Before
public void setUp() throws Exception {
author = new Author("Daniiar", "daniyarflash.m01@gmail.com", 'm');
}
@Test
public void TestGetName(){
assertEquals(author.getName(), "Daniiar");
}
@Test
public void TestGetEmail(){
assertEquals(author.getEmail(), "daniyarflash.m01@gmail.com");
}
@Test
public void TestSetEmail(){
String newEmail = "gmail@gmail.com";
author.setEmail(newEmail);
assertEquals(author.getEmail(), newEmail);
}
@Test
public void TestGetGender(){
assertEquals(author.getGender(), 'm');
}
@Test
public void TestToString(){
assertEquals(author.toString(), "Author[name=Daniiar,email=daniyarflash.m01@gmail.com,gender=m]");
}
}
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);
}
}
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