Write a program that allows you to create a file of customers for a company. The
ID: 3661598 • Letter: W
Question
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. b. Write a program that creates a file of items carried by the company. Include a three-digit item number and up to a 20-character description for each item. Issue an error message if the user tries to store an item number that has already been used. Save the program as CreateItemFile.java. c. Write an application that takes customer orders. Allow a user to enter a customer number and item ordered. Display an error message if the customer number does not exist in the customer file or the item does not exist in the item file; otherwise, display all the customer information and item information. Save the program as CustomerItemOrder.java.
Explanation / Answer
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
import java.text.*;
public class CreateCustomer
{
public static void main(String[] args)
{
final int NUMBER_OF_RECORDS = 1000;
//allows you to create a file of customers for a company
final String ID = "0000";
final String NAME_FORMAT = " ";
final int NAME_LENGTH = NAME_FORMAT.length();
final String ZIP_CODE = "00000";
final String delimiter = ",";
String defaultRecord = ID + delimiter + NAME_FORMAT + delimiter + ZIP_CODE + System.getProperty("line.separator");
final int RECORD_SIZE = defaultRecord.length();
Scanner input = new Scanner(System.in);
Path filename = Paths.get("Customers.txt");
Path file = filename.toAbsolutePath();
createEmptyFile(file, defaultRecord, NUMBER_OF_RECORDS);
String userInput;
OutputStream outputFile = null;
final String QUIT = "9999";
// 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.
System.out.print("Enter Customer ID " + QUIT + " >> ");
userInput = input.nextLine();
try {
outputFile = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputFile));
while (!(userInput.equals(QUIT))) {
StringBuilder contact = new StringBuilder(userInput);
System.out.print("Enter customers last name >> ");
userInput = input.nextLine();
contact.append(delimiter + userInput.trim());
System.out.print("Enter Zip Code >> ");
userInput = input.nextLine();
contact.append(delimiter + userInput.trim());
contact.append(System.getProperty("line.separator"));
String s = contact.toString();
writer.write(s);
//Issue an error message, and do not save the record if the user tries to enter a previously used ID number
System.out.print(" Enter Customer id name or " + QUIT + " >> ");
userInput = input.nextLine();
}
writer.close();
} catch (Exception e) {
System.out.println("Error message: " + e);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.