Вы находитесь на странице: 1из 5

Đây là dạng bài thi mà các bạn sẽ thi trong môn Learn Java By Example!

Đây là bài quản lý sách mà tôi đã viết có in ra theo bảng như C và bắt lỗi nhập vào !
#########################################
Đầu tiên là Book.java
##############################################3
import java.util.Scanner;
class InvalidPriceException extends Exception{
String errMsg;
InvalidPriceException(String errMsg){
this.errMsg=errMsg;
}
public String getMsg(){
return errMsg;
}
}
public class Book {
String title;
String author;
String email;
int id;
float price;
Book(){
title=author=email="";
id=0;
price=0.0f;
}
Book(String title,String author,String email,int id,float price) throws InvalidPriceException{
if(price<0) throw new InvalidPriceException("Price must >0");
this.title=title;
this.author=author;
this.email=email;
this.id=id;
this.price=price;
}
public void addNew(){
Scanner input=new Scanner(System.in);
System.out.print("\n Enter title:");
title=input.nextLine();
while(this.title.equalsIgnoreCase("")){
System.out.print("\n Title is not blank!");
System.out.print("\n Enter title:");
title=input.nextLine();
}
System.out.print("\n Enter author:");
author=input.nextLine();
System.out.print("\n Enter email:");
email=input.nextLine();
while(this.email.equalsIgnoreCase("")){
System.out.print("\n Email is not blank!");
System.out.print("\n Enter email:");
email=input.nextLine();
}
int count=0;
for(int i=0;i<this.email.length();i++)
if(this.email.charAt(i)=='@')
count++;
if(count!=1){
System.out.print("\n Email must have @");
System.out.print("\n Enter email:");
email=input.nextLine();
}
System.out.print("\n Enter id:");
id=input.nextInt();
System.out.print("\n Enter price:");
price=input.nextFloat();
assert(price>0):"Price must have >0";
}
public void display(){
System.out.printf("\n|%4d|%-10s|%-10s|%-15s|%11.1f|",id,title,author,email,price);
}

}
###########################################
Thu 2 là BookCatalog.java
#########################################
import java.util.Scanner;
public class BookCatalog {
int counter;
int limit;
Book[] book;
BookCatalog(){
counter=0;
limit=10;
book=new Book[limit];
}
public void addBook(){
if(counter==limit)
System.out.print("\n Not have spaces to add!");
else{
book[counter]=new Book();
book[counter].addNew();
counter++;

}
}
public void displayAll(){
if(counter==0)
System.out.print("\n Book catalog is empty.");
else{
System.out.print("\n-------------------------------------------------------");
System.out.print("\n|ID | Title | Author | Email | Price |");
System.out.print("\n-------------------------------------------------------");
for(int i=0;i<counter;i++)
book[i].display();
System.out.print("\n-------------------------------------------------------");
}
}
public void searchBook(){
int status=0;
Scanner input=new Scanner(System.in);

if(counter>0){
System.out.print("\n Enter title:");
String title=input.nextLine();
for(int i=0;i<counter;i++)
if(book[i].title.equals(title)){
System.out.print("\n-------------------------------------------------------");
System.out.print("\n|ID | Title | Author | Email | Price |");
System.out.print("\n-------------------------------------------------------");
book[i].display();
System.out.print("\n-------------------------------------------------------");
status=1;
}
if(status!=1)
System.out.print("\n Not found!");
}
else
System.out.print("\n Book catalog is empty.");
}
public void updateBook(){
int status=0;
Scanner input=new Scanner(System.in);
if(counter==0)
System.out.print("\n Not have book in catalog!");
else{
System.out.print("\n Enter id to update:");
int id=input.nextInt();
for(int i=0;i<counter;i++)
if(book[i].id==id){
Scanner input1=new Scanner(System.in);
System.out.print("\n Enter new title:");
String newTitle=input1.nextLine();
book[i].title=newTitle;
System.out.print("\n Update sucessfully!");
status=1;
break;
}

}
if(status==0)
System.out.print("\n Not book!");
}
}
################################################
Thứ 3 là lớp Test.java
#################################################
import java.util.Scanner;
public class Interface {
public static void main(String[] args){
int choice;
Scanner input=new Scanner(System.in);
BookCatalog bc=new BookCatalog();
do
{
System.out.print("\n-----------------------------------------");
System.out.print("\n|1. Add a new Book to the Catalog . |");
System.out.print("\n|2. Display catalog . |");
System.out.print("\n|3. Search the book follow title. |");
System.out.print("\n|4. Update the book name follow id. |");
System.out.print("\n|5. Exit the application. |");
System.out.print("\n|Please choose a function: |");
System.out.print("\n-----------------------------------------");
choice=input.nextInt();
switch(choice)
{
case 1:
//System.out.print("\n You have choose function 1");
bc.addBook();
break;
case 2:
//System.out.print("\n You have choose function 2");
bc.displayAll();
// break;
case 3:
// System.out.print("\n You have choose function 3");
bc.searchBook();
break;
case 4:
// System.out.print("\n You have choose function 4");
bc.updateBook();
break;
case 5:
break;
default:
System.out.print("\n You have choose false");
break;
}
}while(choice!=5);
}
}

Вам также может понравиться