Search code examples
javafileappendclass-variablesprintstream

How do I instantiate a PrintStream that all methods can use, and will append to the same file?


I've been searching around for some time, and can't find a way to do this that I understand. The answers I get involve a few lines of code, and different printer objects (FileOutputStream, FileWriter, bufferedwriter, etc) The PrintStream ps is used in two other methods in the program. It works fine when I instantiate in each method, but then I can't append to the file, it just overwrites it. Is there a simple way to do this with a class variable?

import java.util.Scanner;
import java.io.PrintStream;

public class Main {

    public static void main(String[] args) throws Exception {

    Scanner sc = new Scanner(System.in);
    PrintStream ps = new PrintStream("brooklynCarpet.txt");


    double length, width, price, discount, area, carpetPrice, labor, 
    laborCost, installPrice, discountAmt, subTotal, tax, grandTotal;

            System.out.println("Length of room (feet)?");
            length = sc.nextDouble();

            System.out.println("Width of room (feet)?");
            width = sc.nextDouble();

            System.out.println("Customer discount (percent)?");
            discount = sc.nextDouble();

            System.out.println("Cost per square foot (xxx.xx)?");
            price = sc.nextDouble();

    area = length * width;
    carpetPrice = calcPrice(area, price);
    laborCost = calcLabor(area);
    installPrice = calcInstall(laborCost, carpetPrice);
    discountAmt = calcDisc(installPrice, discount);
    subTotal = calcSub(installPrice, discountAmt);
    tax = calcTax(subTotal);
    grandTotal = subTotal + tax;
    labor = 0.35;

    printHeader(length, width, area);

    printBill(price, carpetPrice, labor, laborCost, installPrice,
    discount, discountAmt, subTotal, tax, grandTotal);

}


// first function calculates carpet price.

public static double calcPrice(double area, double price) {

double carpetPrice;

carpetPrice = area * price;

return carpetPrice;

}

// second function calculates labor cost

public static double calcLabor(double area) {

    double labor = 0.35;

    double laborCost = area * labor;

    return laborCost;
}

// third function calculates installed price ie. carpet + labor

public static double calcInstall(double carpetPrice, double laborCost) {

    double installedPrice = carpetPrice + laborCost;

    return installedPrice;
}

// fourth function calculates discount ie % 0.01 * installed

public static double calcDisc(double installedPrice, double discount) {

    double discountAmt = installedPrice * (discount * 0.01);

    return discountAmt;
}

// fifth function calculates subtotal ie installed - discountamt

public static double calcSub(double installedPrice, double discountAmt) {

    double subTotal = installedPrice - discountAmt;

    return subTotal;
}

// sixth function calculates tax. subTotal * 0.085

public static double calcTax(double subTotal) {

    double taxRate = 0.085;

    double tax = subTotal * taxRate;

    return tax;
}

// want to print with the next function

public static void printHeader(double length, double width, double area) throws Exception{

    ps.printf("CARPET STORE\n");
    ps.printf("\n\n\t\t\tMEASUREMENT");
    ps.printf("\n\nLength\t\t\t\t %.2f feet", length);
    ps.printf("\n\nWidth\t\t\t\t %.2f feet", width);
    ps.printf("\n\nArea\t\t\t\t %.2f square feet", area);

}

// and this one as well

public static void printBill(double price, double carpetPrice, double labor
, double laborCost, double installPrice, double discount, 
double discountAmt, double subTotal, double tax, double grandTotal) throws Exception{

    ps.printf("\t\t\t\t\t  CHARGES\n\nDESCRIPTION\t\t\tCOST/SQ.FT.\t\t\tCHARGE/ROOM");
    ps.printf("\nCARPET\t\t\t\t\t" + price + "\t\t\t\t\t" + carpetPrice);
    ps.printf("\nLabor\t\t\t\t\t" + "%.2f" + "\t\t\t   $" + "%.2f", labor, laborCost);
    ps.printf("\n\t\t\t\t\t\t\t\t\t\t----------");
    ps.printf("\nINSTALLED PRICE\t\t   " + "$%.2f", installPrice);
    ps.printf("\nDiscount\t\t\t\t" + "%.2f" + " %%" + "\t\t\t\t%.2f" ,discount, discountAmt);
    ps.printf("\nSUBTOTAL\t\t\t\t\t\t\t\t\t" + "%.2f\n", subTotal);
    ps.printf("\nTax\t\t\t\t\t\t\t\t\t\t    " + "%.2f\n", tax);
    ps.printf("\nTotal\t\t\t\t\t\t\t\t\t   $" + "%.2f\n", grandTotal);

}

    }

Solution

  • Just declare ps as a static member of the Main class, instead of as a local variable of the main method:

    public class Main {
        private static PrintStream ps;
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            try { 
                // Don't declare `ps` here; just initialize it...
                ps = new PrintStream("brooklynCarpet.txt"); 
                // remaining logic of `main` below...
            } catch (FileNotFoundException fnf){
                System.out.println("Unexpected exception: " + fnf.toString());
            }
        }
        // ...
     }
    

    Now any method in the Main class can access 'ps`.