Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume you are the owner of Tower Records, which sells music CDs and albums, and

ID: 3630797 • Letter: A

Question

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 (here is 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.


albums.txt example:

Rock Steady
No Doubt
2002
$18.98
Revolver
The Beatles
1966
$14.00
Dangerously in Love
Beyonce featuring Jean Paul
2003
$18.50
Blood on the Dance Floor: History in the Making
Michael Jackson
1997
$16.50
Master of Puppets
Metallica
1986
$13.29

Explanation / Answer

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader; import java.util.Calendar; public class MusicAlbums { public static void main(String[] args) { String strNewData = ""; try { FileInputStream fstream = new FileInputStream("c:\Album.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; int recorditem = 0; int currentyear=-9999; while ((strLine = br.readLine()) != null) { recorditem++; if(recorditem == 3) { currentyear = Integer.parseInt(strLine); strNewData = strNewData + strLine + " "; } else if(recorditem == 4) { if( currentyear == -9999) { break; } int yearDiff = Calendar.getInstance().get(Calendar.YEAR) - currentyear; strLine = strLine.replace("$", ""); if( yearDiff > 1 && Calendar.YEAR - currentyear 5 && Calendar.YEAR - currentyear 10) { strNewData = strNewData + "$" + (Double.parseDouble(strLine) + (Double.parseDouble(strLine)*0.1)) + " "; } currentyear = -9999; recorditem = 0; } else { strNewData = strNewData + strLine + " "; } } in.close(); //System.out.println (strNewData); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } try { BufferedWriter out = new BufferedWriter(new FileWriter("c:\NewAlbum.txt")); out.write(""+strNewData); out.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote