Write a Java program to read customer details from an input text file correspond
ID: 3832668 • Letter: W
Question
Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System
The input text file i.e. customers.txt has customer details in the following format:
A sample data line is provided below.
“John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99
Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost
Note: if a customer is non-member, then the corresponding memberID field is empty in the input file.
Once the program starts, it must read the customers data from text file (customers.txt). The customer data in the text file is clean and does not need any validations.
For every customer record in the text file, you must create a customer object of type Customer class and add or concatenate its String data (i.e., using the toString() method) to a String variable, such as customersStr.
Then, the program displays the customers information (stored in the String customersStr variable) along with net movie ticket cost in the following table format; use the toString() method in the Customer class to concatenate Customer objects data into the String customersStr variable.
Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost
The program must display the output using JOptionPane ShowMessage box.
For further ticketing, use JOptionPane input dialog box to read the inputs and construct objects of Customer class type defined below.
- String: customerName ie. First name Lastname
- String movieName
- String: memberID // this could be null fornon-members
- double: movieTicketCost
For membership info, the program must ask if the customer wants to be member and if so, read memberID as input. Assume a default member discount of 5%. For non-members, the memberID value in the object must be set to null.
Make sure to have both member and non-member customers.
After reading the customer information from user, construct the Customer objects, perform the calculations and concatenate its String data (i.e., using the toString() method) to String variable customersStr.
Finally, write the output (String variable customersStr) to another text file, file output.txt, in the same comma separated format.
The program must have the functionality broken into appropriate methods, such as readFromFile(), writeToFile(), addCustomerMovieReservation(), displayOutput(), etc.
Explanation / Answer
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author SuNiL SiNgH
*/
public class CustomerDetails extends JFrame implements ActionListener{
String customerName,memberID,movieName,customerStr;
double movieTicketCost,totalDiscount,salesTax,netMovieTicketCost;
JButton bt1,bt2;
JPanel pl;
public CustomerDetails(){
bt1=new JButton("Get Customer Details");
bt2=new JButton("Register Customer Details");
pl=new JPanel();
pl.add(bt1);
pl.add(bt2);
add(pl);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt1.setActionCommand("Get");
}
public void addCustomerMovieRegistration(){
customerName=JOptionPane.showInputDialog(this,"Enter customer name");
memberID=JOptionPane.showInputDialog(this,"Enter member");
movieName=JOptionPane.showInputDialog(this,"MovieName");
movieTicketCost=Double.parseDouble(JOptionPane.showInputDialog(this,"Cost of ticket"));
if(!memberID.equals("")){
totalDiscount=(movieTicketCost*5)/100;
}
salesTax=(movieTicketCost*14)/100;
netMovieTicketCost=movieTicketCost-totalDiscount+salesTax;
}
public void display(){
customerStr="""+customerName+"","+memberID+",""+movieName+"","+movieTicketCost+","+totalDiscount+","+salesTax+","+netMovieTicketCost;
JOptionPane.showMessageDialog(this,customerStr);
}
public void readFromFile() throws FileNotFoundException{
String str;
try {
BufferedReader br=new BufferedReader(new FileReader("customer.txt"));
while(( str=br.readLine())!=null){
JOptionPane.showMessageDialog(this,str);
}
br.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,"File not found");;
}
}
public void writeToFile() throws FileNotFoundException{
try {
BufferedWriter br=new BufferedWriter(new FileWriter("customerNew.txt"));
br.write(customerStr);
br.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,"File not found");;
}
}
public static void main(String args[]) {
CustomerDetails cd=new CustomerDetails();
cd.setSize(700,500);
cd.setVisible(true);
cd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if(str.equals("Get")){
try {
readFromFile();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this,"Customer details file Not Found");
}
}else{
try {
addCustomerMovieRegistration();
display();
writeToFile();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this,"Customer registration file Not Found");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.