我正在做一个简短的项目,它是一种电子商店库存。这是文本文件中商品的编号,描述,数量和价格的列表:
65321,Tablet,54,150.00
91524,Monitors,24,125.50
25013,Printers,30,65.75
32841,Router,67,90.15
53214,Modem,50,50.50
26442,Hard Drive,14,40.89
30224,Power Supplies,44,125.44
41557,CPU,39,149.54
71561,Headphones,40,75.24
这是我到目前为止所做的:
InventoryCalculate.java
ArrayList<BookItem> inventory = new ArrayList<>();
BufferedReader inFile = new BufferedReader(new FileReader("C:\\Users\\Documents\\Data\\stock.txt"));
String line;
line = input.nextLine();
String[] tokens = line.split(",");
int itemNumber = Integer.parseInt(tokens[0]);
String description = tokens[1];
int quantity = Integer.parseInt(tokens[2]);
double unitCost = Double.parseDouble(tokens[3]);
System.out.println("Inventory Item");
System.out.println("Number Description Quantity Unit Price Value");
Inventorystock.java
public class InventoryItem {
private int itemNumber;
private String description;
private int quantity;
private double unitCost;
public InventoryItem(int itemNumber, String description, int quantity, double unitCost) {
this.itemNumber = itemNumber;
this.description = description;
this.quantity = quantity;
this.unitCost = unitCost;
}
public int getItemNumber() {
return itemNumber;
}
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getUnitCost() {
return unitCost;
}
public void setUnitCost(double unitCost) {
this.unitCost = unitCost;
} }
我在弄清楚如何获取每个项目的总价值时遇到了麻烦。在这里我需要看到的结果是这样的
预期输出:
Number Description Quantity Unit Price Value
65321 Tablets 54 150.00 8,100.00
91524 Monitors 24 125.50 3,012.00
25013 Printers 30 65.75 1,972.50
32841 Routers 67 90.15 6,040.05
53214 Modems 50 50.50 2,525.00
26442 Hard Drives 14 40.89 572.46
30224 Power Supplies 44 125.44 5,519.36
41557 CPU 39 149.54 5,832.06
71561 Headphones 40 75.24 3,009.60
答案 0 :(得分:0)
创建一个方法,该方法将物料的总数量乘以单位成本,然后返回结果。将以下代码添加到您的InventoryItem类。
public double getTotalValueOfItem()
{
return unitCost * quantity;
}
答案 1 :(得分:0)
在InventoryItem类中添加另一个属性作为totalValue,并使用逻辑添加setter,getter方法。在显示明细的方法中,保持总价值计算的最佳方法。
public class InventoryItem {
private int itemNumber;
private String description;
private int quantity;
private double unitCost;
private double totalValue;
public InventoryItem(int itemNumber, String description, int quantity, double unitCost) {
this.itemNumber = itemNumber;
this.description = description;
this.quantity = quantity;
this.unitCost = unitCost;
}
public int getItemNumber() {
return itemNumber;
}
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getUnitCost() {
return unitCost;
}
public void setUnitCost(double unitCost) {
this.unitCost = unitCost;
}
public double getTotalValue() {
return totalValue;
}
public void setTotalValue(int quantity, double unitCost) {
this.totalValue = (double) quantity * unitCost;
}
}
您可以添加此方法以打印项目详细信息
public void printDetails() {
System.out.println("Number\tDescription\tQuantity\tUnit Price\tValue");
List < InventoryItem > inventoryItemsList = new ArrayList < InventoryItem > ();
inventoryItemsList.add(new InventoryItem(1, "description 1", 2, 50.0));
inventoryItemsList.add(new InventoryItem(2, "description 2", 3, 40.0));
for (InventoryItem item: inventoryItemsList) {
System.out.println(item.itemNumber + "\t" + item.description + "\t" + item.quantity + "\t" + item.unitCost + "\t" + item.totalValue);
}
}
您可以将清单项目的详细信息存储为字符串或列表,如printDetails方法所示。希望这种“ printDetails”方法能够解决您的问题。如果您真的想将项目详细信息存储为字符串,则可以将所有项目详细信息存储到字符串数组中。
确保以以下方式导入所需的库和模型类。
import java.util.List;
import java.util.ArrayList;
import <package>.InventoryItem;