// 第一部分为实体类 ,放属性及set,get方法
public class Book {
private String No;
private String name;
private String author;
private String pubDate;
private double price;
private int num; //声明book类的属性,并设为私有,用set和get方法存取
public String getNo() {
return No;
}
public void setNo(String no) {
No = no;
}
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 getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Book() {
// TODO Auto-generated constructor stub
}
}
// 第二部分为方法类 用来声明和构造对book操作所需要的方法
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; //方法调用所需要打的包
public class BookBiz {
@SuppressWarnings("rawtypes")
List lt = new ArrayList(); //定义一个实现类名为lt 的ArrayLi集合用来存取book
public boolean userTest(int pwd,String name){ // 验证用户登录
boolean flag = true;
if(name.equals("user")&& pwd == 111){
flag = true;
}else{
flag = false;
}
return flag;
}
public boolean adminTest(int pwd,String name){ //验证管理员登录
boolean flag = true;
if(name.equals("admin")
&& pwd == 111){
flag = true;
}else{
flag = false;
}
return flag;
}
@SuppressWarnings("unchecked")
public void addBook(Book book){ //用ArrayList实现类里的add方法把book对象存取集合
lt.add(book);
}
@SuppressWarnings("rawtypes")
public boolean noTest(String no){ // 自定义的一个noTest 方法用来验证输入的图书编号是否越界
boolean flag = false;
Iterator it = lt.iterator();
while(it.hasNext()){
Book b = (Book)it.next(); //用迭代器的方法遍历
if(b.getNo().equals(no)){
flag = true;
break;
}else{
flag = false;
}
}
return flag;
}
public boolean numTest(int num){ //numTest方法用来验证输入的num是否越界
boolean flag = false;
if(num > 0){
flag = true;
}else{
flag = false;
}
return flag;
}
public void deleteBook(String no){
Iterator it = lt.iterator(); //定义删除图书的方法
while(it.hasNext()){
Book b = (Book)it.next();
if(b.getNo().equals(no)){
lt.remove(b);
break;
}
}
}
@SuppressWarnings("rawtypes")
public void update(String no,String name,String author,String pubDate,double price,int num){
Iterator it = lt.iterator();
while(it.hasNext()){
Book b = (Book)it.next();
if(b.getNo().equals(no)){ //定义修改图书的方法,这里直接遍历根据图书编号找到要修改的对象,然后直接重新把要修改的属
性直接set
b.setAuthor(author);
b.setName(name);
b.setPubDate(pubDate);
b.setPrice(price);
b.setNum(num);
}
}
}
@SuppressWarnings("rawtypes")
public void show(){
Iterator it = lt.iterator();
System.out.println("\t\t*****************欢迎使用图书馆管理系统******************"); //这是用来把存入到集合的对象全部遍历并打印出来
System.out.println("\t\t书号\t书名\t作者\t发布日期\t\t价格\t库存 ");
while(it.hasNext()){
Book b = (Book)it.next();
System.out.print("\t\t"+b.getNo()+"\t"+b.getName()+"\t"+b.getAuthor()+"\t"+
b.getPubDate()+"\t"+b.getPrice()+"\t"+b.getNum()+"\n");
}
}
public void addStock(String no,int num){ //这是图书入库的方法,也就是增加图书数量
for(int i = 0;i < lt.size();i++){
Book b = (Book)lt.get(i);
if(no.equals(b.getNo())){
b.setNum(num+b.getNum());
}
}
}
@SuppressWarnings("rawtypes")
public int deleteStock(String no,int num){
Iterator it = lt.iterator(); //这是图书出库的方法
while(it.hasNext()){
Book b = (Book)it.next();
if(num > b.getNum()){
return 0;
}
if(no.equals(b.getNo())){
num = b.getNum()-num;
}
}
return num;
}
public BookBiz() {
// TODO Auto-generated constructor stub
}
}
//第三部分就是测试类了,在这里要调用方法来实现一些功能
package JavaWork;
import java.util.Scanner;
public class BookTest {
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) {
int pwd = 0;
int back = 0;
int n = 0;
char c = ‘y‘;
int b = 0;
int m = 0;
boolean flag = true; //这里声明以后要用到的变量
BookBiz bz = new BookBiz();
Scanner input = new Scanner(System.in);
Book book = new Book(); //构造对象book来存储图书的所有属性
book.setNo("10001"); book.setName("盗墓笔记"); book.setAuthor("南派三叔");book.setPubDate("1993-2- 4");book.setPrice(56.6);book.setNum(100);bz.addBook(book);
Book book1 = new Book();
book1.setNo("10002");book1.setName("围城"); book1.setAuthor("钱钟书");book1.setPubDate("1996-5-4");book1.setPrice(45.8);book1.setNum(100);bz.addBook(book1);
Book book2 = new Book();
book2.setNo("10003");book2.setName("平凡的心");book2.setAuthor("路遥");book2.setPubDate("2001-10-3");book2.setPrice(45.0);book2.setNum(100);bz.addBook(book2);
Book book3 = new Book();
book3.setNo("10004");book3.setName("哈利波特");book3.setAuthor("罗斯");book3.setPubDate("1999-7-14");book3.setPrice(89.0);book3.setNum(100);bz.addBook(book3);
Book book4 = new Book();
book4.setNo("10005");book4.setName("花千骨"); book4.setAuthor("果果");book4.setPubDate("2006-12-9");book4.setPrice(50.5);book4.setNum(100);bz.addBook(book4); //这里把book先存入到集合中,以方便后来对图书的操作
do{
System.out.println("\t\t\t\t请选择登录类型:");
System.out.print("\t\t\t 1.用户 2.管理员: "); //验证使用者是否是用户还是管理员,当然这里是用户还是管理员必须选一个
m = input.nextInt();
if(m != 1&&m != 2){
System.out.println("\t\t\t\t请重新选择用户或管理员!");
}else{
break;
}
}while(c == ‘y‘);
if(m == 1){
do{
System.out.print("\t\t请输入用户名:");
String name = input.next();
System.out.print("\t\t请输入密码:");
pwd = input.nextInt();
flag = bz.userTest(pwd, name); //调用userTest方法验证用户输入的数据是否正确
if(flag){
System.out.println("\t\t登录成功!");
break;
}else{
System.out.print("\t\t密码错误,请重新输入!"+"\n"); //用循环保证用户输入正确的用户名和密码
}
}while(back != 3);
do{
bz.show(); //调用show方法 来打印已经存入的图书
System.out.print("\t\t请选择进行的操作:1.查看图书 2.购买图书 3.退出 "); //这里选择用户能够对图书进行的操作
n = input.nextInt();
do{
if(n == 1){
break; //设置两层循环保证用户在选择查看图书的时候能够跳出内循环,再开始外循环
}else
if(n == 2){
while(c == ‘y‘){ //while循环用来让用户能够根据图书选择性的购买图书
System.out.println("\t\t\t\t\t请输入欲购买的信息:");
System.out.print("\t\t请输入欲购买图书的ID:");
String no = input.next();
flag = bz.noTest(no); //这里就用到了验证输入的图书编号是否和已经存入的编号的一致问题,如果不一致就得重新回到选择界面
if(flag){
System.out.print("\t\t请输入欲购买图书的数量:");
int num = input.nextInt();
switch(no){
case "10001":
if(book.getNum()<num){ //如果要买的数量超过库存就无法完成购买
System.out.println("\t\t库存不足,无法购买!");break;
}
System.out.println(" \t\t鬼吹灯:27.8");
System.out.println("\t\t数量:"+num);book.setNum(book.getNum()-num);
System.out.println("\t\t合计:"+num*27.8);back = 3;break; //back =3是为了跳出内循环,因为用户没有选择退出就必须不能直接退出
case "10002":
if(book1.getNum()<num){
System.out.println("\t\t库存不足,无法购买!");break;
}
System.out.println(" \t\t魔戒1:54.0");
System.out.println("\t\t数量:"+num);book1.setNum(book1.getNum()-num);
System.out.println("\t\t合计:"+num*54.0);back = 3;break;
case "10003":
if(book2.getNum()<num){
System.out.println("\t\t库存不足,无法购买!");break;
}
System.out.println(" \t\t哈利波特1:43.3");
System.out.println("\t\t数量:"+num);book2.setNum(book2.getNum()-num);
System.out.println("\t\t合计:"+num*43.3);back = 3;break;
case "10004":
if(book3.getNum()<num){
System.out.println("\t\t库存不足,无法购买!");break;
}
System.out.println(" \t\t时间机器:43.5");
System.out.println("\t\t数量:"+num);book3.setNum(book3.getNum()-num);
System.out.println("\t\t合计:"+num*43.5);back = 3;break;
case "10005":
if(book4.getNum()<num){
System.out.println("\t\t库存不足,无法购买!");break;
}
System.out.println(" \t\t宇宙奥秘:87.7");
System.out.println("\t\t数量:"+num);book4.setNum(book4.getNum()-num);
System.out.println("\t\t合计:"+num*87.3);back = 3;break;
default: break;
}
bz.show();
System.out.print("\t\t否继续购买(y/n):"); //这里就是提示用户是否继续购买了
c = input.next().charAt(0);
}else {
System.out.println("\t\t对不起,该书号不存在,无法购买!");
back = 3;
break;
}
}
}else if(n == 3){
back = 4; //用户选择退出,先break跳出内循环,然后再用事先已经赋值好的back来跳出外循环直接跳出退出画面
break;
}else{
System.out.println("\t\t对不起,您输入的选择有误,请重新输入!"); //用户选择了123以外的选择就得重新进行选择
back = 3;
}
}while(back != 3);
}while(back != 4);
}else if(m == 2){
do{
System.out.print("\t\t请输入管理员名:");
String name = input.next();
System.out.print("\t\t请输入密码:");
pwd = input.nextInt();
flag = bz.adminTest(pwd, name);
if(flag){ //验证管理员登录
System.out.println("\t\t登录成功!");
break;
}else{
System.out.println("\t\t密码错误,请重新输入!");
}
}while(c == ‘y‘);
do{
do{
bz.show();
System.out.print("\t\t请选择进行的操作:1.图书入库 2.图书出库 3.新增图书 4.删除图书5.修改图书6.退出 :"); //可供管理员进行的操作
n = input.nextInt();
if(n == 1){
System.out.print("\t\t请输入要入库图书的书号:");
String no= input.next();
flag = bz.noTest(no);
if(flag){ //调用判断编号是否与集合里的一致
System.out.print("\t\t请输入要增加的库存数量:");
int num = input.nextInt();
flag = bz.numTest(num);
if(flag){ //判断输入数量是否合法
bz.addStock(no, num);
break;
}else{
System.out.println("\t\t对不起,该书号不存在,无法完成入库!");
break;
}
}else{
System.out.println("\t\t对不起,您输入的入库数量不正确!");
}
}else if(n == 2){
System.out.print("\t\t请输入要出库图书的书号:");
String no = input.next();
flag = bz.noTest(no); //以后每输入一次编号no和数量num都需要判断是否合法
if(flag){
System.out.print("\t\t请输入要减少的库存数量:");
int num = input.nextInt();
flag = bz.numTest(num);
if(flag){
num = bz.deleteStock(no, num); //调用出库的方法
if(num == 0){
System.out.println("\t\t库存不足,无法出库!");
}else{
switch(no){
case "10001": book.setNum(num);;break;
case "10002": book1.setNum(num);break; //这里把调用出库的方法的返回值再重新给库存赋值
case "10003": book2.setNum(num);break;
case "10004": book3.setNum(num);break;
case "10005": book4.setNum(num);break;
default: break;
}
break;
}
}else{
System.out.println("\t\t对不起,您输入的入库数量不对!");
}
}else{
System.out.println("\t\t对不起,没有该书号,无法完成出库!");
}
}else if(n == 3){
c= ‘y‘;
while(c == ‘y‘){
Book book5 = new Book(); //存对象就得构造对象,因为ArrayList是不唯一有序的集合所以定义一个对象可用循环来循环使用
System.out.print("\t\t请输入要增加图书的书号:");
String no = input.next();
book5.setNo(no);
System.out.print("\t\t请输入要添加图书的书名:");
String name = input.next();
book5.setName(name);
System.out.print("\t\t请输入要添加图书的作者:");
String author = input.next();
book5.setAuthor(author);
System.out.print("\t\t请输入要添加图书的出版日期:");
String pubDate = input.next();
book5.setPubDate(pubDate);
System.out.print("\t\t请输入要添加图书的价格:");
double price = input.nextDouble();
do{
if( price < 0){ //这里还要判断输入的价格是否合法
System.out.print("\t\t对不起,您输入的价格不正确,请重新输入:");
price = input.nextDouble();
}else{
break;
}
}while(price > 0);
book5.setPrice(price);
System.out.print("\t\t请输入要添加图书的库存:");
int num = input.nextInt();
do{
if(num < 0){
System.out.print("\t\t对不起,您输入的库存不正确,请重新输入:");
num = input.nextInt();
}else{
break;
}
}while(num > 0);
book5.setNum(num);
bz.addBook(book5);
System.out.print("\t\t是否继续添加(y/n):");
c = input.next().charAt(0);
}
break;
}else if(n == 4){
System.out.print("\t\t请输入要删除图书的书号:");
String no = input.next();
flag = bz.noTest(no);
if(flag){
bz.deleteBook(no); //调用remove方法直接删除对象
break;
}else{
System.out.println("\t\t对不起,您输入的书号不正确!");
break;
}
}else if(n == 5){
System.out.print("\t\t请输入要修改图书的书号:");
String no = input.next();
System.out.print("\t\t请输入要修改图书的书名:");
String name = input.next();
System.out.print("\t\t请输入要修改图书的作者:");
String author = input.next();
System.out.print("\t\t请输入要修改图书的出版日期:");
String pubDate = input.next();
System.out.print("\t\t请输入要修改图书的价格:");
double price = input.nextDouble();
do{
if( price < 0){
System.out.print("对不起,您输入的价格不正确,请重新输入:");
price = input.nextDouble();
}else{
break;
}
}while(price > 0);
System.out.print("\t\t请输入要修改图书的库存:");
int num = input.nextInt();
do{
if(num < 0){
System.out.print("对不起,您输入的库存不正确,请重新输入:");
num = input.nextInt();
}else{
break;
}
}while(num > 0);
bz.update(no, name, author, pubDate, price, num); //x修改一个对象其实就是重新set它的属性,调用update方法来实现
}else if(m == 6){
back = 7;
break;
}else{
System.out.println("\t\t对不起,您输入的选择有误,请重新选择!");
}
}while(back != 6); //back!=6和back !=7是为了给选择进行的操作提供操作之后来打印操作之后的状态
}while(back != 7);
}
System.out.println("\t\t谢谢使用,再见!");
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。