Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functionality code for banking server app #1

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.flagsmithspringbootbankingapp;

import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Getter
@Setter
public class Account {
String id;
String accountHolderName;
double balance;

public Account(String accountHolderName) {
this.id = UUID.randomUUID().toString();
this.accountHolderName = accountHolderName;
this.balance = 0;
}
}


@Getter
@Setter
class AddMoneyRequest {
String accountId;
double amount;
}


@Getter
@Setter
class WithdrawMoneyRequest {
String accountId;
double amount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.flagsmithspringbootbankingapp;

import java.util.HashMap;

public class AccountService {

private final HashMap<String, Account> UserAccounts;

// initialises empty map to store user's accounts info
public AccountService() {
UserAccounts = new HashMap<>();
}

// get account via accountID
public Account GetAccount(String accountID) {
return this.UserAccounts.get(accountID);
}

// create a new account and add it to the map
public Account CreateAccount(String accountHolderName) {
Account newAccount = new Account(accountHolderName);
this.UserAccounts.put(newAccount.getId(), newAccount);
return newAccount;
}

// add money to user's bank account
public Account AddMoney(String accountId, double amount) {
Account acc = this.GetAccount(accountId);
acc.balance += amount;
return acc;
}

// withdraw money from user's bank account
public Account WithdrawMoney(String accountId, double amount) {
Account acc = this.GetAccount(accountId);
acc.balance += amount;
return acc;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
package com.example.flagsmithspringbootbankingapp;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@RestController
public class FlagsmithSpringBootBankingAppApplication {

private static AccountService accountService;
public static void main(String[] args) {
accountService = new AccountService();
SpringApplication.run(FlagsmithSpringBootBankingAppApplication.class, args);
}

// endpoint to create a new account
@PostMapping(path = "/create-account", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Account> createAccount(@RequestBody String accountHolderName) {
return ResponseEntity.ok(accountService.CreateAccount(accountHolderName));
}

// endpoint to add money to an account
@PostMapping(path = "/add-money", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Account addMoney(@RequestBody AddMoneyRequest req) {
return accountService.AddMoney(req.accountId, req.amount);
}

// endpoint to withdraw money from an account
@PostMapping(path = "/withdraw-money", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Account> withdrawMoney(@RequestBody WithdrawMoneyRequest req) {
return ResponseEntity.ok(accountService.WithdrawMoney(req.accountId, req.amount));

}

// endpoint to check account balance
@GetMapping("/check-balance")
public double checkBalance(@RequestParam(value = "accountId") String accountId) {
Account account = accountService.GetAccount(accountId);
return account.balance;
}

// endpoint to get the current interest rate
@GetMapping("/interest-rate")
public ResponseEntity<Object> getInterestRate() {
return ResponseEntity.ok(3.5);
}
}