簡體   English   中英

JAVA:從txt文件中讀取行並創建一個對象

[英]JAVA : reading lines from a txt file and create an object

我是計算機科學專業的新生,我必須創建一個庫存程序,該程序從以下格式的 txt 文件中讀取有關產品的信息:部門;計量單位;數量;價格;唯一代碼;名稱; (例如:E;U;20;1,50;87678350;Lamp)。隨后我必須: - 計算股票的總價值 - 產品的銷售 - 產品的插入 - 通過唯一代碼搜索產品。 如果有相同唯一碼的行,程序會報錯。

我設法閱讀了 txt 文件中的行,但我不知道如何從中計算股票的總價值。

public class Emporium{
public static void main (String[]args) throws IOException  {
Scanner input = new Scanner(new File("EMPORIUM.txt"));
    input.useDelimiter(";|\n"); 
    Product[] products = new Product[0];
    while(input.hasNext()) {
        String department = input.next();
        String unit=input.next();
        int quantity=input.nextInt();
        double price=input.nextDouble();
        long code=input.nextLong();
        String name=input.next();

        Product newProduct = new Product(department,unit,quantity,price,code,name);   
        products= addProducts(products,newProducts);
    }
    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 department;
protected String unit;
protected int quantity;
protected double price;
protected long code;
protected String name;

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

public Product(String dep,String uom,int qnt,double prz,long cod,String nm) {
    department=dep;
    unit=uom;
    quantity=qnt;
    price=prz;
    code=cod;
    name=nm;

}
@Override
public String toString() {
    return  String.format(department+";"+unity+";"+quantity+";"+formatter.format(price)+";"+code+";"+name);
}   
}   
}

我的問題是:如何讀取文件中產品的價值並將其與其他產品的價格相加? 這意味着我需要對唯一代碼執行相同操作才能找到特定產品。 非常感謝您的幫助。

您已經將您的產品讀入一個數組,您需要遍歷該數組並將每個產品的price * quantity相加。

例如...

double totalValue = Arrays.stream(products).mapToDouble(p -> p.quantity * p.price).sum();

這將迭代每個產品並將每個產品映射到一個雙精度值(該產品的數量乘以價格),然后將所有這些結果相加。

正確的方法是在您的產品類中使用 Getter 和 Setter。

正如您在代碼中看到的那樣,您只需傳入變量並在構造函數中初始化它們,但使用 getter 和 setter 初始化它們更好,因為這是一種良好的編程實踐,並為您的代碼添加了更多功能。

例子

public static class Product {

    protected String department;
    protected String unit;
    protected int quantity;
    protected double price;
    protected long code;
    protected String name;

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

    public Product(String dep,String uom,int qnt,double prz,long cod,String nm) {

    setDep(dep);
    setUom(uom);
    setQnt(qnt);
    setPrz(prz);
    setCod(cod);
    setNm(nm);
    }

    public void setPrz(double prz){
       this.price = price;
    }

    //Other setters

    public double getPrz(){
       return price;
    }

    //Other getters
    @Override
    public String toString() {
        return  String.format(department+";"+unity+";"+quantity+";"+formatter.format(price)+";"+code+";"+name);
    } 

}

使用 Products 類中的 getter 和 setter,您可以:

  1. 創建一個方法來計算 ArrayList 中所有產品的總和
  2. 使用您的唯一標識符在您的陣列中搜索特定產品
  3. 按變量對您的 Arraylist 進行排序,例如部門、價格等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM