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; } }