Description: Assume you are the owner of Tower Records, which sells music CDs an
ID: 3684044 • Letter: D
Question
Description:
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.
Objective:
Write a Java program to read from your inventory text file (here is an example albums.txt file [https://cse.sc.edu/~pade/csce145/assign/Lab20/albums.txt]) which contains information about albums and CDs, including their cost.
Update the cost of the album according to the following rules:
----If an album is between one and five years old, change the cost to show a discount of 5%.
----If an album is between 5 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 to the console
Each record is represented by four lines of text in the files:
1. The title of the album
2. The artist/group name
3. The year of release
4. The price of the album
Explanation / Answer
import java.io.*;
class Test3
{
public static void main(String args[])
{
try{
// Open the file
FileInputStream fstream = new FileInputStream("album.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int l =1; //to manage line number for line 3 as year and line 4 for price
float discount=0.0f;
int year;
float price;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
if(l==3) //to check the year o album
{
try
{
year = Integer.parseInt(strLine);
if(year>=2011 && year <=2015)
{
discount=0.05f;
}
if(year>=2006 && year <=2010)
discount=0.10f;
if(year<2010)
discount=0.50f;
}
catch(Exception e) { }
}
if(l==4) //to manage discount of the album
{
try
{
price = Float.floatValue(strLine); //pick float value of string read from file
price=price - (price * discount);
strLine=String.valueOf(price); //again convert float to string so that it can be printed on console
}
catch(Exception e){System.out.println("Invalid number format");}
l=0; //re initilize the line number to 0 so that it can be picked for next album
}
// Print the content on the console
System.out.println (strLine);
l++;
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}