Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

This is what I have so far. It does not write to the file it creates?

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 WriteCustomeList { public static void main(String[] args){ //set up user input Scanner input = new Scanner(System.in); //set file paths and create files Path inCustomerFile = Paths.get("C:\\Users\\FBI-Surv\\Documents\\NetBeansProjects\\" + "WriteCustomerList\\CustomerList.txt"); Path outCustomerFile = Paths.get("C:\\Users\\FBI-Surv\\Documents\\NetBeansProjects\\" + "WriteCustomerList\\CustomerList.txt"); //declarations final String ID_FORMAT = "0000"; final String fNAME_FORMAT = " "; final int fNAME_LENGTH = fNAME_FORMAT.length(); final String lNAME_FORMAT = " "; final int lNAME_LENGTH = fNAME_FORMAT.length(); final String BALANCEOWED_FORMAT = "0000.00"; String delimiter = " , "; String s = ID_FORMAT + delimiter + fNAME_FORMAT + delimiter + lNAME_FORMAT + delimiter + BALANCEOWED_FORMAT + " " + System.getProperty("line.separator"); final int RECSIZE = s.length(); FileChannel fcIn = null; FileChannel fcOut = null; String idString; int id; String fName; String lName; double balanceOwed; final String QUIT = "9999"; createEmptyFile(inCustomerFile, s); createEmptyFile(outCustomerFile, s); try{ fcIn = (FileChannel)Files.newByteChannel(inCustomerFile, CREATE, WRITE); fcOut = (FileChannel)Files.newByteChannel(outCustomerFile, CREATE, WRITE); System.out.print("Enter customer id number >> "); idString = input.nextLine(); while(!(idString.equals(QUIT))){ id = Integer.parseInt(idString); System.out.print("Enter customers first name >> "); fName = input.nextLine(); StringBuilder sbFN = new StringBuilder(fName); sbFN.setLength(fNAME_LENGTH); fName = sbFN.toString(); System.out.print("Enter customers last name >> "); lName = input.nextLine(); StringBuilder sbLN = new StringBuilder(lName); sbLN.setLength(lNAME_LENGTH); lName = sbLN.toString(); System.out.print("Enter balance owed >> "); balanceOwed = input.nextDouble(); input.nextLine(); DecimalFormat df = new DecimalFormat(BALANCEOWED_FORMAT); s = ID_FORMAT + delimiter + fNAME_FORMAT + delimiter + lNAME_FORMAT + delimiter + df.format(balanceOwed) + " " + System.getProperty("line.separator"); byte data[] = s.getBytes(); ByteBuffer buffer = ByteBuffer.wrap(data); System.out.print("Enter next customer or " + QUIT + " to quit >> "); idString = input.nextLine(); } fcIn.close(); fcOut.close(); } catch (Exception e){ System.out.println("Error message: " + e); } } public static void createEmptyFile(Path file, String s){ final int NUMRECS = 9999; try{ OutputStream outputStr = new BufferedOutputStream(Files.newOutputStream(file, CREATE)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStr)); for(int count = 0; count < NUMRECS; ++count) writer.write(s, 0, s.length()); writer.close(); } catch(Exception e){ System.out.println("Error message: " + e); }

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago