手记

借书系统整个流程完善

package www.imooc.book;

import java.util.Scanner;
import java.util.zip.Inflater;

public class User {
boolean fale = true;
String whatis= ("请问您是否需要租书?\t1:是\t2:否");
String bookis= ("请输入您需要借用的书籍。\t1:序号\t2:书名");
String bookid=("请输入您需要借用的书籍序号");
public Books [] books = {new Books(1, "红楼梦"),
new Books(2, "西游记"),
new Books(3, "水浒传"),
new Books(4, "金瓶梅"),
new Books(5, "悟空传"),
new Books(6, "第一次亲密接触"),
new Books(7, "福尔摩斯"),
new Books(8, "大话西游"),
new Books(9, "水煮三国"),
new Books(10, "java"),
new Books(11, "android"),
new Books(12, "我爱的人"),
};
Scanner uScanner = new Scanner(System.in);
public void JieMian() {
System.out.println("****");
System.out.println("欢迎使用温州图书馆的租书系统");
System.out.println(whatis);
int s1 = uScanner.nextInt();
if (s1==1) {
while (fale) {
System.out.println("目前存在的书籍如下");
System.out.println("序号\t"+"书名");
for(int i = 0 ;i<books.length;i++) {
System.out.println(books[i].toString());
}
this.jinruxitong();
this.bookadd();
}
}else {
System.out.println("谢谢您的光临,再见!");
System.exit(0);
}
}
public void jinruxitong() {
while (fale) {
System.out.println("请问您需要用何种方式借书\t1:序号\t2:书名");
int s2 = uScanner.nextInt();
if (s2==1) {
try {
this.BookId();
fale = false;
} catch (NumException e) {
System.out.println(e.getMessage());
}
}else if (s2==2) {
try {
this.BookName();
fale = false;
}
catch (NameException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}else{
System.out.println("序号输入错误!");
}
}
}

public void BookId() throws NumException {
    System.out.println("请输入图书序号!");
    int bookid = uScanner.nextInt();
    if(bookid>0&&bookid<=books.length+1) {
        for(int i = 0;i<books.length;i++) {
            if (bookid==books[i].getId()) {
                System.out.println("借用的图书如下:");
                System.out.println(books[i].getName());
                }
            }
    }else {
        throw new NumException("图书不存在!");
    }
}
public void BookName() throws NameException{
    System.out.println("请输入图书名称!");
    String bookname = uScanner.next();
    boolean book = true;
    for(int i = 0;i<books.length;i++) {
        if (books[i].getName().equals(bookname)) {
            book = true;    
            }else {
            book= false;
            }
        }
    if (book) {
        System.out.println("借用的图书如下:");
        System.out.println(bookname);
    }else {
        throw new NameException("图书不存在!");
    }
}

public void bookadd() {
    System.out.println("请问是否还需要借书\t1:是\t2:否");
    int bookadd = uScanner.nextInt();
    if (bookadd==1) {
        fale=true;
    }else {
        fale=false;         
        System.out.println("租书结束,谢谢使用!");
        System.exit(0);
    }
}
public void KongZhi() {
    this.JieMian(); }

}

package www.imooc.book;

public class Book {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package www.imooc.book;

public class Books extends Book{
public Books(int id,String name) {
this.setId(id);
this.setName(name);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.getId()+":\t"+super.getName();
}
}
package www.imooc.book;

public class NameException extends Exception{
public NameException() {
}
public NameException(String text) {
super(text);
}
}
package www.imooc.book;

public class NumException extends Exception{
public NumException() {}
public NumException(String text) {
super(text);
}
}
package www.imooc.book;

public class Test {
public static void main(String[] args) {
User user = new User();
user.KongZhi();
}
}

0人推荐
随时随地看视频
慕课网APP