Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Book.java
- -----------------------------------------------------------------
- public class Book {
- private static int idCount = 0;
- private int id;
- private String name;
- private String author;
- private int pageCount;
- public Book(String name, String author, int pageCount){
- this.name = name;
- this.author = author;
- this.pageCount = pageCount;
- this.id = idCount;
- idCount += 1;
- }
- @Override
- public String toString() {
- return String.format("id: %d, name: %s, author: %s, number of pages: %d",id ,name, author, pageCount);
- }
- public static int getBookCount(){
- return idCount;
- }
- }
- -------------------------------------------------------------
- Tester.java
- -------------------------------------------------------------
- public class Tester {
- public static void main(String[] args){
- Book b1 = new Book("Book1", "Author1", 10000);
- Book b2 = new Book("Book2", "Author2", 25);
- Book b3 = new Book("Book3", "Author2", 25);
- Book b4 = new Book("Book4", "Author2", 25);
- Book b5 = new Book("Book5", "Author2", 25);
- System.out.println(b1);
- System.out.println(b5);
- System.out.println(b3);
- System.out.println(b4);
- System.out.println(b2);
- System.out.println("the total number of book is: "+Book.getBookCount());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement