Java - 超级类的打印格式

时间:2014-12-08 18:50:58

标签: java bluej

我的MediaCollection的打印输出格式不是我想要的格式。当我从文件中打印MediaCollection详细信息时,它确实显示了这个:

MediaCollection printout

我宁愿让它显示出类似的东西:

DVD only printout


这是我的超级

public class MediaFormat
{
    public String title;
    public int releaseDate;
    public double price;

    public MediaFormat(String title, int releaseDate, double price)
    {
        this.title = title;
        this.releaseDate = releaseDate;
        this.price = price;
    }

    /**
     * Change the title.
     */

    public void changeTitle(String title)
    {
        this.title = title;
    }

    /**
     * Change Release Date.
     */

    public void changeReleaseDate (int releaseDate)
    {
        this.releaseDate = releaseDate;
    }

    /**
     * Return Price of a CD or DVD.
     */

    public double getPrice()
    {
        return price;
    }

    public String toString()
    {
        return ("\n" + "Title - " + title + "\n" + "Release Date - " + releaseDate + "\n" +    "Price - " + price + "\n"); 
    }
 }

这是我的CD类

public class CD extends MediaFormat
{
    // Defining variables
    public String artist;
    public int playingTime;
    public int nuOfTracks;

    /**
     * Create new constructor for CD object, with all appropriate details.
     */

    public CD(int releaseDate, String title, String artist, int playingTime, int nuOfTracks, double price)
    {
        super(title, releaseDate, price);
        this.artist = artist;
        this.playingTime = playingTime;
        this.nuOfTracks = nuOfTracks;
    }

    /**
     * Change the Artista Name.
     */

    public void changeArtist(String Artist)
    {
        this.artist = artist;
    }

    /**
     * Change Playing Time Duration.
     */

    public void changePlayingTime(int playingTime)
    {
        this.playingTime = playingTime;
    }

    /**
     * Change the number of tracks.
     */

    public void changenuOfTracks(int nuOfTracks)
    {
        this.nuOfTracks = nuOfTracks;
    }

    public String toString()
    {
        String additionals = super.toString();
        return additionals + ("\n" + "Artist - " + artist + "\n" + "Playing Time - " + playingTime + "min" + "\n" + "nuOfTracks - " + nuOfTracks + "\n"); 
    }
}

这是我的DVD课程

public class DVD extends MediaFormat
{
    public String director;
    public String rating;
    public int duration;

    /**
     * Create new constructor for DVD object, with all appropriate details.
     */

    public DVD(int releaseDate, String rating, String director, String title, int duration, double price)
    {
        super(title, releaseDate, price);
        this.rating = rating;
        this.director = director;
        this.duration = duration;
    }

    /**
     * Change the director.
     */

    public void changeDirector(String director)
    {
        this.director = director;
    }

    /**
     * Change rating details.
     */

    public void changeRating(String rating)
    {
        this.rating = rating;
    }

    /**
     * Change duration.
     */

    public void changeDuration(int duration)
    {
        this.duration = duration;
    }

    public String toString()
    {
        String additionals = super.toString();
        return additionals =
        ("\n" + "Rating - " + rating + "\n" + "Director - " + director + "\n" + "Duration - " + duration + "\n"); 
    }
 }

这是我的Media Collection类

public class MediaCollection 
{    
    //declaration of Media collection
    //this is an array + total price

    private ArrayList<MediaFormat> collection = new ArrayList<MediaFormat>();
    private double totalPrice;

    /**
     * Constructor for objects of class MediaCollection
     */
    public MediaCollection()
    {        
    }

