如何在java中格式化输出控制台列?

时间:2018-05-02 03:14:59

标签: java format invoice

我制作了一个创建发票的程序,但当它出现数千个时,输出并不整齐,并且会破坏一切。我如何解决这个问题,以便程序的列与这样的数字更加一致?这是我用来创建程序的代码。如果有人可以提供帮助,那将会非常谨慎。

这是主要方法的那个......

public class InvoicePrinter
{
public static void main(String[] args)
  {

  Address samsAddress=new Address("Sam's Small Appliances", "100 Main 
  Street", "Anytown", "CA", "98765");
  Invoice samsInvoice =new Invoice(samsAddress);
  samsInvoice.add(new Product("Toaster", 29.95),3);
  samsInvoice.add(new Product("Hair Dryer", 24.95),1);      
  samsInvoice.add(new Product("Car Vacuum",19.99),2);
  samsInvoice.add(new Product("Nano Parts",100000),1);
  samsInvoice.addSimple(new Product("Shipping",5.00));

  System.out.println(samsInvoice.format());
  }
}

这些是该计划工作所需的其他程序

import java.util.ArrayList;

public class Invoice
{
  public Invoice(Address anAddress)
  {
  items=new ArrayList<LineItem>();
  billingAddress=anAddress;
  simpleItems= new ArrayList<SimpleLineItem>();
}

public void addSimple(Product aProduct)
{
  SimpleLineItem anItem= new SimpleLineItem(aProduct);
  simpleItems.add(anItem);
}


public void add(Product aProduct, int quantity)
{
  LineItem anItem=new LineItem(aProduct,quantity);
  items.add(anItem);
}   

 public String format()
 {
  String r="                                                 I N V O I C E\n\n"+billingAddress.format()+String.format("\n\n%-30s%8s%5s%8s\n","Description", "Price","Qty","Total");

  for(LineItem i:items)
  {
     r=r+i.format()+"\n";
  }

  for(SimpleLineItem j:simpleItems)
  {
   r=r+j.format() + "\n";
  }

  r = r + String.format("\nAMOUNT DUE: $%8.2f", getAmountDue());
  return r;
  }

 public double getAmountDue()
 {
  double amountDue = 0;
  for (LineItem i : items)
  {
      amountDue = amountDue + i.getTotalPrice();
  }
  for(SimpleLineItem j:simpleItems)
  {
     amountDue = amountDue + j.getPrice();
  }
 return amountDue; 
  }     


 private Address billingAddress;
 private ArrayList<LineItem> items;
 private ArrayList<SimpleLineItem> simpleItems;
}

少数

public class LineItem
{
 public LineItem(Product aProduct, int aQuantity)
 {
  theProduct = aProduct;
  quantity = aQuantity;
 }

 public double getTotalPrice()
 {
  return theProduct.getPrice() *quantity;
 }

 public String format()
 {
  return String.format("%'-30s%'8.2f%'5d%'8.2f", theProduct.getDescription(),theProduct.getPrice(),quantity,getTotalPrice());
 }

 private int quantity;
 private Product theProduct;
}

另一个

public class SimpleLineItem
{
  public SimpleLineItem(Product aProduct)
  {
     theProduct=aProduct;
  }

  public double getPrice()
  {
     return theProduct.getPrice();   
  }

  public String format()
  {
    return String.format("%-30s" +"             " + "%8.2f",
    theProduct.getDescription(), theProduct.getPrice());
  }

  private Product theProduct;
  }  

另外两个

  public class Product
  {  
   public Product(String aDescription,double aPrice)
   {
     description = aDescription;
     price = aPrice;
   }

   public String getDescription()
   {
     return description;
   }

   public double getPrice()
   {
     return price;
   }

   private String description;
   private double price;
   }

最后一个

   public class Address
   {
     public Address(String aName, String aStreet, String aCity, String 
     aState,String aZip)
    {
       name = aName;
       street = aStreet;
       city = aCity;
       state = aState;
       zip = aZip;
    }

   public String format()
   {
     return name + "\n" + street + "\n" + city + ", " + state + " " + zip;
   }

   private String name;
   private String street;
   private String city;
   private String state;
   private String zip;
  }

2 个答案:

答案 0 :(得分:1)

也许你可以在System.out.format和DecimalFormat类上查看Oracle的javadocs

Formatting Numeric Print Output

答案 1 :(得分:0)

所以基本上这种情况发生在您打印出所有内容之前无法确定数字列的总长度时。为此,您需要将数字列的长度设置为最长的数字或您的案例价格长度,并确定所有数字的合理性。因此,您需要将所有数字添加到数组中并循环遍历它们以查找最长的数字。