public class Function {
Scanner sc = new Scanner(System.in);
CarList<Car> carList = new CarList<>();
public int isExist(String name){
for(int i = 0; i<carList.getA().size();i++)
if (carList.getA().get(i).getName().equalsIgnoreCase(name)) return 1;
return -1;
}
public void add(){
String name;
System.out.println("Input Name :");
name = sc.next();
if(isExist(name) != -1){
System.err.println("This car existed in the list ");
} else {
System.out.println("Input price :");
double price = sc.nextDouble();
System.out.println("Input production : ");
int production = sc.nextInt();
carList.getA().add(new Car(name,price,production));
System.err.println("Add new car succesful !"); }
}
public void display(){
if(carList.getA().isEmpty())
System.out.println("List empty !!");
else {
System.err.println("List of cars : ");
for (int i = 0; i < carList.getA().size(); i++)
System.out.println(i+1+"."+carList.getA().get(i));
}
}
public void numberOfItems(){
System.out.println("Number of items : "+carList.getA().size());
}
public void delete(){
int position = 0;
if(carList.getA().isEmpty()){
System.out.println("List is empty !!");
} else {
display();
System.out.println("Enter position to delete ");
position = sc.nextInt();
int amount = carList.getA().size();
if(position>=amount || position < 0) {
System.err.println("There is no car to remove.Enter again : ");
delete();
} else {carList.getA().remove(position-1);
System.err.println("Remove successful !"); }
}
}
public void checkEmpty(){
if (carList.getA().isEmpty()) {
System.out.println("List is Empty !!");
} else {
System.out.println("List isn't Empty !!");
}
}
public void sort(){
Collections.sort(carList.getA(),Comparator.comparing(Car::getName));
Collections.reverse(carList.getA());
System.out.println("Sorting....success !!");
}
public int showMenu(){
System.err.println("__________MENU CAR__________");
System.out.println("1.Add");
System.out.println("2.Display");
System.out.println("3.Get size");
System.out.println("4.Check empty");
System.out.println("5.Delete");
System.out.println("6.Sort");
System.out.println("7.Search");
System.out.println("8.Exit");
System.out.println("Choose your option : ");
int option = sc.nextInt();
return option;
}
public void search(){
System.out.println("Enter name :");
String name1 = sc.nextLine();
int index = Collections.binarySearch(carList.getA(),name1,Collections.reverseOrder());
System.out.println(index);
}
}
public class CarList<T>{
private T obj;
private LinkedList<T> a = (LinkedList<T>) new LinkedList<T>();
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
public LinkedList<T> getA() {
return a;
}
public void setA(LinkedList<T> a) {
this.a = a;
}
}
hí các bác lúc em chạy cái hàm search() thì nó bị lỗi ạ. mong các bác fix giùm em với ạ
Exception in thread “main” java.lang.ClassCastException: class ws10.Car cannot be cast to class java.lang.Comparable (ws10.Car is in unnamed module of loader ‘app’; java.lang.Comparable is in module java.base of loader ‘bootstrap’)
at java.base/java.util.Collections$ReverseComparator.compare(Collections.java:5286)
at java.base/java.util.Collections.indexedBinarySearch(Collections.java:336)
at java.base/java.util.Collections.binarySearch(Collections.java:324)
at ws10.Function.search(Function.java:95)
at ws10.Main.main(Main.java:35)
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?