[go: up one dir, main page]

0% found this document useful (0 votes)
22 views12 pages

Mock Solutions

The document contains multiple Java solutions that implement classes and methods for managing customer data, cycles, perfumes, orders, and movies. Each solution includes functionalities such as calculating total prices, finding customers based on specific criteria, and handling exceptions for invalid data. The code is structured with classes representing different entities and methods for performing operations on these entities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views12 pages

Mock Solutions

The document contains multiple Java solutions that implement classes and methods for managing customer data, cycles, perfumes, orders, and movies. Each solution includes functionalities such as calculating total prices, finding customers based on specific criteria, and handling exceptions for invalid data. The code is structured with classes representing different entities and methods for performing operations on these entities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

SBQ 1

import java.io.*;
import java.util.*;
import java.text.*;
import java.time.Period;
import java.math.*;
import java.util.regex.*;

public class Solution {


public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
ArrayList<Customer> customers = new ArrayList<>();
int n = sc.nextInt();sc.nextLine();

for(int i=0; i<n; i++){


int a = sc.nextInt();sc.nextLine();
String b = sc.nextLine();
int c = sc.nextInt();sc.nextLine();
String d = sc.nextLine();
int num = sc.nextInt();sc.nextLine();
ArrayList<Perfume> perfumes = new ArrayList<>();
for(int j=0; j<num; j++){
int a1 = sc.nextInt();sc.nextLine();
String b1 = sc.nextLine();
double c1 = sc.nextDouble();sc.nextLine();
String d1 = sc.nextLine();
perfumes.add(new Perfume(a1, b1, c1, d1));
}

customers.add(new Customer(a, b, c, d, perfumes));


}

String s = sc.next();
findCustomer(customers, s);
calTot(customers);

public static void findCustomer(ArrayList<Customer> customers, String brand){


ArrayList<String> names = new ArrayList<>();
for(Customer customer : customers){
for(Perfume perfume: customer.perfumes){
if(perfume.brand.equalsIgnoreCase(brand)){
names.add(customer.name);
break;
}
}
}

if(names.isEmpty()) {
System.out.println("No matching customers found.");
}
else{
for(String s: names){
System.out.println(s);
}
}
}
public static void calTot(ArrayList<Customer> customers){
double ans = 0;

for(Customer customer: customers){


for(Perfume perfume: customer.perfumes){
ans += perfume.price;
}
}

System.out.println("Total expenditure on perfumes: $"+ans);


}
}
class Customer{
public int customerId;
public String name;
public int age;
public String gender;
public ArrayList<Perfume> perfumes;

public Customer(int customerId, String name, int age, String gender,


ArrayList<Perfume> perfumes){
this.customerId = customerId;
this.name = name;
this.age= age;
this.gender = gender;
this.perfumes = perfumes;
}
}

class Perfume{
int perfumeId;
String brand;
double price;
String fragrance;

public Perfume(int perfumeId, String brand, double price, String fragrance){


this.perfumeId = perfumeId;
this.brand = brand;
this.price = price;
this.fragrance = fragrance;
}
}

SBQ 2

import java.util.*;

class Customer{
Integer custId;
String custName;
Integer noofcycles;
List<Cycle> cycles;

public Customer(Integer custId,String custName,Integer noofcycles,List<Cycle>


cycles){
this.custId=custId;
this.custName=custName;
this.noofcycles=noofcycles;
this.cycles=cycles;
}
public Integer getCustId(){
return custId;
}
public String getCustName(){
return custName;
}
public Integer getNoCycles(){
return noofcycles;
}
public List<Cycle> getCycles(){
return cycles;
}
}

class Cycle{
Integer cycleId;
String cycleName;
Integer price;

public Cycle(Integer cycleId,String cycleName,Integer price){


this.cycleId=cycleId;
this.cycleName=cycleName;
this.price=price;
}
public Integer getCycleId(){
return cycleId;
}
public String getCycleName(){
return cycleName;
}
public Integer getPrice(){
return price;
}
}
class PriceisNegativeException extends Exception{
public PriceisNegativeException(String message){
super(message);
}
}

