Question

Write a Java program to read customer details from an input text file corresponding to problem...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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");
}
  
}
  
}
  
}

Add a comment
Know the answer?
Add Answer to:
Write a Java program to read customer details from an input text file corresponding to problem...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Java Programming Reading from a Text File Write a Java program that will use an object...

    Java Programming Reading from a Text File Write a Java program that will use an object of the Scanner class to read employee payroll data from a text file The Text file payroll.dat has the following: 100 Washington Marx Jung Darwin George 40 200 300 400 Kar Car Charles 50 22.532 15 30 The order of the data and its types stored in the file are as follows: Data EmployeelD Last name FirstName HoursWorked double HourlyRate Data Tvpe String String...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • *Java* You will write a program to do the following: 1. Read an input file with...

    *Java* You will write a program to do the following: 1. Read an input file with a single line of text. Create a method named load_data. 2. Determine the frequency distribution of every symbol in the line of text. Create a method named 3. Construct the Huffman tree. 4. Create a mapping between every symbol and its corresponding Huffman code. 5. Encode the line of text using the corresponding code for every symbol. 6. Display the results on the screen....

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...

    Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify the Person class in Lab #4. It has four private data members firstNam (char[20]), day, month, year (all int); and two public member functions: setPerson(char *, int, int, int) and printInfo(). The function setPerson sets the first name and birthday in the order of day, month and year. The function printInfo prints first name and birthday. But it should print the date in the...

  • Write in java language ================== Office Supplies Inc., an office supply store, services many customers. As...

    Write in java language ================== Office Supplies Inc., an office supply store, services many customers. As customers’ orders for office supplies are shipped, information is entered into a file. Office Supplies bills their customers once each month. At the end of each month, the Chief Executive Officer requests a report of all customers sorted by their customer id (from lowest to highest). The report includes their bill balance and tax liability. Write a program to produce the outstanding balance report...

  • Simple C++: Getting an input file and doing calculations. Input file to be read from should...

    Simple C++: Getting an input file and doing calculations. Input file to be read from should be named "1030Prob1.dat" it will contain the following:: John Harris 11374 50000.00 Lisa Smith 11985 75000.00 Adam Johnson 12585 68500.00 Sheila Smith 11654 150000.00 Tristen Major 11274 75800.00 Yannic Lennart 15687 58000.00 Lorena Emil 17414 43000.00 Tereza Santeri 12597 48000.00 A company keeps the record of its employees in a text file. The text file is in the following format FirstName LastName ID Salary...

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • JAVA Write a program which will read a text file into an ArrayList of Strings. Note...

    JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT