要求使用List存储Book信息完成如下功能
图书管理系统
1、进书
2、借书
3、还书
4、统计书籍总金额
5、删除
import java.util.*;
public class Books {
//定义书籍
List list = new ArrayList();
Scanner jp = new Scanner(System.in);
//进书初始化;
public void jinshu() {
list.add(new Book("三国演义", 299.0, 30));
list.add(new Book("红楼梦", 199.0, 20));
list.add(new Book("水浒传", 99.0, 10));
}
public void jieshu() {
System.out.println("选择要借的书");
String name = jp.next();
boolean flag = false;
for (int i = 0; i < list.size(); i++) {
Book book1 = list.get(i);
for (Book book : list) {
if (name.equals(book1.getName())) {
System.out.println("要借的数量");
int s1 = jp.nextInt();
//将原来的数量取出来减去要借的数量,再重新赋值
book.setNum(book.getNum() - s1);
flag = true;
break;
}
}
}
if (flag == false) {
System.out.println("书籍名称不存在");
}
}
public void chaxun() {
for (Book book : list) {
System.out.println(book.getName() + " " + book.getPrice() + "元" + " " + book.getNum()+"本");
}
}
public void huanshu() {
System.out.println("选择要还的书");
String name2 = jp.next();
boolean flag = false;
for (int i = 0; i < list.size(); i++) {
Book book1 = list.get(i);
for (Book book : list) {
if (name2.equals(book1.getName())) {
System.out.println("要还的数量");
int s2 = jp.nextInt();
book.setNum(book.getNum() + s2);
flag = true;
break;
}
}
}
if (flag == false) {
System.out.println("你是还的哪里的书?????");
}
chaxun();
}
public void shuprice(){
System.out.println("统计所有书的总金额");
//当集合的泛型是实体类时,只计算类中某一属性的值相加之和
Double sum = list.stream().mapToDouble(Book::getPrice).sum();
System.out.println(sum);
}
public void delete() {
System.out.println("请输入要删除的书籍");
//输入要删除的书籍
String book = jp.next();
for (int i = 0; i < list.size(); i++) {
Book book2 = list.get(i);
if ((book2.getName().equals(book))) {
list.remove(i);
}
}
chaxun();
}
public static void main(String[] args) {
Books shu = new Books();
shu.jinshu();
shu.jieshu();
shu.chaxun();
shu.huanshu();
shu.shuprice();
shu.delete();
}
}
//学生类
public class Book {
private String name ;
private double price ;
private int num ;
public Book() {
}
public Book(String name, double price, int num) {
this.name = name;
this.price = price;
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}
//Map
import java.util.*;
public class Books {
//定义保存书籍的map
Map map = new HashMap();
Scanner jp = new Scanner(System.in);
//进书、(初始化)
public void jinshu() {
Book s1 = new Book("西游记", 36.0, 10);
Book s2 = new Book("红楼梦", 49.0, 20);
Book s3 = new Book("三国演义",89.0, 30);
//放入集合
map.put("b1",s1);
map.put("b2", s2);
map.put("b3", s3);
}
public void jieshu() {
System.out.println("输入要借的书");
//输入要借的书名
String name = jp.next();
//查看map中的value是否有书名
Set keys = map.keySet();
boolean jg = false ;
for (String key : keys) {
//根据key获取value
Book book = map.get(key);
if (name.equals(book.getName())) {
System.out.println("要借的数量");
int s1 = jp.nextInt();
//将原来的数量取出来减去要借的数量,再重新赋值
book.setNum(book.getNum()-s1);
jg = true;
break;
}
}
if (jg==false) {
System.out.println("书籍名称不存在");
}
}
public void chaxun() {
Set keys = map.keySet();
for (String key : keys) {
Book book = map.get(key);
System.out.println(key +" "+book.getName()+" "+book.getNum());
}
}
public void update () {
System.out.println("输入书籍条形码");
//输入要借的书籍
String no = jp.next();
if (map.containsKey(no)) {
//根据key获取书籍
Book bo = map.get(no);
System.out.println("请输入要修改的书籍名称");
String name = jp.next();
bo.setName(name);
}else {
System.out.println("输入的条形码不存在");
}
chaxun();
}
public void delete() {
System.out.println("请输入要删除的条形码");
//输入要删除的书籍
String book = jp.next();
if(map.containsKey(book)) {
map.remove(book);
}
chaxun();
}
public void returnshu() {
}
public static void main(String[] args) {
Books b1 = new Books();
b1.jinshu();
b1.jieshu();
b1.chaxun();
b1.update();
b1.delete();
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)