Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>dev.pooriya</groupId>
<artifactId>Module-Java</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.13.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.3</version>
</plugin>
</plugins>
</build>
</project>
32 changes: 32 additions & 0 deletions src/main/java/exercise1x/AllTransactionsMonthlyStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package exercise1x;

/**
* Represents a monthly statement containing all transactions.
*/
public class AllTransactionsMonthlyStatement implements Statement{

@Override
public String getPeriod() {
return "Period Placeholder";
}

@Override
public int getAccountNumber() {
return 0;
}

@Override
public String getCustomerName() {
return "CustomerName Placeholder";
}

@Override
public int[] getTransactions() {
return new int[0];
}

@Override
public String createStatement() {
return "All transactions...";
}
}
18 changes: 18 additions & 0 deletions src/main/java/exercise1x/EmailStatementSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package exercise1x;

/**
* Sends bank statements by email.
*/
public class EmailStatementSender implements StatementSender {

@Override
public void sendStatement(String statementContent) {

System.out.println(statementContent + " sent via Email");
}

@Override
public void sendStatement(Statement statement){
System.out.println(statement.createStatement() + " Email");
}
}
18 changes: 18 additions & 0 deletions src/main/java/exercise1x/LetterStatementSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package exercise1x;

/**
* Sends bank statements by letter.
*/
public class LetterStatementSender implements StatementSender {

@Override
public void sendStatement(String statementContent) {

System.out.println(statementContent + " sent via Letter");
}

@Override
public void sendStatement(Statement statement){
System.out.println(statement.createStatement() + " letter");
}
}
49 changes: 49 additions & 0 deletions src/main/java/exercise1x/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package exercise1x;

public class Main {

public static void main(String[] args) {

StatementSender email = new EmailStatementSender();
StatementSender letter = new LetterStatementSender();

StatementDeliveryService statementDeliveryService = new StatementDeliveryService();
statementDeliveryService.deliverStatement("hi there", email);
statementDeliveryService.deliverStatement("bye there", letter);

System.out.println("*** Exercise 1.2 ***");

StatementService statementService = new StatementService();

statementService.createAllTransactionStatement(email);
statementService.createAllTransactionStatement(letter);
statementService.createOutgoingTransactionStatement(letter);

}
}

/*

** Exercise 1.1 **

- Look at the example you just created. Why was it useful to use an interface? How would it look if we had only used classes - can you think of any problems that might cause?
- 1) Interface is stateless, it means our code is more flexible.
2) By using Interface, our code is much more open to extend in the future, as each class can implement many interfaces but just one class. and it follows OCP.
3) By using class and inheritance, we are actually saying that childClass IS-A SuperClass, which is not true here, because interface is a contract for a behavior only, so when class implement the interface, they actually CAN-DO what interface offers. (Coupling)
apart tahat, we had to create logic to differentiate statementSender types.

- How will the use of interfaces make the code more maintainable in future? What if we decide we also want the option to send statements via text message?
- As it follows Open/Close principle, it is maintainabe for instance if we want to add a new sending statement method, we can just implement new class and simply override the behavior

- How does using an interface enforce the contract that it sets out? Again think about writing a new text message implementation.
- When a class implements an interface, it has to follow the contracts that interface made, as long as he class does not implement all abstract constants and methods, compiler will complain.

- Similarly, what if we decide in future that physical letters are no longer supported; which code do we need to update?
- By adding @Deprecated annotation for developers (or completly delete it) and updating the specfic class that we want to apply changes on, we can add feature or deactivate existed class/behavor

- Think about how you would test exercise1x.StatementDeliveryService. In practice you wouldn’t want to send an actual email or a letter every time you ran the test, so how could the exercise1x.StatementSender interface help here?
(NB our expectation here is for a testing-implementation as they won’t have covered mocking yet
- with Dummy implementation?! I'm not sure, but I've done it with a fake class, I think we call it dummy implementation!
so that instead of doing actual job, and sending email or any expensive action, we can simply make sure that the specific method that we need has been invoked!

*/
32 changes: 32 additions & 0 deletions src/main/java/exercise1x/OutgoingTransactionsMonthlyStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package exercise1x;

/**
* Represents a monthly statement containing outgoing transactions.
*/
public class OutgoingTransactionsMonthlyStatement implements Statement{

@Override
public String getPeriod() {
return "Period Placeholder";
}

@Override
public int getAccountNumber() {
return 0;
}

@Override
public String getCustomerName() {
return "CustomerName Placeholder";
}

@Override
public int[] getTransactions() {
return new int[0];
}

@Override
public String createStatement() {
return "Outgoing Transactions...";
}
}
37 changes: 37 additions & 0 deletions src/main/java/exercise1x/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
✍️Exercise 1.1
Imagine you are designing a system for a bank to send monthly statements to customers. Some customers prefer to receive physical letters in the post, whereas others prefer to receive emails.

