Thursday, November 4, 2010

Lab 18 -- File Handling -- File Read/Write

Assume you are the owner of Tower Records, which sells music CDs and albums, and you decide to hold a sale to try to reduce your inventory of old records. Write a Java program to read from your inventory text file (refer below for an example file "albums.txt"), which contains information about albums and CDs, including their cost. If an album is between one year and five years old, change the cost to show a discount of 5%. If an album is between 5 years old and 10 years old, give a discount of 10%. If it is more than 10 years old, give a discount of 50%. The album information with the new pricing should be written into a new text file newalbums.txt.

Each record is represented by four lines of text in the files. The first line is the title of the album, the next line is the artist/group name, the next is the year of release, and the next is the price of the album.

Hints: Once you read in each line as a String you can parse the year of release Integer.parseInt(String s) to an int and check how old it is and give a discount on the parsed price (double). Use the substring(int index) method of the String class to get rid of the $ sign in the front of price. Use a loop to do it for all albums.

No comments: