如何使用 java-ascii-table 创建 table 以显示 testObject 的字段值

How to create a table with java-ascii-table to display testObject's field values

问题:

How/what 我需要做什么才能让 java-ascii-table 在给定上下文中显示 testObject 的字段值?

背景:

这是我构建的一个小程序,用于测试我一直在研究的 'Displayer class'。在我作为 building/test Displayer 的应用程序中,我正在从 .csv 中读取数据,然后将其分配给 Product 的实例并将这些实例存储在 ArrayList 中(就像库存一样)。

在当前迭代中,我使用 java-ascii-table。这个小测试程序重现了我的基本需求:创建一个 table 显示 ArrayList 中对象的字段值(ID、名称、类别、价格)。

关于java-ascii-table的信息可以在这里找到:

https://code.google.com/p/java-ascii-table/

这里:

http://bethecoder.com/applications/products/asciiTable.action

这是我的代码基于的示例(这是第一个 link 上的第 5 个示例):

//Example5
//The following example shows rendering the ASCII Table from list of java beans.

        Employee stud = new Employee("Sriram", 2, "Chess", false, 987654321.21d);
        Employee stud2 = new Employee("Sudhakar", 29, "Painting", true, 123456789.12d);
        List<Employee> students = Arrays.asList(stud, stud2);

        IASCIITableAware asciiTableAware = 
                new CollectionASCIITableAware<Employee>(students, 
                                //properties to read
                                "name", "age", "married", "hobby", "salary"); 
        ASCIITable.getInstance().printTable(asciiTableAware);


        asciiTableAware = 
                new CollectionASCIITableAware<Employee>(students, 
                //properties to read
                Arrays.asList("name", "age", "married", "hobby", "salary"), 
                Arrays.asList("STUDENT_NAME", "HIS_AGE")); //custom headers

        ASCIITable.getInstance().printTable(asciiTableAware);

//It prints the following tables in the console.

+----------+-----+---------+----------+----------------+
|   NAME   | AGE | MARRIED |   HOBBY  |     SALARY     |
+----------+-----+---------+----------+----------------+
|   Sriram |   2 |   false |    Chess | 987,654,321.21 |
| Sudhakar |  29 |    true | Painting | 123,456,789.12 |
+----------+-----+---------+----------+----------------+

+--------------+---------+---------+----------+----------------+
| STUDENT_NAME | HIS_AGE | MARRIED |   HOBBY  |     SALARY     |
+--------------+---------+---------+----------+----------------+
|       Sriram |       2 |   false |    Chess | 987,654,321.21 |
|     Sudhakar |      29 |    true | Painting | 123,456,789.12 |
+--------------+---------+---------+----------+----------------+

我的代码:

主要

创建 ArrayListMaker 实例,调用 Displayz 中的方法

这是有问题的方法:

这只是显示一个'logo',不重要:

代码

package playGround2;


public class Main {

    public static void main(String[] arg) {
        ArrayListMaker arrayListMaker = new ArrayListMaker();
        Displayz.displayProduct2(arrayListMaker);
        Displayz.displaySurvivalStoreLogo();    
    }
}

ArrayListMaker

ArrayListMaker 的每个实例都有自己的 ArrayList、testObjectsList。 testObjectsList 是 TestObject 实例的 ArrayList。

package playGround2;

import java.util.ArrayList;

public class ArrayListMaker {
    public ArrayList<TestObject> testObjectsList;

    public ArrayListMaker() {
        testObjectsList = new ArrayList<TestObject>();

        testObjectsList.add( new TestObject("11","One", "This", "10"));
        testObjectsList.add( new TestObject("12", "Two", "That", "20"));
        testObjectsList.add( new TestObject("13", "Three", "Other", "30"));
        testObjectsList.add( new TestObject("14", "four", "something", "40"));
        testObjectsList.add( new TestObject("15", "five", "else", "50"));
        testObjectsList.add( new TestObject("16", "six", "over-there", "60"));
        testObjectsList.add( new TestObject("17", "seven", "Who", "70"));
        testObjectsList.add( new TestObject("18", "eight", "Why", "80"));
    }

    public ArrayList<TestObject> getTestObjects() {
        return this.testObjectsList;
    }
}

测试对象

POJO。字段:ID、名称、类别、价格...setters、getters 等...

package playGround2;

public class TestObject {
    private String ID;
    private String name;
    private String category;
    private String price;

/********************constructors********************/

    public TestObject() {
        // TODO Auto-generated constructor stub
    }

    public TestObject(String ID, String name, String category, String price) {
        this.setID(ID);
        this.setName(name);
        this.setCategory(category);
        this.setPrice(price);
    }

/********************get & set********************/

/**********ID**********/
    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }

/**********name**********/

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

/**********category**********/

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

/**********price**********/

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

Displayz

有显示数据的方法(& a 'logo')。

这是我需要创建 table 的方法。我已经根据上面的例子写了一些代码。但由于这对我来说是新的,我可能会偏离。

代码

package playGround2;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.bethecoder.ascii_table.ASCIITable;
import com.bethecoder.ascii_table.impl.*;
import com.bethecoder.ascii_table.spec.*;

public class Displayz {

    public static void displaySurvivalStoreLogo() {
        BufferedImage bufferedImage = new BufferedImage(144, 32, BufferedImage.TYPE_INT_RGB);

        Graphics graphics = bufferedImage.createGraphics();
        graphics.setFont(new Font("Dialog", Font.PLAIN, 24));

        Graphics2D graphics2d = (Graphics2D) graphics;
        graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics2d.drawString("SurvivalStore", 6, 24);

        try {
            ImageIO.write(bufferedImage, "png", new File("text.png"));
        } 
        catch (IOException e) {
            e.printStackTrace();
        }

        for (int y = 0; y < 32; y++) {
            StringBuilder stringBuilder = new StringBuilder();

            for (int x = 0; x < 144; x++) {
                stringBuilder.append(bufferedImage.getRGB(x, y) == -16777216 ? " " : bufferedImage.getRGB(x, y) == -1 ? "#" : "*");
            }

            if (stringBuilder.toString().trim().isEmpty()) {
                continue;
            }

            System.out.println(stringBuilder);
        }
    } //end of displaySurvivalStore

   public static void displayProduct2(ArrayListMaker arrayListMaker) {          
         IASCIITableAware asciiTableAware = new CollectionASCIITableAware<TestObject>(arrayListMaker.getTestObjects(),"ID", "name", "category", "price"); 
         ASCIITable.getInstance().printTable(asciiTableAware);

        // In this argument(Arrays.asList("name", "category", "price")), Arrays in underlined in red
        asciiTableAware = new CollectionASCIITableAware<TestObject>(arrayListMaker.getTestObjects(), Arrays.asList("ID", "name", "category", "price"), ASCIITable.getInstance().printTable(asciiTableAware);
   }
}

基本上,你的问题归结为这一行...

asciiTableAware = new CollectionASCIITableAware<TestObject>(arrayListMaker.getTestObjects(), Arrays.asList("ID", "name", "category", "price"), ASCIITable.getInstance().printTable(asciiTableAware);

List<?>, List<String>, void 没有构造函数(而且你遗漏了尾部 )...你不小心合并到代码行

应该更像...

asciiTableAware = new CollectionASCIITableAware<TestObject>(arrayListMaker.getTestObjects(), Arrays.asList("ID", "name", "category", "price"));
ASCIITable.getInstance().printTable(asciiTableAware);

但是等等,List<?>, List<String> 也没有构造函数?!?它需要最后一个参数,一个 List<String> 代表标题...

asciiTableAware = new CollectionASCIITableAware<TestObject>(testObjectsList, Arrays.asList("id", "name", "category", "price"), Arrays.asList("A ID", "First Name", "The Category", "Payup"));
ASCIITable.getInstance().printTable(asciiTableAware);

啊,现在可以编译了...

但是等一下,当我们 运行 它...

+------+-------+------------+-------+
|  ID  |  NAME |  CATEGORY  | PRICE |
+------+-------+------------+-------+
| null |   One |       This |    10 |
| null |   Two |       That |    20 |
| null | Three |      Other |    30 |
| null |  four |  something |    40 |
| null |  five |       else |    50 |
| null |   six | over-there |    60 |
| null | seven |        Who |    70 |
| null | eight |        Why |    80 |
+------+-------+------------+-------+

+------+------------+--------------+-------+
| A ID | FIRST NAME | THE CATEGORY | PAYUP |
+------+------------+--------------+-------+
| null |        One |         This |    10 |
| null |        Two |         That |    20 |
| null |      Three |        Other |    30 |
| null |       four |    something |    40 |
| null |       five |         else |    50 |
| null |        six |   over-there |    60 |
| null |      seven |          Who |    70 |
| null |      eight |          Why |    80 |
+------+------------+--------------+-------+

为什么 ID 我们得到 null??!?

API 遵循 Java Bean/coding 方法名称约定,Code Conventions for the Java TM Programming Language and JavaBeans,这意味着它实际上期望 IDId

因此,如果我们将 TestObject 的 set 和 get 方法更改为 setIdgetId 以及 运行,我们将得到

+----+-------+------------+-------+
| ID |  NAME |  CATEGORY  | PRICE |
+----+-------+------------+-------+
| 11 |   One |       This |    10 |
| 12 |   Two |       That |    20 |
| 13 | Three |      Other |    30 |
| 14 |  four |  something |    40 |
| 15 |  five |       else |    50 |
| 16 |   six | over-there |    60 |
| 17 | seven |        Who |    70 |
| 18 | eight |        Why |    80 |
+----+-------+------------+-------+

+------+------------+--------------+-------+
| A ID | FIRST NAME | THE CATEGORY | PAYUP |
+------+------------+--------------+-------+
|   11 |        One |         This |    10 |
|   12 |        Two |         That |    20 |
|   13 |      Three |        Other |    30 |
|   14 |       four |    something |    40 |
|   15 |       five |         else |    50 |
|   16 |        six |   over-there |    60 |
|   17 |      seven |          Who |    70 |
|   18 |      eight |          Why |    80 |
+------+------------+--------------+-------+