0

Here is my Allowance class

public class Allowance {
    private String Id;
    private String Name;
    private String Type;
    private String RemainingAmount;
    private String TotalAmount;
}

This is created list

public List<Allowance> getBList(){
    List<Allowance> BList = new ArrayList<>();
    List.add(new Allowance("5964852","Name1","DATA","10","458965"));
    List.add(new Allowance("651655","Name2","DATA","20","5481662"));
    List.add(new Allowance("123","Name3","VOICE","30","8889625"));
    List.add(new Allowance("123423","Name4","DATA","20","325489"));
    return BList;
}

Here is my Benefits class

public class Benefits {
    private String itemId;
    private String bName;
    private String remaining;
    private String bTotal;
}

So, I want to set Allowance class variables values to Benefits class variables values Like this

Id value --> itemId value

Name value --> bName value

RemainingAmount value ---> remaining value

TotalAmount value ---> bTotal value

Then i want to return Benefits list like this

["itemId" : "5964852","bName": "Name1","remaining":"10","bTotal":"458965"]

Any one can help me ?

10
  • I am not quite sure I understand your question. Do you want a maper/transformer that, given an Allowance, creates a Benefit? Commented Aug 16, 2020 at 6:42
  • Would you please clarify what you mean by "set Allowance class name to Benefits class name"? It's fairly trivial to create a new Benefits object from an Allowance object using a Benefit constructor that takes an Allowance as a parameter (or using a factory) - but it doesn't sound like that's what you want. Commented Aug 16, 2020 at 6:42
  • Sorry, but this question doesn't make sense. You cannot change a class name. And you cannot change a variable or field name. But you can change the value of a field whose name is "name". Commented Aug 16, 2020 at 6:46
  • Yes @Turing85 Commented Aug 16, 2020 at 6:48
  • Actually i want to set Allowance class variable value to Benefits class variable @StephenC @Tibrogargan Commented Aug 16, 2020 at 6:49

3 Answers 3

2

You can transfer the values from an Allowance instance by using the single arg constructor and then poplating the properties of the Benefits class from the Allowance instance that is passed in. You can also override the toString method of the Benefits class to obtain the desired output. The clone method in the allowance class will take in the list you have already created to return a list of Benefits instances.

The Allowance class:

class Allowance {
    private String Id;
    private String Name;
    private String Type;
    private String RemainingAmount;
    private String TotalAmount;

    public String getId() {
        return Id;
    }

    public String getName() {
        return Name;
    }

    public String getType() {
        return Type;
    }

    public String getRemainingAmount() {
        return RemainingAmount;
    }

    public String getTotalAmount() {
        return TotalAmount;
    }

    public Allowance(String id, String name, String type, String remainingAmount, String totalAmount) {
        Id = id;
        Name = name;
        Type = type;
        RemainingAmount = remainingAmount;
        TotalAmount = totalAmount;
    }

public List<Benefits> clone(List<Allowance> allowanceList){
        
        List<Benefits> benefitList = new ArrayList<>();
        for(Allowance allowance : allowanceList){
            benefitList.add(new Benefits(allowance));
        }
        
        return benefitList;
    }
}

The benefits class:

class Benefits{

    private String itemId;
    private String bName;
    private String remaining;
    private String bTotal;

    public Benefits(Allowance allowance){

        this.itemId = allowance.getId();
        this.bName = allowance.getName();
        this.remaining = allowance.getRemainingAmount();
        this.bTotal = allowance.getTotalAmount();

    }

    @Override
    public String toString() {
        return "[ itemId : " + this.itemId + ", bName : " + this.bName + ", remaining : " + this.remaining + ", bTotal :" + this.bTotal + "]";
    }
}

To create the list of Benefit items, you could also create the following List directly:

      List<Benefits> bList = new ArrayList<>();

        Allowance a = new Allowance("5964852","Name1","DATA","10","458965");
        Allowance b = new Allowance("651655","Name2","DATA","20","5481662");
        Allowance c =  new Allowance("123","Name3","VOICE","30","8889625");
        Allowance d = new Allowance("123423","Name4","DATA","20","325489");

        bList.add(new Benefits(a));
        bList.add(new Benefits(b));
        bList.add(new Benefits(c));
        bList.add(new Benefits(d));

Sign up to request clarification or add additional context in comments.

Comments

2

Use stream and lambda is easy to realize it.

List<Allowance> AList = BList.stream().map(B -> {
    new Allowance(
        B.getId(),
        B.getName(),
        B.getRemainingAmount(),
        B.bTotal())
}).collect(Collectors.toList())

Comments

0

So from what I read in the replies to your original question, you'd be looking for something along these lines;

  1. You need to create getters for all the attributes of Allowance.

  2. Then create a constructor that takes an Allowance object as a parameter.

     public Benefits (Allowance a) {
    
         this.itemId = a.getID;
         this.bName = a.getName;
         this.remaning = a.getRemainingAmount;
         this.bTotal = a.getTotalAmount;
     }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.