In: Computer Science
Create a program that allows a user to input customer records (ID number, first name, last name, and balance owed) and save each record to a file. When you run the main program, be sure to enter multiple records.
Once you create the file, open it and display the results to the user
Save the file as CustomerList.java
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class CustomerList {
public static void main(String[] args) throws
Exception {
Scanner sc = new
Scanner(System.in);
PrintWriter pw = new
PrintWriter(new File("output.txt"));
int id;
String fname, lname;
double balance;
String ch = "y";
//reading the data
while (ch.equalsIgnoreCase("y"))
{
System.out.println("Enter Id: ");
id =
sc.nextInt();
//reading the
data from user
System.out.println("Enter first Name and Last name");
fname =
sc.nextLine();
fname =
sc.nextLine();
lname =
sc.nextLine();
System.out.println("Enter balance: ");
balance =
sc.nextDouble();
//printing the
data to the file
pw.println(id +
"\t" + fname + " " + lname + "\t" + balance);
System.out.println("Press y/Y to continue, any other key to exit:
");
ch =
sc.nextLine();
ch =
sc.nextLine();
}
pw.close();
//opening the file and reading and
printing
sc = new Scanner(new
File("output.txt"));
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
pw.close();
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me