Files


Classwork

Part A

Create a class HouseListing that stores an address, an asking price, a number of bedrooms, and a number of baths.

In HouseListing, add a static method readFile that returns an ArrayList of HouseListings, read in from a file (chosen with a JFileChooser) where the file is of the comma-separated form:

1313 Mockingbird Lane,312000,5,3
23 The Pines,252000,3,1
6 French Ave.,188000,2,2
19 Madison St.,239000,3,2

(You may assume that every line of the file is in exactly the right format.)

Manually create such a file with at least 10 addresses.

In a class with a main, have the file read in and then print the list of HouseListings.

 Part B

Make HouseListing serializable.

In the same main, serialize your ArrayList of HouseListings to a file (chosen with a JFileChooser).

In another class with a main (in the same package), read in a serialized file (chosen with a JFileChooser) of HouseListings into an ArrayList and print them.

 EC+30  Then save this list of HouseListings in a file in the comma-separated format as in Part A.

 


Homework

Part A

In a project, create a class Post, representing a post on a social media site.  Give it a String content (the text of the post), an int likes (number of people who liked the post), and an ArrayList of Strings tags (text tags associated with the post). 

Design a format for saving posts to text files  given that

Add to Post a method inFromFile, which takes a String of your format and sets up the Post's instance variables, throwing an InvalidArgumentException if any part of it is not formatted properly.

Add to Post a method outToFile which returns a String of  your format based on the Post's instance variables.

Add to Post a static method readPostFile which takes a String filename and returns an ArrayList of Posts.  In the method, read the file of Posts (if it exists) into the ArrayList.

Add to Post a static method writePostFile which takes an ArrayList of Posts and  a String filename.  In the method, write the ArrayList into the file of Posts (you don't need to worry about the case where the file already exists).

Part B

Create a class Poster, representing someone using a social media site.  Give it a String username and an ArrayList of Posts. 

Include no default constructor, but one that takes a String for the username.  Assume that Posts will be saved in a filename of the form username-posts.txt (if you include no path, it will usually look for the file in the top of your project directory).   Using the isFile method of the File class, check whether this file exists.  If so, open it and read in the posts from it to the ArrayList.

Add a method createPost which gets the information for a Post from the user (Posts always start with 0 likes).  Allow any number of tags.  Do not accept input that would mess up your file format.  Add the Post to the ArrayList.

Add a method archivePosts.  If there are no posts in the ArrayList, tell the user there is nothing to archive.  Otherwise, save all the Posts from the ArrayList into username-posts.txt

In a main, create a Poster and test that creating and archiving posts work.  The project you turn in should include a file so that when this main runs, the Poster you create starts off with at least two posts loaded from a file.