Write a program that allows you to create a file of customers for a company. The
ID: 3801753 • 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
package com;
/*your inconvinience is deeply regreted,i have done modifications in solution b as well,kindly please check ,Thank you :-)
* CreateCustomerFile.java
* solution for a,the question is not clear on number of character the name has to have,however if it six change to 6
* in the while condition as name.length()!=6,i have used name.length()!=7
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class CreateCustomerFile {
private static final String FILENAME = "customer.txt";
public static void main(String args[])throws Exception
{
BufferedWriter bw = null;
FileWriter fw = null;
boolean allPass=false;
try {
File file = new File(FILENAME);
if(!file.exists()) // checking the file already present
fw = new FileWriter(FILENAME);
else
fw=new FileWriter(FILENAME, true);
bw = new BufferedWriter(fw);
// taking user input
Scanner sc= new Scanner(System.in);
System.out.println("Enter User ID:");
String id=sc.nextLine();
System.out.println("Enter Zip Code:");
String zip=sc.nextLine();
String name="";
//forcing user for 7 characters
while(name.length()!=7)
{
System.out.println("The Name should be Strictly 7 characters");
System.out.println("Enter lName:");
name=sc.nextLine();
}
if(file.exists()){
if(idCheck(id))
{
bw.write(" "+id+" "+zip+" "+name);
allPass=true;
}
else
System.out.println("ID already exists");
}
else{
bw.write(id+" "+zip+" "+name);
allPass=true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
if(allPass)
System.out.println("record saved successfully");
else
System.out.println("record not saved");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
// to check id is already present in the file
public static boolean idCheck(String id)throws Exception
{
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.contains(id))
{
return false;
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
return false;
}
}
return true;
}
}
package com;
/*
* CreateItemFile.java
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class CreateItemFile {
private static final String FILENAME = "items.txt";
public static void main(String args[])throws Exception
{
BufferedWriter bw = null;
FileWriter fw = null;
boolean allPass=false;
try {
File file = new File(FILENAME);
if(!file.exists())
fw = new FileWriter(FILENAME);
else
fw=new FileWriter(FILENAME, true);
bw = new BufferedWriter(fw);
Scanner sc= new Scanner(System.in);
System.out.println("Enter Item ID:");
String id=sc.nextLine();
String description=null;
do
{
System.out.println("The description can be upto 20 characters only");
System.out.println("Enter Description:");
description=sc.nextLine();
}
while(!(description.length()<=20));
if(file.exists()){
if(idCheck(id))
{
bw.write(" "+id+" "+description);
allPass=true;
}
else
System.out.println("ID already exists");
}
else{
bw.write(id+" "+description);
allPass=true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
if(allPass)
System.out.println("record saved successfully");
else
System.out.println("record not saved");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static boolean idCheck(String id)throws Exception
{
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.contains(id))
{
return false;
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
return false;
}
}
return true;
}
}
package com;
/*
* CustomerItemOrder.java
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class CustomerItemOrder {
private static String FILENAME = "customer.txt";
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Customer Number/ID:");
String CusId = sc.nextLine();
System.out.println("Enter Item Number/ID:");
String ItemId = sc.nextLine();
boolean presentInCustFile = false;
boolean presentInItemFile = false;
presentInCustFile=idCheck(CusId);
//change file now
FILENAME ="items.txt";
presentInItemFile = idCheck(ItemId);
if(presentInCustFile&&presentInItemFile)
{
printInfoById(CusId,"customer.txt");
printInfoById(ItemId, "items.txt");
}
}
public static void printInfoById(String id,String fileName)
{
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName));
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.contains(id)){
//here u can change according to your needs make the output look more good
//for now printing the line is enough
System.out.println(sCurrentLine);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static boolean idCheck(String id)throws Exception
{
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine="";
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
sCurrentLine=sCurrentLine.replace(" ", "");
if(sCurrentLine.trim().contains(id))
{
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
return false;
}
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.