本文目录一览:
- 1、求一个java图书管理系统代码,不需要图形化,命令行就可以,只要求实现导入图书,查询,删除
- 2、用java编写一个 图书馆图书借阅管理系统
- 3、java做一个简单的图书管理系统,大一新生,用基础做,软件能调试就行
- 4、Java使用面向对象编程思维编写图书管理系统:增加,查询,修改,删除,退出,怎么写?
- 5、JAVA版的图书管理系统
求一个java图书管理系统代码,不需要图形化,命令行就可以,只要求实现导入图书,查询,删除
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookWork {
static ListBook data;
private static Scanner input;
public static void main(String[] args) {
if (!initBook("d:/book.txt")) {
System.out.println("初始图书列表失败 ..");
return;
}
input = new Scanner(System.in);
while (true) {
try {
System.out.println("请输入操作:");
System.out.println("1.找书 2.删除图书 3.退出");
int number = Integer.parseInt(input.next());
if (number == 1) {
findBook();
} else if (number == 2) {
delBook();
} else if (number == 3) {
System.out.println("退出");
break;
} else {
System.out.println("这个不是我要的...重来...");
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("这个不是我要的...重来...");
System.out.println();
}
}
}
private static void delBook() {
System.out.println("请输入要删除的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
data.remove(book);
System.out.println(" 图书 " + book.toString() + " 已删除");
return;
}
}
}
System.out.println("没有您要删除的");
}
private static void findBook() {
System.out.println("请输入要查找的书名或编号:");
String key = input.next();
if (key != null !key.equals("")) {
for (Book book : data) {
if (book.number.equals(key) || book.name.contains(key)) {
System.out.println("找到了 图书 " + book.toString());
return;
}
}
}
System.out.println("没有您要找的");
}
private static boolean initBook(String string) {
try {
System.out.println("图书导入中...");
System.out.println("列表文件 -- " + string);
File file = new File(string);
if (!file.exists()) {
return false;
}
data = new ArrayListBook();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
String[] strings = line.split(",");
Book b = new Book(strings[0], strings[1]);
data.add(b);
System.out.println("导入" + b.toString());
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static class Book {
String number;
String name;
public Book(String number, String name) {
super();
this.number = number;
this.name = name;
}
@Override
public String toString() {
return "Book [编码:" + number + ", 名称:" + name + "]";
}
}
}
001,金瓶梅
002,杂事秘辛
003,飞燕外传
004,控鹤监秘记
005,汉宫春色
用java编写一个 图书馆图书借阅管理系统
---------------------------------------------------
给你修改了三个地方:
1.borrowBooks方法中,将System.out.println("你要借吗?"); 改为:
System.out.println("你要借吗?输入1表示借,其他数字表示不借。");
保证输入的时候输入的数字,否则会报出异常。
2.borrowBooks方法中,将self[score] = all[9]; 改为:self[score] = all[i];
如果是all[9],那么就始终是最后一本书籍信息了。
3.have方法中,你是想将所借的书籍信息都打印出来。修改的比较多,下面注释代码是原来的。
void have(Books[] self) {
// for (int i = 0; i 2; i++) {
// self[i].showBookInfo();
// }
for (int i = 0; i 3; i++) {
if(self[i]!=null)
self[i].showBookInfo();
}
}
****************** 附上所有代码:*************************
import java.util.Scanner;
public class TestBook {
public static void main(String[] args) {
Books all[] = new Books[10];
Books self[] = new Books[3];
all[0] = new Books("java", 1, "12345", "tom", 34.0f, "人民出版社");
all[1] = new Books("c", 2, "12346", "tnn", 31.0f, "人民出版社");
all[2] = new Books("c++", 3, "12445", "mm", 35.0f, "人民出版社");
all[3] = new Books("c#", 4, "12365", "tt", 38.0f, "人民出版社");
all[4] = new Books("j2se", 5, "13345", "tosm", 31.1f, "人民出版社");
all[5] = new Books("j2ee", 6, "18345", "ttm", 32.0f, "人民出版社");
all[6] = new Books("jsp", 7, "12335", "cc", 33.0f, "人民出版社");
all[7] = new Books("net", 8, "12341", "bb", 36.0f, "人民出版社");
all[8] = new Books("ip", 9, "12343", "aa", 37.0f, "人民出版社");
all[9] = new Books("tcp", 10, "22345", "jj", 39.0f, "人民出版社");
Readers r = new Readers("xiaoming", 101, "1", 3);
r.searchAllBooks(all);
r.borrowBooks(all, self);
r.have(self);
r.give(all, self);
}
}
class Readers {
Scanner scan = new Scanner(System.in);
String names;
int nums;
String classes;
int grade;
int score = 0;
// Books self[]=new Books[3];
Readers(String n, int u, String c, int g) {
names = n;
nums = u;
classes = c;
grade = g;
}
void searchAllBooks(Books[] all) {// 查书
for (int i = 0; i 10; i++)
all[i].showBookInfo();
// self[score]=all[0];
}
void give(Books[] all, Books[] self) {// 还书
System.out.println("请输入您要还的书的书号");
int n = scan.nextInt();
for (int i = 0; i 10; i++) {
if (n == all[i].num) {
for (int j = 0; j 3; j++) {
if (self[j] == all[i]) {
self[j] = null;
System.out.println("还书成功");
}
}
}
}
}
void have(Books[] self) {
// for (int i = 0; i 2; i++) {
// self[i].showBookInfo();
// }
for (int i = 0; i 3; i++) {
if(self[i]!=null)
self[i].showBookInfo();
}
}
void giveMoney() {
}
void borrowBooks(Books[] all, Books[] self) {
System.out.println("请输入您要查找的书名:");
String n = scan.next();
int i;
for (i = 0; i 10; i++) {
if (n.equals(all[i].name)) {
all[i].showBookInfo();
break;
}
}
//System.out.println("你要借吗?");
System.out.println("你要借吗?输入1表示借,其他数字表示不借。");
int j;
j = scan.nextInt();
if (j == 1) {
System.out.println("借阅成功");
//self[score] = all[9];
self[score] = all[i];
score += 1;
}
if (score 4) {
System.out.println("您还可以借阅" + (3 - score) + "本");
} else {
System.out.println("对不起,一个人只能借3本");
}
}
}
class Books {
String name;
int num;
String ISBN;
String writer;
float price;
String publisher;
Books(String n, int u, String i, String w, float p, String l) {
name = n;
num = u;
ISBN = i;
writer = w;
price = p;
publisher = l;
}
void showBookInfo() {
System.out.println("**************************");
System.out.println("书名:" + name);
System.out.println("索书号:" + num);
System.out.println("ISBN号:" + ISBN);
System.out.println("价格:" + price);
System.out.println("出版社:" + publisher);
System.out.println("**************************");
}
}
----------------------------------------------------
java做一个简单的图书管理系统,大一新生,用基础做,软件能调试就行
从零开始学Java,本来利用数组只在一个界面就可以实现增删改查功能,学到接口后觉得实现分离效果会更好一些!所以进行了尝试,将管理员和用户中的图书查询,登录,图书出库合并在一起写了一个接口,用户类和管理员类实现这个接口!图书类写了一个抽象类里面都是一些图书的属性,用户类管理员类对图书类进行操作。
具体实现如下:
接口:存在三种方法 用户类和管理员类去实现这个接口,并实现着里面的方法
[java] view plain copy
public interface Person {
public boolean login(String user,String pwd); //登录方法
public void search(); //查询方法
public void reduce(String s,int i); // 图书出库
}
用户类: 实现接口,里面都是用户的方法,在测试类里调用这些方法实现其功能
[java] view plain copy
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class User implements Person{
private final String user = "user";
private final String pwd = "user";
List list = new ArrayList();
public void addBook(Book book){ //增加图书
list.add(book);
}
public boolean login(String user, String pwd) { //登录方法
if(this.user.equals(user) this.pwd.equals(pwd)){
return true;
}
return false;
}
@Override
public void reduce(String s, int i) { //结账
Iterator it = list.iterator();
while(it.hasNext()){
Book b = (Book)it.next();
if(b.getNum().equals(s)){
if(b.getCount() = i){
int m = b.getCount()-i;
b.setCount(m);
System.out.println("你的图书总价为:"+(b.getPrice()*i));
}else
System.out.println("你输入的书本数目超出了当前库存!");
}else{
System.out.println("没找到,你要加入的图书!");
}
}
}
@Override
public void search() { //查询图书当前情况
Iterator it = list.iterator();
while(it.hasNext()){
Book b = (Book)it.next();
System.out.println(b.getNum()+"\t"+b.getName()+"\t"
+b.getAnthor()+"\t"+b.getFromTime()
+"\t"+b.getPrice()+"\t"+b.getCount());
}
}
}
[java] view plain copy
/prepre name="code" class="java"span style="font-size:18px;"管理员类: 实现接口,里面都是管理员的方法,在测试类里调用这些方法实现其功能/spanspan style="font-size:18px;"/span
[java] view plain copy
span style="font-size:18px;"
/span
[java] view plain copy
package com.pv1;
import java.util.*;
public class Admin implements Person {
private final String user = "admin";
private final String pws = "admin";
List list = new ArrayList();
public boolean login(String user, String pwd) { // 登录方法
if (this.user.equals(user) this.pws.equals(pwd)) {
return true;
}
return false;
}
public void addBook(Book book) { // 增加图书
list.add(book);
}
public void search() { // 查询方法
Iterator it = list.iterator();
while (it.hasNext()) {
Book b = (Book) it.next();
System.out.println(b.getNum() + "\t" + b.getName() + "\t"
+ b.getAnthor() + "\t" + b.getFromTime() + "\t"
+ b.getPrice() + "\t" + b.getCount());
}
}
public void reduce(String num, int i) { // 图书出库
for (int j = 0; j list.size(); j++) {
Book b = (Book) list.get(j);
if (b.getNum().equals(num)) {
if (b.getCount() = i) {
int m = b.getCount() - i;
b.setCount(m);
System.out.println("取出成功!现在此书库存为:" + m);
j--;
break;
} else
System.out.println("你输入的书本数目超出了当前库存!");
}
if (j == list.size()) {
System.out.println("没找到,你要加入的图书!");
}
}
}
public void increase(String num, int i) { // 图书入库
for (int j = 0; j list.size(); j++) {
Book b = (Book) list.get(j);
if (b.getNum().equals(num)) {
if (i 0) {
System.out.println("你加入的图书数目不合法!");
} else {
int m = b.getCount() + i;
b.setCount(m);
}
}
if (j == list.size()) {
System.out.println("没找到,你要加入的图书!");
}
}
}
public void delete(String num) { //删除提供编号的图书
int j = 0;
for (j = 0; j list.size(); j++) {
Book b = (Book) list.get(j);
if (b.getNum().equals(num)) {
list.remove(b);
System.out.println("删除成功!");
j--;
break;
}
}
if (j == list.size()) {
System.out.println("未找到你要删除的图书!请确认编号后再删");
}
}
public boolean guanSearch(String num){
for (int j = 0; j list.size(); j++) {
Book b = (Book) list.get(j);
if (b.getNum().equals(num)) {
return true;
}
}
return false;
}
public void guan(String num, String name, String auther, String date,
double price, int count) { // 修改提供编号的图书
int j = 0;
for ( j = 0; j list.size(); j++) {
Book b = (Book) list.get(j);
if (b.getNum().equals(num)) {
b.setAnthor(auther);
b.setCount(count);
b.setFromTime(date);
b.setName(name);
b.setPrice(price);
break;
}
}
}
}
图书类:抽象类,里面全部是属性,方便用户管理员对其操作。
[java] view plain copy
package com.pv1;
import java.util.*;
public class Book {
private String num ;
private String name;
private String anthor;
private String fromTime;
private double price;
private int count;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAnthor() {
return anthor;
}
public void setAnthor(String anthor) {
this.anthor = anthor;
}
public String getFromTime() {
return fromTime;
}
public void setFromTime(String fromTime) {
this.fromTime = fromTime;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
测试类:
[java] view plain copy
package com.pv1;
import java.util.*;
public class BookTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Book bo = new Book(); //在开始时加入一本图书,所用对象
Book bo1 = null; //循环加入图书建立的对象,开始设成空,防止加入图书时被覆盖
List list = new ArrayList(); //建立一个集合,存储管理员要添加的图书信息的对象
Admin ad = new Admin(); //建立管理员对象,管理员操作时调用管理员类中对象
User us = new User(); //建立用户对象,管理员操作时调用用户类中对象
boolean ad1 = true; //接受进入管理员操作界面的返回值
boolean us1 = true; //接受进入用户操作界面的返回值
bo.setAnthor("刘冰"); //书籍为空,加入一本图书
bo.setCount(20);
bo.setNum("1001");
bo.setName("一本道");
bo.setFromTime("2015-2-50");
bo.setPrice(250.0);
ad.addBook(bo); //通过对象传递在管理员类中加入一本图书
us.addBook(bo);
System.out.print("请输入用户名:"); //用户输入 用户名 密码
String user = input.next();
System.out.print("请输入密码:");//
String pwd = input.next();
ad1 = ad.login(user, pwd); //将 用户名 密码 传入管理员类中进行判断返回Boolean类型接受
us1 = us.login(user, pwd); //将 用户名 密码 传入用户类中进行判断返回Boolean类型接受
if (ad1) { //如果管理员类返回true进入 管理员操作界面
System.out.println("图书编号\t" + "书名\t" + "作者\t" + "出版日期\t" + "\t价格\t"
+ "库存");
ad.search();
while (true) {
bo1 = new Book();
System.out.println("1.查询 2.图书入库 3.图书出库 "
+ "4.新增图书 5.修改 6.删除 7.退出\t请输入您的选择:");
int xuan = input.nextInt();
switch (xuan) {
case 1:
System.out.println("图书编号\t" + "书名\t" + "作者\t" + "出版日期\t"
+ "价格\t" + "库存");
ad.search();
break;
case 2:
System.out.print("请输入你想入库的图书编号:");
String num = input.next();
System.out.print("请输入你想添加本图书的数量:");
int cou = input.nextInt();
ad.increase(num, cou);
break;
case 3:
System.out.print("请输入你想取出的图书编号:");
num = input.next();
System.out.print("请输入你想取出图书的数量:");
cou = input.nextInt();
ad.reduce(num, cou);
break;
Java使用面向对象编程思维编写图书管理系统:增加,查询,修改,删除,退出,怎么写?
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 ListBook add(ListBook 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(ListBook list) {
System.out.println("编号\t书名");
for (Book b : list) {
System.out.println(b.getbId() + "\t" + b.getbName());
}
}
/*
* 修改
* */
public static void update(ListBook 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(ListBook 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) {
ListBook 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;
}
}
}
}
JAVA版的图书管理系统
送你一份代码,结构大致按照你的需求了,自己增加一些小功能,不会的话请教你同学。
分给我,钱就不用了。
#include string.h
#define FORMAT "\n%-8d%-8d%-7d%-8d%-7d%-8d%-10s%-9s%-9s\n"
#include stdio.h
#include conio.h
#include malloc.h
#define NULL 0
#define N 100
#define LEN sizeof(struct book)
int M;
struct book
{int Enum;
int Cnum;
char name[10];
char author[10];
char publishor[30];
struct date
{int year;
int month;
int day;}time;
int price;
struct book*next;
}go[N];
void print()
{printf("---------------------------------------------------------------------------\n");
printf("Enum Cnum year month day price name author publishor\n");
printf("---------------------------------------------------------------------------\n");
}
void load()
{FILE *fp;
int i;
if((fp=fopen("book_list","rb"))==NULL)
{printf("cannot open file\n");
return;
}
i=0;
while((fread(go[i],sizeof(struct book),1,fp))!=NULL)
{i++;}
M=i;
fclose(fp);
}
void save(int h)
{FILE *fp;
int i;
if ((fp=fopen("BOOK_LIS","wb"))==NULL)
{printf("cannot open file\n");
return;
}
for (i=0;ih;i++)
if(fwrite(go[i],sizeof(struct book),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void f1()
{FILE *fp;
int i=0;
fp=fopen("book_list","rb");
print();
while((fread(go[i],sizeof(struct book),1,fp))!=NULL)
{printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor);
i++;
}
fclose(fp);
getch();
}
void f2(int h)
{int i;
printf("please input %d book's information\n",h);
printf("Enum Cnum year month day price name author publishor\n");
for(i=0;ih;i++)
{printf("Enum:\n");
scanf("%d",go[i].Enum);
printf("Cnum:\n");
scanf("%d",go[i].Cnum);
printf("please input year month day\n");
scanf("%d%d%d",go[i].time.year,go[i].time.month,go[i].time.day);
printf("price:\n");
scanf("%d",go[i].price);
printf("name:\n");
scanf("%s",go[i].name);
printf("author:\n");
scanf("%s",go[i].author);
printf("publishor:\n");
scanf("%s",go[i].publishor);
}
save(h);
return;
}
void f3()
{int i;
struct book;
char a[20],b[20],c[20];
load();
printf("please input 'name' or 'author':\n");
scanf("%s",a);
if(strcmp(a,"name")==0)
{printf("please input the book's name:\n");
scanf("%s",b);
for(i=0;iM;i++)
if(strcmp(b,go[i].name)==0)
{print();
printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor);
}
}
else
{printf("please input the book's author:\n");
scanf("%s",c);
for(i=0;iM;i++)
if(strcmp(c,go[i].author)==0)
{print();
printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor);
}
}
return;
}
void f4()
{int i,j,k;
struct book t;
load();
for(i=0;iM;i++)
{k=i;
for(j=i+1;jM;j++)
if(go[k].pricego[j].price)k=j;
t=go[i];go[i]=go[k];go[k]=t;
}
print();
for(i=0;iM;i++)
printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor);
}
void f5()
{FILE *fp;
int i,j,M,flag;
char name[10];
if((fp=fopen("book_list","rb"))==NULL)
{printf("cannot open file\n");
return;
}
printf("\norriginal data:\n");
print();
for(i=0;fread(go[i],sizeof(struct book),1,fp)!=0;i++)
printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor);
M=i;
printf("\n input the deleted name:\n");
scanf("%s",name);
for(flag=1,i=0;flagiM;i++)
{if(strcmp(name,go[i].name)==0)
{for(j=i;jM-1;j++)
{go[j].Enum=go[j+1].Enum;
go[j].Enum=go[j+1].Enum;
strcpy(go[j].name,go[j+1].name);
strcpy(go[j].author,go[j+1].author);
strcpy(go[j].publishor,go[j+1].publishor);
go[j].time.year=go[j+1].time.year;
go[j].time.month=go[j+1].time.month;
go[j].time.day=go[j+1].time.day;
go[j].price=go[j+1].price;
}
flag=0;
}
}
if(!flag)
M=M-1;
else
printf("not found!\n");
printf("\nNow,the content of file:\n");
fp=fopen("book_list","wb");
for(i=0;iM;i++)
fwrite(go[i],sizeof(struct book),1,fp);
fclose(fp);
fp=fopen("book_list","wb");
for(i=0;fread(go[i],sizeof(struct book),1,fp)!=0;i++);
printf(FORMAT,go[i].Enum,go[i].Cnum,go[i].time.year,go[i].time.month,go[i].time.day,go[i].price,go[i].name,go[i].author,go[i].publishor);
fclose(fp);
}
main()
{int i,h;
clrscr();
while(1)
{printf ("\n 1 is a project that can output all information.\n");
printf ("\n 2 is a project that can add any book's information.\n");
printf ("\n 3 is a project that can search information.\n");
printf ("\n 4 is a project that can sort.\n");
printf ("\n 5 is a project that can del.\n");
printf ("\n 6 is a project that can leave.\n");
printf("please input number:1 or 2 or 3 or 4 or 5 or 6\n");
scanf("%d",i);
switch (i)
{case 1:f1();break;
case 2:
{printf ("if you want to add book's information,please input a data:h=");
scanf("%d",h);
f2(h);}break;
case 3:
{f3();getch();}break;
case 4:{f4();getch();}break;
case 5:{f5();getch();}break;
case 6:exit (1);
}
clrscr();}
}