简体   繁体   中英

How to merge two lists with different variable types

As the question states I'm wondering what the best way to merge two lists together that have different variable types. I'm unsure if the way I'm going about this problem is the correct way so I'm happy to take all advice regarding this. Basically I'm making a banking application. When the user finishes some transaction data is added to a txt file of the form (Type Date Amount). I want the user to then be able to do some more transactions later and these new transactions added to the end of this txt file.

My Issue is that when I re read the users previous transactions I read them in as a List of strings

BufferedReader br = null;

    try {

        br = new BufferedReader(new FileReader(account.getName()+"Transactions.txt"));

        String line;
        while ((line = br.readLine()) != null) {
            transactions.add(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            br.close();
        }

however the list of transactions to add to my txt file is of the type Transaction not a String.

List<Transaction> transactionsToAdd = new ArrayList<Transaction>()

As an example here is some code in the case the user makes a deposit.

    case 1:
            amount = scanner.DepositScanner();
            balance = account.Deposit(balance, amount);
            System.out.print("Your new balance is: " + balance);

            String depositFullDate = dateManager.getDateAsString();
            Transaction depositTransaction = new Transaction("Depsoit", amount, depositFullDate);
            transactionsToAdd.add(depositTransaction);

            break;

Ideally I want the code to read in the users previous transactions, add their new transactions to the end of the txt file and then save all of these to the same txt file when the user is finished. Is there either a way to read in the users previous transactions as type Transaction rather than String, or combine these two lists somehow to give me a big list to save to my txt file.

My transaction class is below

public class Transaction {
String details;
String date;

Double amount;



public String getDate() {
    return date;
}
public void setDate(String date) {
    this.date = date;
}
public Transaction(String details, double amount, String date) {
    super();
    this.details = details;
    this.date = date;
    
    this.amount = amount;
}
public String getDetails() {
    return details;
}
public void setDetails(String details) {
    this.details = details;
}
public Double getAmount() {
    return amount;
}
public void setAmount(double amount) {
    this.amount = amount;
}

}

Edit 1 - Added code for my transaction class

Convert your List of Transaction to JSON Array as: (follow below link)

Convert a list of string into json format

Then, when you want to create a new transaction, you can read the whole file as an JSON array and get your Transaction objects from it as well. You will need to loop over the JSON array to get your Transaction objects. (To get Transaction objects, you will have to use ObjectMapper).

See this:

https://stackoverflow.com/questions/23109531/json-array-to-pojo#:~:text=Cliente%5B%5D%20clientes%3D%20mapper.,JSON%20and%20in%20the%20POJO .

I actually had the same problem but the solution offered to me does not involve JSON. I am also using a scanner to read the.txt file not BufferedReader(idk how to use it).

Im assuming that your txt files look like these. Containing only those words and separated by a single whitespace. Because you havent shown the method on how you write something in transactions.txt

Deposit 42069 11/05/2021
Deposit 360 11/05/2021
//so on

This is how you would read it and store it in the ArrayList of object type.

//transactionToAdd is the name of your arraylist
//Transaction is the name of your class

String line = reader.readLine();
String[] array = line.split(" ")// use same delimiter used to write
if(array.lenght() ==3){ // to check if data has all three parameter 
    transactionToAdd.add(new Transaction(array[0], Double.parseDouble(array[1]), array[2] )); 
    // you have to handle case if Double.parseDouble(array[2]) throws exception
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM