Write a command-line utility called cddb to maintain a flat-file database of album information. You will allow the user to view album info, add a new entry, or delete an entry, where each entry is an album w/all of the track information, as follows:
Options, and Descriptions
-l — List album
Display an enumerated, alphabetical list of artists†
You will allow the user to choose an artist by entering the number, or to quit by entering a q
You will then list all albums, by release date, enumerated
Allow the user to choose an album, by #, or return to the menu above (artists) by entering a
If an album is entered, list all songs in that album, by track order. Prompt for input to return to the previous (i.e., the album) menu
-d — Delete album. Similar to above, display menus allowing a user to choose an album to delete
-a — Add album. You’ll prompt for artist, album name, release date, and finally the track list. You may assume that they are entered in track order.
You’ll want to check that the album doesn’t already exist before adding it
-h — Show usage msg and quit
†You may assume that all names are entered exactly. That is, do not worry about inconsistencies in spelling or capitalisation. E.g., Led Zeppelin, LedZeppelin, and led zeppelin are 3 different groups for this assignment.
If no argument is supplied, a bad argument is supplied, or multiple arguments, print a usage message and exit.
File Format
Album information will be stored in a flat file w/the format shown in sample.db:
To simplify things, the filename (location of the DB) will be stored in an exported environment variable, $CDDB .
Target Language
You can do this in any language you choose, including Bash or AWK. The language must be currently hosted on the tux machines.
If you, e.g., do this in Java, then you’ll need to supply a shell script called cddb that invokes your program, the JRE, whatever, w/the arguments.
Expert Answer
Please comment if you need more help. This is Java Programme
How to Run :-
—————————————-
1. Crate a new Folder , copy all files here only
2.Crate Java files Album.java , AlbumManager.java and AlbumDemo.java with any Text Editor and Copy paste the Source code below in Respective Java file
3. Create a file “MyPlayList.sh” in any text editor (Script to run is mentioned below please check)
copy script file text into it and save
4. If you are using Linux machine
4.1 Open Terminal
4.2 CD to your folder
4.3 run command sh ./MyPlayList.sh
your programme will run
Script file for execution :
———————————————-
#!/bin/bash
javac Album.java
javac AlbumManager.java
javac AlbumDemo.java
java AlbumDemo
Album.java file :-
————————————————-
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
* This class represents a album
* Contains Artist, Album Name , Release Date and Track list
*/
public class Album implements Comparable{
String artist,albumName;
Date releaseDate;
List<String> trackList = new ArrayList<String>();
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
Album album = (Album)arg0;
return this.releaseDate.compareTo(album.releaseDate);
}
}
AlbumManager.java file :-
————————————————-
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/*
* This class manages all activities of Albums
* Add / Remove/ Get Album ….
*/
public class AlbumManager {
//Album List
private List<Album> albumList = new ArrayList<Album>();
//Java Comporator to sort the list simply
Comparator<Album> albumListComporator = new Comparator<Album>() {
public int compare(Album album1, Album album2) {
// Compare release dates of Albumes , based on this Java will sort the list
return album1.releaseDate.compareTo(album2.releaseDate);
}
};
Scanner sc=new Scanner(System.in);
public void getAlbum(){
Album album = new Album();
//ask user artist
System.out.println(“Enter artist”);
album.artist = sc.next();
if(album.artist == null || album.artist.trim().equals(“”)){
System.out.println(“Enter valid artist”);
album = null;
//Stop application
System.exit(0);
}
//ask user album name
System.out.println(“Enter album name”);
album.albumName = sc.next();
if(album.albumName == null || album.albumName.trim().equals(“”)){
System.out.println(“Enter valid album name”);
album = null;
//Stop application
System.exit(0);
}
if(checkAlbumExist(album.albumName)){
album = null;
return;
}
//ask user date
System.out.println(“Enter album creation Date in yyyy-MM-dd format”);
//If user enters wrong date then Exception will come and Programme should terminate
try{
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Date date1 = sdf.parse(sc.next());
album.releaseDate = date1;
}
catch(Exception e){
System.out.println(“Enter valid Date”);
album = null;
//Stop application
System.exit(0);
}
//Ask user for Track list , user will enter a name or ‘quit’ to quit
while(true){
System.out.println(“Enter artist or ‘quit’ to stop entering”);
String track = sc.next();
if(track == null || track.trim().equals(“”)){
System.out.println(“Enter valid track name”);
album = null;
//Stop application
System.exit(0);
}
else if (track.trim().equalsIgnoreCase(“quit”)){
albumList.add(album);
break;
}
else{
album.trackList.add(track);
}
}
}
public boolean checkAlbumExist(String name){
boolean found = false;
for(int i = 0 ; i < albumList.size() ; i++){
if(albumList.get(i).albumName.equalsIgnoreCase(name)){
found = false;
break;
}
}
return found;
}
public void showAlbums(){
System.out.println(“Sno | Atrist | Album Name | Release Date “);
Collections.sort(albumList);
for(int i = 0 ; i < albumList.size() ; i++){
System.out.println(Integer.toString(i+1) + “. ” + albumList.get(i).artist +” ” + albumList.get(i).albumName + ” ” + albumList.get(i).releaseDate.toString());
}
}
public void showTracks(int albumNum){
for(int i = 0 ; i < albumList.get(albumNum).trackList.size() ; i++){
System.out.println( albumList.get(albumNum).trackList.get(i));
}
}
public void deleteAlbum(int albumNum){
albumList.remove(albumNum);
}
public int getAlbumSize(){
return albumList.size();
}
}
AlbumDemo.java file (Main Method) :-
—————————————————————
import java.util.Scanner;
public class AlbumDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AlbumManager manager = new AlbumManager();
Scanner sc=new Scanner(System.in);
while(true){
System.out.println(“Enter your option or ‘-h’ for help”);
String option = sc.next();
if(option == null || option.trim().equals(“”)){
System.out.println(“Enter Valid option”);
continue;
}
if(option.equals(“-h”)){
System.out.println(“-a — Add album”);
System.out.println(“-l — List album”);
System.out.println(“-d — Delete album”);
}
else if(option.equals(“-a”)){
manager.getAlbum();
}
else if(option.equals(“-l”)){
if(manager.getAlbumSize() == 0){
System.out.println(“No Albums found in Database”);
continue;
}
manager.showAlbums();
System.out.println(“Enter Album Number to select or ‘q’ to quit”);
String achoice = sc.next();
if(achoice == null || achoice.trim().equals(“”)){
System.out.println(“Enter Valid option”);
System.exit(0);
}
if(achoice.trim().equals(“q”)){
System.exit(0);
}
else{
int albumNum = 0;
try{
albumNum = Integer.parseInt(achoice);
}
catch(Exception e){
System.out.println(“Enter Valid option”);
System.exit(0);
}
manager.showTracks(albumNum-1);
System.out.println(“Enter ‘a’ to return next menu”);
achoice = sc.next();
if(achoice == null || achoice.trim().equals(“”)){
System.out.println(“Enter Valid option”);
System.exit(0);
}
else if(achoice.trim().equals(“a”)){
continue;
}
else{
System.out.println(“Enter Valid option”);
System.exit(0);
}
}
}
else if(option.equals(“-d”)){
if(manager.getAlbumSize() == 0){
System.out.println(“No Albums found in Database”);
continue;
}
System.out.println(“Enter Album Number to delete”);
String achoice = sc.next();
if(achoice == null || achoice.trim().equals(“”)){
System.out.println(“Enter Valid option”);
System.exit(0);
}
int albumNum = 0;
try{
albumNum = Integer.parseInt(achoice);
}
catch(Exception e){
System.out.println(“Enter Valid option”);
System.exit(0);
}
manager.deleteAlbum(albumNum-1);
}
}
}
}
Sample Output :-
—————————————
Enter your option or '-h' for help -a Enter artist Ram Enter album name RamAlbum Enter album creation Date in yyyy-MM-dd format 2017-09-23 Enter artist or 'quit' to stop entering Ar1 Enter artist or 'quit' to stop entering Ar2 Enter artist or 'quit' to stop entering Ar3 Enter artist or 'quit' to stop entering quit Enter your option or '-h' for help -l Sno | Atrist | Album Name | Release Date 1. Ram RamAlbum Sat Sep 23 00:00:00 IST 2017 Enter Album Number to select or 'q' to quit 1 Ar1 Ar2 Ar3 Enter 'a' to return next menu a Enter your option or '-h' for help -h -a — Add album -l — List album -d — Delete album Enter your option or '-h' for help -d Enter Album Number to delete 1