1编写一个Book类,该类至少有name和price两个属性。该类要实现Comparable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。在主函数中,选择合适的集合类型存放Book类的若干个对象,然后创建一个新的Book类的对象,并检查该对象与集合中的哪些对象相等
package org.gerrysu;
import java.util.*;
public class Book implements Comparable{
String name;
int price;
public Book(String name,int price){
this.name = name;
this.price = price;
//System.out.println(name.toString());
}
public int compareTo(Object obj) {
Book bk = (Book)obj;
if(bk.price==this.price){
return 1;
}
return 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Book> list = new ArrayList<Book>();
Book bk1 = new Book("java基础教程",29);
Book bk2 = new Book("数据库技术",29);
Book bk3 = new Book("C++基础教程",29);
Book bk4 = new Book("微机原理",39);
Book bk5 = new Book("操作系统",39);
Book bk6 = new Book("高数",59);
Book bk7 = new Book("数据结构",69);
list.add(bk1);
list.add(bk2);
list.add(bk3);
list.add(bk4);
list.add(bk5);
list.add(bk6);
list.add(bk7);
//Iterator<Book> iter = list.iterator();
Collections.sort(list);
System.out.println("输入:");
Scanner bookin = new Scanner(System.in);
String inname = bookin.nextLine();
int np = bookin.nextInt();
Book bkn = new Book(inname,np);
System.out.println("新书"+bkn.name+"与下列图书:");
Iterator<Book> it = list.iterator();
while(it.hasNext()){
Book b= it.next();
if(b.compareTo(bkn)==1)
System.out.println(b.name);
}
System.out.println("价格相同,具体价格为:"+bk1.price+"元");
}
}
package org.gerrysu2;
public class Student implements Comparable{
String name;
int grade;
Student(String name,int grade){
this.name = name;
this.grade = grade;
}
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
if (this.grade > ((Student) arg0).grade)
return -1 ;
else if(this.grade == ((Student) arg0).grade)
return 0;
else return 1;
}
}
package org.gerrysu2;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class ApplicationTest extends JFrame{
ArrayList list = new ArrayList<>();
ApplicationTest(String s){
init(s);
setSize(300, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(String s){
setTitle(s);
JPanel paneln = new JPanel();
paneln.setBorder(new EmptyBorder(5, 5, 5, 5));
this.setContentPane(paneln);
paneln.setLayout(new GridLayout(5, 1, 5, 5));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
TextField txt1 = new TextField();
txt1.setColumns(10);
TextField txt2 = new TextField();
txt2.setColumns(10);
JLabel j1 = new JLabel("姓名");
JLabel j2 = new JLabel("成绩");
JButton btn1 = new JButton("递交");
JButton btn2 = new JButton("排序");
JTextArea t = new JTextArea(10, 10);
JScrollPane scrollPane = new JScrollPane(t);
panel1.add(j1);
panel1.add(txt1);
panel2.add(j2);
panel2.add(txt2);
panel3.add(btn1);
panel3.add(btn2);
paneln.add(panel1);
paneln.add(panel2);
paneln.add(panel3);
paneln.add(scrollPane);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String name = txt1.getText();
int chengji = Integer.parseInt(txt2.getText());
Student stu = new Student(name,chengji);
if(arg0.getSource()==btn1){
t.append(txt1.getText() + ":" + txt2.getText()+"\n" );
list.add(stu);
}
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Collections.sort(list);
if(arg0.getSource()==btn2){
Iterator<Student> it = list.iterator();
while(it.hasNext()){
Student s = it.next();
t.append(s.name + ": " + s.grade + "\n");
}
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationTest apt = new ApplicationTest("学生成绩");
}
}
排序前:
排序后: