Ai
2 Star 0 Fork 0

CS-IMIS-23/20172309_javaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
DVDCollection.java 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
20172309 提交于 2018-04-12 20:37 +08:00 . 例子 8.8 构建类 DVDCollection
//****************************************************************************************
//DVDCollection.java Author:WZW
//
//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 initially 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 necessary.
//------------------------------------------------------------------------------------
public void addDVD(String title, String director, int year, double cost, boolean bluray) {
if (count == collection.length)
increasingSize();
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\n DVD List:\n\n";
for (int dvd = 0; dvd < count; dvd++)
report += collection[dvd].toString() + "\n";
return report;
}
//---------------------------------------------------------------------------------------
//Increases the capacity of the collection by creating a
//larger array and copying the existing collection into it.
//---------------------------------------------------------------------------------------
private void increasingSize()
{
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/20172309_javaProgramming.git
git@gitee.com:CS-IMIS-23/20172309_javaProgramming.git
CS-IMIS-23
20172309_javaProgramming
20172309_javaProgramming
master

搜索帮助