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

a. Write a program that allows you to create a file of customers for a company.

ID: 3596524 • Letter: A

Question

a. Write a program that allows you to create a file of customers for a company. The first part of the program should create an empty file suitable for writing a three-digit ID number, six-character last name, and five-digit zip code for each customer. The second half of the program accepts user input to populate the file. For this exercise, assume that the user will correctly enter ID numbers and zip codes, but force the customer name to seven characters if it is too long or too short. Issue an error message, and do not save the records if the user tries to save a record with an ID number that has already been used. Save the program as CreateCustomerFile.java.

Explanation / Answer

Note : Have to do some modifications.. i will do it..

package org.students;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

public class Test {

/*

* Creating an Scanner class object which is used to get the inputs entered

* by the user

*/

static Scanner sc1 = new Scanner(System.in);

static FileWriter fw = null;

static File f = new File("CustomerData.txt");

public static void main(String[] args) throws IOException {

int id, zipcode;

String lastname;

ArrayList<Customer> arl = new ArrayList<Customer>();

while(true)

{

id = getValidId();

lastname = getValidName();

zipcode = getValidZipcode();

try {

// If file already exists then write data to the existing file

if (f.exists()) {

boolean bool=isDuplicateData(id,lastname,zipcode);

if(bool)

{

fw = new FileWriter(f, true);

fw.write(id);

fw.write(" ");

fw.write(lastname);

fw.write(" ");

fw.write(zipcode);

System.out.println("Data Written to File");

}

else

{

System.out.println("** NOt valid.Duplicate Id **");

continue;

}

} else {

// If no file already exists.Create new File

f.createNewFile();

fw = new FileWriter(f);

fw.write(id);

fw.write(" ");

fw.write(lastname);

fw.write(" ");

fw.write(zipcode);

System.out.println("Data Written to File");

}

//Getting the character from the user 'Y' or 'y' or 'N' or 'n'

System.out.print("Do you want to continue(Y/N) ::");

char ch = sc1.next(".").charAt(0);

if(ch=='Y'||ch=='y')

continue;

else

{

System.out.println(":: Program Exit ::");

fw.close();

break;

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

private static boolean isDuplicateData(int id, String lastname, int zipcode) throws FileNotFoundException {

Scanner sc=null;

int id1,zipcode1;

String lastname1;

// System.out.println("Hello");

if (f.exists())

{

sc=new Scanner(f);

while(sc.hasNext())

{

id1=Integer.parseInt(sc.next());

lastname1=sc.next();

zipcode1=Integer.parseInt(sc.next());

System.out.println(id+" "+id1);

if(id==id1)

{

sc.close();

return false;

}

}

}

sc.close();

return true;

}

private static int getValidZipcode() {

while (true) {

System.out.print("Enter Zipcode :");

int zipcode = sc1.nextInt();

if (countDigits(zipcode) < 5) {

System.out.println("** Too Short.Must be 5 digits **");

continue;

} else if (countDigits(zipcode) > 5) {

System.out.println("** Too Long.Must be 5 digits **");

continue;

} else

return zipcode;

}

}

private static String getValidName() {

while (true) {

System.out.print("Enter Lastname:");

String lastname = sc1.next();

if (lastname.length() < 6) {

System.out

.println("** Too Short.Must be 6 characters Length **");

continue;

} else if (lastname.length() > 6) {

System.out

.println("** Too Long.Must be 6 characters Length **");

continue;

} else

return lastname;

}

}

private static int getValidId() {

while (true) {

System.out.print("Enter Id :");

int id = sc1.nextInt();

int n = countDigits(id);

if (n < 3) {

System.out.println("** Too Short.Must be 3 digits **");

continue;

} else if (n > 3) {

System.out.println("** Too Long.Must be 3 digits **");

continue;

} else

return id;

}

}

private static int countDigits(int n) {

int count = 0;

while (n != 0) {

n /= 10;

++count;

}

return count;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote