如何使用java-ascii-table创建一个表来显示testObject的字段值

时间:2015-07-07 00:09:55

标签: java arraylist console ascii

问题:

如何/如何让java-ascii-table在给定的上下文中显示testObject的字段值?

背景

这是我为测试一个'显示器类而建立的一个小程序。我一直在努力。在我正在构建/测试显示器的应用程序中,我正在从.csv读取数据,然后将其分配给Product&的实例。将这些实例存储在ArrayList中(它就像一个库存)。

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

有关java-ascii-table的信息,请访问:

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

在这里:

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

这是我的代码基于(它是第一个链接上的第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中的方法

这是有问题的方法:

  • Displayz.displayProduct2(arrayListMaker);

这只会显示一个&#39;徽标&#39;,并不重要:

  • Displayz.displaySurvivalStoreLogo();

代码

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

的TestObject

POJO。字段:ID,名称,catagory,价格... setter,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

有显示数据的方法(&amp; a&#39; logo&#39;)。

这是我创建表格所需的方法。我已根据上面的例子编写了一些代码。但由于这对我来说是新的,我可能会离开。

  • displayProduct2()

代码

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

1 个答案:

答案 0 :(得分:1)

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

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 |
+------+------------+--------------+-------+

为什么我们null获得ID???

API遵循方法名称Code Conventions for the Java TM Programming LanguageJavaBeans的Java Bean /编码约定,这意味着它实际上期望IDId

因此,如果我们更改集合并将TestObject的方法设为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 |
+------+------------+--------------+-------+