class Solution{

public static void CalculateTotalPriceByCycleName(List<Customer> cust, String


cycleName)throws Exception{
int total=0;
boolean found=false;
for(Customer c:cust){
for(Cycle cycle:c.getCycles()){
if(cycle.getCycleName().equalsIgnoreCase(cycleName)){
if(cycle.getPrice()<0){
throw new PriceisNegativeException("Invalid Price: Price
cannot be negative.");
}
total+=cycle.getPrice();
found=true;
break;
}
}

if(!found){
System.out.println("No cycle found with mentioned name.");
}else{
System.out.println(total);
}
}
public static void findCustomerNamesOfCyclesByPrice(List<Customer> cust, int
price){
boolean found=false;
for(Customer c:cust){
for(Cycle cycle:c.getCycles()){
if(cycle.getPrice()>price){
System.out.println(c.getCustName());
found=true;
break;
}
}
}
if(!found){
System.out.println("No matching customers found.");
}
}
public static void main(String args[]) throws Exception{
Scanner sc= new Scanner(System.in);

int n=sc.nextInt();
sc.nextLine();
List<Customer> customers=new ArrayList<>();
for(int i=0;i<n;i++){
int id=sc.nextInt();
sc.nextLine();
String name=sc.nextLine();
int nC=sc.nextInt();
sc.nextLine();
List<Cycle>cycles=new ArrayList<>();
for(int j=0;j<nC;j++){
int iii=sc.nextInt();
sc.nextLine();
String cn=sc.nextLine();
int pr=sc.nextInt();
sc.nextLine();
cycles.add(new Cycle(iii, cn, pr));
}
customers.add(new Customer(id, name, nC, cycles));
}

String na=sc.nextLine();
int pp=sc.nextInt();
try{
CalculateTotalPriceByCycleName(customers, na);
}
catch(PriceisNegativeException e){
System.out.println(e.getMessage());
}
findCustomerNamesOfCyclesByPrice(customers, pp);
}
}

Mock 3 Solution

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

class SalaryIsNegativeException extends Exception{


public SalaryIsNegativeException(String message){
super(message);
}
}

public class Service {


public static void main(String args[] ) throws Exception {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(); sc.nextLine();
Unit[] arr1= new Unit[n];
for(int i=0;i<n;i++){
int a=sc.nextInt();sc.nextLine();
String b=sc.nextLine();
int c=sc.nextInt();sc.nextLine();
Employee[] arr2=new Employee[c];
for(int j=0;j<c;j++){
int d=sc.nextInt();sc.nextLine();
String e=sc.nextLine();
int f=sc.nextInt();sc.nextLine();
arr2[j]=new Employee(d,e,f);
}
arr1[i]= new Unit(a,b,arr2);
}

String name=sc.nextLine();
// int ans1=CalculateTotalSalaryByEmployeeName(arr1,name);
// if(ans1==-1) System.out.println("Invalid Salary: Salary cannot be
negative.");
// else if(ans1==0) System.out.println("No Employee found with mentioned
name.");
// else System.out.println(ans1);

CalculateTotalSalaryByEmployeeName(arr1,name);

int salary=sc.nextInt();
ArrayList<String> ans2=findUnitNamesOfEmployeeBySalary(arr1, salary);
if(ans2.size()==0) System.out.println("No matching units found.");
else{
for(int i=0;i<ans2.size();i++) System.out.println(ans2.get(i));
}
}

// public static int CalculateTotalSalaryByEmployeeName(Unit[] arr, String


name){
// int ans=0;
// boolean flag=false;
// for(Unit it:arr){
// int x=it.employees.length;
// for(int i=0;i<x;i++){
// if(it.employees[i].ename.equals(name)){
// if(it.employees[i].salary<0){
// flag=true;
// break;
// }
// else{
// ans+=it.employees[i].salary;
// }
// }
// }
// if(flag==true){
// ans=-1;
// break;
// }
// }
// return ans;
// }

public static void CalculateTotalSalaryByEmployeeName(Unit[] arr, String name){


int ans1=0;
boolean flag=false;
for(Unit it:arr){
int x=it.employees.length;
for(int i=0;i<x;i++){
if(it.employees[i].ename.equals(name)){
flag=true;
try{
if(it.employees[i].salary<0){
throw new SalaryIsNegativeException("Invalid Salary:
Salary cannot be negative.");
}
ans1+=it.employees[i].salary;
} catch(SalaryIsNegativeException e){
System.out.println(e.getMessage());
return;
}
}
}
}
if(flag) System.out.println(ans1);
else System.out.println("No Employee found with mentioned name.");
}

public static ArrayList<String> findUnitNamesOfEmployeeBySalary(Unit[] arr, int


salary){
ArrayList<String> ans= new ArrayList<String>();
for(Unit it:arr){
int x=it.employees.length;
for(int i=0;i<x;i++){
if(it.employees[i].salary>salary) {
if(ans.contains(it.name)==true) continue;
ans.add(it.name);
}
}
}
return ans;
}
}

class Unit{
int id;
String name;
int budget;
Employee[] employees;

public Unit(int id, String name, Employee[] employees){


this.id=id;
this.name=name;
this.employees=employees;
}
}

class Employee{
int eid;
String ename;
int salary;

public Employee(int eid, String ename, int salary){


this.eid=eid;
this.ename=ename;
this.salary=salary;
}
}
Mock 4 Solution

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

class Orders{

int orderId;
String orderName;
int noOfProducts;
List<String> products;
List<Integer> quantities;

Orders(int orderId,String orderName,int noOfProducts,List<String>


products,List<Integer> quantities){

this.orderId=orderId;
this.orderName=orderName;
this.noOfProducts=noOfProducts;
this.products=products;
this.quantities=quantities;

public int getOrderId(){


return orderId;
}

public String getOrderName(){


return orderName;
}

public int getNoOfProducts(){


return noOfProducts;
}

public List<String> getProducts(){


return products;
}

public List<Integer> getQuantities(){


return quantities;
}

class OrdersService{

public void maxQuantity(List<Orders> orders,int orderId){

boolean found=false;
int maxIndex=0;

for(Orders o:orders){

if(o.getOrderId()==orderId){
found=true;
List<String> prod=o.getProducts();
List<Integer> quan=o.getQuantities();

for(int i=1;i<quan.size();i++){
if(quan.get(i)>quan.get(maxIndex)){
maxIndex=i;
}
}
System.out.println(prod.get(maxIndex));
}
}

if(found==false){
System.out.println("Order Id Not Found");
return;
}
}

public void orderByProductName(List<Orders> orders,String productName){

boolean found=false;

for(Orders o:orders){

List<String> products=o.getProducts();

for(String s:products){
if(s.equalsIgnoreCase(productName)){
System.out.println(o.getOrderId());
System.out.println(o.getOrderName());
found=true;
break;
}
}
}

if(found==false){
System.out.println("Product Not Found");
}

public class OrdersTester {


public static void main(String args[] ) throws Exception {

Scanner sc=new Scanner(System.in);


int n=sc.nextInt();
List<Orders> orders=new ArrayList<>();

for(int i=0;i<n;i++){

int a=sc.nextInt(); sc.nextLine();


String b=sc.nextLine();
int c=sc.nextInt(); sc.nextLine();

List<String> products=new ArrayList<>();


List<Integer> quantities=new ArrayList<>();

for(int j=0;j<c;j++){
String x=sc.nextLine();
int y=sc.nextInt(); sc.nextLine();

products.add(x);
quantities.add(y);

orders.add(new Orders(a,b,c,products,quantities));
}

int orderId=sc.nextInt(); sc.nextLine();


String productName=sc.nextLine();

OrdersService service=new OrdersService();


service.maxQuantity(orders, orderId);
service.orderByProductName(orders, productName);

}
}

Mock 5 Solution

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

class Movies{
int id, movieRating,numberOfActors;
String movieName;
List<String> actors;

public Movies(int id, int rating, int nActors, String name, List<String>
actors)
{
this.id = id;
this.movieRating = rating;
this.actors = actors;
this.movieName = name;
this.numberOfActors = nActors;
}
public List<String> getActors(){return actors;}
public int getId(){return id;}
public String getMovieName(){return movieName;}
public int getMovieRating(){return movieRating;}
}

class MovieService{
public List<Movies> moviesBasedOnActors(List<Movies> movies, String actorName )
{
List<Movies> movs = new ArrayList<>();
Set<Integer> set = new HashSet<>();
for(Movies m : movies)
{
for(String actor : m.getActors()){
if(actor.equalsIgnoreCase(actorName))
{
if(set.add(m.getId()))
{
// if(m.getMovieName().equalsIgnoreCase("movie3"))
// System.out.println(actorName);
movs.add(m);
}
}
}
}
return movs;
}

public double avgRatingBasedOnActor(List<Movies> movies, String actorName)


{
double sum = 0.0;
int count = 0;
Set<Integer> set = new HashSet<>();

for(Movies m : movies)
{
for(String actor : m.getActors())
{
if(actor.equalsIgnoreCase(actorName) && set.add(m.getId()))
{
sum += m.getMovieRating();
count++;
}
}
}
if(count == 0)
return 0;
return sum/count;

}
}

public class MovieTester {


public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
List<Movies> movies = new ArrayList<>();
int n = sc.nextInt();
for(int i=0;i<n;i++)
{
int id = sc.nextInt();
String mName = sc.next();
sc.nextLine();
int rating = sc.nextInt();
int nActors = sc.nextInt();
List<String> actors = new ArrayList<>();
for(int j=0;j<nActors;j++)
{
String aName = sc.next();
actors.add(aName);
}
Movies m = new Movies(id, rating, nActors, mName, actors);
// System.out.println(m.getActors());
movies.add(m);
sc.nextLine();
}
MovieService movieService = new MovieService();
String aName = sc.next();
List<Movies> movs = movieService.moviesBasedOnActors(movies, aName);
if(movs.size() == 0)
{
System.out.println("No Movies Found");
}
else{
for(Movies m : movs)
{
System.out.println(m.getId());
System.out.println(m.getMovieName());
System.out.println(m.getMovieRating());
}
}
aName = sc.next();
double rating = movieService.avgRatingBasedOnActor(movies, aName);
if(rating == 0)
System.out.println("No Actor Found");
else
System.out.println("Average Rating for the actor is : "+ rating);
}
}

You might also like