gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:20:42 27 4
gpt4 key购买 nike

问题:

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

背景:

这是我构建的一个小程序,用于测试我一直在开发的“显示类”。在我正在构建/测试 Displayer 的应用程序中,我从 .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

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

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

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

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

测试对象

POJO。字段: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

具有显示数据的方法(和“ Logo ”)。

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

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

最佳答案

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

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 遵循方法名称的 Java Bean/编码约定 Code Conventions for the Java TM Programming LanguageJavaBeans ,这意味着它实际上期待 ID成为Id

所以如果我们改变 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 |
+------+------------+--------------+-------+

关于java - 如何使用 java-ascii-table 创建表来显示 testObject 的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31257729/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com