问题
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.
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);
}
}
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>();
The constructors, respectively:
public CafeOrders(String orderID, String itemName, double price){
super();
this.orderID = orderID;
this.itemName = itemName;
this.price = price;
}
and
public CafeCustomer(int customerID, String customerName){
super();
this.customerID = customerID;
this.customerName = customerName;
this.orderNo = UUID.randomUUID().toString();
}
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.
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...
Any help would be appreciated!
回答1:
EDITED FOR SPECIFIC FORMAT
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:
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());
}
}
}
回答3:
As long as your customer can have multiple orders I would suggest using
Map<CafeCustomer, List<CafeOrder>>
And then what you can do:
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());
});
}
回答4:
While this code works:
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());
}
}
}
}
I want the customer ID,name and order ID to appear once and then on the right the list of the items. Like I showed in my initial post.
Is it possible? I can't seem to find a way to do it.
Like this:
customerID"."customerName "with id" orderID "has purchased" itemName1
itemName2
itemName3
etc...
And it appears exactly like this.
回答5:
modify the code like this :
for(CafeCustomer customer : customers ){
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());
}
}
}
来源:https://stackoverflow.com/questions/47653351/print-2-arraylists-with-specific-format