从文本文件中读取数据并创建对象

从文本文件中读取数据并创建对象

本文介绍了从文本文件中读取数据并创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助:
我正在使用Java进行超市模拟,但我遇到了一个问题,我有一个文本文件(Stock.txt),我在其上有所有的超市库存例如:

I need some help:I'm making a Supermarket simulation on Java, but I've got one problem, I have a text file (Stock.txt) where I have all the supermarket stock on it for example:


  • 0-面包巧克力蛋糕 - $ 12.5-250

  • 1-Meat-Premium牛排 - $ 2.6-120

  • 2-Seafood-Tuna - $ 1.2-14

  • ...

  • 0-Bakery-Chocolate Cake-$12.5-250
  • 1-Meat-Premium Steak-$2.6-120
  • 2-Seafood-Tuna - $1.2-14
  • ...

如果第一个数字是产品的id,则下一个是产品所属的部门,第三个是产品的名称,下一个是价格,以及最后一个数字是该股票有多少件产品。
我有这个类:

Where the first number is the "id" for the product, next is the department the product belongs, third is the name of the product, the next thing is the price, and the last number is how much pieces of the product the stock has.I have this class:

public class Product {
    protected String name;
    protected double price;
    protected String department;
    protected int id;
    protected int stock;
}

所以,基本上我需要做的是从文本中读取每一行文件和创建产品,即第一行产生这样的东西:

So, basically what I need to do is to read each line from the text file and create the product, i.e. for the first line make something like this:

Product product1 = new Product(0,"Bakery","Chocolate Cake", 12.5, 250);

然后将其添加到数组

Product[0] = product1;

对于文本文件中的所有内容,然后,在运行模拟时,每个客户都会购买随机数量的随机产品库存,因此库存数量将减少。最后,当模拟结束时,程序必须在同一文本文件中写入每个产品的修改数量。

For all the things that are in the text file, then, when running the simulation each costumer will buy a random quantity of random products in stock, so the stock number will decrease. Finally, when the simulation ends, the program must write in the same text file, the modify quantity of each product.

事情是,这可能太容易了,但我不知道如何做到这一点,因为用Java读取和写入文件一直是一个真正的问题。我从Java开始编程(我是初学者)。
我有一些使用BufferedReader和StringTokenizer类来阅读和创建对象问题的想法,但我无法弄明白该怎么做,而且我不知道如何进行覆盖问题。
我非常感谢你的帮助!

The thing is that maybe it's too easy to do but I have no idea of how to do this, because reading and writing a file in Java has been a real problem for me since I started programming in Java (I'm a beginner).I have some ideas of using the BufferedReader and the StringTokenizer classes for the reading and creating the object problems, but I can't figure it out how to do it, and I have no idea of how must I do the overwritting problem.I'd really appreciate your help!

哦!顺便说一句,我真的只需要使用数组,所以使用ArrayList或任何其他结构它甚至不是一个选择:(

Oh! By the way, I really need to use only the arrays, so using an ArrayList or any other structure it's not even a choice :(

推荐答案

这对于 Scanner 来说是一个很好的工作来读取数据。至于无法使用 ArrayList 你必须自己动态重新分配数组。

This is a good job for a Scanner to read in the data. As far as not being able to use collections like ArrayList you'll have to dynamically reallocate an array yourself.

尝试以下方法:

public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("Stock.txt"));
    input.useDelimiter("-|\n");

    Product[] products = new Product[0];
    while(input.hasNext()) {
        int id = input.nextInt();
        String department = input.next();
        String name = input.next();
        double price = Double.valueOf(input.next().substring(1));
        int stock = input.nextInt();

        Product newProduct = new Product(name, price, department, id, stock);
        products = addProduct(products, newProduct);
    }

    for (Product product : products) {
        System.out.println(product);
    }
}

private static Product[] addProduct(Product[] products, Product productToAdd) {
    Product[] newProducts = new Product[products.length + 1];
    System.arraycopy(products, 0, newProducts, 0, products.length);
    newProducts[newProducts.length - 1] = productToAdd;

    return newProducts;
}

public static class Product {
    protected String name;
    protected double price;
    protected String department;
    protected int id;
    protected int stock;

    private static NumberFormat formatter = new DecimalFormat("#0.00");

    public Product(String n, double p, String d, int i, int s) {
        name = n;
        price = p;
        department = d;
        id = i;
        stock = s;
    }

    @Override
    public String toString() {
        return String.format("ID: %d\r\nDepartment: %s\r\nName: %s\r\nPrice: %s\r\nStock: %d\r\n",
                id, department, name, formatter.format(price), stock);
    }
}

结果:

ID: 0
Department: Bakery
Name: Chocolate Cake
Price: 12.50
Stock: 250

ID: 1
Department: Meat
Name: Premium Steak
Price: 2.60
Stock: 120

ID: 2
Department: Seafood
Name: Tuna
Price: 1.20
Stock: 14

这篇关于从文本文件中读取数据并创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 00:12