图书馆系统
这是一个简易的系统,里面只有用户信息管理,和个人藏书图书管理。存储数据是用数组存储的,用户信息有:用户名、密码;图书管理有:图书编号、图书名、价格;信息的储存用二元数组方便一点,不过我用的是数组,简单一点
public class Demo01 {
public static int t;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
User u = new User();
while (t != -1) {
u.homePage(t);
}
System.out.println("已退出系统");
}
}
//用户信息管理
class User {
Scanner input = new Scanner(System.in);
public int length = 0;
public String[] name = null;
public String[] pwd = null;
public String[] name1 = null;
public String[] pwd1 = null;
//添加信息
public void add() {
Scanner input = new Scanner(System.in);
String a = null;
int j = 0;
if (length != 0) {
level: while (true) {
System.out.println("请输入用户名:");
a = input.next();
if(a.equals("#") && j > 0){
homePage(0);
return;
}
for (int i = 0; i < name.length; i++) {
if (name[i].equals(a)) {
System.out.println("用户名已被注册,输入#退到首页");
j++;
break;
} else {
break level;
}
}
}
}
length++;
String[] name1 = null;
String[] pwd1 = null;
if (length != 1) {
name1 = new String[length];
pwd1 = new String[length];
for (int i = 0; i < pwd.length; i++) {
name1[i] = name[i];
pwd1[i] = pwd[i];
}
name1[length - 1] = a;
System.out.println("请输入密码:");
pwd1[length - 1] = input.next();
}
name = new String[length];
pwd = new String[length];
if (length == 1) {
System.out.println("请输入用户名:");
name[length - 1] = input.next();
System.out.println("请输入密码:");
pwd[length - 1] = input.next();
}
if (length > 1) {
for (int i = 0; i < name1.length; i++) {
name[i] = name1[i];
pwd[i] = pwd1[i];
}
}
}
//用户登录
public void login() {
System.out.println("请输入用户名:");
String username = input.next();
System.out.println("请输入密码:");
String password = input.next();
Book b = new Book();
for (int i = 0; i < name.length; i++) {
if (name[i].equals(username) && pwd[i].equals(password)) {
System.out.println("欢迎进入数据管理系统");
level: while (true) {
System.out
.println("1.查看书籍 2.书籍添加 3.书籍删除 4.修改 5.退出");
switch (input.nextInt()) {
case 1:
b.see();
break;
case 2:
b.add();
break;
case 3:
b.delete();
break;
case 4:
b.modify();
break;
case 5:
break level;
default:
System.out.println("无效操作");
break;
}
}
} else if (i == name.length - 1 && !name[i].equals(username)
|| !pwd[i].equals(password)) {
System.out.println("密码或用户名错误");
}
}
}
//首页
public void homePage(int t) {
System.out.println("请输入进入的页面,1.注册 2.登录 3.退出");
switch (input.nextInt()) {
case 1:
add();
// System.out.println(name[0]);
break;
case 2:
login();
break;
case 3:
Demo01 d = new Demo01();
d.t = -1;
return;
default:
System.out.println("无效操作,重新输入");
break;
}
}
}
//图书管理
class Book {
public int bookUser[] = null;
public String[] bookName = null;
public double[] Price = null;
public int length;
//图书信息添加
public void add() {
length++;
Scanner input = new Scanner(System.in);
int[] bookUser1 = null;
String[] bookName1 = null;
double[] Price1 = null;
if (length != 1) {
bookUser1 = new int[length];
bookName1 = new String[length];
Price1 = new double[length];
}
bookUser = new int[length];
bookName = new String[length];
Price = new double[length];
System.out.println("请输入图书编号:");
bookUser[length - 1] = input.nextInt();
System.out.println("请输入图书名:");
bookName[length - 1] = input.next();
System.out.println("请输入图书价格:");
Price[length - 1] = input.nextDouble();
}
//图书信息删除
public void delete() {
if (length == 0) {
System.out.println("暂无藏书");
} else {
length--;
Scanner input = new Scanner(System.in);
System.out.println("请输入删除的图书编号");
for (int j = 0; j < Price.length; j++) {
if (input.nextInt() == bookUser[j]) {
bookUser[j] = -1;
bookName[j] = null;
Price[j] = -1.0;
}
}
for (int i = 0; i < Price.length - 1; i++) {
if (bookName[i].equals(null)) {
for (int j = i; j < Price.length - 1; j++) {
bookUser[j] = bookUser[j + 1];
bookName[j] = bookName[j + 1];
Price[j] = Price[j + 1];
}
}
}
int[] bookUser1 = new int[Price.length - 1];
String[] bookName1 = new String[Price.length - 1];
double[] Price1 = new double[Price.length - 1];
if (Price.length != 1) {
for (int i = 0; i < Price1.length; i++) {
bookUser1[i] = bookUser[i];
bookName1[i] = bookName[i];
Price1[i] = Price[i];
}
bookUser = new int[bookUser1.length];
bookName = new String[bookUser1.length];
Price1 = new double[bookUser1.length];
for (int i = 0; i < Price1.length; i++) {
bookUser[i] = bookUser1[i];
bookName[i] = bookName1[i];
Price[i] = Price1[i];
}
}
}
}
//图书信息修改
public void modify() {
if (length == 0) {
System.out.println("暂无藏书");
} else {
System.out.println("请输入要修改的编号:");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
for (int i = 0; i < Price.length; i++) {
if (a == bookUser[i]) {
System.out.println("请修改图书名");
bookName[i] = input.next();
System.out.println("请修改价格");
Price[i] = input.nextDouble();
System.out.println("图书编号" + bookUser[i]);
System.out.println("图书名" + bookName[i]);
System.out.println("价格" + Price[i]);
} else if (i == Price.length - 1 && a != bookUser[i]) {
System.out.println("查无此书");
}
}
}
}
//查看图书信息
public void see() {
if (length == 0) {
System.out.println("暂无藏书");
} else {
System.out.println("输入查看的图书编号:");
Scanner input = new Scanner(System.in);
int a = input.nextInt();
for (int i = 0; i < Price.length; i++) {
if (a == bookUser[i]) {
System.out.println("图书编号" + bookUser[i]);
System.out.println("图书名" + bookName[i]);
System.out.println("价格" + Price[i]);
} else if (i == Price.length - 1 && a != bookUser[i]) {
System.out.println("查无此书");
}
}
}
}
}
里面还存在一些bug,比如图书编号只能填数字,不能填字符串