Java編寫選購圖書項目

public class Books {
    private String book_no;
    private String book_name;
    private String book_author;
    private String release_time;
    private double price;
    private int stock;

    public Books() {
        super();
    }

    public Books(String book_no, String book_name, String book_author,
            String release_time, double price, int stock) {
        super();
        this.book_no = book_no;
        this.book_name = book_name;
        this.book_author = book_author;
        this.release_time = release_time;
        this.price = price;
        this.stock = stock;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public String getBook_no() {
        return book_no;
    }

    public void setBook_no(String book_no) {
        this.book_no = book_no;
    }

    public String getBook_name() {
        return book_name;
    }

    public void setBook_name(String book_name) {
        this.book_name = book_name;
    }

    public String getBook_author() {
        return book_author;
    }

    public void setBook_author(String book_author) {
        this.book_author = book_author;
    }

    public String getRelease_time() {
        return release_time;
    }

    public void setRelease_time(String release_time) {
        this.release_time = release_time;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
public class BookManager {
    private Books[] books;
    private int ID = 10000;
    private double summoney;
    public BookManager() {
        super();
        books = new Books[30];
    }

    // 圖書入庫
    public void addBooks(String ID, int stock) {
        for (Books b : books) {
            if (b.getBook_no().equals(ID)) {
                b.setStock(b.getStock() + stock);
                break;
            }
        }
    }

    // 圖書出庫
    public void delivery(String ID, int stock) {
        for (Books b : books) {
            if (b.getBook_no().equals(ID)) {
                if (b.getStock() > stock) {
                    b.setStock(b.getStock() - stock);
                    break;
                }
            }
        }
    }

    // 查詢所有圖書
    public void showBooks() {
        if (books != null) {
            for (Books books2 : books) {
                if (books2 != null) {
                    System.out.println(books2.getBook_no() + "\t"
                            + books2.getBook_name() + "\t"
                            + books2.getBook_author() + "\t"
                            + books2.getRelease_time() + "\t"
                            + books2.getPrice() + "\t" + books2.getStock());
                }
            }
        } else {
            System.err.print("暫無庫存!");
        }
    }

    // 新增圖書
    public void increaseBook(Books books) {
        for (int i = 0; i < this.books.length; i++) {
            if (this.books[i] == null) {
                this.books[i] = books;
                break;
            }
        }
    }
    
    //自動添加圖書編號
    public String returnNo() {
        if (books != null) {
            ID += 1;
            return String.valueOf(ID);
        } else {
            return "";
        }

    }
    
    // 結帳
        public void account(String ID, int num) {
            for (Books books2 : books) {
                if (books2.getBook
public class Test {

    public static void main(String[] args) {
        System.out.println("***********歡迎使用圖書書店**********");
        System.out.print("請輸入用戶名:");
        Scanner input = new Scanner(System.in);
        String name = input.next();
        System.out.println("請輸入密碼");
        String pwd = input.next();
        Admin admin = new Admin(name, pwd);
        boolean flag1 = admin.identity();
        User user = new User(name, pwd);
        boolean flag2 = user.identity();
        BookManager bookManager = new BookManager();
        if (flag1) {
            System.out.println("登陸成功!");
            System.out.println();
            boolean flag = true;
            System.out.println("********歡迎登陸庫存管理系統*********");
            do {
                System.out
                        .println("請選擇進行的操做:1.圖書入庫 2.圖書出庫 3.查詢所有圖書 4.新增圖書 5.退出");
                int num = input.nextInt();
                switch (num) {
                case 1:
                    System.out.print("請輸入圖書ID:");
                    String bookID = input.next();
                    System.out.print("請輸入入庫的數量");
                    int stock = input.nextInt();
                    bookManager.addBooks(bookID, stock);
                    System.out.print("書號\t書名\t做者\t發佈時間\t價格\t庫存\n");
                    bookManager.showBooks();
                    break;
                case 2:
                    System.out.print("請輸入圖書ID:");
                    String bookID1 = input.next();
                    System.out.print("請輸入出庫的數量");
                    int stock1 = input.nextInt();
                    bookManager.delivery(bookID1, stock1);
                    System.out.print("書號\t書名\t做者\t發佈時間\t價格\t庫存\n");
                    bookManager.showBooks();
                    break;
                case 3:
                    System.out.print("書號\t書名\t做者\t發佈時間\t價格\t庫存\n");
                    bookManager.showBooks();
                    break;
                case 4:
                    System.out.print("請輸入書名:");
                    String authorName = input.next();
                    System.out.print("請輸入做者名:");
                    String author = input.next();
                    System.out.print("請輸入發佈時間:");
                    String time = input.next();
                    System.out.print("請輸入價格:");
                    double price = input.nextDouble();
                    System.out.println("請輸入數量:");
                    int stock2 = input.nextInt();
                    Books book = new Books(bookManager.returnNo(), authorName,
                            author, time, price, stock2);
                    bookManager.increaseBook(book);
                    System.out.print("書號\t書名\t做者\t發佈時間\t價格\t庫存\n");
                    bookManager.showBooks();
                    break;
                default:
                    flag = false;
                    break;
                }

            } while (flag);
        }
        if (flag2) {
            System.out.println("登陸成功!");
            Books books2 = new Books("10001", "鬼吹燈", "天下霸唱", "2008-7-1", 23, 87);
            Books books = new Books("10002", "魔戒1", "莫爾斯", "2005-3-14", 54, 13);
            bookManager.increaseBook(books);
            bookManager.increaseBook(books2);
            boolean flag = true;
            System.out.println("********歡迎光臨圖書館系統*********");
            do {
                System.out.println("請選擇進行的操做:1.查看圖書 2.結帳 3.退出");
                int num = input.nextInt();
                switch (num) {
                case 1:
                    System.out.print("書號\t書名\t做者\t發佈時間\t價格\t庫存\n");
                    bookManager.showBooks();
                    break;
                case 2:
                    System.out.print("請輸入圖書ID:");
                    String bookID1 = input.next();
                    System.out.print("請輸入購買數量");
                    int buy_num = input.nextInt();
                    bookManager.account(bookID1, buy_num);
                    break;

                default:
                    flag = false;
                    break;
                }

            } while (flag);

        }

    }

}
 
 

 

_no().equals(ID)) {
                    summoney = books2.getPrice() * num;
                    System.out.print(books2.getBook_no()+":"+books2.getPrice()+"\n"+
                    "數量:"+num+"\n小記:"+summoney);
                    break;
                }
            }
        }
public class Admin {
    private String admin_name;
    private String admin_pwd;

    public Admin() {
        super();
    }

    public Admin(String admin_name, String admin_pwd) {
        super();
        this.admin_name = admin_name;
        this.admin_pwd = admin_pwd;
    }
    
    // 身份認證
        public boolean identity() {
            if (getAdmin_name().equals("admin") && getAdmin_pwd().equals("admin")) {
                return true;
            } else {
                return false;
            }
        }

    public String getAdmin_name() {
        return admin_name;
    }

    public void setAdmin_name(String admin_name) {
        this.admin_name = admin_name;
    }

    public String getAdmin_pwd() {
        return admin_pwd;
    }

    public void setAdmin_pwd(String admin_pwd) {
        this.admin_pwd = admin_pwd;
    }

}
public class User {
    private String user_name;
    private String user_pwd;
    
    Admin admin = new Admin();
    public User() {
        super();
    }

    public User(String user_name, String user_pwd) {
        super();
        this.user_name = user_name;
        this.user_pwd = user_pwd;
    }

    // 身份認證
    public boolean identity() {
        if (getUser_name().equals("user") && getUser_pwd().equals("user")) {
            return true;
        } else {
            return false;
        }
    }
    
    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_pwd() {
        return user_pwd;
    }

    public void setUser_pwd(String user_pwd) {
        this.user_pwd = user_pwd;
    }

}
相關文章
相關標籤/搜索