Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A. Create a program that allows a user to input customer records (ID number, fir

ID: 3844153 • Letter: A

Question

A. 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. Save the program as WriteCustomerList.java. When you execute the program, be sure to enter multiple records that have the same last name because you will search for repeated first names in part d of this exercise.

B. Write an application that reads the file created by the WriteCustomerList application and displays the records. Save the file as DisplaySavedCustomerList.java.

C. Write an application that allows you to enter any ID number, reads the customer data file created in Exercise "a", and displays the data for the customer. Display an appropriate message if the ID number cannot be found in the input file. Save the file as DisplaySelectedCustomer.java

D. Write an application that allows you to enter any last name and display all the data for the customers with the given last name. Display an appropriate message if the name cannot be found in the input file. Save the file asDisplaySelectedCustomersByName.java.

Explanation / Answer

here is your code: -

records.txt

1 Salil Bansal 25
Sanjay Bansal Bansal 233
3 Ravi Sharma 232

CustomerTest.java


import java.io.IOException;
import java.util.Scanner;

public class CustomerTest {
   public static void main(String[] args) throws IOException {
       Scanner sca = new Scanner(System.in);
       String filename = "records.txt";
       int option = -1;
       while(option != 5) {
           System.out.println();
           System.out.println("Please select the option:");
           System.out.println("1. Add a customer in the list.");
           System.out.println("2. Display the list.");
           System.out.println("3. Search by customer ID.");
           System.out.println("4. Search by customer name.");
           System.out.println("5. Exit");
          
           option = sca.nextInt();
           sca.nextLine();
           switch (option) {
           case 1:
               WriteCustomerList.writeFile(filename);
               break;
           case 2:
               DisplaySavedCustomerList.readFile(filename);
               break;
           case 3:
               DisplaySelectedCustomer.searchByID(filename);
               break;
           case 4:
               DisplaySelectedCustomersByName.searchByName(filename);
               break;
           case 5:
               System.out.println("Thanks..");
               break;
           default:
               System.out.println("Please enter correct choice.");
               break;
           }
       }
      
   }
}

DisplaySelectedCustomersByName.java


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DisplaySelectedCustomersByName {
   public static void searchByName(String filename) throws FileNotFoundException {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the last name to search: ");
       String s = sc.next();
      
       sc = new Scanner(new File(filename));
       String line;
       String[] vals;
       int count = 0;
       System.out.println("Records in File matching with last name entered: ");
       while (sc.hasNextLine()) {
           line = sc.nextLine();
           vals = line.split(" ");
           if (vals[2].equals(s)) {
               count++;
               System.out.println(count + ".");
               System.out.println("ID Number:" + vals[0]);
               System.out.println("First Name:" + vals[1]);
               System.out.println("Last Name:" + vals[2]);
               System.out.println("Balance owned:" + vals[3]);
               System.out.println();
               System.out.println();
           }
       }
      
       if (count == 0) {
           System.out.println("No records in file matching with last name.");
       }

   }
}

DisplaySelectedCustomer.java


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DisplaySelectedCustomer {
   public static void searchByID(String filename) throws FileNotFoundException {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the ID number to search: ");
       String s = sc.next();
      
       sc = new Scanner(new File(filename));
       String line;
       String[] vals;
       int count = 0;
       System.out.println("Records in File matching with ID number: ");
       while (sc.hasNextLine()) {
           line = sc.nextLine();
           vals = line.split(" ");
           if (vals[0].equals(s)) {
               count++;
               System.out.println(count + ".");
               System.out.println("ID Number:" + vals[0]);
               System.out.println("First Name:" + vals[1]);
               System.out.println("Last Name:" + vals[2]);
               System.out.println("Balance owned:" + vals[3]);
               System.out.println();
               System.out.println();
           }
       }
      
       if (count == 0) {
           System.out.println("No records in file matching with ID number.");
       }

   }
}

DisplaySavedCustomerList.java


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DisplaySavedCustomerList {
   public static void readFile(String filename) throws FileNotFoundException {
       Scanner sc = new Scanner(new File(filename));
       String line;
       String[] vals;
       int count = 0;
       System.out.println("Records in File: ");
       while(sc.hasNextLine()) {
           count++;
           line = sc.nextLine();
           vals = line.split(" ");
           System.out.println(count+".");
           System.out.println("ID Number:"+vals[0]);
           System.out.println("First Name:"+vals[1]);
           System.out.println("Last Name:"+vals[2]);
           System.out.println("Balance owned:"+vals[3]);
           System.out.println();
           System.out.println();
       }
       if(count == 0) {
           System.out.println("No records in file.");
       }
      
   }
}

WriteCustomerList.java


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class WriteCustomerList {
   public static void writeFile(String filename) throws IOException {
       Scanner sc = new Scanner(System.in);
       File fil = new File(filename);
       PrintWriter out = new PrintWriter(new FileWriter(fil, true));
      
       System.out.print("Enter ID num: ");
       out.write(sc.next());
       System.out.print("Enter First Name:");
       out.write(" "+sc.next());
       System.out.print("Enter Last Name:");
       out.write(" "+sc.next());
       System.out.print("Enter Balance owned:");
       out.write(" "+sc.next()+" ");
       out.close();
   }
}

Sample Run: -


Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
1
Enter ID num: 1
Enter First Name:Salil
Enter Last Name:Bansal
Enter Balance owned:25

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
1
Enter ID num: Sanjay
Enter First Name:Bansal
Enter Last Name:Bansal
Enter Balance owned:233

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
1
Enter ID num: 3
Enter First Name:Ravi
Enter Last Name:Sharma
Enter Balance owned:232

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
2
Records in File:
1.
ID Number:1
First Name:Salil
Last Name:Bansal
Balance owned:25


2.
ID Number:Sanjay
First Name:Bansal
Last Name:Bansal
Balance owned:233


3.
ID Number:3
First Name:Ravi
Last Name:Sharma
Balance owned:232

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
3
Enter the ID number to search: 1
Records in File matching with ID number:
1.
ID Number:1
First Name:Salil
Last Name:Bansal
Balance owned:25

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
3
Enter the ID number to search: 9
Records in File matching with ID number:
No records in file matching with ID number.

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
4
Enter the last name to search: Bansal
Records in File matching with last name entered:
1.
ID Number:1
First Name:Salil
Last Name:Bansal
Balance owned:25


2.
ID Number:Sanjay
First Name:Bansal
Last Name:Bansal
Balance owned:233

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
4
Enter the last name to search: 4
Records in File matching with last name entered:
No records in file matching with last name.

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
9
Please enter correct choice.

Please select the option:
1. Add a customer in the list.
2. Display the list.
3. Search by customer ID.
4. Search by customer name.
5. Exit
5
Thanks..

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote