Question
Please help me with the Payment Methods endpoint. Java : Simple Rest Server Instructions Create a simple server that uses simple design patterns Factory :
Please help me with the Payment Methods endpoint.
Java: Simple Rest Server
Instructions
Create a simple server that uses simple design patterns
- Factory: Your server should parse the routes and create a new Processor class depending on the route
- Builder: The process method of your Processor should use the Builder pattern to create a response.
- DAO: Make a class that abstracts the data interactions away from the rest of the app
- Singleton: Wrap your dao instance as a singleton class
- Lazy Loading: DAOs should only load data when requested
- DTO: Your JSON parsing, as well as response objects must be DTOs
First parse url, then use factory to instantiate correct Processor class. The factory will internally use the builder pattern to give the processor relevant data. Use builder to set the time, and input parameters.
Processor Class
- Has a process() method that returns a ResponseDto object
- Each endpoint is a subclass of Processor
- Takes a hashmap of key value args
- Use a factory to create these, based on the endpoint
- Every processor uses a builder and returns the output of builder.build()
Response Class
- This class gets stringified with GSON
- Needs a ResponseBuilder helper
- Needs Params Map (String, String)
- Needs response dto
- Needs Response Code (String)
Dao class
- Singleton
- Lazy load data
- 1 DAO per type of endpoint to hold the data
Endpoints
- Payment Methods
- /addPaymentMethod?method=name
- creates a new payment method and stores it in the paymentMethod DAO
- assigns a machine code
- needs Payment DTO + Payment DAO
- fields: name, machineCode
- /getAllPaymentMethods
- Returns list of all payment methods
- /addPaymentMethod?method=name
- Items
- /addItem?name=name&price=price
- Creates a new item and gives it a price
- Assigns machine readable code and returns it
- needs ItemDAO + Item DTO
- fields: name, price, machineCode
- /listItems
- returns all items
- /addItem?name=name&price=price
- Transactions
- /createTransaction?itemCode=code&paymentMethod=paymentCode
- Must have valid payment method
- fields: itemCode, paymentMethod
- /listTransactions
- returns all transaction
- needs TransactionDAO + Transaction DTO
- /createTransaction?itemCode=code&paymentMethod=paymentCode
High level steps
- Parse request and extract the endpoint
- Parse the get args into a hashmap
- Use factory to instantiate correct processor class
- Call .process(args) and pass in the arguments on this class (args is a hash map of the get params)
- Inside the processor it can get or put data through the DAO
- Business Logic goes in processor Load/Store logic goes in DAO
- Returns a single ResponseDto
- Stringify this response dto and pass it into the output stream
Return Format
You will need a response class that looks like this before passing to JSON.
"{
"date": "2019-02-10T20:40:59.257Z", // time that this was generated
"params": {
"a": 2 // full list of all key value get params
},
responseCode: OK/ERROR
"response": {}, // put your responses here
}"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started