IN JAVA Create a command-line application for Miami Sports an Internet café that
ID: 3697635 • Letter: I
Question
IN JAVA
Create a command-line application for Miami Sports an Internet café that uses a Customer class objects to write to a file and reads from a file.
At Miami Sports, the user enters a customer ID number, names, credit limit, business line, and sales region for the customer, and the completed object is written to a file
When the user enters 999, the output file is closed.
Do the following to help you create the above application:
Create a Customer class that can capture customer’s data in the form of a customer ID number, customer names, credit limit, business description, and sales region (South, North, Central, East, and West)
The application should have the capability to prompt the user in order to capture customer data and must use serialization to store each customer’s record to a file called misports_cust.dat.
In order to accomplish the above task, you need to do the following:
Declare six instance variables for the pieces of data about a customer as listed above in (a) i.e. private int custNumber
private String custNames, etc.
Create two constructors; one default and the other with parameters.
You are required to provide appropriate getters and setters.
Create an output stream for file misports_cust.dat to facilitate the writing of an object to a file.
Create a Customer[5] array that will help in capturing five customers’ data and writing it to a file as an array object.
Save the program as MISCustomer.java.
Deserialization:
Create a program that reads the file created in Question#1 (a) above displaying each customer’s data to the screen. Save the program as MISCustomerRecord.java.
Explanation / Answer
Hi, I have implemented your requirement. Please note that i have not implemented 999 to close the output file since you mentioned Cutomer[5] it means only 5 customer you need.
Customer.java
import java.io.Serializable;
public class Customer implements Serializable {
private int custNumber;
private String custName;
private int creditLimit;
private String creditDesc;
private String businessLine;
private String region;
public Customer(){
}
public Customer(int custNumber, String custName, int creditLimit, String creditDesc, String businessLine, String region){
this.custNumber = custNumber;
this.custName = custName;
this.creditLimit = creditLimit;
this.creditDesc = creditDesc;
this.businessLine = businessLine;
this.region = region;
}
public int getCustNumber() {
return custNumber;
}
public void setCustNumber(int custNumber) {
this.custNumber = custNumber;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public int getCreditLimit() {
return creditLimit;
}
public void setCreditLimit(int creditLimit) {
this.creditLimit = creditLimit;
}
public String getCreditDesc() {
return creditDesc;
}
public void setCreditDesc(String creditDesc) {
this.creditDesc = creditDesc;
}
public String getBusinessLine() {
return businessLine;
}
public void setBusinessLine(String businessLine) {
this.businessLine = businessLine;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String toString(){
return getCustNumber()+" "+getCustName()+" "+getCreditLimit()+" "+getCreditDesc()+" "+getBusinessLine()+" "+getRegion()+" ";
}
}
MISCustomer.java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class MISCustomer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Customer c[] = new Customer[5];
Scanner scan = new Scanner(System.in);
File file = new File("misports_cust.dat");
for(int i=0; i<c.length; i++){
System.out.println("Please enter Customer Number :");
int custNumber = scan.nextInt();
System.out.println("Please enter Customer Name :");
String custName = scan.next();
System.out.println("Please enter Credit Limit :");
int creditLimit = scan.nextInt();
System.out.println("Please enter Credit Description :");
String creditDesc = scan.next();
System.out.println("Please enter Business Line : ");
String businessLine = scan.next();
System.out.println("Please enter Region :");
String region = scan.next();
c[i] = new Customer(custNumber, custName, creditLimit, creditDesc, businessLine, region);
}
FileOutputStream fout = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(c);
oos.close();
System.out.println("misports_cust.dat file has been generated");
}
}
Output:
Please enter Customer Number :
111
Please enter Customer Name :
Suresh
Please enter Credit Limit :
100
Please enter Credit Description :
qqqq
Please enter Business Line :
wwww
Please enter Region :
North
Please enter Customer Number :
222
Please enter Customer Name :
Sekhar
Please enter Credit Limit :
200
Please enter Credit Description :
wwww
Please enter Business Line :
eeee
Please enter Region :
South
Please enter Customer Number :
3
Please enter Customer Name :
Anshu
Please enter Credit Limit :
300
Please enter Credit Description :
yyy
Please enter Business Line :
uuuu
Please enter Region :
East
Please enter Customer Number :
4
Please enter Customer Name :
Revathi
Please enter Credit Limit :
400
Please enter Credit Description :
gggg
Please enter Business Line :
hhhhh
Please enter Region :
West
Please enter Customer Number :
5
Please enter Customer Name :
Geethanshu
Please enter Credit Limit :
500
Please enter Credit Description :
ggg
Please enter Business Line :
nnnn
Please enter Region :
West
misports_cust.dat file has been generated
MISCustomerRecord.java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class MISCustomerRecord {
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
File file = new File("misports_cust.dat");
FileInputStream fin = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fin);
Customer[] c = (Customer[]) ois.readObject();
for(int i=0; i<c.length; i++){
System.out.println(c[i].toString());
}
ois.close();
}
}
Output:
111 Suresh 100 qqqq wwww North
222 Sekhar 200 wwww eeee South
3 Anshu 300 yyy uuuu East
4 Revathi 400 gggg hhhhh West
5 Geethanshu 500 ggg nnnn West