java控制台输出图书馆管理系统(只用java代码不用数据库和GUI等)

版权声明:本文为博主原创文章,转载需在正文上方显眼位置注明文章的原链接。 https://blog.csdn.net/zeng308041977/article/details/76165950

java控制台输出图书馆管理系统(只用java代码不用数据库和GUI,java入门的新手秒懂)

在个项目中,我只用数组保存数据,和只用for循环和if条件语句来完成,连数组工具类都没用,所以导致要用到很多的条件语句,虽然比较局限,但可以让新手快速体验一下做小项目的乐趣,也可以练练for和if这些语句,练练一下它们之间嵌套的逻辑关系等,因为时间有限所以没有去优化了,主要还是让新手们体验下做java项目的面向对象编程思想(OOP),给新手们体验一下做项目的乐趣,嘿嘿!

话不多说了,直接切入正题。

设计思路如下图:


首先用eclipse建立包和类(为了简单明了我的项目结构设计如下):

//在UserData.java里边输入以下代码

package com.jiaxun.library.data;

public class UserData {
	private String username = "小询";//用户名
	private String password = "zengjiaxun";//密码
	public String getUser() {
		return username;
	}
	public String getPassword() {
		return password;
	}
	
}
//在BookData.java中输入以下代码

package com.jiaxun.library.data;


public class BookData {
	//书库
	public BookData() {
		{
			String noreaders= "无";
			for (int i = 0; i < borrowreaders.length; i++) {
				borrowreaders[i]=noreaders;
			}
			for (int i = 0; i < 5; i++) {
				switch (i) {
				case 0:
					booknames[i]="神级强者在都市";
					authors[i]="日当午";
					pubdates[i]="2016-03-31";
					sumpaginations[i]="1320";
					break;
				case 1:
					booknames[i]="都市修仙高手";
					authors[i]="雪无泪";
					pubdates[i]="2017-07-25";
					sumpaginations[i]="656";
					break;
				case 2:
					booknames[i]="护花神兵";
					authors[i]="鸡飞蛋打";
					pubdates[i]="2016-01-31";
					sumpaginations[i]="1120";
					break;
				case 3:
					booknames[i]="七界仙尊 ";
					authors[i]="无良书生";
					pubdates[i]="2017-04-04";
					sumpaginations[i]="1310";
					break;
				case 4:
					booknames[i]="恋上调皮公主";
					authors[i]="残落筱筱";
					pubdates[i]="2012-08-20";
					sumpaginations[i]="954";
					break;
				}
			}
													
		}
	}

	public String[] booknames = new String[10];// 书名
	public String[] authors = new String[10];// 作者
	public String[] pubdates = new String[10];// 出版日期
	public String[] sumpaginations = new String[10];// 总页数
	public String[] borrowreaders = new String[10];// 借阅人

}
//在Bo.java中输入以下代码

package com.jiaxun.library.bo;

import java.util.Scanner;

import com.jiaxun.library.data.BookData;
import com.jiaxun.library.data.UserData;

public class Bo {
	public static BookData book = new BookData();
	UserData user = new UserData();
	// 管理员登录验证
	public int login(String username, String password) {
		if (username.equals(user.getUser())) {
			if (password.equals(user.getPassword())) {
				return 1;// 账号和密码验证通过
			} else {
				return 0;// 密码错误
			}
		} else {
			return -1;// 账号不存在
		}
	}
	/*
	 * 菜单功能展示列表
	 * 
	 * 图书信息展示功能
	 */
	public void showBook() {
		System.out.println("本图书馆共有" + (book.booknames.length - remainSpace()) + "本书");
		int a = bookinfo();
		if (a == 0) {
			// 展示所有图书信息
			showAll();
		} else if (a > 0 && a <= book.booknames.length) {
			// 展示相关图书信息
			showPart(a);
		} else {
			System.out.println("输入的编号不合法!");
			showBook();
		}
	}
	// 展示所有图书信息
	public void showAll() {
		for (int i = 0; i < book.booknames.length; i++) {
			if (book.booknames[i] != null) {
				System.out.println("---------------------------------------编号" + (i + 1)
						+ "--------------------------------------------------------------");
				System.out.println("书籍编号为" + (i + 1) + "的详细信息为:");
				System.out.print("书名:" + book.booknames[i] + " ");
				System.out.print("作者:" + book.authors[i] + " ");
				System.out.print("出版日期:" + book.pubdates[i] + " ");
				System.out.print("总页数:" + book.sumpaginations[i] + " ");
				System.out.println("借阅者:" + book.borrowreaders[i]);
			}
		}
		System.out.println(
				"---------------------------------------------------------------------------------------------------------");
	}
	// 展示相关图书信息
	public void showPart(int a) {
		int i = a;
		System.out.println(
				"---------------分界线---------------------------------------------------------------------------------------------------");
		System.out.println("书籍编号" + (i) + "的详细信息为:");
		System.out.print("书名:" + book.booknames[i - 1] + " ");
		System.out.println("作者:" + book.authors[i - 1] + " ");
		System.out.print("出版日期:" + book.pubdates[i - 1] + " ");
		System.out.print("总页数:" + book.sumpaginations[i - 1] + " ");
		System.out.println("借阅者:" + book.borrowreaders[i - 1]);
		System.out.println(
				"---------------------------------------------------------------分界线---------------------------------------------------");
	}
	//根据编号判断要全局展示还是部分展示
	public int bookinfo() {
		for (int i = 0; i < book.booknames.length; i++) {
			if (book.booknames[i] != null) {
				System.out.println("书名编号" + (i + 1) + ":" + book.booknames[i] + " ");
			}
		}
		System.out.print("输入相应编号即可查看相应书籍的详细信息(输入0则查看所有信息):");
		Scanner scanner = new Scanner(System.in);
		int i = scanner.nextInt();
		return i;
	}
	// 录入新书籍判断
	public int addBook(String bookname, String author, String pubdate, String sumpagination) {
		int flag = 0;// 1为成功,0为图书馆已经存在该新书,-1为仓库爆满
		if (book.booknames[book.booknames.length - 1] != null) {
			flag = -1;// 爆满
		}
		if (book.booknames[book.booknames.length - 1] == null) {

			for (int i = 0; i < book.booknames.length; i++) {

				if (bookname.equals(book.booknames[i])) {
					flag = 0;// 已存在book.booknames[i]
					break;
				}
				if (!(bookname.equals(book.booknames[i]))) {
					// 添加书籍
					add(bookname, author, pubdate, sumpagination);
					flag = 1;// 成功
					break;
				}
			}
		}
		return flag;
	}
	// 录入新书籍
	public void add(String bookname, String author, String pubdate, String sumpagination) {
		for (int i = 0; i < book.booknames.length; i++) {
			if (book.booknames[i] == null) {
				book.booknames[i] = bookname;
				book.authors[i] = author;
				book.pubdates[i] = pubdate;
				book.sumpaginations[i] = sumpagination;
				break;
			}
		}
	}
	// 存放书本的剩余容量
	public int remainSpace() {
		int count = 0;
		try {
			for (int i = 0; i < book.booknames.length; i++) {
				if (book.booknames[i] == null) {
					++count;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return count;
	}
	// 修改时根据书名查找相关的书籍信息
	public int selectBook(String bookname) {
		int flag = 0;
		for (int i = 0; i < book.booknames.length; i++) {
			if (bookname.equals("")) {
				flag = -1;// -1为未输入书名
				break;
			}
			// 其他情况下为匹配馆里的图书
			if (bookname.equals(book.booknames[i])) {
				flag = 1;// 1找到相关书名
				showPart(i + 1);
				break;
			}
			if (!bookname.equals(book.booknames[i])) {
				if (i == book.booknames.length - 1) {
					flag = 0;// 0为未找到
				}
			}
		}
		return flag;
	}
	// 根据书名查找索引,有此书返其索引,无返-1
	public int selectIndex(String bookname) {
		int flag = -1;
		for (int i = 0; i < book.booknames.length; i++) {
			if (bookname.equals(book.booknames[i])) {
				flag = i;
			}
		}
		return flag;// 找不到此书
	}
	// 修改书籍信息 1书名 2作者 3出版日期 4总页数 5借阅者
	public void modinfo(int a, String value, int index) {

		switch (a) {
		case 1:
			book.booknames[index] = value;
			break;

		case 2:
			book.authors[index] = value;
			break;
		case 3:
			book.pubdates[index] = value;
			break;
		case 4:
			book.sumpaginations[index] = value;
			break;
		case 5:
			book.borrowreaders[index] = value;
			break;
		}

	}
	// 删除书籍功能
	public void deleteBook(String bookname) {
		int a = selectIndex(bookname);
		book.booknames[a] = null;
		book.authors[a] = null;
		book.pubdates[a] = null;
		book.sumpaginations[a] = null;
		book.booknames[a] = null;

	}
	// 检查书是否存在馆里和是否已借出
	public int testReader(String bookname) {

		// 馆里有此书,查找此书的索引,有此书a>=0,无返-1
		int a = selectIndex(bookname);
		// 馆里有此书
		if (a >= 0) {
			if ((book.borrowreaders[a].equals("无") || book.borrowreaders[a].equals("")))// 无人借此书,""为扫描器的直接回车值
				return 0;//未借
			else
				return 1;//已借
		} else {
			return -1;// 馆里没有此书
		}

	}
	//将借阅者设置为无或者为空
	public void setBorrowReader(String bookname){
		int a = selectIndex(bookname);
		book.borrowreaders[a]="无";
	}
	//查询指定书的借阅者
	public String getBorrowReader(String bookname){
		int a = selectIndex(bookname);
		return book.borrowreaders[a];
	}

	//书籍借阅功能
	public void borrow(String bookname,String readername){
		int a = selectBook(bookname);
		book.borrowreaders[a]=readername;
	}
}
//在LibraryUI.java中输入以下代码
package com.jiaxun.library.ui;

import java.util.Scanner;

import com.jiaxun.library.bo.Bo;

public class LibraryUI {

	Bo bo = new Bo();

	public static void main(String[] args) {
		LibraryUI ui = new LibraryUI();
		ui.welcome();// 欢迎界面

	}

	// 欢迎界面
	public void welcome() {
		System.out.println("*********************************");
		System.out.println("********小☆询图书馆管理系统********");
		System.out.println("*********************************");
		loginBoundary();// 登录界面
	}

	// 管理员登录界面
	public void loginBoundary() {
		System.out.print("管理员账号:");
		Scanner scanner = new Scanner(System.in);
		String username = scanner.nextLine();
		System.out.print("登 录 密 码 :");
		Scanner scanner1 = new Scanner(System.in);
		String password = scanner1.nextLine();
		int flag = bo.login(username, password);// 登录验证
		switch (flag) {
		case 0:// 密码错误
			System.out.println("密码错误,请重新输入。");
			loginBoundary();
			break;

		case 1:// 验证通过
			System.out.println("登录成功!");
			showMenu();// 登录成功后跳转至菜单功能管理
			break;
		case -1:// 账户不存在
			System.out.println("您输入的管理员账号不存在,请确认后再输入。");
			loginBoundary();
			break;
		}
	}

	// 菜单功能展示列表
	public void showMenu() {
		System.out.println("请输入功能编号进入相应的功能:");
		System.out.println("所有的图书信息——编号:1");
		System.out.println("录入新的书籍——编号:2");
		System.out.println("修改书籍基本信息——编号:3");
		System.out.println("删除书籍功能——编号:4");
		System.out.println("还书功能——编号:5");
		System.out.println("书籍借阅功能——编号:6");
		System.out.println("退出系统——编号:7");
		System.out.print("请输入功能编号:");
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();//
		switch (a) {
		case 1:// 图书信息
			showBook();
			returnMethod();// 返回主菜单
			break;
		case 2:// 录入新书籍
			addBook();
			break;
		case 3:// 修改书籍基本信息
			modBookInfo();
			break;
		case 4:// 删除书籍
			delete();
			break;
		case 5:// 还书
			returnBook();
			break;
		case 6:// 借阅
			borrowBook();
			break;
		case 7:// 退出登录
			welcome();
			scanner.close();
			break;
		}
	}

	// 返回主菜单功能
	public void returnMethod() {
		System.out.print("按下ENTER键返回到功能主菜单:");
		Scanner scanner = new Scanner(System.in);
		String i = scanner.nextLine();
		showMenu();
	}

	// 图书信息展示功能
	public void showBook() {
		bo.showBook();
	}

	// 录入新书籍功能
	public void addBook() {
		System.out.print("请输入要增加的书籍名称:");// 书名
		Scanner scanner = new Scanner(System.in);
		String bookname = scanner.nextLine();
		System.out.print("请输入" + bookname + "的作者:");// 作者
		Scanner scanner1 = new Scanner(System.in);
		String author = scanner1.nextLine();
		System.out.print("请输入" + bookname + "的出版日期(格式如2000-01-01):");// 出版日期
		Scanner scanner2 = new Scanner(System.in);
		String pubdate = scanner2.nextLine();
		System.out.print("请输入" + bookname + "的总页数(页):");// 总页数
		Scanner scanner3 = new Scanner(System.in);
		String sumpagination = scanner3.nextLine();
		int flag = bo.addBook(bookname, author, pubdate, sumpagination);
		// 1为成功,0为图书馆已经存在该新书,-1为仓库爆满
		switch (flag) {
		case 1:
			System.out.println("新书籍" + bookname + "添加成功,目前图书馆还有" + bo.remainSpace() + "个位置可以存放新书籍。");
			returnMethod();// 书籍添加界面中的返回功能,可选择继续添加或者回到主菜单
			break;
		case 0:
			System.out.println("添加书籍失败," + bookname + "已存在该图书馆中!");
			returnMethod();// 书籍添加界面中的返回功能,可选择继续添加或者回到主菜单
			break;
		case -1:
			System.out.println("添加失败,图书馆已经存储量已经爆满了,请删除部分书籍后再进行添加。");
			showMenu();
			break;
		}
	}

	// 修改书籍信息
	public void modBookInfo() {
		System.out.print("请输入您要操作的书名:");
		Scanner scanner = new Scanner(System.in);
		String bookname = scanner.nextLine();
		// 根据书名查找相关的书籍信息
		int flag = bo.selectBook(bookname);
		switch (flag) {
		case 0:
			System.out.print("未找到相关书籍,输入0回车进入该层功能,输入1回车返回到功能主菜单。");
			int a = scanner.nextInt();
			if (a == 0) {
				modBookInfo();
			} else if (a == 1) {

				showMenu();
			}
			break;

		case -1:
			System.out.println("书名为空,输入0回车进入该层功能,输入1回车返回到功能主菜单。");
			int a1 = scanner.nextInt();
			if (a1 == 0) {
				modBookInfo();
			} else {
				showMenu();
			}
			break;

		case 1:
			System.out.print("以上信息为相关书名查找的结果,其中(数字“1”代表书名,数字“2”代表作者,数字“3”代表出版日期,数字“4”代表总页数,数字“5”代表借阅者)输入相关数字可以修改相关信息:");
			int a2 = scanner.nextInt();// 1书名 2作者 3出版日期 4总页数 5借阅者
			if (a2 > 0 && a2 < 6) {
				System.out.print("请输入您想修改的" + sortName(a2) + ":");
				Scanner scanner2 = new Scanner(System.in);
				String value = scanner2.nextLine();
				int index = bo.selectIndex(bookname);
				bo.modinfo(a2, value, index);
				System.out.println(bookname + "这本书的相关信息修改成功!");
				returnMethod();
			} else {
				System.out.println("您输入的数字不合法。请重试!");
				modBookInfo();
			}

			break;

		}
	}

	// 修改的分类名称
	public String sortName(int flag) {
		if (flag == 1)
			return "书名";
		if (flag == 2)
			return "作者";
		if (flag == 3)
			return "出版日期";
		if (flag == 4)
			return "总页数";
		else
			return "借阅者";
	}

	// 删除书籍功能
	public void delete() {
		System.out.print("请输入要被删除的书名:");
		Scanner scanner = new Scanner(System.in);
		String bookname = scanner.nextLine();
		int flag = bo.testReader(bookname);
		// 存在此书并无人借
		if (flag == 0) {
			bo.deleteBook(bookname);
			System.out.println(bookname + "已从图书馆成功移除!输入0回到主菜单,输入1则继续删除");
			delReturn();
		} else if (flag == 1) {// 有人借
			System.out.println("此书已借出,请等待归还后再删除。输入0回到主菜单,输入1则继续删除");
			delReturn();
		} else {// 不存在此书
			System.out.println("本馆不存在此书,请仔细较对。输入0回到主菜单,输入1则继续删除");
			delReturn();
		}

	}

	// 删除的返回操作
	public void delReturn() {

		Scanner scanner1 = new Scanner(System.in);
		int i = scanner1.nextInt();
		if (i == 0) {
			showMenu();
		} else if (i == 1) {
			delete();
		}
	}

	// 还书功能
	public void returnBook() {
		System.out.print("请输入要还的书籍名称:");
		Scanner scanner = new Scanner(System.in);
		String bookname = scanner.nextLine();
		int flag = bo.testReader(bookname);
		if (flag == 0) {
			System.out.println(bookname + "暂未借出。");
			System.out.print("输入0回车返回至主菜单,输入其他键则继续还书");
			String a = scanner.nextLine();
			if(a.equals("0")){
				showMenu();
			}else {
				returnBook();
			}
		} else if (flag == -1) {
			System.out.println(bookname + "不存在该图书馆中。");
			System.out.print("输入0回车返回至主菜单,输入其他键则继续还书");
			String a = scanner.nextLine();
			if(a.equals("0")){
				showMenu();
			}else {
				returnBook();
			}
		} else if (flag == 1) {

			System.out.println(bo.getBorrowReader(bookname) + "已经还书成功!");
			bo.setBorrowReader(bookname);
			System.out.print("输入0回车返回至主菜单,输入其他键则继续还书");
			String a = scanner.nextLine();
			if(a.equals("0")){
				showMenu();
			}else {
				returnBook();
			}
		}
	}

	// 书籍借阅功能
	public void borrowBook() {
		System.out.print("请输入要借的书名:");
		Scanner scanner = new Scanner(System.in);
		String bookname = scanner.nextLine();
		int flag = bo.testReader(bookname);
		switch (flag) {
		case 0://存在此书并无人借的情况

			System.out.println("请输入借阅者的名字:");
			String readername = scanner.nextLine();
			bo.borrow(bookname,readername);
			System.out.println(readername+"从该图书馆里借出了以"+bookname+"为名的书籍。");
			System.out.print("输入0回车返回至主菜单,输入1回车则继续借书:");
			int a0 = scanner.nextInt();
				if(a0==0)
					showMenu();
				if(a0==1)
					borrowBook();
			break;

		case 1://存在此书被借的情况
			System.out.println("借书失败," + bookname + "已被借!");
			System.out.print("输入0回车返回至主菜单,输入1回车则继续借书:");
			int a = scanner.nextInt();
			switch (a) {
			case 0:
				showMenu();
				break;

			case 1:
				borrowBook();
				break;
				
				}
		case -1://不存在此书的情况
			System.out.println("借书失败," + bookname + "不存在该图书馆中。");
			System.out.print("输入0回车返回至主菜单,输入1回车则继续借书:");
			int a1 = scanner.nextInt();
			switch (a1) {
			case 0:
				showMenu();
				break;

			case 1:
				borrowBook();
				break;

			}
		}
	}
}



 

展开阅读全文

没有更多推荐了,返回首页