Skip to content

Commit 3377fe7

Browse files
committed
Added comments
1 parent d31b0d5 commit 3377fe7

2 files changed

Lines changed: 180 additions & 89 deletions

File tree

04-16-2021.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Day: Fri
22
Date: 04-16-2021
3-
Time: 04:57 pm
3+
Time: 09:11 pm
44

55
---------------------Receipt----------------------------
66
| Bread Name | | Price | | Quantity | | Total |
7-
Cheese Bread 20.0 4 80.0
8-
Emapanada 100.0 2 200.0
9-
Total: 280.0
7+
Cheese Bread 20.0 1 20.0
8+
Emapanada 100.0 5 500.0
9+
Total: 520.0
1010
----------------------------------------------------------
11-
Payment: 300.0
12-
Change: 20.0
11+
Payment: 1000.0
12+
Change: 480.0
1313

1414
Thank you for purchasing our product!
1515
Please come again!
Lines changed: 174 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,246 @@
1+
2+
//import Buffer Reader - Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
13
import java.io.BufferedReader;
4+
// import File - defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems
25
import java.io.File;
6+
//import FileNotFoundExeception - This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist.
37
import java.io.FileNotFoundException;
8+
//import File Reader - Convenience class for reading character files.
49
import java.io.FileReader;
10+
//import FileWriter - Convenience class for writing character files.
511
import java.io.FileWriter;
12+
//import IOException - Signals that an I/O exception of some sort has occurred.
613
import java.io.IOException;
14+
//import SimpleDateFormat - SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner.
715
import java.text.SimpleDateFormat;
16+
//import ArrayList - Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.
817
import java.util.ArrayList;
9-
import java.util.Date; //imports date
18+
//import Date - It allowed the interpretation of dates as year, month, day, hour, minute, and second values.
19+
import java.util.Date;
20+
//import InputMismatchException - Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
1021
import java.util.InputMismatchException;
11-
import java.util.Scanner; //imports scanner
22+
//import Scanner - A simple text scanner which can parse primitive types and strings using regular expressions.
23+
import java.util.Scanner;
1224

