Reputation: 11
I have a resturant system using java fx, when a customer orders that order is added to the 'customer' class. The customer is then added to the 'table' class.
At the end im trying to display each individual customers order by looping through an array list of them and i cant figure out the syntax
an arraylist called allcustomers is set like this
allcustomers = tbl.getCustomers();
printed out it looks like this
[Customer{customernumber=null, customerorder=[burger'7.99]}]
im trying to loop through the 'allcustomers' arraylist and just get the food items but im unsure how how?
this is the full code
public class PaymentScreenController {
public ArrayList<Customer> allcustomers;
public Customer cus1;
public ArrayList cusarray;
private Table tbl = new Table();
@FXML
public void initialize() {
allcustomers = tbl.getCustomers();
// Unsure about how to do this for loop
for ( : allcustomers) {
}
any help would be appreciated thanks
Upvotes: 0
Views: 151
Reputation: 1
As I see, Customer has ArrayList of orders nested inside of a Customer object. So, first you need to iterate over all customers in allCustomers and then iterate over all orders in customerOrder list. Star output is used to separate orders from each customer.
for (Customer customer : allCustomers) {
System.out.println("*****")
for(String singleOrder: customer.customerOrder){
System.out.println(singleOrder);
}
}
Also, you can do it like this :
for (Customer customer : allCustomers) {
System.out.println("*****")
System.out.println(Arrays.toString(customer.customerOrder));
}
Upvotes: 0
Reputation: 7604
Enhanced for loops are used like this:
for (Customer customer : allCustomers) {
//To display the customer's order or some other attribute
System.out.println(customer.customerOrder);
}
With an indexed for loop:
for (int i = 0; i < allCustomers.size(); i ++) {
Customer customer = allCustomers.get(i);
System.out.println(customer.customerOrder);
//This could be turned into one line, but shows how you index in ArrayLists
}
See the comment by LinuxServer below for a cooler version that uses streams to go through the list.
Upvotes: 1
Reputation: 380
for (Customer customer : allcustomers) { // Iterate through all customers
print("Customer Number: " + customer.customernumber); // Print Customer Number
print("Customer Order: " + customer.customerorder.toString()); // Print the order array
}
Using a normal for loop
for (int i = 0; i < allcustomers.length; i++) {
print("Customer Number: " + allcustomers[i].customernumber); // Print Customer Number
print("Customer Order: " + allcustomers[i].customerorder.toString()); // Print the order array
}
Upvotes: -1
Reputation: 377
Here are some examples for solving your problem.
For-each loop
for (Customer customer : allCustomers) {
//...
}
indexed for loop
for (int i = 0; i < allCustomers.size(); i++) {
Customer customer = allCustomers.get(i);
//...
}
Streams (JDK 8 >)
allCustomers.stream().filter(customer -> /*some condition here*/).forEach(customer -> {
//...
});
I hope it's helpful.
Upvotes: 1