Modify this program to open the file "Customers.dat" so that all data can be read, and data written or appended to this file. Add an option 2 and 3 to the menu so that the menu appears as below:Menu options '2' and '3' should now be considered a valid choice. 1. Add Data 2. Update Data 3. Display all data X. Exit Program Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber. Use this number as the base when adding new customer data. The program should be able to start multiple times and add data with increasing customerNumbers. There should be no duplicated customer numbers. Menu Option 2 should do the following tasks Prompt for a customer number to change. Enter the address information ( Street 1 & 2, City , State and Zipcode) Find the record for the customer number and replace the address information. Menu Option 3 should do the following tasks Reset the file pointer to the beginning of the file. For each record in the file, display the customer number, customer name and all address information. The items isDeleted and newLine are not to be displayed. Modify the following code: #include #include #include using namespace std; const int NAME_SIZE = 20; const int STREET_SIZE = 30; const int CITY_SIZE = 20; const int STATE_CODE_SIZE = 3; struct Customers { long customerNumber; char name[NAME_SIZE]; char streetAddress_1[STREET_SIZE]; char streetAddress_2[STREET_SIZE]; char city[CITY_SIZE]; char state[STATE_CODE_SIZE]; int zipCode; char isDeleted; char newLine; }; void add_data(int no_of_records, ofstream& fout) { struct Customers c; cout << "Enter the Customer data:" << endl; cout << "Name:"; cin >> c.name; cout << "Street Address 1:"; cin >> c.streetAddress_1; cout << "Street Address 2:"; cin >> c.streetAddress_2; cout << "City:"; cin >> c.city; cout << "State:"; cin >> c.state; cout << "Zip Code:"; cin >> c.zipCode; cout << endl; c.customerNumber = no_of_records; c.isDeleted = 'N'; c.newLine = '\n'; cout << "Customer Details are:" << endl; cout << "\tCustomer Number:" << c.customerNumber << endl; cout << "\tName:" << c.name << endl; cout << "\tStreet Address 1:" << c.streetAddress_1 << endl; cout << "\tStreet Address 2:" << c.streetAddress_2 << endl; cout << "\tCity:" << c.city << endl; cout << "\tState:" << c.state << endl; cout << "\tZipCode:" << c.zipCode << endl; fout.write(reinterpret_cast(&c), sizeof(Customers)); } void display_menu(int no_of_records, ofstream& fout) { string choice; cout << "Choose the task:" << endl; cout << "1. Add Data" << endl; cout << "X. Exit Program" << endl; cin >> choice; if (choice == "1") { add_data(no_of_records, fout); no_of_records = no_of_records + 1; display_menu(no_of_records, fout); } else if (choice == "X") { cout << "Done!" << endl; } else { cout << "Invalid Choice!!! Please try again." << endl; display_menu(no_of_records, fout); } } int main() { ofstream fout; fout.open("Customers.dat"); if (!fout) { cout << "Cannot open file!" << endl; return 1; } fout.close(); fout.open("Customers.dat", ios::binary | ios::app); if (!fout) { cout << "Cannot open file to append!" << endl; return 1; } int no_of_records = 0; display_menu(no_of_records, fout); fout.close(); ifstream inputFile; inputFile.open("Customers.dat", ios::binary); if (!inputFile) { cout << "Cannot open the file to read!" << endl; return 1; } Customers c; cout << "Customer Details are:" << endl; while (inputFile.read((char*)&c, sizeof(Customers))) { cout << "\tCustomer Number:" << c.customerNumber << endl; cout << "\tName:" << c.name << endl; cout << "\tStreet Address 1:" << c.streetAddress_1 << endl; cout << "\tStreet Address 2:" << c.streetAddress_2 << endl; cout << "\tCity:" << c.city << endl; cout << "\tState:" << c.state << endl; cout << "\tZipCode:" << c.zipCode << endl; cout << endl; } return 0; }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter8: Arrays
Section: Chapter Questions
Problem 8PE
icon
Related questions
Question

