0

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!

5 Answers5

0
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());
           }
       }
   }
  • While this works well, I would want it to print the customer name and the id only once and then next to them, the list of items. – Marios Louvaris Dec 05 '17 at 12:52
  • 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()); } } } – Shiksha Verma Dec 05 '17 at 14:39
  • Thanks a lot, this works with some small visual changes I made. – Marios Louvaris Dec 05 '17 at 14:53
0

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());
});
}
Dawid Fieluba
  • 1,271
  • 14
  • 34
  • That would be the most efficient and better solution. Thanks! But due to the current implementation with the other features as well, it would be unwise of me to do that. – Marios Louvaris Dec 05 '17 at 12:38
0

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;
}
Abdane
  • 137
  • 1
  • 12
0

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.

  • check my answer below, change the part where i specify the indentation of characters "%1$40s" to whatever you like, the number "40" is the spacing before the string, change it as you like. – Abdane Dec 05 '17 at 14:50
0

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());
       }
   }

}