Skip to content

Commit 4d30ff3

Browse files
getwordsdonejzheaux
authored andcommitted
A Guide to Constructors
Issue: BAEL-2345
1 parent c7015ee commit 4d30ff3

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.constructors;
2+
3+
import java.time.LocalDateTime;
4+
5+
class BankAccount {
6+
String name;
7+
LocalDateTime opened;
8+
double balance;
9+
10+
@Override
11+
public String toString() {
12+
return String.format("%s, %s, %f", this.name, this.opened.toString(), this.balance);
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public LocalDateTime getOpened() {
20+
return opened;
21+
}
22+
23+
public double getBalance() {
24+
return this.balance;
25+
}
26+
}
27+
28+
class BankAccountEmptyConstructor extends BankAccount {
29+
public BankAccountEmptyConstructor() {
30+
this.name = "";
31+
this.opened = LocalDateTime.now();
32+
this.balance = 0.0d;
33+
}
34+
}
35+
36+
class BankAccountParameterizedConstructor extends BankAccount {
37+
public BankAccountParameterizedConstructor(String name, LocalDateTime opened, double balance) {
38+
this.name = name;
39+
this.opened = opened;
40+
this.balance = balance;
41+
}
42+
}
43+
44+
class BankAccountCopyConstructor extends BankAccount {
45+
public BankAccountCopyConstructor(String name, LocalDateTime opened, double balance) {
46+
this.name = name;
47+
this.opened = opened;
48+
this.balance = balance;
49+
}
50+
51+
public BankAccountCopyConstructor(BankAccount other) {
52+
this.name = other.name;
53+
this.opened = LocalDateTime.now();
54+
this.balance = 0.0f;
55+
}
56+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.constructors;
2+
3+
import java.time.LocalDateTime;
4+
5+
class Transaction {
6+
final BankAccountEmptyConstructor bankAccount;
7+
final LocalDateTime date;
8+
final double amount;
9+
10+
public Transaction(BankAccountEmptyConstructor account, LocalDateTime date, double amount) {
11+
this.bankAccount = account;
12+
this.date = date;
13+
this.amount = amount;
14+
}
15+
16+
/*
17+
* Compilation Error :'(, all final variables must be explicitly initialised.
18+
* public Transaction() {
19+
* }
20+
*/
21+
22+
public void invalidMethod() {
23+
// this.amount = 102.03; // Results in a compiler error. You cannot change the value of a final variable.
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.constructors;
2+
3+
import com.baeldung.constructors.*;
4+
5+
import java.util.logging.Logger;
6+
import java.time.LocalDateTime;
7+
import java.time.Month;
8+
9+
import org.junit.Test;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.assertThatCode;
12+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
13+
14+
public class ConstructorUnitTest {
15+
final static Logger LOGGER = Logger.getLogger(ConstructorUnitTest.class.getName());
16+
17+
@Test
18+
public void givenNoExplicitContructor_whenUsed_thenFails() {
19+
BankAccount account = new BankAccount();
20+
assertThatThrownBy(() -> { account.toString(); }).isInstanceOf(Exception.class);
21+
}
22+
23+
@Test
24+
public void givenNoArgumentConstructor_whenUsed_thenSucceeds() {
25+
BankAccountEmptyConstructor account = new BankAccountEmptyConstructor();
26+
assertThatCode(() -> {
27+
account.toString();
28+
}).doesNotThrowAnyException();
29+
}
30+
31+
@Test
32+
public void givenParameterisedConstructor_whenUsed_thenSucceeds() {
33+
LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00);
34+
BankAccountParameterizedConstructor account =
35+
new BankAccountParameterizedConstructor("Tom", opened, 1000.0f);
36+
37+
assertThatCode(() -> {
38+
account.toString();
39+
}).doesNotThrowAnyException();
40+
}
41+
42+
@Test
43+
public void givenCopyContructor_whenUser_thenMaintainsLogic() {
44+
LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00);
45+
BankAccountCopyConstructor account = new BankAccountCopyConstructor("Tim", opened, 1000.0f);
46+
BankAccountCopyConstructor newAccount = new BankAccountCopyConstructor(account);
47+
48+
assertThat(account.getName()).isEqualTo(newAccount.getName());
49+
assertThat(account.getOpened()).isNotEqualTo(newAccount.getOpened());
50+
51+
assertThat(newAccount.getBalance()).isEqualTo(0.0f);
52+
}
53+
}

0 commit comments

Comments
 (0)