打印2个具有特定格式的ArrayList [英] Print 2 ArrayLists with specific format
问题描述
我正在尝试格式化程序的输出,因此用户看起来很容易阅读.我不太擅长格式化文本,非常欢迎您的帮助.
I am trying to format the output of my program, so it looks easily readable by the user. I am not that good at formatting text and I would really welcome your help.
我正在尝试打印/显示(在我的JTextArea中).我已经有一种方法可以显示在那里:
I am trying to print/display(in my JTextArea). I already have a method to display there:
public void display(String... lines){
for(String line:lines){
System.out.println(line);
}
}
现在,我有2个ArrayList,它们包含一些通过使用程序添加到其中的数据.
Now, I have the 2 ArrayLists that contain some data which was added to them through the use of the program.
itemOrders = new ArrayList<CafeOrders>();
customers = new ArrayList<CafeCustomer>();
构造函数分别为:
public CafeOrders(String orderID, String itemName, double price){
super();
this.orderID = orderID;
this.itemName = itemName;
this.price = price;
}
和
public CafeCustomer(int customerID, String customerName){
super();
this.customerID = customerID;
this.customerName = customerName;
this.orderNo = UUID.randomUUID().toString();
}
我这样做的方式是,orderNo = orderID.
首先询问客户名称,然后当他从项目列表中进行选择时,每个项目都与客户ID一起添加到CafeOrders ArrayList中.
I made it in such a way, that orderNo = orderID.
First the customer name is asked, then when he selects from a list of items, each item is added to the CafeOrders ArrayList along with the customer's ID.
我想创建一个以特定格式或类似格式打印这2个ArrayList的方法:
I want to create a method that prints those 2 ArrayLists in the specific format, or something similar:
customerName "with id" orderID "has purchased" itemName1
itemName2
itemName3
etc...
任何帮助将不胜感激!
推荐答案
针对特定格式进行了编辑
向@Shiksha Verma添加额外的代码以仅打印一次客户名称和ID:
Adding extra code to @Shiksha Verma to print the customer name and ID only once:
int x = 0;
for(CafeCustomer customer : customers ){
for(CafeOrders order: itemOrders ){
if(customer.getOrderNo.equals(order.getOrderId()) ){
if(x == 0){
System.out.println(customer.getCustomerName() +"with Id " +
customer.getCustomerId()+ "has purchased: "
+order.getItemName());
x = 1;
continue;
}
System.out.printf("%1$40s",order.getItemName());
}
}
x = 0;
}
这篇关于打印2个具有特定格式的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!