控制台编写图书馆系统

这是一个简单的图书馆系统,包括用户信息管理和个人藏书管理。数据存储采用数组结构,用户信息包含用户名和密码,图书信息包括编号、书名和价格。虽然使用数组实现,但存在一些问题,如图书编号只能输入数字,不支持字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图书馆系统

这是一个简易的系统,里面只有用户信息管理,和个人藏书图书管理。存储数据是用数组存储的,用户信息有:用户名、密码;图书管理有:图书编号、图书名、价格;信息的储存用二元数组方便一点,不过我用的是数组,简单一点


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,比如图书编号只能填数字,不能填字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值