import java.io.*; import java.util.*; import java.text.*; import java.lang.*; // Class n00845431 public class Project4 static class EmailComparator implements Comparator <Vehicle> public int compare(vehicle one, Vehicle two) Vehicle p1 =(Vehicle) one; Vehicle p2 =(Vehicle) two; return (p1.getemail().compareto(p2.getemail())); public static void main(string[] args) throws FileNotFoundException //creating arraylist for vechicle class File file = new File(args[0]); System.out.println(file.exists()); Scanner input = new Scanner(file); ArrayList<Vehicle> arrayofvehicles = new ArrayList<Vehicle>(); while(input.hasnextline()) String test = input.nextline(); ///if type vehicle if (test.equals("vehicle")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); // create vehicle object arrayofvehicles.add(new Vehicle(name, address, phone, email)); else if (test.equals("car")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); boolean convertible = input.nextboolean(); input.nextline(); String color = input.nextline(); // create car object arrayofvehicles.add(new Car(name, address, phone, email, convertible, color));
else if (test.equals("american car")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); boolean convertible = input.nextboolean(); input.nextline(); String color = input.nextline(); boolean detroit = input.nextboolean(); boolean union = input.nextboolean(); // create american car object arrayofvehicles.add(new AmericanCar(name, address, phone, email, convertible, color, detroit, union)); else if (test.equals("foreign car")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); boolean convertible = input.nextboolean(); input.nextline(); String color = input.nextline(); String manufacturer = input.nextline(); //boolean detroit = input.nextboolean(); //boolean union = input.nextboolean(); float importduty = input.nextfloat(); input.nextline(); input.nextline(); // create foreign car object arrayofvehicles.add(new ForeignCar(name, address, phone, email, convertible, color, manufacturer, importduty)); else if (test.equals("bicycle")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); int speed = input.nextint(); // create bicycle object arrayofvehicles.add(new Bicycle(name, address, phone, email, speed)); else if (test.equals("truck")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); float tons = input.nextfloat(); float cost = input.nextfloat(); input.nextline(); String date = input.nextline(); arrayofvehicles.add(new Truck(name, address, phone, email, tons, cost, date));
input.close(); // Calling Methods System.out.println("**********Printing Details**********"); // Calls printall System.out.println("The Array List"); printall(arrayofvehicles); // Calls Email Id System.out.println("Sorting by Email ID"); sort(arrayofvehicles); // Calls numberofentries printrecord(arrayofvehicles); // Calls justbt System.out.println("Just Bicycles and Trucks"); justbt(arrayofvehicles); // Calls areacode System.out.println("Area Code 987"); areacode(arrayofvehicles); //main // Methods //Prints the ArrayList public static void printall(arraylist<vehicle> array) for (int x = 0; x <= array.size() -1; x++) System.out.println(array.get(x).toString());
//Sorts email id in arraylist public static void sort(arraylist<vehicle> array) Collections.sort(array, new EmailComparator()); for (int i = 0; i <= array.size() -1; i++) System.out.println(array.get(i).toString()); //Prints number of entries public static void printrecord(arraylist<vehicle> array) System.out.println("The number of records: " + array.size()); //Prints number of entries public static void justbt(arraylist<vehicle> array) for (int x = 0; x <= array.size() -1; x++) if (array.get(x) instanceof Bicycle) System.out.println(array.get(x).toString()); else if (array.get(x) instanceof Truck) System.out.println(array.get(x).toString()); //Prints number of entries public static void areacode(arraylist<vehicle> array) String number; for (int x = 0; x <= array.size() -1; x++) number = array.get(x).getphone(); if (number.substring(1,4).equals("987")) System.out.println(array.get(x).toString()); //class // Vehicle as parent class class Vehicle //data members declared as private private String ownername; private String address; private String phone; private String email;
//null public Vehicle () //parameterized constructor of vechicle class public Vehicle(String ownername, String address, String phone, String email) this.ownername = ownername; this.address= address; this.phone = phone; this.email = email; //getters and setters for data members public String getownername() return ownername; public void setownername(string newownername) this.ownername = newownername; public String getaddress() return address; public void setaddress(string newaddress) this.address = newaddress; public String getphone() return phone; public void setphone(string newphone) this.phone = newphone; public String getemail() return email; public void setemail(string newemail) this.email = newemail; public boolean equals(object obj) return true; //tostring(0 returns String type contains data member values of a vehicle class
public String tostring() return "Type of Vehicle: Vehicle\n" + "Owner's name : " + ownername + "\n" + "Address : " + address + "\n" + "Phone : " + phone + "\n" + "Email : " + email; //Car is child class inherits from vechile class Car extends Vehicle //datamembers intilation as private private boolean convertible; private String color; //parameterized constructor of Car class public Car(String ownername, String address, String phone, String email, boolean convertible, String color) //calling Vechicle class constructor using super super(ownername, address, phone, email); setconvertible(convertible); setcolor(color); //getters and setters for convertible and getcolor data members of car public boolean getconvertible() return convertible; public void setconvertible(boolean newconvertible) this.convertible = newconvertible; public String getcolor() return color; public void setcolor(string newcolor) this.color = newcolor; //tostring() method returns String type And over rides parent class is Vehicle public String tostring() return super.tostring() + "Type of Vehicle: Car\n" + "Convertible : " + convertible + "\n" +
"Color : " + color; //end car class /*AmericanCar inherits Car and Car inherits vechile.i.e multilevel inhertience between vechile and AmericanCar*/ // Subclass of Car class AmericanCar extends Car //Adding two data members private boolean madedetroit; private boolean unionshop; //parameterized constructor of American car public AmericanCar(String ownername, String address, String phone, String email, boolean convertible, String color, boolean madedetroit, boolean unionshop) //calling car class parameterized constructor super(ownername, address, phone, email, convertible, color); setmadedetroit(madedetroit); setunionshop(unionshop); //getter and setters for madedetroit, unionshop data members public boolean getmadedetroit() return madedetroit; public void setmadedetroit(boolean newmadedetroit) this.madedetroit = newmadedetroit; public boolean getunionshop() return unionshop; public void setunionshop(boolean newunionshop)
this.unionshop = newunionshop; //overriding super class tostring() and returns adding with newly added variables public String tostring() return super.tostring() + "Type of Vehicle: American Car\n" + "Made in Detroit : " + madedetroit + "\n" + "Union shop : " + unionshop; //child class ForeignCar inherits from Car parent class // Subclass of Car class ForeignCar extends Car // ForeignCar data members intilization private String manufacturercountry; private float importduty; //parameterized constructor for ForeignCar public ForeignCar(String ownername, String address, String phone, String email, boolean convertible, String color, String manufacturercountry, float importduty) super(ownername, address, phone, email, convertible, color); this.manufacturercountry = manufacturercountry; this.importduty = importduty; //getters and setters for manufacturercountry, importduty public String getmanufacturercountry() return manufacturercountry;
public void setmanufacturercountry(string newmanufacturercountry) this.manufacturercountry = newmanufacturercountry; public float getimportduty() return importduty; public void setimportduty(float newimportduty) this.importduty = newimportduty; //tostring() overrides parent class car public String tostring() return super.tostring() + "Type of Vehicle: Foreign Car\n" + "Manufacturer country : " + manufacturercountry + "\n" + "Import duty : " + importduty; //child class Bicycle inherits from vehicle parent class //Bicycle is child class inherits from vechile class Bicycle extends Vehicle //initialize data member as private private int numberspeeds; //parameter constructor of Bicycle public Bicycle(String ownername, String address, String phone, String email, int numberspeeds) super(ownername, address, phone, email); this.numberspeeds = numberspeeds; //getter and setter of numberspeeds data member
public int getnumberspeeds() return numberspeeds; public void setnumberspeeds(int newnumberspeeds) this.numberspeeds = newnumberspeeds; //overriding tostring method() return String type public String tostring() return super.tostring() + "Type of Vehicle: Bicycle\n" + "Number of speeds : " + numberspeeds; //child class Truck inherits parent class Vechicle //Truck is child class inherits from vechile class Truck extends Vehicle private float numbertons; private float truckcost; private String datepurchased; //parameter constructor of Truck public Truck(String ownername, String address, String phone, String email, float numbertons, float truckcost, String datepurchased) super(ownername, address, phone, email); this.numbertons = numbertons; this.truckcost = truckcost; this.datepurchased = datepurchased; //getters and setter for Truck data members
public float getnumbertons() return numbertons; public void setnumbertons(float newnumbertons) this.numbertons = newnumbertons; public float gettruckcost() return truckcost; public void settruckcost(float newtruckcost) this.truckcost = newtruckcost; public String getdatepurchased() return datepurchased; public void setdatepurchased(string newdatepurchased) this.datepurchased = newdatepurchased; //tostring() override superclass method public String tostring() return super.tostring() + "Type of Vehicle: Truck\n" + "Number of tons : " + numbertons + "\n" + "Truck cost : " + truckcost + "\n" + "Date purchased : " + datepurchased;
/* sets, null cnstuctors, email comparator, type of vehicles, Boolean after each class... */