如何实现参考数据Java/DB

发布于 2024-10-19 06:53:14 字数 217 浏览 1 评论 0原文

我有一个表,其中包含一些需要合并到 Java 程序中的因素。起初,我考虑对数字进行硬编码,但尝试创建适合这些因素的数据结构似乎很痛苦。所以我想四处询问一下,看看将其作为数据库、平面文件或 java 中的参考数据来实现是否会更好。该数字每六个月改变一次,并用于数学计算。

想法?

因子列表

I've got a table with some factors that I need to incorporate into a Java program. At first I was thinking of hardcoding the number but it seems like a pain trying to create a data structure that will fit the factors. So I wanted to ask around and see if it would be better to implement this as reference data in a database, a flat file or in java. The number would change every six months and would be used for mathematical computations.

Thoughts?

Factor List

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-黛色若梦 2024-10-26 06:53:14

对于像这样缓慢变化的数据,我会使用外部配置文件。根据您的数据结构,CSV 似乎效果很好,并且业务用户可以轻松使用 Excel 进行编辑。

如果它会更频繁地更改,您需要以编程方式生成数据,或者您想提供用于编辑数据的 UI,您可以将其移动到数据库。

For slow-changing data like this, I would use an external config file. Based on the structure of your data, it seems that a CSV would work well, and would be easy for a business user to edit using Excel.

If it will change more often, you need to generate the data programmatically, or you want to provide a UI for editing the data, you could move it to a database.

○闲身 2024-10-26 06:53:14

无论您如何存储数据,您都必须创建一个数据结构来包含数据。但此类数据的数据结构不必很复杂。它只是带有属性的值列表。您不必将它们存储在复杂的表状结构中。

当将数据表示为单个列表时,从平面文本文件加载数据也非常容易。

public class DataTable {

    private List<Entry> table = new ArrayList<Entry>();

    public double getValue(Sex sex, MaritalStatus maritalStatus, AgeInterval ageInterval, Type type) {
        for (Entry entry : table) {
            if (entry.sex == sex && entry.maritalStatus == maritalStatus && entry.ageInterval == ageInterval && entry.type == type) {
                return entry.value;
            }
        }
        throw new IllegalArgumentException("Unknown value");
    }

    public void load(String filename) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
            String line;
            while ((line = reader.readLine()) != null) {
                StringTokenizer t = new StringTokenizer(line, ":");
                table.add(new Entry(
                        Sex.valueOf(t.nextToken()),
                        MaritalStatus.valueOf(t.nextToken()),
                        AgeInterval.valueOf(t.nextToken()),
                        Type.valueOf(t.nextToken()),
                        Double.valueOf(t.nextToken())));
            }
        } catch (IOException e) {
            throw new IllegalStateException("Failed to read the data file", e);
        }
    }

}

enum Sex {M, F}
enum MaritalStatus {SINGLE, MARRIED}
enum AgeInterval {I16_21, I22_35, I35_55, I55}
enum Type {GD, NGD} // Whatever this is ...

class Entry {
    Sex sex;
    MaritalStatus maritalStatus;
    AgeInterval ageInterval;
    Type type;
    double value;

    Entry(Sex sex, MaritalStatus maritalStatus, AgeInterval ageInterval, Type type, double value) {
        this.sex = sex;
        this.maritalStatus = maritalStatus;
        this.ageInterval = ageInterval;
        this.type = type;
        this.value = value;
    }
}

数据文件将如下所示:

M:SINGLE:I16_21:GD:1.10
F:SINGLE:I16_21:GD:1.20
...

You would have to create a data structure to contain the data regardless of how you store them. But the data structure for this kind of data does not have to be complex. It is just a list of values with attributes. You don't have to store them in a complex table-like structure.

Loading the data from a flat text file would also be quite easy when representing the data as a single list.

public class DataTable {

    private List<Entry> table = new ArrayList<Entry>();

    public double getValue(Sex sex, MaritalStatus maritalStatus, AgeInterval ageInterval, Type type) {
        for (Entry entry : table) {
            if (entry.sex == sex && entry.maritalStatus == maritalStatus && entry.ageInterval == ageInterval && entry.type == type) {
                return entry.value;
            }
        }
        throw new IllegalArgumentException("Unknown value");
    }

    public void load(String filename) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
            String line;
            while ((line = reader.readLine()) != null) {
                StringTokenizer t = new StringTokenizer(line, ":");
                table.add(new Entry(
                        Sex.valueOf(t.nextToken()),
                        MaritalStatus.valueOf(t.nextToken()),
                        AgeInterval.valueOf(t.nextToken()),
                        Type.valueOf(t.nextToken()),
                        Double.valueOf(t.nextToken())));
            }
        } catch (IOException e) {
            throw new IllegalStateException("Failed to read the data file", e);
        }
    }

}

enum Sex {M, F}
enum MaritalStatus {SINGLE, MARRIED}
enum AgeInterval {I16_21, I22_35, I35_55, I55}
enum Type {GD, NGD} // Whatever this is ...

class Entry {
    Sex sex;
    MaritalStatus maritalStatus;
    AgeInterval ageInterval;
    Type type;
    double value;

    Entry(Sex sex, MaritalStatus maritalStatus, AgeInterval ageInterval, Type type, double value) {
        this.sex = sex;
        this.maritalStatus = maritalStatus;
        this.ageInterval = ageInterval;
        this.type = type;
        this.value = value;
    }
}

The data file would look like this:

M:SINGLE:I16_21:GD:1.10
F:SINGLE:I16_21:GD:1.20
...
浅浅淡淡 2024-10-26 06:53:14

您可以将其表示为 XML,但这对于此类数字数据来说可能有点繁重。但是 XML 可以让您具有相当的描述性和自记录性。然后您可以轻松地将其解析为 Java(或您选择的其他语言)。

部分 XML 示例:

<dataset>
  <gd>
    <16to21>
      <single>
        <male>1.10</male>
        <female>1.20</female>
      </single>
      <married>
        <male>0.90</male>
        <female>0.80</female>
      </married>
    </16to21>
    ...
  </gd>
  <ngd>
    ...
  </ngd>

You could represent it as XML, but that might be a little heavy for such numeric data. But the XML would allow you to be fairly descriptive and self documenting. Then later you could easily parse this into Java(or another language of your choice).

Partial XML example:

<dataset>
  <gd>
    <16to21>
      <single>
        <male>1.10</male>
        <female>1.20</female>
      </single>
      <married>
        <male>0.90</male>
        <female>0.80</female>
      </married>
    </16to21>
    ...
  </gd>
  <ngd>
    ...
  </ngd>

梦屿孤独相伴 2024-10-26 06:53:14

分解字段的一种方法是性别、年龄、婚姻状态、GD_VS_NGD、表内的数据以及您使用此数据的时间段的一些标识符,除非您不需要保留数据记录。

One way that you could break up the fields is gender, age, marital_status, GD_VS_NGD, the data inside the table, and some identifier for the time period that you are using this data for unless you do not need to keep records of the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文