Lab#SE01-2: Maven/Gradle Person and Account stored in JSON
Java SE Lab
Create a Maven/Gradle Java SE Project with three classes and Junit to test objects and operations. From here you should add new features: factory pattern, JSON parser and save this JSON on local storage.
0.1 Context
- This project is the Lab#SE#01-2 (go Lab#SE01-1), you will need to have a basic understanding of the Java programming language, as well as some familiarity with Maven or Gradle for managing dependencies and building the project.
- Overall, this project will provide an opportunity for you to learn and apply the basics of Java programming, as well as gain experience with Maven or Gradle, JUnit, user input via the console and data-storage.
- By completing this project, you will have a starting foundation in these technologies and be able to build more complex and sophisticated Java applications in the future:
singleton
,factory
,maven
,JSON
0.2 Goal
- The goal of this project is to create three classes in Java (
Person
,Account
andManager
) that implement different algorithms or data structures, and to test them using JUnit. - Now, in this version v2.0 you will store the data on local JSON and use the
factory
pattern. For that, you will need to create theAccount
interface
and two subclasses:SavingsAccount
andCheckingAccount
. - Use the
Lombok
dependency to write getters and setters.
0.3 Tasks
The tasks involved in this project include: - The goal of this project is to create three classes in Java (Person
, Account
and Manager
) that implement different algorithms or data structures, test them using JUnit. - Implementing as well two basic pattern-designs: singleton
and factory
. - Creating Account
interface
and two subclasses: SavingsAccount
and CheckingAccount
. - Writing JUnit tests to verify that the classes work as expected. - Creating a StorageManager
class or similar, Utilities package, to store the data in local JSON. - You should attach the JUnit Test HTML results to documentation. - You should control your versions with GitHub or similar.
0.4 Optional
- As an optional task, you could also consider allowing the user to input data via the console, rather than using
hard-coded test data
in your JUnit tests. This would add dynamism and be able to interact with the classes. - Add
Credit Card
class and create the new featurebuy Credit Card
inAccountManager
.
1 Solving discussion
1.1 Base Classes: factory
Here is an example of how you could create a Lombok Java class for a Person
and Account
using the factory pattern:
You can then use these classes as follows:
The AccountManager
class uses the factory pattern to create Account
objects based on the specified accountType. This allows you to create different types of accounts without having to specify the exact class to use, and makes it easier to add new types of accounts in the future.
1.2 Base Classes: local storage-JSON
Here is an example of what a Lombok-based Person class and Account class might look like in Java:
The @Data annotation is a **Lombok* annotation that automatically generates getters and setters for all non-static fields, as well as equals()
, hashCode()
, and toString()
methods. This can help reduce boilerplate code and make your classes more concise and readable.
To save data to a local JSON file, you could use a JSON library like Jackson
to convert the objects to JSON strings, and then write those strings to a file. Here is an example of how you might do that:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class AccountManager {
private ObjectMapper objectMapper = new ObjectMapper();
public void saveAccount(Account account) throws JsonProcessingException {
String json = objectMapper.writeValueAsString(account);
// write the JSON string to a file
}
}
How to save an object as a JSON string to a file on your local storage (in this case, c:/data/accounts.json) using Java SE:
import java.io.FileWriter;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class AccountManager {
private ObjectMapper objectMapper = new ObjectMapper();
public void saveAccount(Account account) throws JsonProcessingException, IOException {
String json = objectMapper.writeValueAsString(account);
FileWriter fileWriter = new FileWriter("c:/data/accounts.json");
fileWriter.write(json);
fileWriter.close();
}
}
2 Step-by-step
- Create
interface
Account - Create subclasses
SavingsAccount
andCheckingAccount
- Create
AccountManger
- Check factory
- Parse
Account
objects to String-JSON - Save
String
on local .json file