[英]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;我是计算机科学专业的新生,我必须创建一个库存程序,该程序从以下格式的 txt 文件中读取有关产品的信息:部门;计量单位;数量;价格;唯一代码;名称; (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.
(例如:E;U;20;1,50;87678350;Lamp)。随后我必须: - 计算股票的总价值 - 产品的销售 - 产品的插入 - 通过唯一代码搜索产品。 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.我设法阅读了 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);
}
}
}
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.您已经将您的产品读入一个数组,您需要遍历该数组并将每个产品的
price * quantity
相加。
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.正确的方法是在您的产品类中使用 Getter 和 Setter。
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.正如您在代码中看到的那样,您只需传入变量并在构造函数中初始化它们,但使用 getter 和 setter 初始化它们更好,因为这是一种良好的编程实践,并为您的代码添加了更多功能。
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:使用 Products 类中的 getter 和 setter,您可以:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.