-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReciept.java
More file actions
executable file
·91 lines (74 loc) · 1.82 KB
/
Reciept.java
File metadata and controls
executable file
·91 lines (74 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.ArrayList;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* Reciept class contains the methods to print the reciept in the terminal as
* well as a method to save the reciept to a .txt file.
*
* @author Vanessa Esli Tovar
*/
public class Reciept{
private float totalTax;
private double totalCost;
private ArrayList<String> output;
/**
* Constructor.
*/
public Reciept(){
this.output = new ArrayList<String>();
}
/**
* Adds a line (type String) to the ArrayList<String> output.
*
* @param s Line of type String that user wishes to add to the ArrayList output.
*/
public void addLine(String s){
this.output.add(s);
}
/**
* To string method.
*/
public String toString(){
StringBuffer buffer = new StringBuffer();
for (String s : this.output){
// append every string in this.output
buffer.append(s);
// add new line character after adding a string
buffer.append("\n");
}
return buffer.toString();
}
/**
* Prints the reciept to the terminal.
*/
public void printReciept(){
System.out.println("=====================");
System.out.println("OUTPUT:");
for (String s : this.output){
System.out.println(s);
}
System.out.println("=====================");
}
/**
* Saves the output in a .txt file.
*/
public void save(String outputFileName){
try {
String content = this.toString();
File file = new File(outputFileName);
// if the file doesn't already exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}