I need to get the balance for a particular customer id and account id.
I have these two java classes. ( both classes have their get and set methods )
Customer
public class Customer { private int custid; private String name; private String address; private String email; private int pin; private List<Account> accounts = new ArrayList<>(); public Customer(){ } public Customer(int custid,String name, String address, String email, int pin, List<Account> accounts) { this.custid = custid; this.name = name; this.address = address; this.email = email; this.pin = pin; this.accounts = accounts; }
Account
public class Account { private int accid; private int sortCode; private int accNumber; private String accType; private double currentBalance; private List<Transaction> transactions = new ArrayList<>(); public Account(){ } public Account(int accid,int sortCode, int accNumber, String accType, double currentBalance, List<Transaction> transactions) { this.accid = accid; this.sortCode = sortCode; this.accNumber = accNumber; this.accType = accType; this.currentBalance = currentBalance; this.transactions = transactions; }
I have these two Customer service and Account service classes. Here is a method that is inside the CustomerService and Account Service
CustomerService
public Customer getCustomer(int id) { return cList.get(id-1); }
AccountService
public Account getAccount(int accid) { return aList.get(accid-1); }
I need to take two parameters in my get request like so. I have the below in a separate class.
@GET @Path("/{customerID}/{accountID}") @Produces(MediaType.APPLICATION_JSON) public Customer getBalance(@PathParam("customerID") int cID,@PathParam("accountID") int aID ) { //gets customer for CustomerServices and returns it return customerService.getCustomer(cID); }
How can I return the balance on given customer id and their account id?
Advertisement
Answer
@GET @Path("/{customerID}/{accountID}") @Produces(MediaType.TEXT_PLAIN) public String getBalance(@PathParam("customerID") int cid, @PathParam("accountID") int aid) { //Get specific customer from customers using id Customer c = customerService.getCustomer(cid); //Get a list of the accounts on that customer List<Account> accounts = c.getAccounts(); Account a = accounts.get(aid - 1); Double newBalance = a.getCurrentBalance(); return "Balance for Customer ID " + cid + " with account ID " + aid + " is " + newBalance; }
That’s my answer in case anyone is looking at this post and needs something similar.