Java All Assignment
Java All Assignment
import java.util.*;
class Menu{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
double bill=0;
boolean exit=false;
int qty=0;
System.out.println("Welcome to cafe");
while(!exit){
System.out.println("1.Dosa:50");
System.out.println("2.Idli:30");
System.out.println("3.Samosa:20");
System.out.println("4.Kachori:20");
System.out.println("5.Dhokla:25");
System.out.println("0.Total bill");
switch(sc.nextInt()){
case 1:
System.out.println("enter the quantity");
qty=sc.nextInt();
bill+=qty*50;
break;
case 2:
System.out.println("enter the quantity");
qty=sc.nextInt();
bill+=qty*30;
break;
case 3:
System.out.println("enter the quantity");
qty=sc.nextInt();
bill+=qty*20;
break;
case 4:
System.out.println("enter the quantity");
qty=sc.nextInt();
bill+=qty*20;
break;
case 5:
System.out.println("enter the quantity");
qty=sc.nextInt();
bill+=qty*25;
break;
case 0:
System.out.println("Your total bill = "+bill);
exit=true;
break;
default:
System.out.println("Invalid choice!");
exit=true;
}
}
sc.close();
}
}
Calculator
package day1_Assignment;
import java.util.*;
class Calculator{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
boolean exit=false;
System.out.println("Welcome to Calculator");
System.out.println("enter two number");
int n1=sc.nextInt();
int n2=sc.nextInt();
while(!exit){
System.out.println("1.Add");
System.out.println("2.Subtract");
System.out.println("3.Multiply");
System.out.println("4.Divide");
System.out.println("5.Exit");
switch(sc.nextInt()){
case 1:
System.out.println("Addition : "+(n1+n2));
break;
case 2:
System.out.println("Subtraction : "+(n1-n2));
break;
case 3:
System.out.println("Multiplication : "+(n1*n2));
break;
case 4:
System.out.println("Division : "+(n1/n2));
break;
case 5:
System.out.println("Exiting...........");
exit=true;
break;
default:
System.out.println("Invalid choice!");
exit=true;
}
}
sc.close();
}
}
Day 2:
Class
package day2_assignment;
class Point2D{
private int x;
private int y;
Point2D(int x,int y){
this.x=x;
this.y=y;
}
String show(){
return "cordinates : x = "+x+" , y = "+y;
}
boolean isEqual(Point2D p1){
if(this.x==p1.x && this.y==p1.y) return true;
else return false;
}
double calculate(Point2D p){
return Math.sqrt(Math.pow(((p.x)-(this.x)),2)+Math.pow(((p.y)-(this.y)),2));
}
Tester :
package day2_assignment;
import java.util.Scanner;
System.out.println("p1 : "+p1.show());
System.out.println("p2 : "+p2.show());
if(p1.isEqual(p2)){
System.out.println("cordinates of p1 and p2 is same");
}
else{
System.out.println("cordinates of p1 and p2 is different");
System.out.println("Distance between two point are : "+p1.calculate(p2));
}
sc.close();
}
}
Day 3:
Java Class:
package com.cdac.core;
private int x;
private int y;
Tester :
package com.cdac.tester;
import java.util.Scanner;
import com.cdac.core.Point2D;
if (p1.isEqual(p2)) {
System.out.println("cordinates of p1 and p2 is same");
} else {
System.out.println("cordinates of p1 and p2 is different");
System.out.println("Distance between two point are : " + p1.calculate(p2));
}
System.out.println("enter the offset for new Points");
Point2D newpoint=p1.addoffset(sc.nextInt(), sc.nextInt());
System.out.println("New Points : " + newpoint.show());
sc.close();
}
Test Menu
package com.cdac.tester;
import java.util.Scanner;
import com.cdac.core.Point2D;
}
else {
System.out.println("Invalid index!!!");
}
break;
case 2:
System.out.println("Points Details!!");
for(Point2D a:p) {
System.out.println(a.show());
}
break;
case 0:
System.out.println("Exiting......");
break;
default:
System.out.println("Invalid Choice!!!!");
}
}while(choice!=0);
}
}
Day 4:
Fruit class
package com.app.fruits;
}
@Override
public String toString() {
return "Fruit :----"+" color: "+color+ " ,weight: "+weight+" ,name:
"+name+" ,isFresh :"+isFresh;
}
public abstract String taste();
Apple :
package com.app.fruits;
Orange class:
package com.app.fruits;
Mango class:
package com.app.fruits;
Alphonso class:
package com.app.fruits;
Tester:
package com.app.tester;
import java.util.Scanner;
import com.app.fruits.*;
public class FruitBasket {
if(index<basket.length) {
System.out.println(" enter the details of Alphanso - color
weight name ");
basket[index]=new
Alphonso(sc.next(),sc.nextDouble(),sc.next());
index++;
}
else System.out.println("Basket is Full ! ");
break;
case 2:
if(index<basket.length) {
System.out.println(" enter the details of Orange - color
weight name " );
basket[index]=new
Orange(sc.next(),sc.nextDouble(),sc.next());
index++;
}
else System.out.println("Basket is Full ! ");
break;
case 3:
if(index<basket.length) {
System.out.println(" enter the details of Apple - color
weight name");
basket[index]=new
Apple(sc.next(),sc.nextDouble(),sc.next());
index++;
}
else System.out.println("Basket is Full ! ");
break;
case 4:
System.out.println("Baskte Fruits:");
for(Fruit f:basket) {
if(f!=null) {
System.out.println(f.getName());
flag=true;
}
}
if(!flag) System.out.println("Basket is empty!!!!");
break;
case 5:
System.out.println("All details of fruits");
for(Fruit f:basket) {
if(f!=null) {
System.out.println(f);
System.out.println("taste of fruit : "+f.taste());
flag=true;
}
}
if(!flag) System.out.println("Basket is empty!!!!");
break;
case 6:
System.out.println("Enter the basket index");
int i=sc.nextInt()-1;
for(Fruit f:basket) {
if(f!=null) {
if("sour".equals(f.taste())){
f.setStale();
System.out.println("operration succesfull!!!!");
}
}
else {
System.out.println("Basket is empty!!!!");
}
}
break;
case 0:
System.out.println("Exiting.....................");
exit=true;
}
}
sc.close();
}
}
Day 6:
BankAccount class
package com.app.bank;
@Override
public String toString() {
return "BankAccount [accountNo=" + accountNo + ", fisrtName=" + fisrtName + ",
lastName=" + lastName
+ ", balance=" + balance + ", accType=" + accType + "]";
}
BankingOperation:
package com.app.bank;
Implementation :
package com.app.bank;
@Override
public BankAccount addAccount(String fisrtName, String lastName, double balance,
String accType) {
@Override
public void diplayAcc(BankAccount b) {
System.out.println(b);
@Override
public void depositFund(BankAccount b, double balance) {
b.setBalance(balance);
Tester :
package com.app.test;
import java.util.Scanner;
import com.app.bank.BankAccount;
import com.app.bank.BankingOperationImpl;
case 3:
System.out.println("Enter the account number and fund");
int aNo=scanner.nextInt();
double bal=scanner.nextDouble();
for(BankAccount b: accounts) {
if(b.getAccount()==aNo) {
b1.depositFund(b, bal);
}
}
break;
case 0:
System.out.println("Exiting..........");
exit=true;
break;
}
}
scanner.close();
Day 7:
Bank:
package com.bank;
this.accountNo = count;
this.fisrtName = fisrtName;
this.lastName = lastName;
this.balance = balance;
this.accType = accType;
}
@Override
public String toString() {
return "BankAccount [accountNo=" + accountNo + ", fisrtName=" +
fisrtName + ", lastName=" + lastName
+ ", balance=" + balance + ", accType=" + accType + "]";
}
public void setBalance(double balance) {
this.balance=this.balance+balance;
}
Services :
package com.bank.service;
import com.bank.BankAccount;
Implementation :
package com.bank.service;
import com.bank.BankAccount;
@Override
public String addAccount(String fisrtName, String lastName, double balance, String
accType) {
if(index<accounts.length) {
accounts[index]=new BankAccount(fisrtName, lastName, balance, accType);
return "account is created !!!!";
}
return "Bank have not space";
}
@Override
public void diplayAcc() {
for(BankAccount b:accounts) {
if(b!=null) {
System.out.println(b);
}
}
@Override
public String depositFund(int acNo,double balance) {
for(BankAccount b:accounts) {
if(b!=null) {
if(b.getAccount()==acNo) {
b.setBalance(balance);
return "Operation Succesful!!!!!!";
}
}
}
return "Something Wrong";
}
Handling:
package custom_handling;
}
Validations:
package util;
import custom_handling.BalanceValidationException;
}
}
Tester :
package ui;
import java.util.Scanner;
import com.bank.service.BankOperationImpl;
import custom_handling.BalanceValidationException;
import util.Accountsvalidation;
public class MyBank {
catch (BalanceValidationException e) {
System.out.println(e.getMessage());
}
}
}
Day 8
Bank class:
package com.bank;
import java.time.LocalDate;
import java.util.Objects;
@Override
public String toString() {
return "BankAccount [accountNo=" + accountNo + ", fisrtName=" + firstName + ",
lastName=" + lastName
+ ", balance=" + balance + ", accType=" + accType + ", DOB=" +
dob + "]";
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BankAccount)
return this.accountNo == ((BankAccount) obj).accountNo;
return false;
}
Account type:
package com.bank;
package com.bank.service;
import com.bank.BankAccount;
import custom_handling.BankException;
void displayAcc();
package com.bank.service;
import java.time.LocalDate;
import com.bank.AccountTypes;
import com.bank.BankAccount;
import custom_handling.BankException;
import util.Accountsvalidation;
@Override
public void displayAcc() {
for (BankAccount b : accounts) {
if (b != null) {
System.out.println(b);
}
}
@Override
public void displaySummary(int accNo) throws BankException {
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == accNo) {
System.out.println(b);
}
}
}
throw new BankException("Account not found!!!!");
}
@Override
public String depositFund(int acNo, double balance) {
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == acNo) {
b.setBalance(b.getBalance() + balance);
return "Operation Succesful!!!!!!";
}
}
}
return "Something Wrong";
}
@Override
public String withdrowMoney(int acNo, double balance) throws BankException {
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == acNo) {
if (b.getBalance() < balance)
throw new BankException("Insufficient
balance!!!!!");
else if (b.getBalance() - balance < 5000)
throw new BankException(
"You have to maintain minumum
balance your balance is : " + b.getBalance());
else {
b.setBalance(b.getBalance() - balance);
return "Operation Succesful!!!!!!";
}
}
}
}
return "Something Wrong";
}
@Override
public String fundTransfer(int srcAccNo, int destAccNo, double fund) throws
BankException {
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == srcAccNo) {
if (b.getBalance() < fund)
throw new BankException("Insufficient
balance!!!!!");
else if (b.getBalance() - fund < 5000)
throw new BankException(
"You have to maintain minumum
balance your balance is : " + b.getBalance());
else {
double bal = b.getBalance() - fund;
b.setBalance(bal);
}
}
}
}
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == destAccNo) {
double bal = b.getBalance() + fund;
b.setBalance(bal);
}
}
}
return "Operation Succesful!!!!!!";
}
package com.bank.service;
import java.time.LocalDate;
import java.util.ArrayList;
import com.bank.AccountTypes;
import com.bank.BankAccount;
import custom_handling.BankException;
import util.Accountsvalidation;
public BankOperationImplArrayList() {
@Override
public String addAccount(int accNo, String firstName, String lastName, double balance,
String accType, String dob)
throws BankException {
AccountTypes type = Accountsvalidation.AccountTypeValidation(accType);
if (accounts.contains(acc)) {
throw new BankException("Account number already exist!!!!");
}
@Override
public void displayAcc() {
for (BankAccount b : accounts) {
System.out.println(b);
}
@Override
public String depositFund(int acNo, double balance) throws BankException {
int index = getIndex(acNo);
if (index != -1) {
BankAccount b = accounts.get(index);
b.setBalance(b.getBalance() + balance);
return "Operation Succesful!!!!!!";
}
throw new BankException("Account not found");
@Override
public String withdrowMoney(int acNo, double balance) throws BankException {
int index = getIndex(acNo);
balanceCheck(acNo,balance);
return "Operation Successfull!!!!";
}
@Override
public void displaySummary(int accNo) throws BankException {
int index = getIndex(accNo);
if (index == -1) {
throw new BankException("Account not found!!!!");
}
System.out.println(accounts.get(index));
@Override
public String fundTransfer(int srcAccNo, int destAccNo, double fund) throws
BankException {
balanceCheck(srcAccNo, fund);
int index =getIndex(destAccNo);
if (index != -1) {
BankAccount b = accounts.get(index);
double bal = b.getBalance() + fund;
b.setBalance(bal);
return "Operation Succesful!!!!!!";
}
Handling:
package custom_handling;
@SuppressWarnings("serial")
public class BankException extends Exception {
public BankException(String msg) {
super(msg);
}
Validations :
package util;
import java.time.LocalDate;
import java.time.Period;
import com.bank.AccountTypes;
import custom_handling.BankException;
}
public static AccountTypes AccountTypeValidation(String actype) throws
IllegalArgumentException {
AccountTypes type=AccountTypes.valueOf(actype.toUpperCase());
return type;
}
public static void AgeValidation(String dob,LocalDate date) throws BankException{
int age=Period.between(date,LocalDate.now()).getYears();
if(age<18)
throw new BankException("you are below 18");
Tester :
package ui;
import java.util.Scanner;
import com.bank.service.BankOperationImpl;
import com.bank.service.BankOperationImplArrayList;
import custom_handling.BankException;
import util.Accountsvalidation;
case 2:
System.out.println("Enter your account number and
deposit fund");
System.out.println(b1.depositFund(sc.nextInt(),
sc.nextDouble()));
break;
case 3:
System.out.println("Enter the account number and
ammount");
System.out.println(b1.withdrowMoney(sc.nextInt(),
sc.nextDouble()));
break;
case 4:
System.out.println("Accounts details !!!!!!!!!!");
b1.displayAcc();
break;
case 5:
System.out.println("Enter your Account number");
b1.displaySummary(sc.nextInt());
break;
case 6:
// catch (BalanceValidationException e) {
// System.out.println(e.getMessage());
// }
}
}
Day 9 and 10:
package core.customer;
import java.time.LocalDate;
this.customerId = count++;
this.firstname = firstname;
this.lasttname = lasttname;
this.email = email;
this.password = password;
this.resAmmount = resAmmount;
this.dob = dob;
this.plan = plan;
}
@Override
public String toString() {
return "Customer [customerId=" + customerId + ", firstname=" + firstname + ",
lasttname=" + lasttname
+ ", email=" + email + ", password=********" + ", resAmmount=" +
resAmmount + ", dob=" + dob
+ ", plan=" + plan + "]";
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Customer) {
Customer other = (Customer) obj;
return this.email.equals(other.email);
}
return false;
}
}
—----------------------------------------------------------------------------------------------------------------------------
package core.customer;
—----------------------------------------------------------------------------------------------------------------------------
package core.customer.custom_exception;
@SuppressWarnings("serial")
public class CustomerException extends Exception {
}
—----------------------------------------------------------------------------------------------------------------------------
package core.customer.services;
import core.customer.custom_exception.CustomerException;
void display();
}
—----------------------------------------------------------------------------------------------------------------------------
package core.customer.services;
import java.time.LocalDate;
import core.customer.Customer;
import core.customer.ServicePlan;
import core.customer.custom_exception.CustomerException;
import core.customer.validation.CustomerValidation;
@Override
public String signUp(String firstname, String lasttname, String email, String password,
double resAmmount,
String dob, String plan) throws IllegalArgumentException,
CustomerException {
LocalDate d = CustomerValidation.parseDate(dob);
ServicePlan p = CustomerValidation.validatePlanAndRegAmount(plan,
resAmmount);
CustomerValidation.emailValidation(email);
CustomerValidation.passwordValidation(password);
Customer c = new Customer(firstname, lasttname, email, password,
resAmmount, d, p);
customers[index++] = c;
return "registration successfull!!!! with id :" + c.getCustomerId();
}
@Override
public void display() {
System.out.println("All customer details !!!!!");
for (Customer c : customers) {
if (c != null)
System.out.println(c);
}
}
@Override
public String signIn(String email, String password) throws CustomerException {
Customer c = emailFind(email);
if (c.getPassword().equals(password))
return "Sign In Successfully ! Hello " + c.getFirstname() + " " +
c.getLasttname();
throw new CustomerException("Invalid password!!!!!!!!!");
}
@Override
public String changePassword(String email, String oldpass, String newpass) throws
CustomerException {
Customer c = emailFind(email);
if (c.getPassword().equals(oldpass)) {
c.setPassword(newpass);
return "Successful password updation on cutomer id : " +
c.getCustomerId();
}
throw new CustomerException("Invalid password!!!!!!!!!");
}
@Override
public String unsuscribeCustomer(String email) throws CustomerException {
for (int i = 0; i < customers.length; i++) {
Customer c = customers[i];
if (c != null) {
if (c.getEmail().equals(email)) {
customers[i] = null;
return "Your are Unsuscribed!!!!!";
}
}
}
throw new CustomerException("Invalid email !!!!!!!");
}
—----------------------------------------------------------------------------------------------------------------------------
package core.customer.services;
import java.time.LocalDate;
import java.util.ArrayList;
import core.customer.Customer;
import core.customer.ServicePlan;
import core.customer.custom_exception.CustomerException;
import core.customer.validation.CustomerValidation;
public CustomerOperationImplArrayList() {
customers = new ArrayList<>();
}
@Override
public String signUp(String firstname, String lasttname, String email, String password,
double resAmmount,
String dob, String plan) throws IllegalArgumentException,
CustomerException {
LocalDate d = CustomerValidation.parseDate(dob);
ServicePlan p = CustomerValidation.validatePlanAndRegAmount(plan,
resAmmount);
CustomerValidation.emailValidation(email);
CustomerValidation.passwordValidation(password);
Customer c = new Customer(firstname, lasttname, email, password,
resAmmount, d, p);
customers.add(c);
return "registration successfull!!!! with id :" + c.getCustomerId();
@Override
public void display() {
System.out.println("All customer details !!!!!");
for (Customer c : customers) {
System.out.println(c);
}
@Override
public String signIn(String email, String password) throws CustomerException {
Customer c = emailFind(email);
if (c.getPassword().equals(password))
return "Sign In Successfully ! Hello " + c.getFirstname() + " " +
c.getLasttname();
throw new CustomerException("Invalid password!!!!!!!!!");
}
@Override
public String changePassword(String email, String oldpass, String newpass) throws
CustomerException {
Customer c = emailFind(email);
if (c.getPassword().equals(oldpass)) {
c.setPassword(newpass);
return "Successful password updation on cutomer id : " +
c.getCustomerId();
}
throw new CustomerException("Invalid password!!!!!!!!!");
}
@Override
public String unsuscribeCustomer(String email) throws CustomerException {
for (int i = 0; i < customers.size(); i++) {
Customer c =customers.get(i);
if (c.getEmail().equals(email)) {
customers.remove(i);
return "Your are Unsuscribed!!!!!";
}
}
throw new CustomerException("Invalid email !!!!!!!");
}
}
—----------------------------------------------------------------------------------------------------------------------------
package core.customer.services;
import java.time.LocalDate;
import core.customer.Customer;
import core.customer.ServicePlan;
import core.customer.custom_exception.CustomerException;
import core.customer.validation.CustomerValidation;
@Override
public String signUp(String firstname, String lasttname, String email, String password,
double resAmmount,
String dob, String plan) throws IllegalArgumentException,
CustomerException {
LocalDate d = CustomerValidation.parseDate(dob);
ServicePlan p = CustomerValidation.validatePlanAndRegAmount(plan,
resAmmount);
CustomerValidation.emailValidation(email);
CustomerValidation.passwordValidation(password);
Customer c = new Customer(firstname, lasttname, email, password,
resAmmount, d, p);
customers[index++] = c;
return "registration successfull!!!! with id :" + c.getCustomerId();
@Override
public void display() {
System.out.println("All customer details !!!!!");
for (Customer c : customers) {
if (c != null)
System.out.println(c);
}
}
@Override
public String signIn(String email, String password) throws CustomerException {
Customer c = emailFind(email);
if (c.getPassword().equals(password))
return "Sign In Successfully ! Hello " + c.getFirstname() + " " +
c.getLasttname();
throw new CustomerException("Invalid password!!!!!!!!!");
}
@Override
public String changePassword(String email, String oldpass, String newpass) throws
CustomerException {
Customer c = emailFind(email);
if (c.getPassword().equals(oldpass)) {
c.setPassword(newpass);
return "Successful password updation on cutomer id : " +
c.getCustomerId();
}
throw new CustomerException("Invalid password!!!!!!!!!");
}
@Override
public String unsuscribeCustomer(String email) throws CustomerException {
for (int i = 0; i < customers.length; i++) {
Customer c = customers[i];
if (c != null) {
if (c.getEmail().equals(email)) {
customers[i] = null;
return "Your are Unsuscribed!!!!!";
}
}
}
throw new CustomerException("Invalid email !!!!!!!");
}
—----------------------------------------------------------------------------------------------------------------------------
package core.customer.validation;
import java.time.LocalDate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import core.customer.ServicePlan;
import core.customer.custom_exception.CustomerException;
}
public static void passwordValidation(String password) throws CustomerException {
String pass="^\\d{8}$";
Pattern p = Pattern.compile(pass);
Matcher m = p.matcher(password);
if(!m.matches()) throw new CustomerException("Password must be exactly 8 digits
(e.g., 12345678)!!!!!!");
}
public static LocalDate parseDate(String dob)throws IllegalArgumentException{
return LocalDate.parse(dob);
}
—----------------------------------------------------------------------------------------------------------------------------
package core.customer.tester;
import java.util.Scanner;
import core.customer.services.CustomerOperationImplArrayList;
import core.customer.services.CustomerOperationsImpl;
break;
case 2:
System.out.println("Enter your email and
password");
System.out.println(op.signIn(sc.next(), sc.next()));
break;
case 3:
System.out.println("Enter your email, old
password,new password");
System.out.println(op.changePassword(sc.next(),
sc.next(), sc.next()));
break;
case 4:
System.out.println("Enter your email");
System.out.println(op.unsuscribeCustomer(sc.next()));
break;
case 5:
op.display();
break;
case 0:
System.out.println("Exiting...........");
exit = true;
break;
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("Pls retry ...");
sc.nextLine();
}
}
}
}
Day 11 - 12
package com.bank;
import java.time.LocalDate;
import java.util.Objects;
@Override
public String toString() {
return "BankAccount [accountNo=" + accountNo + ", fisrtName=" + firstName + ",
lastName=" + lastName
+ ", balance=" + balance + ", accType=" + accType + ", DOB=" +
dob + "]";
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BankAccount)
return this.accountNo == ((BankAccount) obj).accountNo;
return false;
}
@Override
public int compareTo(BankAccount o) {
if(this.accountNo<o.accountNo)return -1;
else if(this.accountNo==o.accountNo) return 0;
else return 1;
}
package com.bank;
package com.bank.service;
import com.bank.BankAccount;
import custom_handling.BankException;
void displayAcc();
package com.bank.service;
import java.time.LocalDate;
import com.bank.AccountTypes;
import com.bank.BankAccount;
import custom_handling.BankException;
import static util.Accountsvalidation.*;
@Override
public String addAccount(int accNo, String firstName, String lastName, double balance,
String accType, String dob)
throws BankException {
AccountTypes type = AccountTypeValidation(accType);
double bal = BalanceValidation(type,balance);
LocalDate d = parseDate(dob);
AgeValidation(dob, d);
for (BankAccount b : accounts) {
if (b != null) {
int objaccNo = b.getAccount();
accNo = AccountValidation(objaccNo, accNo);
}
}
accounts[index++] = new BankAccount(accNo, firstName, lastName, bal, type,
d);
return "account is created !!!!";
@Override
public void displayAcc() {
for (BankAccount b : accounts) {
if (b != null) {
System.out.println(b);
}
}
}
@Override
public void displaySummary(int accNo) throws BankException {
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == accNo) {
System.out.println(b);
}
}
}
throw new BankException("Account not found!!!!");
@Override
public String depositFund(int acNo, double balance) {
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == acNo) {
b.setBalance(b.getBalance() + balance);
return "Operation Succesful!!!!!!";
}
}
}
return "Something Wrong";
}
@Override
public String withdrowMoney(int acNo, double balance) throws BankException {
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == acNo) {
if (b.getBalance() < balance)
throw new BankException("Insufficient
balance!!!!!");
else if (b.getBalance() - balance < 5000)
throw new BankException(
"You have to maintain minumum
balance your balance is : " + b.getBalance());
else {
b.setBalance(b.getBalance() - balance);
return "Operation Succesful!!!!!!";
}
}
}
}
return "Something Wrong";
}
@Override
public String fundTransfer(int srcAccNo, int destAccNo, double fund) throws
BankException {
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == srcAccNo) {
if (b.getBalance() < fund)
throw new BankException("Insufficient
balance!!!!!");
else if (b.getBalance() - fund < 5000)
throw new BankException(
"You have to maintain minumum
balance your balance is : " + b.getBalance());
else {
double bal = b.getBalance() - fund;
b.setBalance(bal);
}
}
}
}
for (BankAccount b : accounts) {
if (b != null) {
if (b.getAccount() == destAccNo) {
double bal = b.getBalance() + fund;
b.setBalance(bal);
}
}
}
return "Operation Succesful!!!!!!";
}
@Override
public void sortByAccountNo() {
// TODO Auto-generated method stub
@Override
public void sortByAccounttype() {
// TODO Auto-generated method stub
@Override
public void sortByDOBandBalance() {
// TODO Auto-generated method stub
package com.bank.service;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import com.bank.AccountTypes;
import com.bank.BankAccount;
import custom_handling.BankException;
import static util.Accountsvalidation.*;
public BankOperationImplArrayList() {
accounts = new ArrayList<>();
@Override
public String addAccount(int accNo, String firstName, String lastName, double balance,
String accType, String dob)
throws BankException {
AccountTypes type = AccountTypeValidation(accType);
if (accounts.contains(acc)) {
throw new BankException("Account number already exist!!!!");
}
@Override
public void displayAcc() {
for (BankAccount b : accounts) {
System.out.println(b);
}
@Override
public String depositFund(int acNo, double balance) throws BankException {
int index = getIndex(acNo);
if (index != -1) {
BankAccount b = accounts.get(index);
b.setBalance(b.getBalance() + balance);
return "Operation Succesful!!!!!!";
}
throw new BankException("Account not found");
}
@Override
public String withdrowMoney(int acNo, double balance) throws BankException {
balanceCheck(acNo,balance);
return "Operation Successfull!!!!";
}
@Override
public void displaySummary(int accNo) throws BankException {
int index = getIndex(accNo);
if (index == -1) {
throw new BankException("Account not found!!!!");
}
System.out.println(accounts.get(index));
@Override
public String fundTransfer(int srcAccNo, int destAccNo, double fund) throws
BankException {
balanceCheck(srcAccNo, fund);
int index =getIndex(destAccNo);
if (index != -1) {
BankAccount b = accounts.get(index);
double bal = b.getBalance() + fund;
b.setBalance(bal);
return "Operation Succesful!!!!!!";
}
Collections.sort(accounts);
System.out.println("Operation done!!!!");
}
@Override
public void sortByAccounttype() {
Collections.sort(accounts,new Comparator<BankAccount>() {
@Override
public int compare(BankAccount o1, BankAccount o2) {
return o1.getAccType().compareTo(o2.getAccType());
});
System.out.println("Operation done!!!!");
}
@Override
public void sortByDOBandBalance() {
Collections.sort(accounts,new Comparator<BankAccount>() {
@Override
public int compare(BankAccount o1, BankAccount o2) {
int typeval=o1.getDob().compareTo(o2.getDob());
if(typeval==0){
// if(o1.getBalance()<o2.getBalance())return -1;
// else if(o1.getBalance()==o2.getBalance())return 0;
// return 1;
return ((Double)o1.getBalance()).compareTo(o2.getBalance());//it is
compreTo method of Double so in o2 its auto boxing double to Double
}
return typeval;
}
});
System.out.println("Operation done!!!!");
}
package custom_handling;
@SuppressWarnings("serial")
public class BankException extends Exception {
public BankException(String msg) {
super(msg);
}
package util;
import java.time.LocalDate;
import java.time.Period;
import com.bank.AccountTypes;
import custom_handling.BankException;
}
public static AccountTypes AccountTypeValidation(String actype) throws
IllegalArgumentException {
AccountTypes type=AccountTypes.valueOf(actype.toUpperCase());
return type;
}
public static void AgeValidation(String dob,LocalDate date) throws BankException{
int age=Period.between(date,LocalDate.now()).getYears();
if(age<18)
throw new BankException("you are below 18");
package ui;
import java.util.Scanner;
import com.bank.service.BankOperationImpl;
import com.bank.service.BankOperationImplArrayList;
import custom_handling.BankException;
import util.Accountsvalidation;
case 2:
System.out.println("Enter your account number and
deposit fund");
System.out.println(b1.depositFund(sc.nextInt(),
sc.nextDouble()));
break;
case 3:
System.out.println("Enter the account number and
ammount");
System.out.println(b1.withdrowMoney(sc.nextInt(),
sc.nextDouble()));
break;
case 4:
System.out.println("Accounts details !!!!!!!!!!");
b1.displayAcc();
break;
case 5:
System.out.println("Enter your Account number");
b1.displaySummary(sc.nextInt());
break;
case 6:
// catch (BalanceValidationException e) {
// System.out.println(e.getMessage());
// }
}
Day 13
package com.app.core;
@Override
public String toString() {
return "Student [rollNo=" + rollNo + ", name=" + name + ", marks=" + marks + "]";
}
//override equals to replace ref equality by content eq - PK (UID) - identitty
// @Override
// public boolean equals(Object o)
// {
// System.out.println("in student eq");
// if(o instanceof Student)
// {
// Student s=(Student)o;
// return this.rollNo.equals(s.rollNo);
// }
// return false;
// }
//override hashCode to satisfy the contract between equals n hashCode
// @Override
// public int hashCode() {
// System.out.println("in student hashCode");
//// return 100;
// return this.rollNo.hashCode();
// }
package com.app.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
//// ---------------- Delete student details having marks < specified marks
System.out.println("-----------------");
System.out.println("enter the marks");
int marks = sc.nextInt();
if (s.getMarks()<marks) {
it.remove();
}
for (Student f : students.values())
System.out.println(f);
}
Day 14
package com.app.core;
@Override
public String toString() {
return "Emp [basic=" + basic + "]";
}
}
package com.app.core;
@Override
public double computeSalary() {
// TODO Auto-generated method stub
return getBasic()+3000;
}
}
package com.app.core;
@Override
public double computeSalary() {
// TODO Auto-generated method stub
return getBasic()+ 1000;
}
}
package com.app.core;
@Override
public double computeSalary() {
// TODO Auto-generated method stub
return getBasic()+ 700;
}
package com.app.core;
@Override
public double computeSalary() {
// TODO Auto-generated method stub
return getBasic()+ 2000;
}
package com.app.core;
@Override
public double computeSalary() {
// TODO Auto-generated method stub
return getBasic()+ 600;
}
package com.app.core;
@Override
public double computeSalary() {
// TODO Auto-generated method stub
return getBasic() + 500;
}
}
package com.app.utils;
import java.util.ArrayList;
import java.util.List;
import com.app.core.Emp;
package com.app.tester;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import com.app.core.Mgr;
import com.app.core.SalesMgr;
import com.app.core.TempWorker;
import com.app.utils.GenericUtils;
/*
* 1. Consider Emp inheritance hierarchy
write a static method in GenericUtils i/f -package : com.app.utils
to return sum of salaries of all emps from the specified list.
Test cases -- AL<Mgr> , Vector<TempWorker> ,
LinkedList<SalesMgr>
*/
public class Test1 {
}
package com.app.tester;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Vector;
import com.app.utils.GenericUtils;
/*
* 2. Solve - Write a generic method to return max element
* from ANY List
eg - Test cases
ArrayList<Integer>
LinkedList<Double>
Vector<LocalDate>
*/
public class Test2 {
Vector<LocalDate> dates=
new Vector<>(Arrays.asList(parse("2025-10-20"),
parse("2024-11-20"),
parse("2022-09-25"),
parse("2024-11-29"),
parse("2025-03-20")
));
System.out.println("Max Date (Latest Date) "+GenericUtils.findMax(dates));
package com.app.tester;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import com.app.core.Mgr;
import com.app.core.SalesMgr;
import com.app.core.TempWorker;
/*
* 3. Refer to Emp based organization hierarchy
Sort the emps as per their basic salary - must use Custom ordering
eg Test cases
AL | LinkedList | Vector of data type - Emp , Mgr , Worker or PermanentWorker , or
SalesMgr .....
*/
public class Test3 {
@Override
public int compare(Mgr o1, Mgr o2) {
return ((Double)o1.getBasic()).compareTo(o2.getBasic());
}
});
System.out.println(mgrs);
@Override
public int compare(TempWorker o1, TempWorker o2) {
return ((Double)o1.getBasic()).compareTo(o2.getBasic());
}
});
System.out.println(tempWorkers);
LinkedList<SalesMgr> salesMgrs=new LinkedList<>(
List.of(new SalesMgr(3800),
new SalesMgr(4100),
new SalesMgr(4000)));
Collections.sort(salesMgrs,new Comparator<SalesMgr>() {
@Override
public int compare(SalesMgr o1, SalesMgr o2) {
return ((Double)o1.getBasic()).compareTo(o2.getBasic());
}
});
System.out.println(salesMgrs);
Day 16
package com.app.core;
import java.time.LocalDate;
public Student(String rollNo, String name, LocalDate dob, Subject subject, double gpa) {
super();
this.rollNo = rollNo;
this.name = name;
this.dob = dob;
this.subject = subject;
this.gpa = gpa;
}
@Override
public String toString() {
return "Student rollNo=" + rollNo + ", name=" + name + ", dob=" + dob + ",
subject=" + subject + ", gpa=" + gpa
+ " adr : " + address;
}
package com.app.core;
@Override
public String toString() {
return "Address [city=" + city + ", state=" + state + ", phoneNo=" + phoneNo + "]";
}
package com.app.core;
package custom_exception;
package utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.app.core.Address;
import com.app.core.Student;
// add a static method to accept list of student details & rets a map populated with the
same
public static Map<String, Student> populateMap(List<Student> list) {
HashMap<String, Student> hm = new HashMap<>();
list.forEach(s -> hm.put(s.getRollNo(), s));
return hm;
package tester;
import static utils.StudentCollectionUtils.populateList;
import static utils.StudentCollectionUtils.populateMap;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import com.app.core.Student;
import com.app.core.Subject;
import custom_exception.StudentCollectionException;
public class Test {
//3.1 Display details of the students who have taken a specified subject
// System.out.println("enter the subject");
// Subject sb= Subject.valueOf(sc.next().toUpperCase());
// stmap.values()
// .stream()
// .filter(p -> p.getSubject()==sb)
// .forEach(p -> System.out.println(p));
// 3.2 Print sum of marks of students of all students from the specified state
// i/p : name of the state
// System.out.println("enter the state");
// String st=sc.next().toUpperCase();
// double sum = stmap.values()
// .stream()
// .filter(p->p.getAddress().getState().equals(st))
// .mapToDouble(p->p.getGpa())
// .sum();
// System.out.println(sum);
// 3.4 Print no of failures for the specified subject chosen from user.
// i/p : subject name
// (failure is GPA < 5.0 , out of 1-10)
//
// 3.5 Display names of students enrolled in a specified subject , securing marks >
specified marks
// i/p : subject name , marks
// System.out.println("enter the subject and marks");
// Subject sb= Subject.valueOf(sc.next().toUpperCase());
// double gpa=sc.nextDouble();
//
// stmap.values()
// .stream()
// .filter(p -> p.getGpa()>gpa && p.getSubject()==sb)
// .forEach(p->System.out.println(p.getName()));
// 3.7 Display student details for specified subject , sorted as per DoB
System.out.println("enter the subject");
Subject sb= Subject.valueOf(sc.next().toUpperCase());
Comparator< Student> c=(p1,p2)->p1.getDob().compareTo(p2.getDob());
stmap.values()
.stream()
.filter(p->p.getSubject()==sb)
.sorted(c)
.forEach(p->System.out.println(p));
}catch (Exception e) {
System.out.println(e.getMessage());
}
Day 18
package com.app.core;
import java.time.LocalDate;
package com.app.core;
@Override
public String toString() {
return "Address [city=" + city + ", state=" + state + ", phoneNo=" + phoneNo + "]";
}
package com.app.core;
public enum Subject {
JAVA, R, ML, CLOUD, PYTHON, DBT
}
package com.app.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.app.core.Address;
import com.app.core.Student;
// somewhere in day 18
return students;// fixed size student list , where students are linked with address.
}
public static Map<String, Student> populatedMap(){
List<Student> st=populateList();
// Map<String, Student> map=new HashMap<>();
// Map<String, Student>map = st.stream().collect(Collectors.toMap(i-
>i.getRollNo(),i->i));
// st.forEach(i->map.put(i.getRollNo(), i));
return st.stream().collect(Collectors.toMap(i->i.getRollNo(),i->i));
}
}
package threads;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Map;
import com.app.core.Student;
@Override
public void run() {
System.out.println("In run method of"+ Thread.currentThread().getName()+"is
started");
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
Comparator<Student> c = (p1, p2) ->
p1.getDob().compareTo(p2.getDob());
map.values().stream().sorted(c).forEach(i -> pw.println(i));
} catch (IOException e) {
System.out.println("catch of "+Thread.currentThread().getName()
+e.getMessage());
}
System.out.println("In run method of"+ Thread.currentThread().getName()+"is
over");
}
}
package threads;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Map;
import com.app.core.Student;
@Override
public void run() {
System.out.println("In run method of"+ Thread.currentThread().getName()+"is
started");
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
Comparator<Student> c = (p1, p2) -> {
int
val=p1.getSubject().name() .compareTo(p2.getSubject().name());
if(val==0) {
return ((Double)p1.getGpa()).compareTo(p2.getGpa());
}
return val;
};
map.values().stream().sorted(c).forEach(i -> pw.println(i));
} catch (IOException e) {
System.out.println("in catch of"+Thread.currentThread().getName()
+e.getMessage());
}
System.out.println("In run method of"+ Thread.currentThread().getName()+"is
over");
package threads;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Map;
import com.app.core.Student;
@Override
public void run() {
System.out.println("In run method of"+ Thread.currentThread().getName()+"is
started");
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
Comparator< Student> c=(p1,p2)-
>((Double)p2.getGpa()).compareTo(p1.getGpa());
map.values().stream().filter(i-
>i.getAddress().getCity().equals(city)).sorted(c).forEach(i-> pw.println(i.getRollNo()+"
"+i.getName()+" "+i.getGpa()));
} catch (IOException e) {
System.out.println("in catch of"+Thread.currentThread().getName()
+e.getMessage());
}
System.out.println("In run method of"+ Thread.currentThread().getName()+"is
over");
}
}
package threads;
import java.util.Map;
import java.util.Scanner;
import com.app.core.Student;
}
catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("main is over");