《图书管理系统》JavaSE:循环、判断
立即下载
资源介绍:
内容概要:新增图书、查看图书列表、删除、借阅、归还、图书排行榜、退出程序;
适用人群:Java初学者,想要用项目巩固自身知识掌握的学者
package book.management.system;
import java.lang.invoke.SwitchPoint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
import java.util.SimpleTimeZone;
public class Project1 {
//所需要提前加载的成员变量
static Scanner scanner = new Scanner(System.in);
static String[] bookNames = new String[50];
static String[] bookStatus = new String[50];
static String[] bookRentDate = new String[50];
static int[] bookRentCounts = new int[50];
public static void main(String[] args) {
inputBook();
menu();
}
//初始化数组
public static void inputBook(){
bookNames[0] = "罗马假日";
bookStatus[0] = "已借出";
bookRentDate[0] = "2010-07-01";
bookRentCounts[0] = 30;
bookNames[1] = "风声鹤唳";
bookStatus[1] = "可借";
bookRentDate[1] = "\t\t";
bookRentCounts[1] = 19;
bookNames[2] = "浪漫满屋";
bookStatus[2] = "可借";
bookRentDate[2] = "\t\t";
bookRentCounts[2] = 24;
}
//主菜单
public static void menu() {
int num = 0;
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("请选择要使用的功能:");
num = scanner.nextInt();
switch (num) {
case 1: {
System.out.println("-->新增图书");
inputNewBook();
returnMenu();
break;
}
case 2: {
System.out.println("-->查看图书");
selectBook();
returnMenu();
break;
}
case 3: {
System.out.println("-->删除图书");
deleteBook();
returnMenu();
break;
}
case 4: {
System.out.println("-->借出图书");
rentBook();
returnMenu();
break;
}
case 5: {
System.out.println("-->归还图书");
returnBook();
returnMenu();
break;
}
case 6: {
System.out.println("-->图书排行榜");
bookRankList();
returnMenu();
break;
}
case 7: {
System.out.println("感谢使用本系统!欢迎下次使用!");
System.exit(5201314);//退出系统
}
default:{
System.out.println("输入错误,请输入阿拉伯数字1234567");
returnMenu();
}
}
}
//用于实现循环菜单功能
public static void returnMenu(){
System.out.println("******************************");
System.out.println("输入0返回:");
int i = scanner.nextInt();
if (i == 0)
menu();
}
//1、新建图书方法
private static void inputNewBook() {
int i = 0;
System.out.println("请输入书名:");
String newBookName = scanner.next();
//在for的循环条件里判断第一个空值
for (i = 0; i < bookNames.length && bookNames[i] != null; i++){
//如果需要判断是否为null不能使用equals
if (bookNames[i].equals(newBookName)){
System.out.println("已有此名称的书目,序号为:" + (i + 1));
return;
}
}
//不需要判断是否为空,因为前面的循环已经实现了这个功能,因为第一个null他就停了
/*
for (i = 0; i < bookNames.length; i++){
//必须用==,不能用.eqlues()方法
if (bookNames[i] == null)
break;
}
*/
bookNames[i] = newBookName;
bookStatus[i] = "可借";
bookRentDate[i] = "\t\t";
bookRentCounts[i] = 0;
System.out.println("新增" + newBookName + "成功!");
}
//2、查看图书方法
public static void selectBook(){
System.out.println("序号\t状态\t名称\t借出日期\t\t借出次数");
for (int i = 0; i < bookNames.length && bookNames[i] != null; i++){
System.out.println((i + 1) + "\t\t" + bookStatus[i] + "\t" + bookNames[i] + "\t" + bookRentDate[i] + "\t" + bookRentCounts[i]);
}
}
//3、删除图书方法
public static void deleteBook(){
int i = 0;
System.out.println("请输入要删除的书的名字:");
String deleteBookName = scanner.next();
for (i = 0; i < bookNames.length && bookNames[i] != null; i++){
//如果需要判断是否为null不能使用equals
if (bookNames[i].equals(deleteBookName)){
System.out.println("存在此名称的书目,准备删除操作,序号为:" + (i + 1));
if(bookStatus[i].equals("可借")){
System.out.print(deleteBookName + "未被借出,进行删除操作......");
for (int j = i; j < bookNames.length && bookNames[j] != null; j++){
//删除到发现下一本书是null,就把这本书的下标赋值为null,因为他已经赋给前面的值,所以不会消失
if (bookNames[j + 1] != null){
bookNames[j] = bookNames[j + 1];
bookStatus[j] = bookStatus[j + 1];
bookRentDate[j] = bookRentDate[j + 1];
bookRentCounts[j] = bookRentCounts[j + 1];
continue;
}
else {
bookNames[j] = null;
bookStatus[j] = null;
bookRentDate[j] = null;
bookRentCounts[j] = 0;
break;
}
}
}else {
System.out.println(deleteBookName + "已被借出,无法进行删除操作,操作失败");
return;
}
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到相应书目,请重新键入或查看图书列表");
return;
}
//4、借出图书方法
public static void rentBook(){
int i = 0;
System.out.println("请输入要借的书的名字:");
String rentBookName = scanner.next();
for (i = 0; i < bookNames.length && bookNames[i] != null; i++){
//如果需要判断是否为null不能使用equals
if (bookNames[i].equals(rentBookName)){
System.out.println("存在此名称的书目,准备借出操作,序号为:" + (i + 1));
if(bookStatus[i].equals("可借")){
System.out.print(rentBookName + "未被借出,进行借书操作......");
System.out.println("请输入借书时间(日期格式:年年年年-月月-日日):");
String rentTime = scanner.next();
bookStatus[i] = "已借出!";
bookRentDate[i] = rentTime;
bookRentCounts[i]