The design of the Book class (Java)

Read the test program and design a Book class.

Function interface is defined:
class {} Book
class has attributes are four private book name , price , author , year of publication , and the corresponding set and get methods; class has a constructor parameter containing four, four parameter followed by book name , price , author , year of publication .

Sample referee test procedure:


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

    /*计算所有book的总价*/
    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;
    }
}

/* 请在这里填写答案 */

Input example: Trisomy
, 100, Anonymous, 1998
Five Thousand Years, 50, Editorial Office, 2015
Underwater World, 50, Anonymous 2, 2000
Trisomy 1,100, Anonymous
3, 2017 Trisomy 3,100, Anonymous 4, 1998

Sample output:
400

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

}

Guess you like

Origin blog.csdn.net/weixin_51430516/article/details/115119865