1 Star 0 Fork 0

CS-IMIS-23/20172314

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
DVDCollection.java 2.51 KB
一键复制 编辑 原始数据 按行查看 历史
Yi 提交于 7年前 . 输出DVD的信息
//*************************************************************************************
// DVDCollection.java Author:Lewis/Loftus
//
// Represents a collection of DVD movies.
//*************************************************************************************
import java.text.NumberFormat;
public class DVDCollection
{
private DVD[] collection;
private int count;
private double totalCost;
//------------------------------------------------------------------------------------
// Constructor: Creates an intially empty collection.
//------------------------------------------------------------------------------------
public DVDCollection()
{
collection = new DVD[100];
count = 0;
totalCost = 0.0;
}
//--------------------------------------------------------------------------------------------
// Adds a DVD to the collection, increasing the size of the collection array if neccessary.
//--------------------------------------------------------------------------------------------
public void addDVD(String title, String director, int year, double cost, boolean bluray)
{
if (count == collection.length)
increaseSize();
collection[count] = new DVD(title, director, year, cost, bluray);
totalCost += cost;
count++;
}
//----------------------------------------------------------------------------------------------------
// Returns a report describing the DVD collection.
//----------------------------------------------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
report += "My DVD Collection\n\n";
report += "Number of DVDs:" + count+"\n";
report += "Total cost:"+fmt.format(totalCost)+"\n";
report += "Average cost:" + fmt.format(totalCost/count);
report += "\n\nDVD List:\n\n";
for (int dvd =0; dvd <count; dvd++)
report += collection[dvd].toString()+ "\n";
return report;
}
//------------------------------------------------------------------------------------------------------------------
// Increase the capacity of the collection by creating a larger array and copying the existing collection into it.
//------------------------------------------------------------------------------------------------------------------
private void increaseSize()
{
DVD[] temp = new DVD[collection.length*2];
for (int dvd = 0; dvd < collection.length; dvd++)
temp[dvd] = collection[dvd];
collection = temp;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172314.git
git@gitee.com:CS-IMIS-23/20172314.git
CS-IMIS-23
20172314
20172314
master

搜索帮助