Set集合小应用(图书购买系统)

用户类:

public class User {

private String id;
private String pwd;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User(String id, String pwd) {
super();
this.id = id;
this.pwd = pwd;
}
public User() {
super();
}


}


书类;

package Problem_6;
 //编号 书名 价格 出版商
public class Book {
private String num;
private String name;
private double price;
private String publishers;
public String getNum() {
return num;
}
public void setNum(String num) {
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 String getPublishers() {
return publishers;
}
public void setPublishers(String publishers) {
this.publishers = publishers;
}
public Book(String num, String name, double price, String publishers) {
super();
this.num = num;
this.name = name;
this.price = price;
this.publishers = publishers;
}
public Book() {
super();
}
@Override
public String toString() {
return "编号:"+num+"\t"+",书名:" + name +"\t"+ ", 价格:" + price + "\t"+", 出版商:" + publishers + "";
}

}


测试类+功能实现类:

package Problem_6;


import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeSet;


/*1:Java从入门到精通,69.8,明日科技
2:鬼吹灯,32,天下霸唱
3:盗墓笔记,32,南派三叔
4:java编程思想,89.9,Bruce Eckel
5:java核心技术,94,凯 S. 霍斯特曼*/
public class Test implements Comparator<Book> {


public static void main(String[] args) {
// TODO Auto-generated method stub

TreeSet<Book> ts=new TreeSet<>(new Test());
ts.add(new Book("1","Java从入门到精通",69.8,"明日科技"));
ts.add(new Book("2","鬼吹灯",32.0,"天下霸唱"));
ts.add(new Book("3","盗墓笔记",32.0,"南派三叔"));
ts.add(new Book("4","java编程思想",89.9,"Bruce Eckel"));
ts.add(new Book("5","java核心技术",94.0,"凯 S. 霍斯特曼"));

login(ts);

}

//登录验证
public static void login(TreeSet<Book> ts){
User user=new User("admin","123");
System.out.println("请输入用户名");
Scanner sc=new Scanner(System.in);
String name=sc.next();
System.out.println("请输入密码");
String pwd=sc.next();
if(name.equals(user.getId())&&pwd.equals(user.getPwd())){
view_book(ts);
buyBook(ts);
}else{
System.out.println("用户名或密码错误,请重新输入:");
login(ts);
}
}


@Override
public int compare(Book o1, Book o2) {
if(o1.getName().length()!=o2.getName().length()){
return o1.getName().length()-o2.getName().length();
}else{
if(o1.getPrice()!=o2.getPrice()){
return (int) (o1.getPrice()-o2.getPrice());
}
}
return 0;
}

//显示图书
public static void view_book(TreeSet<Book> ts){
Iterator<Book> it=ts.iterator();
while(it.hasNext()){
System.out.println(it.next().toString());
}
}


//购买图书
public static void buyBook(TreeSet<Book> ts){
double money = 0.0;
System.out.println("请输入您要购买的图书的编号,有多本的时候用(,)隔开:");
Scanner sc=new Scanner(System.in);
String books=sc.next();
String[] b=books.split(",");
String str=" ";
Iterator<Book> it=ts.iterator();
while(it.hasNext()){
Book book=it.next();
for(int i=0;i<b.length;i++){
if(b[i].equals(book.getNum())){
money=money+book.getPrice();
str=str+book.getName()+",";

}
}
}

System.out.println("你购买的书为:"+str+"\t"+"总价为:"+money+"\t"+"打折后的价格为:"+getRebate(money));
}


//计算折扣
public static double getRebate(double money){
double price=0.0;
if(money>=100.0){
price=money*(0.7);
}else if(money<100&&money>=50){
price=money-10.0;
}
return price;
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值