⑴ 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 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;
}
}
}
}
⑵ 用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("**************************");
}
}
----------------------------------------------------