J3-Aquacool / CapgTestingBatchJuly-2022

0 stars 0 forks source link

2 assignement code #1

Open ShivamCapg-75 opened 2 years ago

ShivamCapg-75 commented 2 years ago

@J3-Aquacool

  1. Create and Custom Exception Called InsufficientFundsException,this exception should be raise when the user is trying to withdraw the amount which is greater then the balance amount.

2.Create a Collection of the type ArrayList and store Vehicle object.

Vehicle object has the following attributes

vname brand price

show us the sorting of Vehicle by name,price and brand


//import java.util.; //import java.io.; //import java.math.; //import java.util.regex.; //import java.text.*;

class UserDefinedException { public static void main(String args[]) { System.out.println("Shivam HDFC Bank Account\n" ); Account acct = new Account(); System.out.println("Current balance : " + acct.balance()); System.out.println("Withdrawing Amount 200"); acct.withdraw(200); System.out.println("Current balance : " + acct.balance()); acct.withdraw(1000); } } class Account { private int balance = 1000; public int balance() { return balance; } public void withdraw(int amount) throws NotSufficientFundException { if (amount > balance) { throw new NotSufficientFundException(String.format("Current balance %d is less than requested amount %d", balance, amount)); } balance = balance - amount; } public void deposit(int amount) { if (amount <= 0) { throw new IllegalArgumentException( String.format("Invalid deposit amount %s", amount)); } } } class NotSufficientFundException extends RuntimeException { private String message; public NotSufficientFundException(String message) { this.message = message; } public NotSufficientFundException(Throwable cause, String message) { super(cause); this.message = message; } public String getMessage() { return message; } }



// Java Program to Sort an ArrayList

import java.util.*;

class Car { String Vname; String Brand; int Price;

Car(String Vname, String Brand, int Price)
{
    this.Vname = Vname;
    this.Brand = Brand;
    this.Price = Price;
}

}

class PriceComparator implements Comparator {

public int compare(Car c1, Car c2)
{
    if (c1.Price == c2.Price)
        return 0;
    else if (c1.Price > c2.Price)
        return 1;
    else
        return -1;
}

}

class GFG {

public static void main(String[] args)
{
    // Creating the ArrayList object
    ArrayList<Car> c = new ArrayList<Car>();
    c.add(new Car("Car", "Kia", 200000));
    c.add(new Car("Car", "creta", 1000000));
    c.add(new Car("Scooty", "Jupitar", 500000));
    c.add(new Car("Car", "Mercedes", 4500000));
    c.add(new Car("Scooty", "Activa" ,20000));

    Collections.sort(c, new PriceComparator());

    for (Car car : c) {

        // Print the sorted ArrayList
        System.out.println(car.Price + " " + car.Brand + " " + car.Vname);
    }
}

}

ShivamCapg-75 commented 2 years ago

@J3-Aquacool

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

class Info { String Name; String House; String Role; String Status; String Dies;

Info(String Name,String House,String Role, String Status, String Dies) {

this.Name=Name;
this.House=House;
this.Role=Role;
this.Status=Status;
this.Dies=Dies;

} public String getName() { return Name; } public String getHouse() { return House; } public String getRole() { return Role; } public String getStatus() { return Status; } public String getDies() { return Dies; }

public String toString() { return Name+ " "+House+" "+Role+" "+Status+" "+Dies; }

public ArrayList getGryffindor(ArrayList i) {

  ArrayList<String> gryffindor=new ArrayList<>();
    for (Info person: i) {

        if (person.getHouse()=="Gryffindor") {

            gryffindor.add(person.getName());
        }
    }

  return gryffindor;

}

  public ArrayList getAlive(ArrayList<Info> i)
  {
      ArrayList<Info> alive=new ArrayList<>();
        for (Info person: i) {

            if (person.getDies()=="No") 
                alive.add(person);
        }

        alive.sort((o1, o2) -> o1.getHouse().compareTo(o2.getHouse()));
        return alive;
  }

  public ArrayList getFamily(ArrayList<Info> i)
  {

      ArrayList<Info> family=new ArrayList<>();
        for (Info person: i) {

            if (person.getStatus()=="Family") {

                family.add(person);
            }
        }

      return family;
  }

  public ArrayList getFaculty(ArrayList<Info> i)
  {

      ArrayList<Info> faculty=new ArrayList<>();
        for (Info person: i) {

            if (person.getDies()=="Yes"&& person.getRole()=="Faculty") {

                faculty.add(person);
            }
        }
        faculty.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
        return faculty;
  }

  }

public class HarryPotter {

public static void main(String[] args) {

ArrayList<Info> i =new ArrayList<Info>();
ArrayList<Info> result2=new ArrayList<Info>();
ArrayList<Info> result3=new ArrayList<Info>();
ArrayList<String> result1,result4 = new ArrayList<String>();

i.add(new Info("Harry Potter","Gryffindor ","Student","Self","No"));
i.add(new Info("Ginny Weasley","Gryffindor ","Student","Friend ","No"));
i.add(new Info("Ron Weasley","Gryffindor ","Student","Friend","No"));
i.add(new Info("Hermione Granger","Gryffindor ","Student","Friend ","No"));        
i.add(new Info("Neville Longbottom","Gryffindor ","Student","Friend","No"));
i.add(new Info("Oliver Wood","Gryffindor ","Student","Friend","No"));
i.add(new Info("Luna Lovegood","Ravenclaw","Student","Friend","No"));
i.add(new Info("Cho Chang","Ravenclaw","Student","Friend","No"));
i.add(new Info("Cedric Diggory","Hufflepuff","Student","Friend","Yes"));
i.add(new Info("Hannah Abbot","Hufflepuff","Student","Friend","No"));
i.add(new Info("Draco Malfoy","Slytherin","Student","Enemy","No"));
i.add(new Info("Vincent Crabbe","Slytherin","Student","Enemy","Yes"));
i.add(new Info("Gregory Goyle","Slytherin","Student","Enemy","No"));
i.add(new Info("Penelope Clearwater","Slytherin","Student","Enemy","No"));
i.add(new Info("Albus Dumbledore","Gryffindor ","Faculty","Friend","Yes"));
i.add(new Info("Severus Snape","Slytherin ","Faculty","Enemy","Yes"));
i.add(new Info("Remus Lupin","Gryffindor  ","Faculty","Friend","Yes"));
i.add(new Info("Horace Slughorn","Slytherin  ","Faculty","Friend","No"));
i.add(new Info("Rubeus Hagrid","Gryffindor   ","Faculty","Friend","No"));
i.add(new Info("Minerva McGonagall","Gryffindor","Faculty","Friend","No"));
i.add(new Info("James Potter","Gryffindor","Student","Family","Yes"));
i.add(new Info("Sirius Black","Gryffindor","Student","Family","Yes"));
i.add(new Info("Lily Potter","Gryffindor","Student","Family","Yes"));
i.add(new Info("Peter Pettigrew","Gryffindor","Student","Enemy","Yes"));
i.add(new Info("Tom Marvolo Riddle","Slytherin","Student","Enemy","Yes"));

Info obj=new Info(null, null, null, null, null);
result1=obj.getGryffindor(i);
System.out.println("People who are from gryffindor are:  " +result1);
result2=obj.getAlive(i);
System.out.println("\nPeople who are alive are : " +result2);
result3=obj.getFamily(i);
System.out.println("\nFamily members of Harry Potter are : " +result3);
result4=obj.getFaculty(i);
System.out.println("\nFaculty who died are : " +result4);

} }