实现项目任务
java类
package java1;import java.util.ArrayList;
import java.util.Scanner;public class Test {public static void main(String[] args) {//8.创建一个集合,用于存放相同个体,一个个添加,注意作用范围,将数组放在外面ArrayList list =new ArrayList();while(true){ //3.加入死循环,让输入一直有System.out.println();System.out.println("--->项目功能如下:<---");System.out.println("--->1.展示书籍<---");System.out.println("--->2.上架书籍<---");System.out.println("--->3.下架书籍<---");System.out.println("--->4.退出应用<---");System.out.println();System.out.println("请输入功能编号:");//1.借助scanner类输入东西Scanner sc =new Scanner(System.in);int choice =sc.nextInt(); //2.录入数据if(choice==1){System.out.println(" 1.展示书籍");//System.out.println(list.size());//遍历数组(集合)来获取书for(int i =0;i<list.size();i++){// if((list.size())==0){ //9.运用if语句输入没有书的时候
// System.out.println("目前傻淙图书馆没有书");
// }
// else{//System.out.println(list);Book b=(Book)(list.get(i));System.out.println(b.getbNo()+ " "+b.getbName()+" "+b.getbAuthor());
// }}}if(choice==2){System.out.println(" 2.上架书籍");//5.从键盘录入书籍信息System.out.println("请输入书籍编号:");int bNo=sc.nextInt();System.out.println("请输入书籍名字:");String bName=sc.next();System.out.println("请输入书籍作者:");String bAuthor=sc.next();System.out.println("录入完毕");//6.创建对象,将数据给对象Book b=new Book();b.setbNo(bNo);b.setbName(bName);b.setbAuthor(bAuthor); //7.运用set方法录入list.add(b);}if(choice==3){System.out.println(" 3.下架书籍");//10.录入书籍编号System.out.println("请输入你要下架的书籍的编号");int delNo =sc.nextInt();for(int i=0;i<=list.size();i++){//11.遍历数组找到需要删除的书籍编号Book b=(Book)list.get(i);//12.获取书籍编号,便将书籍转为Book类if(b.getbNo()==delNo){list.remove(b);System.out.println("下架成功");break;}}}if(choice==4){System.out.println(" 4.退出应用");break;//4.跳出死循环}}}
}
总结:
package java1;import java.util.ArrayList;
import java.util.Scanner;public class Test {public static void main(String[] args) {//8.创建一个集合,用于存放相同个体,一个个添加,注意作用范围,将数组放在外面888888888888888888888888888888888888888ArrayList list =new ArrayList();while(true){ //3.加入死循环,让输入一直有33333333333333333333333333333333333333333System.out.println();System.out.println("--->项目功能如下:<---");System.out.println("--->1.展示书籍<---");System.out.println("--->2.上架书籍<---");System.out.println("--->3.下架书籍<---");System.out.println("--->4.退出应用<---");System.out.println();System.out.println("请输入功能编号:");//1.借助scanner类输入东西111111111111111111111111111111111111111111111111111111Scanner sc =new Scanner(System.in);int choice =sc.nextInt(); //2.录入数据22222222222222222222222222222222222222222222if(choice==1){System.out.println(" 1.展示书籍");//System.out.println(list.size());//遍历数组(集合)来获取书for(int i =0;i<list.size();i++){//9.运用for循环变量书籍输出999999999999999999999// if((list.size())==0){ //9.运用if语句输入没有书的时候
// System.out.println("目前傻淙图书馆没有书");
// }
// else{//System.out.println(list);Book b=(Book)(list.get(i));//10101010.获取list的值用于输出10101010101010101010System.out.println(b.getbNo()+ " "+b.getbName()+" "+b.getbAuthor());
// }}}if(choice==2){System.out.println(" 2.上架书籍");//5.从键盘录入书籍信息55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555System.out.println("请输入书籍编号:");int bNo=sc.nextInt();System.out.println("请输入书籍名字:");String bName=sc.next();//注意没有Int了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!System.out.println("请输入书籍作者:");String bAuthor=sc.next();System.out.println("录入完毕");//6.创建对象,将数据给对象666666666666666666666666666666666666666666666666666666666666Book b=new Book();//b.setbNo(bNo);b.setbName(bName);b.setbAuthor(bAuthor); //7.运用set方法录入77777777777777777777777777777777777777777777777777777777list.add(b);//添加列表元素++++++++++++++++++++++++++++++++++++++++}if(choice==3){System.out.println(" 3.下架书籍");//11 11 11 11 11 11 11 11 11 11 11 11 11 11 .录入下架书籍编号System.out.println("请输入你要下架的书籍的编号");int delNo =sc.nextInt();for(int i=0;i<=list.size();i++){//12 12 12 12 12 1 21 21 2121 2 121 21 12 12 12 12 12 12.遍历数组找到需要删除的书籍编号Book b=(Book)list.get(i);//13 13 13 13 13 13 13.获取书籍编号,便将书籍转为Book类if(b.getbNo()==delNo){//14.14.14.14.14.14.用remove删除list的元素list.remove(b);System.out.println("下架成功");break;}}}if(choice==4){System.out.println(" 4.退出应用");break;//4.跳出死循环444444444444444444444444444444444444444444444444444444444444444444444444444444444}}}
}
Book类
package java1;public class Book {private int bNo;private String bName;private String bAuthor;public Book() {// 默认构造器的实现,可能需要对字段进行初始化}public Book(int bNo, String bName, String bAuthor) {this.bNo = bNo;this.bName = bName;this.bAuthor = bAuthor;}public int getbNo() {return bNo;}public void setbNo(int bNo) {this.bNo = bNo;}public String getbName() {return bName;}public void setbName(String bName) {this.bName = bName;}public String getbAuthor() {return bAuthor;}public void setbAuthor(String bAuthor) {this.bAuthor = bAuthor;}
}