java簡易圖書管理系統
阿新 • • 發佈:2018-12-20
1.自己做的
package BOOk; public class Book { private String name; private String author; private double price; private int store; private String publishDate; public Book(String name, String author, double price, int store, String publishDate) { this.name = name; this.author = author; this.price = price; this.store = store; this.publishDate = publishDate; } public Book() { super(); // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getStore() { return store; } public void setStore(int store) { this.store = store; } public String getPublishDate() { return publishDate; } public void setPublishDate(String publishDate) { this.publishDate = publishDate; } public void add(){ } }
主程式碼
package BOOk; import java.util.Scanner; public class newBook { public static void main(String[] args) { System.out.println("歡迎使用圖書書店"); System.out.println("請輸入使用者名稱:"); Scanner input = new Scanner(System.in); String userName = input.nextLine(); String trueUserNamer = "admin"; System.out.println("請輸入密碼:"); String mima = input.nextLine(); String password = "admin"; if ((userName.equals(trueUserNamer)) && (mima.equals(password))) System.out.println("登入成功"); else System.out.println("登入失敗"); Book[] book = new Book[5]; Book b1 = new Book("鬼吹燈", "天下霸唱", 33, 6, "2004-2-12"); Book b2 = new Book("魔界", "莫爾斯", 45, 4, "2001-1-1"); Book b3 = new Book("時間機器", "威爾", 64, 5, "2000-1-22"); Book b4 = new Book("哈利波特", "羅一夢", 12, 3, "1998-4-12"); Book b5 = new Book("暫無", "暫無", 0, 0, "暫無"); book[0] = b1; book[1] = b2; book[2] = b3; book[3] = b4; book[4] =b5; do { System.out.println("~~~~~~~~歡迎使用本圖書管理系統~~~~~~~~"); System.out.println("\t1.檢視圖書"); System.out.println("\t2.圖書入庫"); System.out.println("\t3.圖書出庫"); System.out.println("\t4.新增圖書"); System.out.println("\t5.退出"); System.out.println("請進行你的操作"); int num = input.nextInt(); switch (num) { case 1:// 查詢 System.out.println("書名\t作者\t價格\t庫存\t出版日期"); for (int i = 0; i < book.length; i++) { Book b = book[i]; System.out.println(b.getName() + "\t" + b.getAuthor() + "\t" + b.getPrice() + "\t" + b.getStore() + "\t" + b.getPublishDate()); } break; case 2: // 圖書入庫; System.out.println("請輸入書名"); String name = input.next(); if (name.equals("鬼吹燈") || name.endsWith("魔界") || name.endsWith("哈利波特") || name.endsWith("時間機器")) System.out.println("請輸入增加庫存"); int store = input.nextInt(); for (int i = 0; i <= book.length; i++) { if (name.equals(book[i].getName())) { book[i].setStore(book[i].getStore() + store); break; } } break; case 3:// 出庫 System.out.println("請輸入書名"); String name1 = input.next(); if (name1.equals("鬼吹燈") || name1.endsWith("魔界") || name1.endsWith("哈利波特") || name1.endsWith("時間機器")) System.out.println("請輸入要借的數量"); int store1 = input.nextInt(); for (int i = 0; i <= book.length; i++) { if (name1.equals(book[i].getName())) { book[i].setStore(book[i].getStore() - store1); break; } } break; case 4: System.out.println("請輸入書名"); String name2=input.next(); System.out.println("請輸入作者"); String author2=input.next(); System.out.println("請輸入價格"); double price2=input.nextDouble(); System.out.println("請輸入庫存"); int store2=input.nextInt(); System.out.println("請輸入出版日期"); String publicDate=input.next(); b5.setName(name2);b5.setAuthor(author2);b5.setPrice(price2);b5.setStore(store2); b5.setPublishDate(publicDate); break; case 5: return; } } while (true); } }
執行效果
2.老師做的
public class Book { //書號 書名 作者 釋出日期 價格 庫存 private int id; private String name; private String author; private String date; private double price; private int count; public Book(int id, String name, String author, String date, double price, int count) { super(); this.id = id; this.name = name; this.author = author; this.date = date; this.price = price; this.count = count; } public Book() { super(); // TODO Auto-generated constructor stub } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } 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; } }
public class Admin {
private static String userName;
private static String password;
private static String realName;
static{
userName = "admin";
password = "admin";
realName = "金庸";
}
public static String getUserName() {
return userName;
}
public static String getPassword() {
return password;
}
public static String getRealName() {
return realName;
}
}
主程式碼
import java.util.Arrays;
import java.util.Scanner;
import com.sxt.bms.entity.Admin;
import com.sxt.bms.entity.Book;
public class TestBook {
static Scanner sc = new Scanner(System.in); //列印物件
//初始化圖書陣列
static Book[] book = new Book[5];
static{
book[0] = new Book(10001,"鬼吹燈","天下霸唱","2008-7-1",27.8,87);
book[1] = new Book(10002,"盜墓筆記","三叔","2008-7-1",35,100);
book[2] = new Book(10003,"西遊記","吳承恩","2008-7-1",27.8,87);
book[3] = new Book(10004,"水滸傳","羅貫中","2008-7-1",50,87);
book[4] = new Book(10005,"三國演義","施耐庵","2008-7-1",89,120);
}
/**
* 管理員登入
*/
public static void login(){
System.out.println("請輸入使用者名稱:");
String userName = sc.nextLine();
System.out.println("請輸入密碼:");
String password = sc.nextLine();
if(Admin.getUserName().equals(userName)&&Admin.getPassword().equals(password)){
System.out.println("登陸成功");
//跳轉到主介面
index();
}else{
System.out.println("使用者名稱或密碼錯誤");
login();
}
}
/**
* 主介面功能
*/
public static void index(){
//請選擇進行的操作:1.圖書入庫 2.圖書出庫 3.查詢全部圖書 4.新增圖書 5.退出
System.out.println("*********歡迎【"+Admin.getRealName()+"】登入庫存管理系統**********");
queryAllBook();//查詢全部圖書
System.out.println("1.圖書入庫");
System.out.println("2.圖書出庫");
System.out.println("3.查詢全部圖書");
System.out.println("4.新增圖書");
System.out.println("5.退出");
System.out.println("請選擇:");
int num = Integer.parseInt(sc.nextLine());
switch(num){
case 1:enterBook();index();break;//入庫
case 2:outBook();index();break; //出庫
case 3:queryAllBook();index();break;//查詢全部圖書
case 4:addBook();index();break; //新增圖書
case 5:logout();return;//退出
default:System.out.println("輸入錯誤");index();break;
}
}
/**
* 查詢所有圖書
*/
public static void queryAllBook(){
System.out.println("書號\t書名\t作者\t釋出日期\t\t價格\t庫存");
for(int i = 0;i<book.length;i++){
Book b = book[i];
System.out.println(b.getId()+"\t"+b.getName()+"\t"+b.getAuthor()+"\t"+
b.getDate()+"\t"+b.getPrice()+"\t"+b.getCount());
}
}
/**
* 圖書入庫
*/
public static void enterBook(){
System.out.println("請輸入圖書資訊");
System.out.println("請輸入圖書ID:");
int id = sc.nextInt();
int index = -1;
//找圖書的編號和輸入的編號做相等比較
for(int i = 0;i<book.length;i++){
int bId = book[i].getId();
if(bId==id){
index = i;
break;
}
}
//入庫
if(index!=-1){
System.out.println("請輸入入庫的數量:");
int count = sc.nextInt();
//入庫
book[index].setCount(book[index].getCount()+count);
}else{
System.out.println("圖書編號不存在");
enterBook();
}
}
/**
* 圖書出庫
* @param args
*/
public static void outBook(){
System.out.println("請輸入圖書資訊");
System.out.println("請輸入圖書ID:");
int id = sc.nextInt();
int index = -1;
//找圖書的編號和輸入的編號做相等比較
for(int i = 0;i<book.length;i++){
int bId = book[i].getId();
if(bId==id){
index = i;
break;
}
}
//出庫
if(index!=-1){
System.out.println("請輸入出庫的數量:");
int count = sc.nextInt();
//出庫之前先判斷,出庫數量不能大於倉庫的數量
int bCount = book[index].getCount();
if(count>bCount){
System.out.println("出庫的數量不能大於本書的庫存");
outBook();
}else{
//出庫
book[index].setCount(bCount-count);
}
}else{
System.out.println("圖書編號不存在");
outBook();
}
}
/**
* 新增圖書
* @param args
*/
public static void addBook(){
System.out.println("請輸入圖書資訊");
System.out.println("請輸入書名:");
String name = sc.nextLine();
System.out.println("請輸入作者");
String author = sc.nextLine();
System.out.println("請輸入出版時間:");
String date = sc.nextLine();
System.out.println("請輸入價格:");
double price = Double.parseDouble(sc.nextLine());
System.out.println("請輸入庫存:");
int count = Integer.parseInt(sc.nextLine());
//圖書編號自增
int id = book[book.length-1].getId()+1;
//1 先封裝到書籍物件
Book newBook = new Book(id, name, author, date, price, count);
//2 把書籍物件封裝到陣列中
book = Arrays.copyOf(book, book.length+1);
book[book.length-1] = newBook;
}
/**
* 登出
* @param args
*/
public static void logout(){
System.out.println("歡迎下次光臨!!!");
}
public static void main(String[] args) {
System.out.println("----歡迎使用尚學堂圖書書店----");
login();
}
}
總結:1.注意程式碼框架的搭建
2.注意新增圖書的呼叫
3.學習呼叫各種方法