提问者:小点点

如何使用javaascii表创建表以显示testObject的字段值


问题:

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

背景:

这是我建立的一个小程序,用来测试我正在开发的“显示器类”。在我构建/测试Displayer的应用程序中,我从. csv中读取数据,然后将其分配给产品实例

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

有关java-ascii-table的信息可以在此处找到:

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

在这里:

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

这是我基于代码的示例(这是第一个链接上的第五个示例):

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

这只是显示一个“标志”,并不重要:

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

测试对象

波乔。字段:ID、名称、类别、价格...二传手、吸盘手等...

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

显示z

具有显示数据的方法(

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

    < li>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个答案

匿名用户

本质上,你的问题归结为这条线…

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

List没有构造函数

它应该更像是...

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

但是等等,< code >列表没有构造函数

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遵循方法名称的JavaBean/编码约定、JavaTM编程语言和JavaBeans的代码约定,这意味着它实际上期望IDId

因此,如果我们将<code>TestObject</code>的set和get方法更改为<code>setId</code<和<code>getId</code>并再次运行它,我们将得到

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