Skip to content

Instantly share code, notes, and snippets.

@priyankahdp
Created February 19, 2023 16:47
Show Gist options
  • Save priyankahdp/bf152bff0626e425a371067a0606a4ed to your computer and use it in GitHub Desktop.
Save priyankahdp/bf152bff0626e425a371067a0606a4ed to your computer and use it in GitHub Desktop.
package com.designpatterns.adapter;
public interface AbcAccountsAPI {// This was old API
public String getAccountName();
public String getDescription();
public double getDebitAmount();
public double getCreditAmount();
public int getYear();
public int getMonth();
public int getDate();
public void setAccountName(String accountName);
public void setDescription(String description);
public void setDebitAmount(double debitAmount);
public void setCreditAmount(double creditAmount);
public void setYear(int year);
public void setMonth(int month);
public void setDate(int date);
}
package com.designpatterns.adapter;
public class AbcAccountsAPIImpl implements AbcAccountsAPI {
private String accountName;
private String description;
private double debitAmount;
private double creditAmount;
private int year;
private int month;
private int date;
@Override
public String getAccountName() {
return accountName;
}
@Override
public void setAccountName(String accountName) {
this.accountName = accountName;
}
@Override
public String getDescription() {
return description;
}
@Override
public void setDescription(String description) {
this.description = description;
}
@Override
public double getDebitAmount() {
return debitAmount;
}
@Override
public void setDebitAmount(double debitAmount) {
this.debitAmount = debitAmount;
}
@Override
public double getCreditAmount() {
return creditAmount;
}
@Override
public void setCreditAmount(double creditAmount) {
this.creditAmount = creditAmount;
}
@Override
public int getYear() {
return year;
}
@Override
public void setYear(int year) {
this.year = year;
}
@Override
public int getMonth() {
return month;
}
@Override
public void setMonth(int month) {
this.month = month;
}
@Override
public int getDate() {
return date;
}
@Override
public void setDate(int date) {
this.date = date;
}
}
package com.designpatterns.adapter;
public class AbcToXyzAdapter implements XyzAccountsAPI {//This is the Abc to Xyz adapter
private String ledgerAccountName;
private String shortDescription;
private double debitValue;
private double creditValue;
private String transactionDate;
private final AbcAccountsAPI abcAccountsAPI;// set variable as final,
// since its required to be instantiated in constructor
public AbcToXyzAdapter(AbcAccountsAPI abcAccountsAPI) {
this.abcAccountsAPI = abcAccountsAPI;
adapterDataMapping();
}
private void adapterDataMapping() {
setLedgerAccountName(abcAccountsAPI.getAccountName());
setShortDescription(abcAccountsAPI.getDescription());
setDebitValue(abcAccountsAPI.getDebitAmount());
setCreditValue(abcAccountsAPI.getCreditAmount());
setTransactionDate(abcAccountsAPI.getYear() + "-" + abcAccountsAPI.getMonth() + "-" + abcAccountsAPI.getDate());
}
@Override
public String getLedgerAccountName() {
return ledgerAccountName;
}
@Override
public void setLedgerAccountName(String ledgerAccountName) {
this.ledgerAccountName = ledgerAccountName;
}
@Override
public String getShortDescription() {
return shortDescription;
}
@Override
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
@Override
public double getDebitValue() {
return debitValue;
}
@Override
public void setDebitValue(double debitValue) {
this.debitValue = debitValue;
}
@Override
public double getCreditValue() {
return creditValue;
}
@Override
public void setCreditValue(double creditValue) {
this.creditValue = creditValue;
}
@Override
public String getTransactionDate() {
return transactionDate;
}
@Override
public void setTransactionDate(String transactionDate) {
this.transactionDate = transactionDate;
}
}
package com.designpatterns.adapter;
public class AdapterDemoApplication {// This is main application to test the output
public static void main(String[] args) {
AbcAccountsAPI abcAccountsAPI = new AbcAccountsAPIImpl();
abcAccountsAPI.setAccountName("3316561_FIXED_ASSETS");
abcAccountsAPI.setDescription("Account for Assets");
abcAccountsAPI.setDebitAmount(150000);
abcAccountsAPI.setCreditAmount(320550);
abcAccountsAPI.setYear(2022);
abcAccountsAPI.setMonth(12);
abcAccountsAPI.setDate(31);
XyzAccountsAPI xyzAccountsAPI = new AbcToXyzAdapter(abcAccountsAPI);
testAdapterData(xyzAccountsAPI);
}
private static void testAdapterData(XyzAccountsAPI xyzAccountsAPI) {
System.out.println(xyzAccountsAPI.getLedgerAccountName());
System.out.println(xyzAccountsAPI.getShortDescription());
System.out.println(xyzAccountsAPI.getCreditValue());
System.out.println(xyzAccountsAPI.getDebitValue());
System.out.println(xyzAccountsAPI.getTransactionDate());
}
}
package com.designpatterns.adapter;
public interface XyzAccountsAPI {// This is new API
public String getLedgerAccountName();
public String getShortDescription();
public double getDebitValue();
public double getCreditValue();
public String getTransactionDate();
public void setLedgerAccountName(String ledgerAccountName);
public void setShortDescription(String shortDescription);
public void setDebitValue(double debitValue);
public void setCreditValue(double creditValue);
public void setTransactionDate(String transactionDate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment