英文:
Getting specific elements from arraylist
问题
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<Customer> cusarray;
private Table tbl = new Table();
@FXML
public void initialize() {
allcustomers = tbl.getCustomers();
// Unsure about how to do this for loop
for (Customer customer : allcustomers) {
// Your code to extract food items from each customer's order
// For example:
List<String> foodItems = customer.getFoodItems(); // Replace with actual method name
for (String foodItem : foodItems) {
// Process each food item, like printing or storing
}
}
}
}
(Note: I've assumed that you have a method getFoodItems()
in your Customer
class that returns a list of food items from the customer's order. Replace "getFoodItems()"
with the actual method name if it's different in your code.)
英文:
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
答案1
得分: 1
增强型for循环的使用方式如下所示:
for (Customer customer : allCustomers) {
// 显示顾客的订单或其他属性
System.out.println(customer.customerOrder);
}
使用索引for循环:
for (int i = 0; i < allCustomers.size(); i++) {
Customer customer = allCustomers.get(i);
System.out.println(customer.customerOrder);
// 这可以被转化为一行,但展示了如何在ArrayList中使用索引
}
请参阅下面LinuxServer的评论,其中有一个更酷的版本,使用流(streams)遍历列表。
英文:
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.
答案2
得分: 1
以下是解决您问题的一些示例。
For-each循环
for (Customer customer : allCustomers) {
//...
}
索引for循环
for (int i = 0; i < allCustomers.size(); i++) {
Customer customer = allCustomers.get(i);
//...
}
流(Streams)(JDK 8及以上)
allCustomers.stream().filter(customer -> /*在此添加条件*/).forEach(customer -> {
//...
});
希望对您有所帮助。
英文:
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.
答案3
得分: 0
根据我的理解,客户拥有嵌套在客户对象内部的订单ArrayList。
因此,首先您需要遍历allCustomers中的所有客户,然后遍历customerOrder列表中的所有订单。
星号输出用于区分每个客户的订单。
for (Customer customer : allCustomers) {
System.out.println("*****");
for (String singleOrder : customer.customerOrder) {
System.out.println(singleOrder);
}
}
另外,您也可以像这样实现:
for (Customer customer : allCustomers) {
System.out.println("*****");
System.out.println(Arrays.toString(customer.customerOrder));
}
英文:
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));
}
答案4
得分: -1
for (Customer customer : allcustomers) { // 遍历所有顾客
print("Customer Number: " + customer.customernumber); // 打印顾客编号
print("Customer Order: " + customer.customerorder.toString()); // 打印订单数组
}
使用普通的 for 循环
for (int i = 0; i < allcustomers.length; i++) {
print("Customer Number: " + allcustomers[i].customernumber); // 打印顾客编号
print("Customer Order: " + allcustomers[i].customerorder.toString()); // 打印订单数组
}
英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论