1. 定義一個表示圖書的類,名字為Book,屬性包含:書名、價格、出版社、作者
1.? ? ? ? ?定義一個表示圖書的類,名字為Book,屬性包含:書名、價格、出版社、作者等信息,編寫一個方法,實現輸出一本書的基本信息的功能,要求屬性都是私有的,為私有屬性提供訪問器方法。編寫測試類使用Book類創(chuàng)建對象,使用構造方法給屬性賦值,并測試對象的全部方法。
package suannaidezuoye;
import java.util.*;
public class Main {
? ? public static void main(String[] args) {
? ? ? ? List <Book>books=new ArrayList<Book>();
? ? ? ? Scanner in=new Scanner(System.in);
? ? ? ? for(int i=0;i<5;i++)
? ? ? ? {? ? String str=in.nextLine();
? ? ? ? ? ? String []data=str.split(",");
? ? ? ? ? ? Book book=new Book(data[0],Integer.parseInt(data[1]),data[2],Integer.parseInt(data[3]));
? ? ? ? ? ? books.add(book);
? ? ? ? }
? ? ? ? System.out.println(totalprice(books));? ??
? ? }
?
? ? public static int totalprice(List <Book>books)?
? ? {? int result=0;
? ? ? ? for(int i=0;i<books.size();i++){result+=books.get(i).getPrice();}
? ? ? ? return result;
? ? }
}
package suannaidezuoye;
class Book {
? ? private String name;
? ? private int price ;
? ? private String auther;
? ? private int time;
? ? public void setName(String name){
? ? ? ? this.name=name;
? ? }
? ? public String getName(){
? ? ? ? return name ;
? ? }
? ??
? ? public void setPrice(int price){
? ? ? ? this.price=price;
? ? }
? ? public int getPrice(){
? ? ? ? return price ;
? ? }
? ? public void setAuther(String auther){
? ? ? ? this.auther=auther;
? ? }
? ? public String getAuther(){
? ? ? ? return auther ;
? ? }
? ? public void setTime(int time){
? ? ? ? this.time=time;
? ? }
? ? public int getTime(){
? ? ? ? return time ;
? ? }
? ??
? ? public Book(String name, int price, String auther, int time) {
? ? ? ? this.name = name;
? ? ? ? this.price = price;
? ? ? ? this.auther = auther;
? ? ? ? this.time = time;
? ? }
}