CD

/*
@startuml

class CD {
    - title: String
    - artist: String
    - cost: double
    - tracks: int
    + CD(title: String, artist: String, cost: double, tracks: int)
    + toString(): String
}

class CDCollection {
    - collection: CD[]
    - count: int
    - totalCost: double
    + CDCollection()
    + addCD(title: String, artist: String, cost: double, tracks: int): void
    + toString(): String
    - increaseSize(): void
}

class Tunes {
    + main(args: String[]): void
}

class Scanner {
}

CDCollection "1" o-- "many" CD
Tunes --> CDCollection
Tunes ..> Scanner : uses

@enduml
 */

//************************************************************
// CD.java
// Represents a music CD with title, artist, price, and track count.
//************************************************************
class CD {

    private final String title;
    private final String artist;
    private final double cost;
    private final int tracks;

    public CD(final String title, final String artist, final double cost, final int tracks) {
        this.title = title;
        this.artist = artist;
        this.cost = cost;
        this.tracks = tracks;
    }

    @Override
    public String toString() {
        final java.text.NumberFormat fmt = java.text.NumberFormat.getCurrencyInstance();
        return String.format("%-25s %-20s %-10s %6d", title, artist, fmt.format(cost), tracks);
    }
}

//************************************************************
// CDCollection.java
// Manages a collection of CD objects, allowing addition and reporting.
//************************************************************
class CDCollection {

    private CD[] collection;
    private int count;
    private double totalCost;

    public CDCollection() {
        collection = new CD[100];
        count = 0;
        totalCost = 0.0;
    }

    public void addCD(final String title, final String artist, final double cost, final int tracks) {
        if (count == collection.length) {
            increaseSize();
        }
        collection[count] = new CD(title, artist, cost, tracks);
        totalCost += cost;
        count++;
    }

    private void increaseSize() {
        final CD[] temp = new CD[collection.length * 2];
        System.arraycopy(collection, 0, temp, 0, collection.length);
        collection = temp;
    }

    @Override
    public String toString() {
        final java.text.NumberFormat fmt = java.text.NumberFormat.getCurrencyInstance();
        final StringBuilder report = new StringBuilder();

        final String header1 = " My CD Collection ";
        final String line1 = "~".repeat((70 - header1.length()) / 2) + header1 + "~".repeat((70 - header1.length() + 1) / 2);
        report.append(line1).append("\n\n");

        report.append(String.format("%-20s: %d\n", "Number of CDs", count));
        report.append(String.format("%-20s: %s\n", "Total cost", fmt.format(totalCost)));
        report.append(String.format("%-20s: %s\n\n", "Average cost", fmt.format(totalCost / count)));

        final String header2 = " CD List ";
        final String line2 = "-".repeat((70 - header2.length()) / 2) + header2 + "-".repeat((70 - header2.length() + 1) / 2);
        report.append(line2).append("\n\n");

        report.append(String.format("%-4s %-25s %-20s %-10s %6s\n", "No.", "Title", "Artist", "Cost", "Tracks"));
        report.append(String.format("%-4s %-25s %-20s %-10s %6s\n",
                "---", "-".repeat(25), "-".repeat(20), "-".repeat(10), "-".repeat(6)));

        for (int i = 0; i < count; i++) {
            report.append(String.format("%-4d %s\n", i + 1, collection[i].toString()));
        }

        return report.toString();
    }
}

//************************************************************
// Tunes.java
// Interactive CD collection builder.
//************************************************************
public class Tunes {

    public static void main(final String[] args) {
        final CDCollection music = new CDCollection();

        System.out.println("Welcome to your CD Collection Manager.");
        System.out.println("Type 'done' as CD title to finish.\n");

        try (java.util.Scanner scanner = new java.util.Scanner(System.in)) {
            while (true) {
                System.out.print("Enter CD title (or 'done' to finish): ");
                final String title = scanner.nextLine().trim();
                if (title.equalsIgnoreCase("done")) {
                    break;
                }
                if (title.isEmpty()) {
                    System.out.println("Title cannot be empty.");
                    continue;
                }

                System.out.print("Enter artist name: ");
                final String artist = scanner.nextLine().trim();
                if (artist.isEmpty()) {
                    System.out.println("Artist name cannot be empty.");
                    continue;
                }

                System.out.print("Enter CD cost: ");
                double cost;
                try {
                    cost = Double.parseDouble(scanner.nextLine().trim());
                    if (cost < 0) {
                        System.out.println("Cost cannot be negative.");
                        continue;
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Invalid cost. Please enter a valid number.");
                    continue;
                }

                System.out.print("Enter number of tracks: ");
                int tracks;
                try {
                    tracks = Integer.parseInt(scanner.nextLine().trim());
                    if (tracks <= 0) {
                        System.out.println("Track count must be positive.");
                        continue;
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Invalid track count. Please enter an integer.");
                    continue;
                }

                music.addCD(title, artist, cost, tracks);
                System.out.println("CD added!\n");
            }
        }

        System.out.println("\nYour Final CD Collection:");
        System.out.println(music);
    }
}

 

Scroll to Top