I'm fairly new to Java programming so this might be a noob question, but I have developed a movie ticket reservation program for a coursework of mine and one of the requirements for it is to allow the user to print out a receipt for the purchase. I have succeeded in getting to print the output to the .txt file but the problem is that all the information prints in a single line but I want them to print in different lines. Can someone please help me out with this?
private void printReceiptActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
showMessageDialog(this, "Your receipt is printing!");
//print receipt to a text file called Receipt
FileWriter fw = new FileWriter("Receipt.txt");
PrintWriter pw = new PrintWriter(fw);
//calculation of prices
float adultPrice = (float) (countA * 9.00);
float studentPrice = (float) (countSt * 8.00);
float seniorPrice = (float) (countSe * 7.00);
float childPrice = (float) (countC * 5.00);
//total price for tickets
float totalPrice = adultPrice + studentPrice + seniorPrice + childPrice;
try {
String queryString = "SELECT title, movietheatre FROM movies WHERE movieid = " + movieID;
ResultSet results = myStatement.executeQuery(queryString);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
StringBuilder strBuilder = new StringBuilder();
while (results.next()) {
strBuilder.append("\t Cinema Ticket Receipt");
strBuilder.append("\n");
strBuilder.append("Movie Title : ").append(results.getString(1));
strBuilder.append("\n");
strBuilder.append("Time : ").append(time);
strBuilder.append("\n");
strBuilder.append("Date : ").append(date);
strBuilder.append("\n");
strBuilder.append("Movie Hall : ").append(results.getString(2));
strBuilder.append("\n");
strBuilder.append("\n");
strBuilder.append("Adult ticket price : ").append(formatter.format(adultPrice));
strBuilder.append("\n");
strBuilder.append("Student ticket price : ").append(formatter.format(studentPrice));
strBuilder.append("\n");
strBuilder.append("Seniors ticket price : ").append(formatter.format(seniorPrice));
strBuilder.append("\n");
strBuilder.append("Children ticket price : ").append(formatter.format(childPrice));
strBuilder.append("\n");
strBuilder.append("\n");
strBuilder.append("Total price : £").append(formatter.format(totalPrice));
strBuilder.append("\n");
}
results.close();
//String formatStr = strBuilder.toString();
//out.write(String.format(formatStr));
pw.print(strBuilder.toString());
pw.close();
} catch (SQLException sqle) {
out.println(sqle);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
Logger.getLogger(Receipt.class.getName()).log(Level.SEVERE, null, ex);
}
showMessageDialog(this, "Receipt printing complete.");
}