Java使用面向对象编程思维编写图书管理系统:增加,查询,修改,删除,退...
发布网友
发布时间:2022-04-27 10:03
我来回答
共2个回答
热心网友
时间:2022-04-08 05:48
package com.bms;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// book对象
public class Book {
private String bId; // 编号
private String bName; // 书名
// getset方法
public String getbId() {
return bId;
}
public void setbId(String bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
//构造方法
public Book() {
}
public Book(String bId, String bName) {
this.bId = bId;
this.bName = bName;
}
/*
* 增加
* */
public static List<Book> add(List<Book> list) {
Scanner sn = new Scanner(System.in);
System.out.print("请输入编号:");
String bid = sn.next();
System.out.print("请输入名称:");
String bName = sn.next();
Book book = new Book(bid, bName);
for (Book b : list) {
if (b.bId.equals(book.bId)) {
System.out.println("编号重复,请重新输入!");
return list;
}
}
list.add(book);
System.out.println("添加成功!");
return list;
}
/*
* 查询
* */
public static void query(List<Book> list) {
System.out.println("编号\t书名");
for (Book b : list) {
System.out.println(b.getbId() + "\t" + b.getbName());
}
}
/*
* 修改
* */
public static void update(List<Book> list) {
query(list);
Scanner sc = new Scanner(System.in); // 键盘输入的对象
System.out.print("请输入编号:");
String s = sc.next();
Integer id = null;
for (int i = 0; i < list.size(); i++) {
id = list.get(i).getbId().equals(s) ? i : null;
}
if (id == null) {
System.out.println("输入的编号不存在,请重新选择!");
return;
}
System.out.print("请输入新的书名:");
String newName = sc.next();
list.get(id).setbName(newName);
System.out.print("修改成功!");
}
/*
* 删除
* */
public static void del(List<Book> list) {
query(list);
Scanner sc = new Scanner(System.in); // 键盘输入的对象
System.out.print("请输入编号:");
String s = sc.next();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getbId().equals(s)) {
list.remove(i);
return;
}
}
System.out.println("输入的编号不存在,请重新选择!");
}
}
/*
* 测试*/
class Test {
public static void main(String[] args) {
List<Book> bookList = new ArrayList<>(); // 存放所有图书的列表
bookList.add(new Book("1", "Java 基础")); // 图书的列表添加一本图书
System.out.print("欢迎进入图书管理系统,");
boolean b = true;
while (b) {
System.out.print("请选择:\n1.增加\n2.查询\n3.修改\n4.删除\n5.退出\n(输入序号):");
Scanner sn = new Scanner(System.in); // 键盘输入的对象
String select = sn.next();
switch (select) {
case "1":
System.out.println("您选择了增加");
Book.add(bookList);
break;
case "2":
System.out.println("您选择了查询:");
Book.query(bookList);
break;
case "3":
System.out.println("您选择了修改");
Book.update(bookList);
break;
case "4":
System.out.println("您选择了删除");
Book.del(bookList);
break;
case "5":
System.out.println("您选择了退出");
b = false;
System.out.println("退出程序!");
break;
default:
System.out.println("输入错误的序号,请重新输入");
break;
}
}
}
}
热心网友
时间:2022-04-08 07:06
用JDBC技术,先让java连接数据库,再通过使用增删改查语句改变数据库内容就完成了