By GokiSoft.com|
09:23 28/07/2021|
Java Basic
[Share Code] Examination & Test >> Quản lý sách OOP + Java - Hướng dẫn chữa bài tập - C2010G
Examination & Test >> Quản lý sách OOP + Java
#Utility.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson11;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Utility {
public static String convertDateToString(Date datetime) {
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
return format.format(datetime);
}
public static Date convertStringToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
try {
return format.parse(str);
} catch (ParseException ex) {
Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson11;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Main {
public static List<Book> bookList;
public static List<Author> authorList;
public static Scanner scan;
public static void main(String[] args) {
bookList = new ArrayList<>();
authorList = new ArrayList<>();
scan = new Scanner(System.in);
int choose;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch(choose) {
case 1:
inputBook();
break;
case 2:
displayBook();
break;
case 3:
inputAuthor();
break;
case 4:
findBookByAuthor();
break;
case 5:
System.out.println("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
break;
}
} while(choose != 5);
}
static void showMenu() {
System.out.println("1. Nhap thong tin sach");
System.out.println("2. Hien thi");
System.out.println("3. Nhap thong tin tac gia");
System.out.println("4. Tim kiem theo tac gia");
System.out.println("5. Thoat");
System.out.println("Chon: ");
}
private static void inputBook() {
System.out.println("Nhap so sach can them: ");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
Book book = new Book();
book.input();
bookList.add(book);
}
}
private static void displayBook() {
System.out.println("============= Thong tin sach =============");
for (Book book : bookList) {
book.display();
}
}
private static void inputAuthor() {
System.out.println("Nhap so tac gia can them: ");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
Author author = new Author();
author.input();
authorList.add(author);
}
}
private static void findBookByAuthor() {
System.out.println("Tim quan sach cua tac gia: ");
String nickname = scan.nextLine();
for (Book book : bookList) {
if(book.getNickname().equalsIgnoreCase(nickname)) {
book.display();
}
}
}
public static boolean checkExistAuthor(String nickname) {
if(authorList.size() == 0) return false;
for (Author author : authorList) {
if(author.getNickname().equalsIgnoreCase(nickname)) {
return true;
}
}
return false;
}
}
#Book.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson11;
import java.util.Date;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Book {
String bookName, nickname;
Date publishDate;
public Book() {
}
public Book(String bookName, String nickname, Date publishDate) {
this.bookName = bookName;
this.nickname = nickname;
this.publishDate = publishDate;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Book -> Nhap ten sach: ");
bookName = scan.nextLine();
//Khi nhập tên bút danh của tác giả, nếu chưa tồn tại thì -> Xuất hiện màn hình nhập thông tin cho tác giả đó luôn.
System.out.println("Book -> Nhap nickname: ");
nickname = scan.nextLine();
if(!Main.checkExistAuthor(nickname)) {
//Nhap thong tin moi cho tac gia
Author author = new Author(nickname);
author.inputWithoutNickname();
Main.authorList.add(author);
}
System.out.println("Book -> Nhap ngay xuat ban (dd/mm/YYYY): ");
publishDate = Utility.convertStringToDate(scan.nextLine());
}
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "Book{" + "bookName=" + bookName + ", nickname=" + nickname + ", publishDate=" + Utility.convertDateToString(publishDate) + '}';
}
}
#Author.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson11;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Author {
String fullname, nickname,address;
Date birthday;
public Author() {
}
public Author(String fullname, String nickname, String address, Date birthday) {
this.fullname = fullname;
this.nickname = nickname;
this.address = address;
this.birthday = birthday;
}
public Author(String nickname) {
this.nickname = nickname;
}
public String getFullname() {
return fullname;
}
public String getNickname() {
return nickname;
}
public String getAddress() {
return address;
}
public Date getBirthday() {
return birthday;
}
public void inputWithoutNickname() {
Scanner scan = new Scanner(System.in);
System.out.println("Author -> Nhap ten: ");
fullname = scan.nextLine();
System.out.println("Author -> Nhap dia chi: ");
address = scan.nextLine();
System.out.println("Author -> Nhap ngay sinh (dd/mm/YYYY): ");
birthday = Utility.convertStringToDate(scan.nextLine());
}
public void input() {
inputWithoutNickname();
Scanner scan = new Scanner(System.in);
//Mỗi tác giả có bút danh duy nhất, ko được phép nhập trùng lặp tên bút danh của tác giả
System.out.println("Author -> Nhap nickname: ");
while(true) {
nickname = scan.nextLine();
boolean exist = Main.checkExistAuthor(nickname);
if(exist) {
System.out.println("Author -> Nickname da ton tai -> vui long nhap moi: ");
} else {
break;
}
}
}
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "Author{" + "fullname=" + fullname + ", nickname=" + nickname + ", address=" + address + ", birthday=" + Utility.convertDateToString(birthday) + '}';
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)