1325
public class ReceiptGenerator {
14-
26+
// Main method
1527
public static void main(String[] args) throws FileNotFoundException, IOException {
16-
Date myDate = new Date();
17-
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
18-
String myDateString = sdf.format(myDate);
19-
20-
SimpleDateFormat sdfDay = new SimpleDateFormat("E");
21-
String myDayString = sdfDay.format(myDate);
2228

23-
SimpleDateFormat sdfHour = new SimpleDateFormat("hh:mm aa");
24-
String myHourString = sdfHour.format(myDate);
25-
26-
ArrayList<String> itemList = new ArrayList<>();
27-
ArrayList<String> itemListToDisplay = new ArrayList<>();
28-
ArrayList<Double> priceList = new ArrayList<>();
29-
ArrayList<Double> currentPriceList = new ArrayList<>();
30-
ArrayList<Integer> quantityList = new ArrayList<>();
29+
Date myDate = new Date(); // Create object of Date
30+
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); // Set date to format and assign it to SimpleDateFormat
31+
String myDateString = sdf.format(myDate); // Format date to string and assign it to myDateString
32+
33+
SimpleDateFormat sdfDay = new SimpleDateFormat("E"); // Set day to format. E is for the name of the week
34+
String myDayString = sdfDay.format(myDate);// Format date to string and pass it to myDayString variable
35+
36+
SimpleDateFormat sdfHour = new SimpleDateFormat("hh:mm aa"); // Set hour format
37+
String myHourString = sdfHour.format(myDate); // Format hour to string and pass it to myHourString variable
38+
39+
ArrayList<String> itemList = new ArrayList<>(); // Create an ArrayList object for itemList
40+
ArrayList<String> itemListToDisplay = new ArrayList<>(); // Create an ArrayList object for itemListToDisplay
41+
ArrayList<Double> priceList = new ArrayList<>(); // Create an ArrayList object for priceList
42+
ArrayList<Double> currentPriceList = new ArrayList<>(); // Create an ArrayList for currentPriceList
43+
ArrayList<Integer> quantityList = new ArrayList<>(); // Create an ArrayList object for quantityList
44+
/*
45+
* Creates a buffering character-input stream that uses a default-sized input
46+
* buffer. And Creates a new FileReader, given the name of the file to read and
47+
* assign to a variable called iL
48+
*/
3149
BufferedReader iL = new BufferedReader(new FileReader("itemList.txt"));
50+
/*
51+
* Creates a buffering character-input stream that uses a default-sized input
52+
* buffer. And Creates a new FileReader, given the name of the file to read and
53+
* assign to a variable called pL
54+
*/
3255
BufferedReader pL = new BufferedReader(new FileReader("priceList.txt"));
3356

34-
String nameOfReceipt = myDateString + ".txt";
57+
String nameOfReceipt = myDateString + ".txt"; // Adds myDateString to a txt file and assign it to nameOfReceipt
58+
59+
/*
60+
* Creates a new File instance by converting the given pathname string into an
61+
* abstract pathname
62+
*/
3563
File file = new File(nameOfReceipt);
64+
// Constructs a FileWriter given the File to write
3665
FileWriter fw = new FileWriter(file);
3766

67+
/*
68+
* Constructs a new Scanner that produces values scanned from the specified
69+
* input stream
70+
*/
3871
Scanner s = new Scanner(System.in);
39-
char responseYN = 0;
40-
String lineiL, linepL;
41-
double payment, change;
42-
double total = 0;
43-
int ID;
44-
int quantity;
72+
char responseYN = 0; // Initialize variable(responseYn) in char
73+
String lineiL, linepL; // Initialize variables(LineiL, linepL) in String
74+
double payment, change; // Initialize variables(payment, change) in double
75+
double total = 0; // Initialize variable(total) in double
76+
int ID; // Initialize variable(ID) in int
77+
int quantity; // Initialize variable(quantity) in int
78+
// Prints this text for console UI
4579
System.out.println("==================== WELCOME TO ALYSHA'S BAKESHOP ====================\n");
4680

47-
System.out.println("Day: " + myDayString);
48-
System.out.println("Date: " + myDateString);
49-
System.out.println("Time: " + myHourString + "\n");
81+
System.out.println("Day: " + myDayString); // Prints the current Day
82+
System.out.println("Date: " + myDateString); // Prints the current Date
83+
System.out.println("Time: " + myHourString + "\n"); // Prints the current Hour
5084

51-
System.out.println("| ID/Bar Code |\t\t| BREAD NAME |\t\t\t| PRICE |");
85+
System.out.println("| ID/Bar Code |\t\t| BREAD NAME |\t\t\t| PRICE |"); // Prints for console UI
5286

87+
/*
88+
* While assign iL(BufferReader) to lineiL(String) and reads the line of
89+
* text(readLine) where it is not equal to null
90+
*/
5391
while ((lineiL = iL.readLine()) != null) {
92+
// Appends the specified element to the end of the priceList
5493
itemListToDisplay.add(lineiL);
5594
}
95+
/*
96+
* While assign pL(BufferReader) to linepL(String) and reads the line of
97+
* text(readLine) where it is not equal to null
98+
*/
5699
while ((linepL = pL.readLine()) != null) {
100+
/*
101+
* Appends the specified element to the end of the priceList and Double class
102+
* wraps a value of the primitive type double in an object.
103+
*/
57104
priceList.add(Double.parseDouble(linepL));
58105
}
59106

60-
iL.close();
61-
pL.close();
62-
try {
63-
for (int i = 0; i <= itemListToDisplay.size(); i++) {
64-
for (int e = 0; e <= priceList.size(); e++) {
65-
System.out.println("");
107+
iL.close(); // Closes the stream and releases any system resources associated with it.
108+
pL.close(); // Closes the stream and releases any system resources associated with it.
109+
try { // try to test this block of code
110+
111+
// 2d Array
112+
for (int i = 0; i <= itemListToDisplay.size(); i++) { // For loop the number of elements in the itemList
113+
for (int e = 0; e <= priceList.size(); e++) { // For Loop the number of elements in the priceList
114+
System.out.println(""); // Add a new line
115+
116+
// \t - for tab spacing
117+
// Prints and returns(.get) the element/item at the specified position of these
118+
// lists.
66119
System.out.println(e + "\t\t\t " + itemListToDisplay.get(e) + "\t\t\t " + priceList.get(e));
67120
}
68121
}
69-
} catch (IndexOutOfBoundsException a) {
70-
122+
} catch (IndexOutOfBoundsException a) { // Catch any exceptions if possible
71123
}
72-
// Loop for getting the order
73-
74-
do {
75-
try {
76-
System.out.print("ID/Bar Code: ");
77-
ID = s.nextInt();
78-
System.out.println(" ");
79124

80-
itemList.add(itemListToDisplay.get(ID));
81-
currentPriceList.add(priceList.get(ID));
82-
System.out.print("Quantity: ");
83-
quantity = s.nextInt();
84-
quantityList.add(quantity);
85-
System.out.println(" ");
86-
87-
System.out.print("Add more orders? Y/N: ");
88-
responseYN = s.next().charAt(0);
89-
System.out.println(" ");
90-
91-
} catch (InputMismatchException k) {
125+
// Loop for getting the order
126+
do { // This is a do while loop
127+
try { // Try to test this block of code
128+
System.out.print("ID/Bar Code: "); // Prints this text
129+
ID = s.nextInt(); // Read user input
130+
System.out.println(" "); // Add a new line
131+
132+
itemList.add(itemListToDisplay.get(ID)); /*
133+
* Appends(.add) the specify element/item to the end of itemList and
134+
* returns(.get) the specified position in itemListToDsiplay
135+
*/
136+
137+
currentPriceList.add(priceList.get(ID));/*
138+
* Appends(.add) the specify element/item to the end of currentPriceList
139+
* and returns(.get) the specified position in priceList
140+
*/
141+
System.out.print("Quantity: "); // Prints this text
142+
quantity = s.nextInt(); // Read user input
143+
quantityList.add(quantity); // Appends the specified element/item to the end of quantityList
144+
System.out.println(" "); // Add a new line
145+
146+
System.out.print("Add more orders? Y/N: "); // prints this text
147+
responseYN = s.next().charAt(0); // Read user input and return the char at the specific index in a string
148+
System.out.println(" "); // Add a new line
149+
150+
} catch (InputMismatchException k) { // Catch a input mismatch by a user
151+
// And perform this command after mismatch
92152
System.out.println("Invalid Input");
93153
System.out.println("Please try again...");
94-
} catch (IndexOutOfBoundsException e) {
154+
} catch (IndexOutOfBoundsException e) { // Catch a exception by a user
155+
// And perform this command after exception
95156
System.out.println("Invalid Input");
96157
System.out.println("Do you still want to continue? ");
97-
responseYN = s.next().charAt(0);
158+
responseYN = s.next().charAt(0); // Read user input and return the char at the specific index in a string
98159
}
160+
// Both y and Y are accepted in user input simply it's not case-sensitive
99161
} while (responseYN == 'y' || responseYN == 'Y');
100162

101-
try {
102-
iL.close();
163+
try { // Try to test this block of code
164+
iL.close(); // Closes the stream and releases any system resources associated with it.
165+
// Prints this text for console UI
103166
System.out.println("-------------------------Summary-------------------------");
104167

105-
System.out.println("Day: " + myDayString);
106-
System.out.println("Date: " + myDateString);
107-
System.out.println("Time: " + myHourString);
108-
System.out.println("\n");
168+
System.out.println("Day: " + myDayString); // Prints Day
169+
System.out.println("Date: " + myDateString); // Prints Date
170+
System.out.println("Time: " + myHourString); // Prints Time
171+
System.out.println("\n"); // This is a line break
109172

173+
// Prints this text for console UI
110174
System.out.println("| Bread Name |\t\t| Price |\t| Quantity|\t| Total |");
111175

112-
fw.write("Day: " + myDayString);
113-
fw.write("\nDate: " + myDateString);
114-
fw.write("\nTime: " + myHourString);
176+
fw.write("Day: " + myDayString); // Writes Day in a text file
177+
fw.write("\nDate: " + myDateString); // Writes Date in a text file
178+
fw.write("\nTime: " + myHourString); // Writes Day in a text file
115179

180+
// Prints this text for console UI
116181
fw.write("\n\n---------------------Receipt----------------------------\n");
182+
// Prints this text for console UI
117183
fw.write("| Bread Name |\t\t| Price |\t\t| Quantity |\t\t| Total |");
118184

119-
total = 0;
120-
for (int i = 0; i <= itemList.size(); i++) {
121-
for (int e = 0; e <= quantityList.size(); e++) {
185+
total = 0; // Initialize total to 0
186+
// 2d Array
187+
for (int i = 0; i <= itemList.size(); i++) { // for loop the number of elements in the itemList
188+
for (int e = 0; e <= quantityList.size(); e++) { // for loop the number of elements in the quantityList
189+
190+
/*
191+
* adds(+) the total of specified elements in the quantityList times(x) the
192+
* currentPriceList and assign(=) it to the total variable
193+
*/
122194
total += quantityList.get(e) * currentPriceList.get(e);
195+
196+
/*
197+
* Writes the string of itemList, currentPriceList, and quantityList x
198+
* currentPriceList to a txt file
199+
*/
123200
fw.write("\n" + itemList.get(e) + " \t\t\t\t\t" + currentPriceList.get(e) + " \t\t\t\t\t\t"
124201
+ quantityList.get(e) + " \t\t\t\t\t\t" + (quantityList.get(e) * currentPriceList.get(e)));
125202

126-
System.out.println(" ");
203+
System.out.println(" "); // adds a new line
204+
205+
/*
206+
* Prints the string of of itemList, currentPriceList, and quantityList x
207+
* currentPriceList to the console
208+
*/
127209
System.out.println(itemList.get(e) + "\t\t " + currentPriceList.get(e) + "\t\t " + quantityList.get(e)
128210
+ " \t\t " + (quantityList.get(e) * currentPriceList.get(e)));
129211
}
130212
}
131-
} catch (IndexOutOfBoundsException e) {
213+
} catch (IndexOutOfBoundsException e) { // Catch exception if possible
132214

133215
}
134216

135-
System.out.println("\nTotal: " + total);
217+
System.out.println("\nTotal: " + total); // Prints the total
136218

219+
// Prints for console UI
137220
System.out.println("-------------------------------------------------------");
138221

139-
System.out.print("Payment: ");
140-
payment = s.nextDouble();
141-
change = payment - total;
142-
System.out.println(" ");
222+
System.out.print("Payment: "); // Prints this text
223+
payment = s.nextDouble(); // Initialized payment to double
224+
change = payment - total; // Subtract payment to total and assign it to variable change
225+
System.out.println(" "); // Added new line
226+
227+
System.out.print("Change: " + change); // Prints change
228+
System.out.println("\nThank you for purchasing our product! "); // prints this text for Console UI
229+
System.out.println("Please come again! "); // Prints this text for Console UI
230+
fw.write("\nTotal: " + String.valueOf(total)); // Converts the value of total to String and writes it in a txt file
143231

144-
System.out.print("Change: " + change);
145-
System.out.println("\nThank you for purchasing our product! ");
146-
System.out.println("Please come again! ");
147-
fw.write("\nTotal: " + String.valueOf(total));
232+
// Writes this text for Console UI in a txt file
148233
fw.write("\n----------------------------------------------------------");
234+
235+
// Converts the value of payment to String and writes it in a txt file
149236
fw.write("\nPayment: " + String.valueOf(payment));
237+
238+
// Converts the value of change to String and writes it in a txt file
150239
fw.write("\nChange: " + String.valueOf(change));
151-
fw.write("\n\n\t\t\t\t\tThank you for purchasing our product!");
152-
fw.write("\n\t\t\t\t\t\t\t\t\t\tPlease come again!");
153-
fw.close();
240+
241+
// note: "\n" = new line & "\t" for tab spacing
242+
fw.write("\n\n\t\t\t\t\tThank you for purchasing our product!"); // Writes this text in a txt file
243+
fw.write("\n\t\t\t\t\t\t\t\t\t\tPlease come again!");// Writes this text in a txt file
244+
fw.close(); // Closed the writing in a txt file
154245
}
155246
}

0 commit comments

Comments
 (0)