我正在研究一个购物程序,我正在尝试查找库存产品的总价值。与每个项目相关的数据都存在于名为“ SHOP.txt”的文件的行中,例如:“ H; L; 10;€10,50; 83259875; YellowPaint”(即文件的第一行,其中所有产品以这种格式保存,用“;”分隔:部门,计量单位,数量,价格,代码,名称)。要计算总值,程序将从上述行中读取每个令牌,并乘以价钱。
我已经在文件中保存了大约10种产品,但是当我尝试编译代码时,我一直遇到错误,并且仅计算第一种产品的值。
public static void main(String[] args) throws IOException {
while (running) {
System.out.println("\nPress 0 to load the inventory. " + "\nPress 1 to save and close"
+ "\nPress 2 to add products to the inventory" + "\nPress 3 to find products"
+ "\nPress 4 for the total value of the inventory");
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("insert the name of the file to load");
Loading(in.next());
break;
case 1:
saveAndQuit();
break;
case 2:
addProduct();
break;
case 3:
inputCode();
break;
case 4:
inventoryValue();
break;
}
}
System.exit(0);
}
private static void inventoryValue() throws FileNotFoundException {
Scanner scanner = new Scanner(new File("SHOP.txt"));
scanner.useDelimiter(";|\n");
Product[] products = new Product[0];
while (scanner.hasNext()) {
String department = scanner.next();
String unityOfMeasure = scanner.next();
int quantity = scanner.nextInt();
double price = scanner.nextDouble();
String code = scanner.next();
String name = scanner.next();
Product newProduct = new Product(department, unityOfMeasure, quantity, price, code, name);
products = newProduct(products, newProduct);
double totalValue = Arrays.stream(products).mapToDouble(p -> p.quantity * p.price).sum();
System.out.println("Total Value: " + totalValue + "\n\n");
for (Product product : products) {
System.out.println(product);
}
}
}
private static Product[] newProduct(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 newProduct;
}
这是所要求的完整代码。 产品类别:
public class Product implements Serializable {
protected String department;
protected String unityOfMeasure;
protected int quantity;
protected double price;
protected String code;
protected String name;
private static NumberFormat formatter = new DecimalFormat("#0.00");
public Product(String dep, String uom, int qnt, double prz, String cod, String nm) {
reparto = dep;
unitaDiMisura = uom;
quantità = qnt;
prezzo = prz;
codice = cod;
nome = nm;
}
// setters
public void setDep(String rep) {
this.department = department;
}
public void setPrz(double prz) {
this.price = price;
}
public void setUdm(String udm) {
this.unityOfMeasure = unityOfMeasure;
}
public void setQnt(int qnt) {
this.quantity = quantity;
}
public void setCod(String cod) {
this.code = code;
}
public void setNm(String nm) {
this.name = name;
}
// getters
public String getDep() {
return department;
}
public String getUom() {
return unityOfMeasure;
}
public double getPrz() {
return price;
}
public int getQnt() {
return quantity;
}
public String getCod() {
return code;
}
public String getNm() {
return name;
}
public double getTotal() {
return quantity * price;
}
public void remove() {
this.quantity--;
}
public String toString() {
// ----quantity not less than 0 ----
if (quantity < 0) {
System.out.println(quantity = 0);
}
return String.format(department + ";" + unityOfMeasure + ";" + quantity + ";" + "€" + formatter.format(price) + ";"
+ code+ ";" + name + " \n");
}
}
商店类别:
public class Shop implements Serializable {
public List<Product> collection;
public Shop() {
collection = new ArrayList<Product>();
}
public void addProduct(Product product) {
collection.add(product);
}
public void sellProduct(String name) {
for (Product product : collection) {
if (name.equals(product.getNm())) {
if (product.getQnt() >= 0) {
prodotto.remove();
}
return;
}
}
}
public void duplicatedProduct(String code) {
for (Product product : collection) {
if (code.equals(product.getCod())) {
System.out.println("Error. Duplicated product");
}
return ;
}
}
@Override
public String toString() {
String total = "\n";
Iterator<Product> i = collection.iterator();
while (i.hasNext()) {
Product l = (Product) i.next();
total = total + l.toString();
}
return total;
}
}
主类:
public class Main {
static String fileName = null;
static Shop shp = new Shop();
static Scanner in = new Scanner(System.in);
static boolean running = true;
public static void main(String[] args) throws IOException {
while (running) {
System.out.println("\nPress 0 to load inventory. " + "\nPress 1 to save and quit"
+ "\nPress 2 to add product to the inventory" + "\nPress 3 to find a product"
+ "\nPress 4 for the total value of the inventory");
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Insert the name file to load.");
Loading(in.next());
break;
case 1:
saveAndQuit();
break;
case 2:
addProduct();
break;
case 3:
inputCode();
break;
case 4:
inventoryValue();
break;
}
}
System.exit(0);
}
private static void inventoryValue() throws FileNotFoundException {
Scanner scanner = new Scanner(new File("SHOP.txt"));
scanner.useDelimiter(";|\n");
Product[] products = new Product[0];
while (scanner.hasNext()) {
String department = scanner.next();
String unityOfMeasure = scanner.next();
int quantity = scanner.nextInt();
double price = scanner.nextDouble();
String code = scanner.next();
String name = scanner.next();
Product newProduct = new Product(department, unityOfMeasure, quantity, price, code, name);
products = newProduct(products, newProduct);
double totalValue = Arrays.stream(products).mapToDouble(p -> p.quantity * p.price).sum();
System.out.println("Total Value: " + totalValue + "\n\n");
for (Product product : products) {
System.out.println(product);
}
}
}
private static Product[] newProduct(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 newProduct;
}
private static void inputCode() throws IOException {
String code;
String line = null;
System.out.println("\nInsert code: ");
code = in.next();
try {
FileReader fileReader = new FileReader("SHOP.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
String[] token = line.split(";");
if ((";" + line + ";").contains((";" + code + ";"))) {
System.out.println(line);
}
}
bufferedReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Impossible to open the file ");
} catch (IOException ex) {
System.out.println("error opening the file ");
}
}
private static void addProduct() {
String department;
String unityOfMeasure;
int quantity;
double price;
String code;
String name;
System.out.println("\ninsert department: ");
department= in.next();
System.out.println("\ninsert unity of measure: ");
unityOfMeasure = in.next();
System.out.println("\ninserit quantity: ");
quantity = in.nextInt();
System.out.println("\ninsert price: ");
price = in.nextDouble();
System.out.println("\ninsert code: ");
code = in.next();
System.out.println("\ninsert name: \n");
name = in.next();
Product p = new Product(department, unityOfMeasure, quantity, price, code, name);
shp.addProduct(p);
}
private static void saveAndQuit() {
running = false;
PrintWriter printWriter = null;
try {
printWriter = new PrintWriter(new FileWriter("SHOP.txt", true));
printWriter.println(shp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (printWriter != null) {
printWriter.close();
}
}
}
private static void Loading(String name) throws IOException {
FileReader fr;
fr = new FileReader("SHOP.txt");
BufferedReader br;
br = new BufferedReader(fr);
String s;
while (true) {
s = br.readLine();
if (s == null)
break;
System.out.println(s);
}
br.close();
fr.close();
}
}
这是存储产品的txt文件(SHOP.txt)的内容:
H;L;10;10,50;83259875;YellowPaint
E;U;20;1,50;87678350;Lamp
H;L;10;10,50;83259891;BluePaint
H;L;10;10,00;83259892;RedPAint
H;U;30;12,00;98123742;Hammer
G;U;80;15,00;87589302;Seeds
G;U;3;130,00;17483921;Lawnmower
这是我希望解决的错误:
总值:105.0
H; L; 10;€10,50; 83259875; YellowPaint
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Main.inventoryValue(Main.java:68)
at Main.main(Main.java:47)
我该如何解决这个问题?
答案 0 :(得分:1)
在您说要存储价格两倍的文件中,它应该只是一个十进制值。您在其前面有一个“€”符号,并以“,”代替“。”。如果那里还有另一个符号,scanner.nextDouble()将不起作用。我建议您存储的价格中不包含货币符号和小数而不是逗号,并且只需在计划显示总额的位置前面附加“€”即可:
System.out.println("Total Value: €" + totalValue + "\n\n");