1.概述
书城的基本功能:
- 展示书籍
- 上新书籍
- 下架书籍
- 退出应用
书城项目所涉及到的知识点:
- 用户交互–键盘录入
- 分支结构,循环结构
- 面向对象的思维,封装对象
- 集合的使用
2.菜单的编写
这部分代码就是读取用户键盘的录入,然后执行菜单功能
package bookstorw;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
while(true) {
System.out.println("欢迎来到书城:");
System.out.println("1.展示书籍:");
System.out.println("2.上新书籍:");
System.out.println("3.下架书籍:");
System.out.println("4.退出应用:");
//借助Scanner类,扫描键盘输入
Scanner sc = new Scanner(System.in);
System.out.println("请输入整数序号。");
//获取键盘输入的整数值
int choice = sc.nextInt();
if (choice == 1) {
System.out.println("1>>>>>展示书籍");
}
if (choice == 2) {
System.out.println("2>>>>>上新书籍");
}
if (choice == 3) {
System.out.println("3>>>>>下架书籍");
}
if (choice == 4) {
System.out.println("4>>>>>退出应用");
break;
}
}
}
}
3.上新书籍
此处应当抽象出来一个书籍的类。每新增书籍,我们都应该实例化该类。
创建一个书籍对象,他有三个属性,书号,书籍名字还有作者名
package bookstorw;
public class Book {
//书籍编号
private int bno;
//书籍名称
private String bname;
//书籍作者
private String bwriter;
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
public String getBwriter() {
return bwriter;
}
public void setBwriter(String bwriter) {
this.bwriter = bwriter;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public Book() {
}
public Book(int bno, String bname, String bwriter) {
this.bno = bno;
this.bname = bname;
this.bwriter = bwriter;
}
public static void main(String[] args) {
}
}
然后重写choice==2这个分支
if (choice == 2) {
System.out.println("2>>>>>上新书籍");
//从键盘录入书籍信息
System.out.println("请输入书籍编号:");
int bNo = sc.nextInt();
System.out.println("请输入书籍名字:");
String bName = sc.next();
System.out.println("请输入书籍作者:");
String bWriter = sc.next();
//创建一个书籍对象
Book b = new Book();
b.setBno(bNo);
b.setBname(bName);
b.setBwriter(bWriter);
list.add(b);
}
在while循环前创建一个集合,这样这个集合的作用域更大,在choice==1的时候也可以使用
4.展示书籍与下架书籍
重写choice==1
for循环来遍历输出集合里的所有对象
if (choice == 1) {
System.out.println("1>>>>>展示书籍");
for(int i = 0;i <= list.size() - 1; i++){
//获取到i之后,将他强制转换为book类型的对象
Book b = (Book)(list.get(i));
System.out.println(b.getBno()+"----"+b.getBname()+"-----"+b.getBwriter());
}
}
下架书籍
遍历整个集合对象,寻找与所输入的整型相等的书籍编号,然后删除
if (choice == 3) {
System.out.println("3>>>>>下架书籍");
System.out.println("录入需要下架的书籍的编号:");
int deleteBook = sc.nextInt();
for(int i = 0;i <= list.size() - 1; i++){
//获取到i之后,将他强制转换为book类型的对象
Book b = (Book)(list.get(i));
if(b.getBno() == deleteBook){
list.remove(b);
System.out.println("书籍下架成功。");
break;
}
}
}
5.全部代码
总共两个文件
Test.java
package bookstorw;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//创建一个集合,存放书籍对象
ArrayList list = new ArrayList();
while(true) {
System.out.println("欢迎来到书城:");
System.out.println("1.展示书籍:");
System.out.println("2.上新书籍:");
System.out.println("3.下架书籍:");
System.out.println("4.退出应用:");
//借助Scanner类,扫描键盘输入
Scanner sc = new Scanner(System.in);
System.out.println("请输入整数序号。");
//获取键盘输入的整数值
int choice = sc.nextInt();
if (choice == 1) {
System.out.println("1>>>>>展示书籍");
for(int i = 0;i <= list.size() - 1; i++){
//获取到i之后,将他强制转换为book类型的对象
Book b = (Book)(list.get(i));
System.out.println(b.getBno()+"----"+b.getBname()+"-----"+b.getBwriter());
}
}
if (choice == 2) {
System.out.println("2>>>>>上新书籍");
//从键盘录入书籍信息
System.out.println("请输入书籍编号:");
int bNo = sc.nextInt();
System.out.println("请输入书籍名字:");
String bName = sc.next();
System.out.println("请输入书籍作者:");
String bWriter = sc.next();
//创建一个书籍对象
Book b = new Book();
b.setBno(bNo);
b.setBname(bName);
b.setBwriter(bWriter);
list.add(b);
}
if (choice == 3) {
System.out.println("3>>>>>下架书籍");
System.out.println("录入需要下架的书籍的编号:");
int deleteBook = sc.nextInt();
for(int i = 0;i <= list.size() - 1; i++){
//获取到i之后,将他强制转换为book类型的对象
Book b = (Book)(list.get(i));
if(b.getBno() == deleteBook){
list.remove(b);
System.out.println("书籍下架成功。");
break;
}
}
}
if (choice == 4) {
System.out.println("4>>>>>退出应用");
break;
}
}
}
}
Book.java
package bookstorw;
public class Book {
//书籍编号
private int bno;
//书籍名称
private String bname;
//书籍作者
private String bwriter;
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
public String getBwriter() {
return bwriter;
}
public void setBwriter(String bwriter) {
this.bwriter = bwriter;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public Book() {
}
public Book(int bno, String bname, String bwriter) {
this.bno = bno;
this.bname = bname;
this.bwriter = bwriter;
}
public static void main(String[] args) {
}
}