Create a StatementSender interface with a method #sendStatement that accepts a String parameter statementContent
What should the return type of this method be?
What is the visibility of interface methods? Why?
Make sure to write Javadoc on both the class and method
Why is this Javadoc important? Think about who we are aiming this javadoc at (i.e. who will read it / benefit from it)?
Create implementations of LetterStatementSender and EmailStatementSender
The implementations of #sendStatement should simply print to the console a success method to say that the statement has been sent via the given delivery method (in reality these methods would likely call out to some additional processing or third party email/postal service, but we don’t need to do that here)
Now create a class StatementDeliveryService with a method #deliverStatement(String statementContent, StatementSender statementSender)
This method should send the content using the sender
Now create a main method that creates a new StatementDeliveryService and sends a statement via both letter and email
Reflections
Think about the following questions, make notes and be prepared to talk through your thoughts in the workshop.

Look at the example you just created. Why was it useful to use an interface? How would it look if we had only used classes - can you think of any problems that might cause?
How will the use of interfaces make the code more maintainable in future? What if we decide we also want the option to send statements via text message?
How does using an interface enforce the contract that it sets out? Again think about writing a new text message implementation.
Similarly, what if we decide in future that physical letters are no longer supported; which code do we need to update?
Think about how you would test StatementDeliveryService. In practice you wouldn’t want to send an actual email or a letter every time you ran the test, so how could the StatementSender interface help here? (NB our expectation here is for a testing-implementation as they won’t have covered mocking yet


✍️Exercise 1.2
Create a Statement interface and add a new #sendStatement method to StatementSender that accepts the new interface
What is it called when you have two versions of the same method?
What methods might Statement have? Think about the type of information you’d expect to see on a bank statement.
Create two implementations of Statement:
AllTransactionsMonthlyStatement - As the name suggests, includes all transactions, both ingoing and outgoing for the current month
OutgoingTransactionsMonthlyStatement - Only includes the outgoing transactions for the current month
How should the implementation behaviour differ?
Create a new class StatementService that:
Has methods to create and send both types of statements
Uses StatementDeliveryService to send the statements
Note we’ll need a new method on StatementDeliveryService that accepts a Statement
Refactor your main method to use StatementService
You’re now starting to build up a hierarchy of classes and interfaces. Have a look back through the reflection questions about and apply them to your refactored system - are the answers the same? Are interfaces even more important now? Imagine we increased the complexity even further by adding more services and functionality - do interfaces become more or less important?
13 changes: 13 additions & 0 deletions src/main/java/exercise1x/Statement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package exercise1x;

/**
* Represents a bank statement.
*/
public interface Statement {

String getPeriod();
int getAccountNumber();
String getCustomerName();
int[] getTransactions();
String createStatement();
}
27 changes: 27 additions & 0 deletions src/main/java/exercise1x/StatementDeliveryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package exercise1x;

/**
* Delivers bank statements using a StatementSender.
*/
public class StatementDeliveryService {

/**
* Delivers statement content using the given sender.
*
* @param statementContent the content of the statement
* @param statementSender the sender used to deliver the statement
*/
public void deliverStatement(String statementContent, StatementSender statementSender) {
statementSender.sendStatement(statementContent);
}

/**
* Delivers a statement using the given sender.
*
* @param statement the statement to deliver
* @param statementSender the sender used to deliver the statement
*/
public void deliverStatement(Statement statement, StatementSender statementSender) {
statementSender.sendStatement(statement);
}
}
34 changes: 34 additions & 0 deletions src/main/java/exercise1x/StatementSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package exercise1x;

/**
* Interface for sending monthly bank statements to customers.
*/
public interface StatementSender {

/**
* Sends the bank statement content via the implemented delivery method.
* @param statementContent the content of the bank statement to be sent
*/
void sendStatement(String statementContent);

/**
* Sends a statement using the implemented delivery method.
* @param statement the statement to send
*/
void sendStatement(Statement statement);
}


/*
** Exercise 1.1 **

- What should the return type of this method be?
- As what this method does is to print the content, so return type should be void.

- What is the visibility of interface methods? Why?
- As this method is the core functionality of the interface and all classes must override (implement) it, the access modifier should be public.

- Why is this Javadoc important? Think about who we are aiming this javadoc at (i.e. who will read it / benefit from it)?
- It's really helpfull for other developers to understand the purpose of interface and its method, the valid parameters and even return type.

*/
29 changes: 29 additions & 0 deletions src/main/java/exercise1x/StatementService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package exercise1x;

public class StatementService {

/**
* Creates and delivers different types of bank statements.
*/
StatementDeliveryService deliveryService = new StatementDeliveryService();

/**
* Creates and delivers a statement containing all transactions.
*
* @param sender the sender used to deliver the statement
*/
void createAllTransactionStatement(StatementSender sender){
Statement allTransactions = new AllTransactionsMonthlyStatement();
deliveryService.deliverStatement(allTransactions,sender);
}

/**
* Creates and delivers a statement containing outgoing transactions.
*
* @param sender the sender used to deliver the statement
*/
void createOutgoingTransactionStatement(StatementSender sender){
Statement outgoingTransactions = new OutgoingTransactionsMonthlyStatement();
deliveryService.deliverStatement(outgoingTransactions,sender);
}
}
Loading