Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have two arraylists that contain objects, preferredCustomer<> and Customer<>.

ID: 3754422 • Letter: I

Question

I have two arraylists that contain objects, preferredCustomer<> and Customer<>. In a seperate class "ProcessOrder" I want to reference one of the objects in the array, and edit one of the elements. How do I specify which object i want to edit, and then what element i want to edit?

Here is the code:

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

public Main() {

// TODO Auto-generated constructor stub

}

public static void main(String[] args) {

ArrayList customer = new ArrayList<>();

ArrayList preferredCustomer=new ArrayList<>();

try (Scanner scanner = new Scanner(new FileInputStream("preferred.dat"));) {

while (scanner.hasNextLine()) {

String temp[]=scanner.nextLine().split(" ");

String id = temp[0];

String fname = temp[1];

String lname = temp[2];

float money = Float.parseFloat(temp[3]);

float discount=Float.parseFloat(temp[4].replace("%", ""));

discount=(float) ((100-discount)/100.0);

Customer u = new Customer(id, fname, lname, money);

customer.add(u);

PreferredCustomer pc=new PreferredCustomer(id, fname, lname, money, discount);

preferredCustomer.add(pc);

}

} catch (IOException e) {

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

}

try (Scanner scanner = new Scanner(new FileInputStream("customer.dat"));) {

while (scanner.hasNextLine()) {

String temp[]=scanner.nextLine().split(" ");

String id = temp[0];

String fname = temp[1];

String lname = temp[2];

float money = Float.parseFloat(temp[3]);

Customer u = new Customer(id, fname, lname, money);

customer.add(u);

}

} catch (IOException e) {

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

}

}

}

Explanation / Answer

Here you can specify the object by getting the object based on the index

customer.get(2).money=100;

get(2) means it will gives the 2nd elements in the array ArrayList so on 2nd object if you want modify any values get(i).maoney =100; similarly you can modify other value also

Fell free to comment here for any other clarifications