-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBooks.java
More file actions
40 lines (28 loc) · 1.05 KB
/
AddBooks.java
File metadata and controls
40 lines (28 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
import java.util.ArrayList;
public class AddBooks {
public static void addBook(ArrayList<Books> books){
Scanner sc = new Scanner(System.in);
Books book = new Books();
int ID;
System.out.println("Enter book ID: ");
ID = sc.nextInt();
for (Books IDBooks : books){
if (IDBooks.getID() == ID){
System.out.println("Book with ID " + ID + " already exists.");
return;
}
}
book.setID(ID);
System.out.println("Enter book title: ");
sc.nextLine();
book.setTitle(sc.nextLine());
System.out.println("Enter book author: ");
book.setAuthor(sc.nextLine());
System.out.println("Enter publish date: ");
book.setYear(sc.nextInt());
books.add(book);
System.out.println("\n" + "Book successfully added: '" + book.getTitle() + "' by: " + book.getAuthor() + ", written in " + book.getYear() +
", with ID number: " + book.getID() + "\n");
}
}