阅读测试程序,设计一个Book类。
函数接口定义:
class Book{}
该类有 四个私有属性 分别是 书籍名称、 价格、 作者、 出版年份,以及相应的set 与get方法;该类有一个含有四个参数的构造方法,这四个参数依次是书籍名称、 价格、 作者、 出版年份 。
裁判测试程序样例:
import .*;
public class Main {
public static void main(String[] args) {
List books=new ArrayList();
Scanner in=new Scanner();
for(int i=0;i<5;i++)
{ String str=();
String []data=(",");
Book book=new Book(data[0],(data[1]),data[2],(data[3]));
(book);
}
(totalprice(books));
}
/*计算所有book的总价*/
public static int totalprice(List <Book>books)
{ int result=0;
for(int i=0;i<();i++){result+=(i).getPrice();}
return result;
}
}
/* 请在这里填写答案 */
输入样例:
三体,100,无名氏,1998
上下五千年,50,编辑部,2015
海底世界,50,无名氏2,2000
三体1,100,无名氏3,2017
三体3,100,无名氏4,1998
输出样例:
400
结尾无空行
class Book{
//书籍名
private String name;
//价格
private int price;
//作者
private String author;
//出版年份
private int year;
//有参构造方法
Book(String name,int price,String author,int year){
this.name = name;
this.price = price;
this.author = author;
this.year = year;
}
//四个成员相应的set与get方法
//name
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//price
public void setPrice(int price){
this.price = price;
}
public int getPrice(){
return price;
}
//autor
public void setAuthor(String author){
this.author = author;
}
public String getAuthor(){
return author;
}
//year
public void setYear(int year){
this.year = year;
}
public int getYear(){
return year;
}
}