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: 3630804 • 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.

Explanation / Answer

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class album { public static void main(String[] args){ String fileName = "albums.txt"; Scanner inputStream = null; System.out.println("The file: " + fileName + " contains the following lines: "); int date=0; double price=0; try{ inputStream = new Scanner(new File(fileName)); } catch(FileNotFoundException e){ System.out.println("Error opening the file " + fileName); System.exit(0); } while(inputStream.hasNextLine()){ String line = inputStream.nextLine(); System.out.println(line); line=inputStream.nextLine(); System.out.println(line); line = inputStream.nextLine(); date=Integer.parseInt(line); System.out.println(line); line= inputStream.nextLine(); price=Double.parseDouble(line.substring(1)); System.out.println(line); if(date 2010-5){ price = price -(price * 0.05); System.out.println(price); } else if(date 2010-10){ price = price - (price * 0.1); System.out.println(price); } else { price = price - (price * 0.5); System.out.println(price); } } inputStream.close(); } }
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