20.7k views
1 vote
Java Exercise

Create a class consisting of the following information.

1. no parameter constructor that creates an empty ArrayList of bank account data (account number, name) that are both Strings.

2. a clear method that removes all data from the list.

3. a method that accepts 1 parameter, an account holder and adds the account holder to the list.

4.a method that accepts 2 parameters, a File reference and an encrypting key to use. It writes all account holder information in the list (simple CSV data for each account holder, now encrypted) to the file and returns a boolean parameter that indicates that the writing of the file was complete. (HINT: Use try/catch for FileNotFoundException)

User L Smeets
by
7.9k points

1 Answer

6 votes

Here's an example Java class that fulfills the requirements mentioned:

```java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

public class BankAccountData {

private List<String> accountData;

// Constructor to create an empty ArrayList of bank account data

public BankAccountData() {

accountData = new ArrayList<>();

}

// Method to clear all data from the list

public void clear() {

accountData.clear();

}

// Method to add an account holder to the list

public void addAccountHolder(String accountHolder) {

accountData.add(accountHolder);

}

// Method to write account holder information to a file with encryption

public boolean writeToFile(File file, String encryptionKey) {

try (PrintWriter writer = new PrintWriter(file)) {

for (String accountHolder : accountData) {

String encryptedData = encryptData(accountHolder, encryptionKey);

writer.println(encryptedData);

}

return true;

} catch (FileNotFoundException e) {

System.out.println("Error: File not found.");

return false;

}

}

// Helper method to encrypt the account holder data

private String encryptData(String data, String encryptionKey) {

// Implement your encryption algorithm here

// Example: Simple XOR encryption

StringBuilder encryptedData = new StringBuilder();

for (int i = 0; i < data.length(); i++) {

encryptedData.append((char) (data.charAt(i) ^ encryptionKey.charAt(i % encryptionKey.length())));

}

return encryptedData.toString();

}

}

```

In this class, we have a no-parameter constructor that initializes an empty ArrayList called `accountData`. The `clear()` method clears all data from the list. The `addAccountHolder()` method accepts an account holder as a parameter and adds it to the list. The `writeToFile()` method accepts a `File` reference and an encryption key. It writes each account holder's information, encrypted using the encryption key, to the file. It returns `true` if the writing of the file was successful, and `false` if a `FileNotFoundException` occurs.

Please note that the `encryptData()` method provided is a simple XOR encryption example and may not be suitable for secure encryption. You may need to implement a more secure encryption algorithm depending on your requirements.

User Nando
by
8.4k points