如果我希望能够在文件“ Cart.txt”中显示“总销售额”,该怎么办?

时间:2018-11-15 18:06:26

标签: java file

我希望Cart.txt文件能够写入文件本身中的所有销售额。该文件现在只显示:

3,2,Shoes
3,4,Shirt
2,5,Car

这是当前输出:

run:
Enter how many items you are buying
3
Enter the items you are buying, structured as followed 
Quantity,Price,Item Name:
3,2,Shoes
3,4,Shirt
2,5,Car
Those values were written to Cart.txt
Sold 3 of Shoes at $2.00 each. 
Sold 3 of Shirt at $4.00 each. 
Sold 2 of Car at $5.00 each. 
Total sales: $28.00

这是代码本身:

package shop;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class Shop 
{

public static void main(String[] args)
{
    String fileName = "Cart.txt";
    PrintWriter outputStream = null;

    try
    {
        outputStream= new PrintWriter (fileName);
    }
    catch (FileNotFoundException e)
    {
        System.out.println("Error opening the file "+ fileName);
        System.exit(0);
    }

    System.out.println("Enter how many items you are buying");
    Scanner keyboard = new Scanner (System.in);
    Scanner intinput = new Scanner (System.in);
    int input = intinput.nextInt();

    System.out.println("Enter the items you are buying, structured as followed"
            + " \nQuantity,Price,Item Name:");


    for(int count=1; count<=input; count++)
    {
        String line = keyboard.nextLine();
        outputStream.println(line);

    }

    outputStream.close();
    System.out.println("Those values were written to "+ fileName);

    try
    {
        Scanner inputStream = new Scanner(new File(fileName));
        String line = inputStream.toString();
        double total = 0;
    for(int count=1; count<=input; count++)
    {

            line = inputStream.nextLine();
            String[] ary = line.split (",");
            int quantity = Integer.parseInt (ary[0]);
            double price = Double.parseDouble(ary[1]);
            String description = ary[2];
            System.out.printf("Sold %d of %s at $%1.2f each. \n",
                    quantity, description, price);
            total += quantity * price;  

    }

        System.out.printf("Total sales: $%1.2f\n", total);
        inputStream.close();

    }
    catch (FileNotFoundException e)
    {
        System.out.println("Cannot find file " + fileName);
    }
    catch (IOException e)
    {
        System.out.println("Problem with input file " + fileName);
    }

}
}

2 个答案:

答案 0 :(得分:0)

执行以下操作:

  1. 计算总数后写入文件: Option Explicit Sub copyRangeBetweenLookUpValues() Dim lookUpValue1 As String Dim lookUpValue2 As String Dim lookUpValue1R As Long, lookUpValue1C As Long, lookUpValue2R As Long, lookUpValue2C As Long 'set your lookup values here lookUpValue1 = "Sponsor de l'Indice Marché Site Internet" lookUpValue2 = "DEFINITIONS APPLICABLES AUX(EVENTUELS), AU" 'find row and column of first value lookUpValue1R = Cells.Find(lookUpValue1).Row lookUpValue1C = Cells.Find(lookUpValue1).Column 'find row and column of second value lookUpValue2R = Cells.Find(lookUpValue2).Row lookUpValue2C = Cells.Find(lookUpValue2).Column 'copy range between these 2 values (but without values so first Row+1, second Row -1) Range(Cells(lookUpValue1R + 1, lookUpValue1C), Cells(lookUpValue2R - 1, lookUpValue2C)).Copy 'paste Range("B1").PasteSpecial xlPasteAll End Sub
  2. 将总数写入文件后,关闭outputStream.println(total);

编辑:

在此代码块中进行以下更改:

outputStream

在这里,我们计算所有条目的总数,然后将其写入文件一次。

输出

  

3,2,鞋子

     

3,4,衬衫

     

2,5,Car

     

总销售额:28.0美元

答案 1 :(得分:0)

我将保持运行总计,然后将其添加到最后:

Double total = 0; 

for(int count=1; count<=input; count++)
        {
            String line = keyboard.nextLine();
            outputStream.println(line);
            String[] arr= line.split(",");
            total += (Double.parseDouble(arr[0]) * Double.parseDouble(arr[1]));
        }
outputStream.println("Total: " + total);