    /**
     * reads in DVD information to the collection from a file 
     * @param  inputFileName  the name of the file containing 
     * the DVD information
     */ 
    public void readInMedia(String inputFileName)
    {
        FileReader reader;
        try
        {
            reader = new FileReader(inputFileName);
            Scanner in = new Scanner(reader);
            while (in.hasNextLine())
            {
                String mediaInfo = in.nextLine();
                Scanner fields = new Scanner(mediaInfo).useDelimiter("\\s*;\\s*");

                String mediaType = fields.next().trim();

                if(mediaType.equals("DVD")){
                    int releaseDate = fields.nextInt();
                    String rating = fields.next().trim();
                    String director = fields.next().trim();
                    String title = fields.next().trim();
                    int duration = fields.nextInt();
                    double price = fields.nextDouble();

                    //create a DVD object and add it to your collection.
                    DVD d = new DVD(releaseDate, rating, director, title, duration, price);
                    collection.add(d);
                    totalPrice = totalPrice + price;
                }

                if(mediaType.equals("CD")){
                    int releaseDate = fields.nextInt();
                    String title = fields.next().trim();
                    String artist = fields.next().trim();
                    int playingTime = fields.nextInt();
                    int nuOfTracks = fields.nextInt();
                    double price = fields.nextDouble();

                    //create a CD object and add it to the collection.
                    CD c = new CD(releaseDate, title, artist, playingTime, nuOfTracks, price);
                    collection.add(c);
                    totalPrice = totalPrice + price;
                }

            }
            reader.close();
        }
        catch (IOException exception)
        {
            System.out.println("Error processing file" 
                + exception);
        }
        catch (NoSuchElementException exception)
        {
            System.out.println("incorrect file format" 
                + exception);
        }
        catch (NumberFormatException exception)
        {
            System.out.println("Input was not a number" 
                + exception);
        }
    }

    /**
     * This is the for loop which prints details for each instance in the collection.
     */
    public void printMediaList()
    {
        for (int i=0; i< (collection.size()); i++)
        {
            System.out.println(collection.get(i).toString());
        }
    }

    public void printTotal()
    {
        System.out.println("--------------------------------");
        System.out.println("Total Price in pounds - ");
        System.out.printf("%.2f", totalPrice);
    }
 }

1 个答案:

答案 0 :(得分:1)

您的DVD.java和CD.java存在一个小问题。这是变化。

CD.java

 public String toString()
    {
        return  ("Title - "+title + "\nArtist - " + artist + "\nRelease Date"+ releaseDate+"\nNumber Of Tracks - " +nuOfTracks+"\nDuration - " + playingTime + "min" + "\nPrice - $"+price+"\n---------------------------------------------------------------------");
    }

DVD.java

 public String toString()
    {
        return  ("Title - "+title + "\nDirector - " + director + "\nRelease Date - "+ releaseDate+"\nRating - " +rating+"\nDuration - " + duration + "min" + "\n"+"Price - $"+price)+"\n---------------------------------------------------------------------";
    }

您无需在toString中调用super。由于DVD​​和CD扩展了MediaFormat类,因此MediaFormat的属性在子类(DVD和CD)中可见。输出的小格式会得到您想要的结果

输出:请注意我的键盘上没有英镑符号,因此请用$;替换它;请在CD和DVD java文件中相应更改

Title - The Shawshank Redemption
Director - Frank Darabont
Release Date - 1994
Rating - 15
Duration - 142min
Price - $3.0
---------------------------------------------------------------------
Title - The Godfather
Director - Francis Ford Coppola
Release Date - 1972
Rating - 18
Duration - 175min
Price - $9.52
---------------------------------------------------------------------
Title - Popular Problems
Artist - Leonard CohenRelease Date2014
Number Of Tracks - 9
Duration - 33min
Price - $7.98
---------------------------------------------------------------------
Title - Pulp Fiction
Director - Quentin Tarantino
Release Date - 1994
Rating - 18
Duration - 154min
Price - $4.97
---------------------------------------------------------------------
Title - St Dominic's Preview
Artist - Van MorrisonRelease Date1972
Number Of Tracks - 7
Duration - 40min
Price - $14.75
---------------------------------------------------------------------