I am working on an assignement where I have created two clas

I am working on an assignement where I have created two classes(Book and Car). I also created a custom interface(IMessage). Book\'s parameters are Author, Title, and Price. Car\'s parameters are Make and Model. I made a List that held all objects type book and and a list that holds all objects of type IMessage. The list that holds the objects of type IMessage includes both Books and Cars. I need to right a for loop that goes through the interfaceList and if the item is a Book I need to add the Author and Title only to a listbox and if the item is a Car I ned to add the make and model to a list box.

So far I have:

foreach (IMessage c in interfaceList)

{

   if (c is Book)

   {

   }

   else if(c is Car)

   {

  

   }

}

I don\'t know how to select the items I need from each object in the list. Also if I do listbox.items.add(c) it gives me the classes of the item\'s not the item themselves.

Thanks in advance!

Solution

ANSWER:

public interface IMessage { }

public class Car implements IMessage {

public String model;
   public String make;

   public String getModel() {
       return model;
   }

   public void setModel(String model) {
       this.model = model;
   }

   public String getMake() {
       return make;
   }

   public void setMake(String make) {
       this.make = make;
   }

   @Override
   public String toString() {
       return \"Car [model=\" + model + \", make=\" + make + \"]\";
   }

}

public class Book implements IMessage {

   public String aurthor;
   public String title;
   public String price;

   public Book() {
       price = \"200\";
   }
  
   public String getAurthor() {
       return aurthor;
   }

   public void setAurthor(String aurthor) {
       this.aurthor = aurthor;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getPrice() {
       return price;
   }

   public void setPrice(String price) {
       this.price = price;
   }

   @Override
   public String toString() {
       return \"Book [aurthor=\" + aurthor + \", title=\" + title + \", price=\"
               + price + \"]\";
   }

}

// NewComparator for comparing two objects

import java.util.Comparator;

public class NewComparator implements Comparator<Object> {

// Here sorting Book by Aurthor and Car by Model

@Override
   public int compare(Object list, Object newOne) {

       if (list instanceof Book && newOne instanceof Book) {
          
           return ((Book) list).getAurthor().compareTo(((Book) newOne).getAurthor());
          
       } else if (list instanceof Car && newOne instanceof Car) {
          
           return ((Car) list).getModel().compareTo(((Car) newOne).getModel());
          
       } else if (list instanceof Book && newOne instanceof Car){
          
           return ((Book) list).getAurthor().compareTo(((Car) newOne).getModel());
          
       }
       else if (list instanceof Car && newOne instanceof Book) {
           return ((Car) list).getModel().compareTo(((Book) newOne).getAurthor());
       }
       else {
           System.out.println(\"__This will never execute___\");
           return 0;
       }

   }

}

// Main Class to Run the Program

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Main {

   public static List<IMessage> list = new ArrayList<IMessage>();
   public static void main(String[] args) {
      
       List<IMessage> list = new CopyOnWriteArrayList<IMessage>();
       Book book = null;
       Car car = null;
      
       book = new Book();
       book.setAurthor(\"Balaguruswamy\");
       book.setTitle(\"C Language\");
       list.add(book);
       book = new Book();
       book.setAurthor(\"Forugend\");
       book.setTitle(\"Java\");
       list.add(book);
      
      
       car = new Car();
       car.setModel(\"New One\");
       car.setMake(\"Ford\");
       list.add(car);
       car = new Car();
       car.setModel(\"Old One\");
       car.setMake(\"Tata\");
       list.add(car);
      
       System.out.println(\"Before sorting\");
       for(IMessage mes : list) {
           System.out.println(mes);
       }
       System.out.println(\"After Sorting\");
       Collections.sort(list, new NewComparator());
      
       for(IMessage mes : list) {
           System.out.println(mes);
       }
   }

}

Its working fine.

Note: sorting Book by Aurthor and Car by Model

Let me know any concerns. Thank You

I am working on an assignement where I have created two classes(Book and Car). I also created a custom interface(IMessage). Book\'s parameters are Author, Title
I am working on an assignement where I have created two classes(Book and Car). I also created a custom interface(IMessage). Book\'s parameters are Author, Title
I am working on an assignement where I have created two classes(Book and Car). I also created a custom interface(IMessage). Book\'s parameters are Author, Title
I am working on an assignement where I have created two classes(Book and Car). I also created a custom interface(IMessage). Book\'s parameters are Author, Title

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site