Question

Write a program in Java using netbeans IDE that stores, retrieves, adds, and updates addresses as...

Write a program in Java using netbeans IDE that stores, retrieves, adds, and updates addresses as shown in figure 17.20. Use a fixed length string for storing each attribute in the address. Use random access file for reading and writing an address. Assume that the size of name, street, city, state, and zip is 32, 32,20, 2, 5 chars, respectively.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java program to retrieve,add,update the addresses from the file.

Here there are two Java Classes. 1) Address.java 2) CreateAddressFile.java

Copy these classes to your editor and execute CreateAddressFile.java

Address.java : Class to hold the address properites and functions to write and read from the File.

import java.io.IOException;

import java.io.RandomAccessFile;

class Address {

private String name;

private String street;

private String city;

private String state;

private String zip;

// function to read the random access file and set the address properties.

void read(RandomAccessFile raf) throws IOException {

char[] temp = new char[32];

for (int i = 0; i < temp.length; i++)

temp[i] = raf.readChar();

name = new String(temp);

temp = new char[32];

for (int i = 0; i < temp.length; i++)

temp[i] = raf.readChar();

street = new String(temp);

temp = new char[20];

for (int i = 0; i < temp.length; i++)

temp[i] = raf.readChar();

city = new String(temp);

temp = new char[2];

for (int i = 0; i < temp.length; i++)

temp[i] = raf.readChar();

state = new String(temp);

temp = new char[5];

for (int i = 0; i < temp.length; i++)

temp[i] = raf.readChar();

zip = new String(temp);

}

//function to read the properties and add to the random access file.

void write(RandomAccessFile raf) throws IOException {

StringBuffer sb;

if (name != null)

sb = new StringBuffer(name);

else

sb = new StringBuffer();

sb.setLength(32);

raf.writeChars(sb.toString());

if (street != null)

sb = new StringBuffer(street);

else

sb = new StringBuffer();

sb.setLength(32);

raf.writeChars(sb.toString());

if (city != null)

sb = new StringBuffer(city);

else

sb = new StringBuffer();

sb.setLength(20);

raf.writeChars(sb.toString());

if (state != null)

sb = new StringBuffer(state);

else

sb = new StringBuffer();

sb.setLength(2);

raf.writeChars(sb.toString());

if (zip != null)

sb = new StringBuffer(zip);

else

sb = new StringBuffer();

sb.setLength(5);

raf.writeChars(sb.toString());

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getStreet() {

return street;

}

public void setStreet(String street) {

this.street = street;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public String getState() {

return state;

}

public void setState(String state) {

this.state = state;

}

public String getZip() {

return zip;

}

public void setZip(String zip) {

this.zip = zip;

}

//fucntion to find the size of the file based on the records length.

int size() {

return 2 * (32 + 32 + 20 + 2+ 5);

}

}

======================================================================================

2) CreateAddressFile.java : Class that holds Address data and do create/retrieval/update operations on it in the RandomAccessFile.

/*

* Java Program to Create/Update and Read the Address using RandomAccess File.

*/

import java.io.RandomAccessFile;

public class CreateAddressFile {

public static void main(String[] args) throws Exception {

// Sample data to store 3 address into the file.  

String[] names = { "A", "B", "C" };

String[] streets = { "abc", "def", "xyz" };

String[] cities = { "Boston", "Newyork", "Seatle" };

String[] states = { "LA", "CA", "NY" };

String[] zips = { "12345", "67891", "23456" };

// opening the file address.dat in read/write mode to store addresses.

RandomAccessFile raf = new RandomAccessFile("address.dat", "rw");

Address address = new Address();

for (int i = 0; i < names.length; i++) {

address.setName(names[i]);

address.setStreet(streets[i]);

address.setCity(cities[i]);

address.setState(states[i]);

address.setZip(zips[i]);

address.write(raf);

}

// opening the created file address.dat to read and display the content.

raf = new RandomAccessFile("address.dat", "rw");

address = new Address();

int numRecords = names.length;

for (int i = 0; i < numRecords; i++) {

address.read(raf);

System.out.print(address.getName() + " ");

System.out.print(address.getStreet() + " ");

System.out.print(address.getCity() + " ");

System.out.print(address.getState() + " ");

System.out.println(address.getZip());

}

System.out.println("------------ Update the Street as MNO for the Name :A -------------");

//pointing the file pointer back to start to update the the street as 'MNO' for name 'A'

raf.seek(0);

for (int i = 0; i < numRecords; i++) {

address.read(raf);

if (address.getName().trim().equals("A")) {

address.setStreet("MNO");

raf.seek(raf.getFilePointer() - address.size());

address.write(raf);

raf.seek(raf.getFilePointer() - address.size());

address.read(raf);

}

System.out.print(address.getName() + " ");

System.out.print(address.getStreet() + " ");

System.out.print(address.getCity() + " ");

System.out.print(address.getState() + " ");

System.out.println(address.getZip());

}

}

}

=======================================================================================

OUTPUT

A abc Boston LA 12345

B def Newyork CA 67891

C xyz Seatle NY 23456

------------------------- Update the Street as MNO for the name :A -------------------------------

A MNO Boston LA 12345

B def Newyork CA 67891

C xyz Seatle NY 23456

  

Add a comment
Know the answer?
Add Answer to:
Write a program in Java using netbeans IDE that stores, retrieves, adds, and updates addresses as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java - In NetBeans IDE: • Create a class called Student which stores: - the name...

    Java - In NetBeans IDE: • Create a class called Student which stores: - the name of the student - the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into...

  • signature 1. Create a new NetBeans Java project. The name of the project has to be...

    signature 1. Create a new NetBeans Java project. The name of the project has to be the first part of the name you write on the test sheet. The name of the package has to be testo You can chose a name for the Main class. (2p) 2. Create a new class named Address in the test Two package. This class has the following attributes: city: String-the name of the city zip: int - the ZIP code of the city...

  • In this assignment, you will write one (1) medium size C program. The program needs to...

    In this assignment, you will write one (1) medium size C program. The program needs to be structured using multiple functions, i.e., you are required to organize your code into distinct logical units. The following set of instructions provide the specific requirements for the program. Make sure to test thoroughly before submitting. Write   a   program,   named   program1.c,   that   reads   and   processes   employee   records   (data   about   an   employee).   Each   employee   record   must   be   stored   using   a   struct   that   contains   the   following  ...

  • Assignment 1. You are to write a simple program using Netbeans Java IDE  that consists of two...

    Assignment 1. You are to write a simple program using Netbeans Java IDE  that consists of two classes. The program will allow the user to place an order for a Tesla Model X car. 2. Present the user with a menu selection for each option on the site. We will use JOptionPane to generate different options for the user. See the examples posted for how to use JOPtionPane. 3. Each menu will have multiple items each item will be in the...

  • (JAVA) Using classes and inheritance, design an online address book to keep track of the names

    (JAVA)Using classes and inheritance, design an online address book to keep track of the names, addresses, phone numbers and birthdays of family members, friends and business associates. Your program should be able to handle a maximum of 500 entries.Define the following classes: Class Address to store a street name, city, state and zip code. Class Date to store the day, month and year. Class Person to store a person's last name and first name. Class ExtPerson that extends the class...

  • write in java and please code the four classes with the requirements instructed You will be...

    write in java and please code the four classes with the requirements instructed You will be writing a multiclass user management system using the java. Create a program that implements a minimum of four classes. The classes must include: 1. Employee Class with the attributes of Employee ID, First Name, Middle Initial, Last Name, Date of Employment. 2. Employee Type Class that has two instances of EmployeeType objects: salaried and hourly. Each object will have methods that calculates employees payrol...

  • Write in C++ please: Write a class named MyVector using the following UML diagram and class...

    Write in C++ please: Write a class named MyVector using the following UML diagram and class attribute descriptions. MyVector will use a linked listed instead of an array. Each Contact is a struct that will store a name and a phone number. Demonstrate your class in a program for storing Contacts. The program should present the user with a menu for adding, removing, and retrieving Contact info. MyVector Contact name: string phone: string next: Contact -head: Contact -tail: Contact -list_size:...

  • Write a Java program which will store, manipulate, and print student registration information. As part of...

    Write a Java program which will store, manipulate, and print student registration information. As part of the solution, identify the following classes: Student Admissions. The class Student must have the following fields – Name, Address, Id number, Courses, and Date, where: Name is a user defined class comprising of at minimum first name and last name. Address is a user defined class comprising of fields - street, city, state, and zip code. Date is a predefined class in the java.util...

  • LANGUAGE = JAVA (IDE Eclipse preferred) Lab 21 AEIOU Counter Objective: Write a program that reads...

    LANGUAGE = JAVA (IDE Eclipse preferred) Lab 21 AEIOU Counter Objective: Write a program that reads a file and counts the number of times the vowels ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ occurs exactly in that order. It ignores consonants It ignores any type of space It ignores case The only thing it cannot ignore is if another vowel occurs out of order These count AEIOU aeiou hahehihohu Take it out These do not AEIuO Taco is good Take it over...

  • C++ : Please include complete source code in answer This program will have names and addresses...

    C++ : Please include complete source code in answer This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record. When the program is run, it will search for a birthday or an anniversary using the current date to compare with the saved date. It will then generate the appropriate card message. Because this will be an interactive system, your program should begin by displaying a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT