简体   繁体   中英

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

I'm a new student in computer science and i have to create an inventory program that read informations about products from a txt file in this format: Department;unit of measure;quantity;price;unique code;name; (example: E;U;20;1,50;87678350;Lamp) .Subsequently i have to : -calculate the total value of the stock -selling of a product -insertion of a product -searching of a product by unique code. If there are lines with the same unique code, the program will report an error.

I managed to read the lines in the txt file but i dont have any idea on how to calculate the total value of the stock from it.

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

My question is: How can i read the value of the product in the file and sum it with the prices of the other products ? This mean that i need to do the same with the unique code to find a specific product. Thank you very much in advance for your assistance.

You've already read your products into an array, you need to loop through the array and add together price * quantity for each one.

For example...

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

This will iterate over each product and map each product to a double (which is the quantity times the price for that product) it then sums all of those results.

The correct method would be to have Getters and Setters in your Product Class.

As you can see in your code, you just pass in your variables and initiate them in your constructor, but initializing them using getters and setters is better as it is a good programming practice and adds more functionality to your code.

Example

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

}

With getters and setters in your Products Class, you can:

  1. Create a method that calculates the sum of all Products in your ArrayList
  2. Search for a particular product in your array using your unique identifier
  3. Sort your Arraylist by a variable, be it department, price etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM