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.
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
Write a program in Java using netbeans IDE that stores, retrieves, adds, and updates addresses as...
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 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...
The Exercise Write a Java program that stores and retrieves details about a ``basket'' of items bought in a supermarket. Items should have a code, a name, and a price. Once created, it should be possible to change the price of an item, but not its name or code. 1. Write a class for individual items. Do not forget to include constructors, accessors and mutator methods as required. 2. Create another class to store a list of items (the shopping...
Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes for individuals (sample input data is attached). Store the data in an object designed to store a first name (string), last name (string), and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by a tab character. Then, after the input has been read in, print the list in an appropriate...
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 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, 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 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...
Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following requirement. You and your partner will be writing a simple Java program that implements some basic file I/O operations. You can use this code as the basis for some of your next project. FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each...
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:...