package 加油;
import java.util.Scanner;
public class Book {
public static int size=10;//最多为10本书
static Book[] b=new Book[10];
public static int count=0;//当前书本数
private int id;
private String name ;
private String author;
private String publish;
public Book(int id,String name,String author,String publish) {
this.id=id;
this.name=name;
this.author=author;
this.publish=publish;
}
static void check() {
int flag=0;
for(int i=3;i>0;i--)
{
Scanner input =new Scanner(System.in);
System.out.println("请输入用户名");
String admin=input.nextLine();
System.out.println("请输入密码 ");
String password=input.nextLine();
if (admin.equals("123")&&password.equals("123"))
{
flag=1;
System.out.println("登陆成功");
showmenu();
}
if(flag==0)
{
System.out.println("登陆失败 ");
System.out.println("你还有"+(i-1)+"次机会,请重新输入");
if(i==1)
{
System.out.println("已经无法登陆了 ");
System.exit(0);
}
}
}
}
static void add_book() {
//Scanner input=new Scanner(System.in);
if(count<size) {
System.out.println("一共有"+count+"本书,可以继续添加");
Scanner input=new Scanner(System.in);
System.out.println("请添加书号");
int id=input.nextInt();
System.out.println("请添加书名");
String name=input.next();
System.out.println("请添加作者");
String author=input.next();
System.out.println("请添加出版社");
String publish=input.next();
System.out.println("添加成功");
Book book =new Book(id,name,author,publish);
b[count]=book;
count++;
System.out.println("现在已有"+count+"本书");
}
else if(count==10){
System.out.println("图书添加已满");
}
}
static void delete_book(){
int i=0;
int flag=0;
if (count==0) {
System.out.println("书库还没有书,请添加");
return ;
}
System.out.println("请输入你要删除书的ID");
Scanner input=new Scanner(System.in);
int id=input.nextInt();
for(i=0;i<count;i++)
{
if(id==(b[i].id))
{
b[i]=b[i+1];
System.out.println("已经成功删除");
count--;
flag=1;
}
}
if(flag==0)
{
System.out.println("没有找到你要删除的书");
}
}
static void showallbook() {
int i=0;
for(i=0;i<count;i++)
{
System.out.println("学号为 "+b[i].id+"书名为 "+b[i].name+"作者为 "+b[i].author+"出版社 "+b[i].publish);
}
}
static void change_book() {
if (count==0) {
System.out.println("书库还没有书,请添加");
return ;
}
System.out.println("请输入你要修改书的ID");
int flag=0;
Scanner input=new Scanner (System.in);
int id=input.nextInt();
for(int i=0;i<count;i++)
{
if(id==(b[i].id))
{
System.out.println("请输入你要修改书后的ID");
int id1=input.nextInt();
System.out.println("请输入你要修改书后的名字");
String name=input.next();
System.out.println("请输入你要修改书后的author");
String author=input.next();
System.out.println("请输入你要修改书后的publish");
String publish=input.next();
Book book =new Book(id1,name,author,publish);
b[i]=book;
System.out.println("已经修改完成");
flag=1;
}
}
if(flag==0)
{
System.out.println("没有找到你要修改的书");
}
}
static void search_book(){
int i=0;
int flag=0;
if (count==0) {
System.out.println("书库还没有书,请添加");
return ;
}
Scanner input=new Scanner(System.in);
System.out.println("请输入你要查找书的ID");
int id=input.nextInt();
for(i=0;i<count;i++)
{
if(id==(b[i].id))
{
flag=1;
System.out.println("学号为 "+b[i].id+"书名为 "+b[i].name+"作者为 "+b[i].author+"出版社 "+b[i].publish);
}
}
if(flag==0)
{
System.out.println("没有找到你要的书");
}
}
static 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");
while(true) {
Scanner input =new Scanner(System.in);
int a=input.nextInt();
switch (a)
{
case 1:add_book();
break;
case 2:delete_book();
break;
case 3:change_book();
break;
case 4:search_book();
break;
case 5:
return ;
default: System.out.println("输入非法,请重新输入!");
}
}
}
public static void main(String[] args) {
check();
showmenu();
}
}
Java 图书管理系统
最新推荐文章于 2024-04-30 19:02:00 发布