gpt4 book ai didi

Java - 具有父类(super class)的打印格式

转载 作者:行者123 更新时间:2023-12-01 12:09:14 25 4
gpt4 key购买 nike

我的 MediaCollection 的打印输出不是我想要的格式。当我从文件打印 MediaCollection 详细信息时,它确实显示以下内容:

MediaCollection printout

我更希望它显示类似的内容:

DVD only printout

<小时/>

这是我的 super 类(class)

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 类(class)

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 类(class)

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");
}
}
<小时/>

这是我的媒体 Collection 类(class)

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);
}
}

最佳答案

您的 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
---------------------------------------------------------------------

关于Java - 具有父类(super class)的打印格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27364837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com