打印 2 个具有特定格式的 ArrayLists
Print 2 ArrayLists with specific format
我正在尝试格式化我的程序的输出,因此它看起来很容易被用户阅读。我不太擅长格式化文本,非常欢迎您的帮助。
我正在尝试 print/display(在我的 JTextArea 中)。我已经有一个方法可以在那里显示:
public void display(String... lines){
for(String line:lines){
System.out.println(line);
}
}
现在,我有 2 个 ArrayList,其中包含一些通过使用程序添加到它们的数据。
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。
我想创建一个以特定格式或类似格式打印这 2 个 ArrayList 的方法:
customerName "with id" orderID "has purchased" itemName1
itemName2
itemName3
etc...
如有任何帮助,我们将不胜感激!
for(CafeCustomer customer : customers ){
for(CafeOrders order: itemOrders ){
if(customer.getOrderNo.equals(order.getOrderId()) ){
System.out.println(customer.getCustomerName() +"with Id " +
customer.getCustomerId() + "has purchased "
+order.getItemName());
}
}
}
只要您的客户可以有多个订单,我建议使用
Map<CafeCustomer, List<CafeOrder>>
然后你可以做什么:
public void display(Map<CafeCustomer, List<CafeOrder>> map)
{
map.forEach( (customer, orderList) -> {
System.out.println(customer.getCustomerName() + " has purchased:");
for(CafeOrder order : orderList)
System.out.println(order.getItemName());
});
}
针对特定格式进行编辑
向@Shiksha Verma 添加额外代码以仅打印一次客户名称和 ID:
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("%1s",order.getItemName());
}
}
x = 0;
}
虽然此代码有效:
public void displayOrders(){
for(CafeCustomer customer : customers ){
for(CafeOrders order: itemOrders ){
if(customer.getOrderNo().equals(order.getOrderID()) ){
display(customer.getCustomerID()+"."+customer.getCustomerName() +"with Order ID " +
customer.getOrderNo() + " has purchased a "
+order.getItemName());
}
}
}
}
我希望客户 ID、姓名和订单 ID 出现一次,然后出现在项目列表的右侧。就像我在最初的 post.
中展示的那样
可能吗?我似乎无法找到一种方法来做到这一点。
像这样:
customerID"."customerName "with id" orderID "has purchased" itemName1
itemName2
itemName3
etc...
它看起来完全像这样。
修改代码如下:
for(CafeCustomer 客户:客户){
System.out.println(customer.getCustomerName() +"with Id " +
customer.getCustomerId()+ "has purchased ::");
for(CafeOrders order: itemOrders ){
if(customer.getOrderNo.equals(order.getOrderId()) ){
System.out.println("\t\t\t\t\t\t\t"+ order.getItemName());
}
}
}
我正在尝试格式化我的程序的输出,因此它看起来很容易被用户阅读。我不太擅长格式化文本,非常欢迎您的帮助。
我正在尝试 print/display(在我的 JTextArea 中)。我已经有一个方法可以在那里显示:
public void display(String... lines){
for(String line:lines){
System.out.println(line);
}
}
现在,我有 2 个 ArrayList,其中包含一些通过使用程序添加到它们的数据。
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。
我想创建一个以特定格式或类似格式打印这 2 个 ArrayList 的方法:
customerName "with id" orderID "has purchased" itemName1
itemName2
itemName3
etc...
如有任何帮助,我们将不胜感激!
for(CafeCustomer customer : customers ){
for(CafeOrders order: itemOrders ){
if(customer.getOrderNo.equals(order.getOrderId()) ){
System.out.println(customer.getCustomerName() +"with Id " +
customer.getCustomerId() + "has purchased "
+order.getItemName());
}
}
}
只要您的客户可以有多个订单,我建议使用
Map<CafeCustomer, List<CafeOrder>>
然后你可以做什么:
public void display(Map<CafeCustomer, List<CafeOrder>> map)
{
map.forEach( (customer, orderList) -> {
System.out.println(customer.getCustomerName() + " has purchased:");
for(CafeOrder order : orderList)
System.out.println(order.getItemName());
});
}
针对特定格式进行编辑
向@Shiksha Verma 添加额外代码以仅打印一次客户名称和 ID:
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("%1s",order.getItemName());
}
}
x = 0;
}
虽然此代码有效:
public void displayOrders(){
for(CafeCustomer customer : customers ){
for(CafeOrders order: itemOrders ){
if(customer.getOrderNo().equals(order.getOrderID()) ){
display(customer.getCustomerID()+"."+customer.getCustomerName() +"with Order ID " +
customer.getOrderNo() + " has purchased a "
+order.getItemName());
}
}
}
}
我希望客户 ID、姓名和订单 ID 出现一次,然后出现在项目列表的右侧。就像我在最初的 post.
中展示的那样
可能吗?我似乎无法找到一种方法来做到这一点。
像这样:
customerID"."customerName "with id" orderID "has purchased" itemName1
itemName2
itemName3
etc...
它看起来完全像这样。
修改代码如下:
for(CafeCustomer 客户:客户){
System.out.println(customer.getCustomerName() +"with Id " +
customer.getCustomerId()+ "has purchased ::");
for(CafeOrders order: itemOrders ){
if(customer.getOrderNo.equals(order.getOrderId()) ){
System.out.println("\t\t\t\t\t\t\t"+ order.getItemName());
}
}
}