Modify this program to open the file "Customers.dat" so that all data can be read, and data written or appended to this file.

  1. Add an option 2 and 3 to the menu so that the menu appears as below:Menu options '2' and '3' should now be considered a valid choice.

    1. 1. Add Data
      2. Update Data
      3. Display all data
      X. Exit Program
  2. Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber. Use this number as the base when adding new customer data.

  3. The program should be able to start multiple times and add data with increasing customerNumbers. There should be no duplicated customer numbers.

  4. Menu Option 2 should do the following tasks

    1. Prompt for a customer number to change.

    2. Enter the address information ( Street 1 & 2, City , State and Zipcode)

    3. Find the record for the customer number and replace the address information.

  5. Menu Option 3 should do the following tasks

    1. Reset the file pointer to the beginning of the file.

    2. For each record in the file, display the customer number, customer name and all address information. The items isDeleted and newLine are not to be displayed.

Modify the following code:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 3;

struct Customers {
long customerNumber;
char name[NAME_SIZE];
char streetAddress_1[STREET_SIZE];
char streetAddress_2[STREET_SIZE];
char city[CITY_SIZE];
char state[STATE_CODE_SIZE];
int zipCode;

char isDeleted;
char newLine;
};

void add_data(int no_of_records, ofstream& fout) {
struct Customers c;
cout << "Enter the Customer data:" << endl;
cout << "Name:";
cin >> c.name;
cout << "Street Address 1:";
cin >> c.streetAddress_1;
cout << "Street Address 2:";
cin >> c.streetAddress_2;
cout << "City:";
cin >> c.city;
cout << "State:";
cin >> c.state;
cout << "Zip Code:";
cin >> c.zipCode;
cout << endl;

c.customerNumber = no_of_records;
c.isDeleted = 'N';
c.newLine = '\n';

cout << "Customer Details are:" << endl;
cout << "\tCustomer Number:" << c.customerNumber << endl;
cout << "\tName:" << c.name << endl;
cout << "\tStreet Address 1:" << c.streetAddress_1 << endl;
cout << "\tStreet Address 2:" << c.streetAddress_2 << endl;
cout << "\tCity:" << c.city << endl;
cout << "\tState:" << c.state << endl;
cout << "\tZipCode:" << c.zipCode << endl;

fout.write(reinterpret_cast<char*>(&c), sizeof(Customers));
}

void display_menu(int no_of_records, ofstream& fout) {
string choice;
cout << "Choose the task:" << endl;
cout << "1. Add Data" << endl;
cout << "X. Exit Program" << endl;
cin >> choice;
if (choice == "1") {
add_data(no_of_records, fout);
no_of_records = no_of_records + 1;
display_menu(no_of_records, fout);
}
else if (choice == "X") {
cout << "Done!" << endl;
}
else {
cout << "Invalid Choice!!! Please try again." << endl;
display_menu(no_of_records, fout);
}
}

int main() {
ofstream fout;
fout.open("Customers.dat");
if (!fout) {
cout << "Cannot open file!" << endl;
return 1;
}
fout.close();
fout.open("Customers.dat", ios::binary | ios::app);
if (!fout) {
cout << "Cannot open file to append!" << endl;
return 1;
}
int no_of_records = 0;
display_menu(no_of_records, fout);
fout.close();

ifstream inputFile;
inputFile.open("Customers.dat", ios::binary);
if (!inputFile) {
cout << "Cannot open the file to read!" << endl;
return 1;
}


Customers c;
cout << "Customer Details are:" << endl;
while (inputFile.read((char*)&c, sizeof(Customers))) {
cout << "\tCustomer Number:" << c.customerNumber << endl;
cout << "\tName:" << c.name << endl;
cout << "\tStreet Address 1:" << c.streetAddress_1 << endl;
cout << "\tStreet Address 2:" << c.streetAddress_2 << endl;
cout << "\tCity:" << c.city << endl;
cout << "\tState:" << c.state << endl;
cout << "\tZipCode:" << c.zipCode << endl;
cout << endl;
}
return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Graphical User Interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,