|
| 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. |
1 | 3 | import java.io.BufferedReader; |
| 4 | +// import File - defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems |
2 | 5 | 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. |
3 | 7 | import java.io.FileNotFoundException; |
| 8 | +//import File Reader - Convenience class for reading character files. |
4 | 9 | import java.io.FileReader; |
| 10 | +//import FileWriter - Convenience class for writing character files. |
5 | 11 | import java.io.FileWriter; |
| 12 | +//import IOException - Signals that an I/O exception of some sort has occurred. |
6 | 13 | import java.io.IOException; |
| 14 | +//import SimpleDateFormat - SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. |
7 | 15 | 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. |
8 | 17 | 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. |
10 | 21 | 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; |
12 | 24 |
|
13 | 25 | public class ReceiptGenerator { |
14 | | - |
| 26 | + // Main method |
15 | 27 | 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); |
22 | 28 |
|
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 | + */ |
31 | 49 | 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 | + */ |
32 | 55 | BufferedReader pL = new BufferedReader(new FileReader("priceList.txt")); |
33 | 56 |
|
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 | + */ |
35 | 63 | File file = new File(nameOfReceipt); |
| 64 | + // Constructs a FileWriter given the File to write |
36 | 65 | FileWriter fw = new FileWriter(file); |
37 | 66 |
|
| 67 | + /* |
| 68 | + * Constructs a new Scanner that produces values scanned from the specified |
| 69 | + * input stream |
| 70 | + */ |
38 | 71 | 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 |
45 | 79 | System.out.println("==================== WELCOME TO ALYSHA'S BAKESHOP ====================\n"); |
46 | 80 |
|
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 |
50 | 84 |
|
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 |
52 | 86 |
|
| 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 | + */ |
53 | 91 | while ((lineiL = iL.readLine()) != null) { |
| 92 | + // Appends the specified element to the end of the priceList |
54 | 93 | itemListToDisplay.add(lineiL); |
55 | 94 | } |
| 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 | + */ |
56 | 99 | 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 | + */ |
57 | 104 | priceList.add(Double.parseDouble(linepL)); |
58 | 105 | } |
59 | 106 |
|
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. |
66 | 119 | System.out.println(e + "\t\t\t " + itemListToDisplay.get(e) + "\t\t\t " + priceList.get(e)); |
67 | 120 | } |
68 | 121 | } |
69 | | - } catch (IndexOutOfBoundsException a) { |
70 | | - |
| 122 | + } catch (IndexOutOfBoundsException a) { // Catch any exceptions if possible |
71 | 123 | } |
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(" "); |
79 | 124 |
|
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 |
92 | 152 | System.out.println("Invalid Input"); |
93 | 153 | 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 |
95 | 156 | System.out.println("Invalid Input"); |
96 | 157 | 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 |
98 | 159 | } |
| 160 | + // Both y and Y are accepted in user input simply it's not case-sensitive |
99 | 161 | } while (responseYN == 'y' || responseYN == 'Y'); |
100 | 162 |
|
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 |
103 | 166 | System.out.println("-------------------------Summary-------------------------"); |
104 | 167 |
|
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 |
109 | 172 |
|
| 173 | + // Prints this text for console UI |
110 | 174 | System.out.println("| Bread Name |\t\t| Price |\t| Quantity|\t| Total |"); |
111 | 175 |
|
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 |
115 | 179 |
|
| 180 | + // Prints this text for console UI |
116 | 181 | fw.write("\n\n---------------------Receipt----------------------------\n"); |
| 182 | + // Prints this text for console UI |
117 | 183 | fw.write("| Bread Name |\t\t| Price |\t\t| Quantity |\t\t| Total |"); |
118 | 184 |
|
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 | + */ |
122 | 194 | 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 | + */ |
123 | 200 | fw.write("\n" + itemList.get(e) + " \t\t\t\t\t" + currentPriceList.get(e) + " \t\t\t\t\t\t" |
124 | 201 | + quantityList.get(e) + " \t\t\t\t\t\t" + (quantityList.get(e) * currentPriceList.get(e))); |
125 | 202 |
|
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 | + */ |
127 | 209 | System.out.println(itemList.get(e) + "\t\t " + currentPriceList.get(e) + "\t\t " + quantityList.get(e) |
128 | 210 | + " \t\t " + (quantityList.get(e) * currentPriceList.get(e))); |
129 | 211 | } |
130 | 212 | } |
131 | | - } catch (IndexOutOfBoundsException e) { |
| 213 | + } catch (IndexOutOfBoundsException e) { // Catch exception if possible |
132 | 214 |
|
133 | 215 | } |
134 | 216 |
|
135 | | - System.out.println("\nTotal: " + total); |
| 217 | + System.out.println("\nTotal: " + total); // Prints the total |
136 | 218 |
|
| 219 | + // Prints for console UI |
137 | 220 | System.out.println("-------------------------------------------------------"); |
138 | 221 |
|
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 |
143 | 231 |
|
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 |
148 | 233 | fw.write("\n----------------------------------------------------------"); |
| 234 | + |
| 235 | + // Converts the value of payment to String and writes it in a txt file |
149 | 236 | fw.write("\nPayment: " + String.valueOf(payment)); |
| 237 | + |
| 238 | + // Converts the value of change to String and writes it in a txt file |
150 | 239 | 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 |
154 | 245 | } |
155 | 246 | } |
0 commit comments