0% found this document useful (0 votes)
635 views

Java Questions

The document describes creating subclasses ElectronicToy and MusicalToy that inherit from the Toy class, with ElectronicToy adding attributes for number of batteries and operating mode and MusicalToy adding an attribute for number of speakers. It provides code to create objects of these subclasses, take user input to indicate which type of toy is being created, and override the toString method to output details specific to each subclass. The code is meant to demonstrate inheritance by extending an existing Toy class to create more specialized toy types.

Uploaded by

Debojit Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
635 views

Java Questions

The document describes creating subclasses ElectronicToy and MusicalToy that inherit from the Toy class, with ElectronicToy adding attributes for number of batteries and operating mode and MusicalToy adding an attribute for number of speakers. It provides code to create objects of these subclasses, take user input to indicate which type of toy is being created, and override the toString method to output details specific to each subclass. The code is meant to demonstrate inheritance by extending an existing Toy class to create more specialized toy types.

Uploaded by

Debojit Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Question 1

Java- Toy and Admin class - 4.1


bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

1. Create Admin and Toy class as per the given structure with setter and getter
methods and create the object for Admin and Toy object use the setter methods to pass
the value to the attributes and print the information.

2 Create a constructor for Admin and Toy with the attributes as parameter. create the
objects using parameterized constructor in the main method of the Source class.

Sample Input
120
Rubber Ducky
Toy
1
3
200
20
20
200
Sample output
ID: 120
ToyName: Rubber Ducky
ToyType: Toy
Min Age: 1
Max Age: 3
Price: 200.0
Quanitity: 20
RefundableAmount: 20.0
Refundable Deposit: 200.0
Code:
import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

class Admin{

private String email;

private String password;

private String name;

Admin(){}

Admin(String email, String password, String name){

this.email=email;

this.password=password;

this.name=name;

// Getter1

public String getEmail() {

return email;

// Setter1

public void setEmail(String email) {

this.email = email;

// Getter2
// Setter2

public void setPassword(String password) {

this.password = password;

// Getter3

public String getName() {

return name;

// Setter3

public void setName(String name) {

this.name = name;

class Toy{

private int toyId;

private String toyName;

private String toyType;

private int minAge;

private int maxAge;

private int price;

private int quantity;

private int rentalAmount;

private int refundableDeposit;

public Toy(){}
public Toy(int toyId, String toyName, String toyType, int minAge,

int maxAge, int price, int quantity, int rentalAmount,

int refundableDeposit){

this.toyId=toyId;

this.toyName=toyName;

this.toyType=toyType;

this.minAge=minAge;

this.maxAge=maxAge;

this.price=price;

this.quantity=quantity;

this.rentalAmount=rentalAmount;

this.refundableDeposit=refundableDeposit;

// Getter1

public int getToyId() {

return toyId;

// Setter1

public void setToyId(int toyId) {

this.toyId = toyId;

// Getter2

public String getToyName() {

return toyName;

// Setter2
public void setToyName(String toyName) {

this.toyName = toyName;

// Getter3

public String getToyType() {

return toyType;

// Setter3

public void setToyType(String toyType) {

this.toyType = toyType;

// Getter4

public int getMinAge() {

return minAge;

// Setter4

public void setMinAge(int minAge) {

this.minAge = minAge;

// Getter5

public int getMaxAge() {

return maxAge;

// Setter5

public void setMaxAge(int maxAge) {


this.maxAge = maxAge;

// Getter6

public int getPrice() {

return price;

// Setter6

public void setPrice(int price) {

this.price = price;

// Getter7

public int getQuantity() {

return quantity;

// Setter7

public void setQuantity(int quantity) {

this.quantity=quantity;

// Getter8

public int getRentalAmount() {

return rentalAmount;

// Setter8

public void setRentalAmount(int rentalAmount) {

this.rentalAmount = rentalAmount;
}

// Getter9

public int getRefundableDeposit() {

return refundableDeposit;

// Setter9

public void setRefundableDeposit(int refundableDeposit) {

this.refundableDeposit = refundableDeposit;

public class Source

public static void main( String[] args )

Toy toy = new Toy();

Admin admin=new Admin();

Scanner in = new Scanner(System.in);

String str1 = in.nextLine();

int i1=Integer.parseInt(str1);

toy.setToyId(i1);

String str2 = in.nextLine();

toy.setToyName(str2);
String str3 = in.nextLine();

toy.setToyType(str3);

String str4 = in.nextLine();

int i4=Integer.parseInt(str4);

toy.setMinAge(i4);

String str5 = in.nextLine();

int i5=Integer.parseInt(str4);

toy.setMaxAge(i5);

String str6 = in.nextLine();

int i6=Integer.parseInt(str6);

toy.setPrice(i6);

String str7 = in.nextLine();

int i7=Integer.parseInt(str7);

toy.setQuantity(i7);

String str8 = in.nextLine();

int i8=Integer.parseInt(str8);

toy.setRentalAmount(i8);
String str9 = in.nextLine();

int i9=Integer.parseInt(str9);

toy.setRefundableDeposit(i9);

System.out.println("ID: "+ toy.getToyId());

System.out.println("ToyName: "+ toy.getToyName());

System.out.println("ToyType: "+ toy.getToyType());

System.out.println("Min Age: "+ toy.getMinAge());

System.out.println("Max Age: "+ toy.getMaxAge());

System.out.println("Price: "+ toy.getPrice());

System.out.println("Quanitity: "+ toy.getQuantity());

System.out.println("RefundableAmount: "+ toy.getRentalAmount());

System.out.println("Refundable Deposit: "+ toy.getRefundableDeposit());

}
Question 7
Inheritance - Sub classes for Toy class- 4.9
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

1. Create classes like ElectronicToy , MusicalToy as a subclass of Toy and add


attributes such as numberOfBatteries , operatingMode (remote/manual) for
ElectronicToy and noOfSpeakers for Musical toy
2. create parameterized constructor to pass all the values for the object
3. get the input from the user 1 for ElectronicToy and 2 for Musical Toy
4. override the toString method and con-cat the Name , type , Mode , Batteries for
ElectronicToy , Name type and noOfSpeakers for Musical Toy

Sample Input
1
Remote Car
Electronic
4
12
500
20
100
500
5
Remote

Sample output

Name : Remote Car


Type : Electronic
Mode : Remote
Batteries : 5
Code:
import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

interface AdminService{

class AdminServiceImpl implements AdminService{

class Toy{

private int toyId;

private String toyName;

private String toyType;

private int minAge;

private int maxAge;

private double price;

private int quantity;

private double rentalAmount;

private double refundableDeposit;

Toy(int toyId,String toyName,String toyType,int minAge,int maxAge,double price,int


quantity,double rentalAmount,double refundableDeposit){

this.toyId=toyId;

this.toyName=toyName;

this.toyType=toyType;

this.minAge=minAge;

this.maxAge=maxAge;

this.price=price;

this.quantity=quantity;

this.rentalAmount=rentalAmount;

this.refundableDeposit=refundableDeposit;
}

public int getToyId(){

return toyId;

public void setToyId(int toyId){

this.toyId=toyId;

public String getToyName(){

return toyName;

public String getToyType(){

return toyType;

public int getMinAge(){

return minAge;

public int getMaxAge(){

return maxAge;

public double getPrice(){

return price;

public int getQuantity(){

return quantity;

public double getRentalAmount(){

return rentalAmount;

public double getRefundableDeposit(){

return refundableDeposit;

}
public void setToyName(String toyName){

this.toyName=toyName;

public void setToyType(String toyType){

this.toyType=toyType;

public void setMinAge(int minAge){

this.minAge=minAge;

public void setMaxAge(int maxAge){

this.maxAge=maxAge;

public void setPrice(double price){

this.price=price;

public void setQuantity(int quantity){

this.quantity=quantity;

public void setRentalAmount(double rentalAmount){

this.rentalAmount=rentalAmount;

public void setRefundableDeposit(double refundableDeposit){

this.refundableDeposit=refundableDeposit;

}
class ElectronicToy extends Toy{

public int numberOfBatteries;

public String operatingMode;

ElectronicToy(int toyId,String toyName,String toyType,int minAge,int maxAge,double price,int


quantity,double rentalAmount,double refundableDeposit,int numberOfBatteries,String
operatingMode){

super(toyId,toyName,toyType,minAge,maxAge,price,quantity,rentalAmount,refundableDeposit);

this.numberOfBatteries=numberOfBatteries;

this.operatingMode=operatingMode;

public String toString(){

return "ToyName: "+super.getToyName()+

"\nType: "+super.getToyType()+

"\nMode: "+operatingMode+

"\nBatteries: "+numberOfBatteries;

class MusicalToy extends Toy{

public int noOfSpeakers;

MusicalToy(int toyId,String toyName,String toyType,int minAge,int maxAge,double price,int


quantity,double rentalAmount,double refundableDeposit,int noOfSpeakers){

super(toyId,toyName,toyType,minAge,maxAge,price,quantity,rentalAmount,refundableDeposit);

this.noOfSpeakers=noOfSpeakers;

public String toString(){

return "ToyName: "+super.getToyName()+

"\nType: Musical "+super.getToyType()+"\nSpeaker: "+noOfSpeakers;

public class Source

{
String[][] toys=new String[5][5];

Source()

toys[0][0]="1";

toys[0][1]="Stickle Bricks";

toys[1][0]="2";

toys[1][1]="Robot Dog";

toys[2][0]="3";

toys[2][1]="Magic 8 Ball";

toys[3][0]="4";

toys[3][1]="Juggling Clubs";

toys[4][0]="5";

toys[4][1]="Chutes and Ladders";

/*public String getToy(int age)

}*/

public static void main( String[] args )

AdminService adminService=new AdminServiceImpl();

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

if(n==1){

String toyName=sc.nextLine();

String toyType=sc.nextLine();
sc.next();

int minAge=sc.nextInt();

int maxAge=sc.nextInt();

double price=sc.nextDouble();

int quantity=sc.nextInt();

double rentalAmount=sc.nextDouble();

double refundableDeposit=sc.nextDouble();

int numberOfBatteries=sc.nextInt();

String operatingMode=sc.next();

ElectronicToy e=new
ElectronicToy(1,toyName,toyType,minAge,maxAge,price,quantity,rentalAmount,refundableDeposit,
numberOfBatteries,operatingMode);

System.out.println(e.toString());

}else if(n==2){

String toyName=sc.next();

String toyType=sc.nextLine();

int minAge=sc.nextInt();

int maxAge=sc.nextInt();

double price=sc.nextDouble();

int quantity=sc.nextInt();

double rentalAmount=sc.nextDouble();

double refundableDeposit=sc.nextDouble();

int noOfSpeakers=sc.nextInt();

MusicalToy m=new
MusicalToy(1,toyName,toyType,minAge,maxAge,price,quantity,rentalAmount,refundableDeposit,no
OfSpeakers);

System.out.println(m.toString());

}
Question 4
Java-Customer and Admin Login 4-11,12
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

1. Provide login facility for admin and customer. After successful login, display
respective console dashboards (menu). Now user can perform his respective actions.

2 get the console input from the user 1 for Customer and 2 for Admin , Name and
password to validate the user based on the role functionalities should be display as
below.

Customers

1.Rent Toys

2.Rental Information for the logged in Customer.

Admin

1.Insert Toys

2.Update Toys

3.Delete Toys

4,Search Toys

Note:Create the classess as per the given structure in the


document

Sample Input
1
Krithick
xxxxxxxx
Sample output
1.Rent Toys
2.Rental Information

Sample Input
2
admin
xxxxxxxxxxxx
Sample output
1.Insert Toys
2.Update Toys
3.Delete Toys
4.Search Toys

Code:
import java.util.Scanner;

interface AdminService{

boolean validateAdmin(String email, String password);

interface CustomerService{

boolean validateCustomer(String email, String password);

class Customer

private int customerId;

private String customerName;

private String email;

private String password;

private String address;

public Customer(int customerId, String customerName, String email, String password, String
address){

this.customerId = customerId;

this.customerName = customerName;
this.password = password;

this.address = address;

this.email = email;

public String getPassword(){

return password;

public String getEmail(){

return email;

class Admin

private String name;

private String email;

private String password;

public Admin(String name,String email, String password) {

this.password = password;

this.name = name;

this.email = email;

public String getPassword(){

return password;

public String getEmail(){

return email;

class AdminServiceImpl extends CustomerServiceImpl implements AdminService

public static Admin[] adminArray=new Admin[5];


AdminServiceImpl()

adminArray[0]=new Admin("Krithick","[email protected]","krithi");

adminArray[1]=new Admin("Rajan","[email protected]","rajan#345");

adminArray[2]=new Admin("Chandrav","[email protected]","wel$234");

adminArray[3]=new Admin("Ankit","[email protected]","kit@56");

adminArray[4]=new Admin("Akilan","[email protected]","ak*76");

public boolean validateAdmin(String email,String password)

for(Admin a:adminArray){

if(a.getEmail().equals(email) && a.getPassword().equals(password))

return true;

return false;

class CustomerServiceImpl implements CustomerService

public static Customer[] customerArray=new Customer[5];

public CustomerServiceImpl()

customerArray[0]=new Customer(100, "Karthi","[email protected]","kar#2","Bangalore");

customerArray[1]=new Customer(101, "Kumar","[email protected]","km#2","Bangalore");

customerArray[2]= new Customer(102, "Rakesh","[email protected]","rak#45","Chennai");

customerArray[3]=new Customer(103, "Rakshan","[email protected]","an#2","Mumbai");

customerArray[4]=new Customer(104, "Virat","[email protected]","at#45","Pune");


}

public boolean validateCustomer(String email,String password)

for(Customer c:customerArray){

if(c.getEmail().equals(email) && c.getPassword().equals(password))

return true;

return false;

public class Source {

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

if(n==1){

String email = sc.next();

String password = sc.next();

CustomerServiceImpl cs = new CustomerServiceImpl();

boolean flag = cs.validateCustomer(email, password);

if(flag)

System.out.println("1.Rent Toys");

System.out.println("2.Rental Information for the logged in Customer.");

else if(n==2){

String email = sc.next();


String password = sc.next();

AdminService as = new AdminServiceImpl();

boolean flag = as.validateAdmin(email, password);

if(flag)

System.out.println("1.Insert Toys"+"\n2.Update Toys"+"\n3.Delete Toys"+"\n4.Search


Toys");

}
Question 2
Java- Customer and Address class - 4.2
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

1. Create Customer and Address class as per the given structure with setter and
getter methods and create the object for Customer and Address object use the setter
methods to pass the value to the attributes and print the information.

Note:Customer and Address should have has a relationship between the classes

2 Create a constructor for Customer and Address with the attributes as parameter.
create the objects using parameterized constructor in the main method of the Source
class.

3.Override toString method and it should return the concatenated string of the class
attributes

Sample Input
100
Chandrav
Jai@123
[email protected]
Chennai
TamilNadu
600045
India
Sample output

100 Chandrav Jai@123 [email protected]


Chennai TamilNadu 600045 India
Code:
import java.util.*; class Customer { private int customerId; private String customerName; private
String password; private Address address; private String email; public Customer() { } @Override
public String toString() { String result = customerId + " "; result += customerName + " "; result +=
password + " "; result += email + "\n"; result += address; return result; } public Customer(int
customerId, String customerName, String password, Address address, String email) { this.customerId
= customerId; this.customerName = customerName; this.password = password; this.address =
address; this.email = email; } public int getCustomerId() { return customerId; } public void
setCustomerId(int customerId) { this.customerId = customerId; } public String getCustomerName() {
return customerName; } public void setCustomerName(String customerName) { this.customerName
= customerName; } public String getPassword() { return password; } public void setPassword(String
password) { this.password = password; } public String getpassword() { return password; } public void
setpassword(String password) { this.password = password; } public Address getAddress() { return
address; } public void setAddress(Address address) { this.address = address; } public String getEmail()
{ return email; } public void setEmail(String email) { this.email = email; } } class Address { private
String city; private String state; private int zip; private String country; public Address() { } public
Address(String city, String state, int zip, String country) { this.city = city; this.state = state; this.zip =
zip; this.country = country; } public String getCity() { return city; } public void setCity(String city) {
this.city = city; } public String getState() { return state; } public void setState(String state) { this.state =
state; } public int getZip() { return zip; } public void setZip(int zip) { this.zip = zip; } public String
getCountry() { return country; } public void setCountry(String country) { this.country = country; }
@Override public String toString() { String result = city + " "; result += state + " "; result += zip + " ";
result += country; return result; } } public class Source { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); int customerId = Integer.parseInt(scanner.next()); String
customerName = scanner.next(); String password = scanner.next(); String email = scanner.next();
String city = scanner.next(); String state = scanner.next(); int zip = Integer.parseInt(scanner.next());
String country = scanner.next(); Address address = new Address(city, state, zip, country); Customer
customer = new Customer(customerId, customerName, password, address, email);
System.out.println(customer); }}
Question 5
Java- Customer -Service 4.7 8
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

1. Create/Modify an interface CustomerService and add methods such as


rent(int toyId) and display(). Please note that the purpose of rent method will
be to add the details of rented toys and the displaymethod should display the
toys rented by the customer.

2. Create/Modify CustomerServiceImpl class with the static array


customerToysRentalInfo and availableToys array. provide implementation to
CustomerService (override all the methods listed in interface).(you should use
the same Customer class with all the attributes as mentioned in the given
structure)assuming the program is accessed by the customer.

 This program should display all the toys to the user and user can rent the toy
by entering the toyid from console
 The display method display the rented toy by the customer as below

Sample Input
120
Sample output
Toy Name: Rubber Ducky
Toy Type: Toy
Quantity: 200
RentalAmount: 20
Code:
import java.util.Scanner;

interface CustomerService

void rent(int toyId);

void display();

class Toy{

int toyId;

String toyName;

String toyType;

int minAge;

int maxAge;

double price;

int rentalAmt;

int refundableDeposit;

int quantity;

public Toy(int toyId, String toyName, String toyType, int minAge, int maxAge, double price, int
refundableDeposit,int quantity, int rentalAmt){

this.toyId=toyId;

this.toyName=toyName;

this.toyType=toyType;

this.minAge=minAge;

this.maxAge=maxAge;

this.price=price;

this.rentalAmt=rentalAmt;

this.refundableDeposit=refundableDeposit;

this.quantity=quantity;
}

public int getToyId(){

return toyId;

public String getToyName(){

return toyName;

public String getToyType(){

return toyType;

public int getQuantity(){

return quantity;

public int getRentalAmount(){

return rentalAmt;

class CustomerServiceImpl implements CustomerService

public static Toy availableToys[]=new Toy[4];

public Toy customerToysRentalInfo[]=new Toy[2];

CustomerServiceImpl()

availableToys[0]=new Toy(120,"Rubber Ducky","Toy",1,3,200,20,200,20);

availableToys[1]=new Toy(130,"Car","Toy",1,5,100,30,20,100);

availableToys[2]=new Toy(150,"Kite","Toy",3,8,100,50,20,100);

availableToys[3]=new Toy(180,"Airplane","Toy",4,7,500,30,50,20);

public void rent(int toyId)

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

if(availableToys[i].getToyId()==toyId){

System.out.println("Toy Name: "+availableToys[i].getToyName());

System.out.println("Toy Type: "+availableToys[i].getToyType());

System.out.println("Quantity: "+availableToys[i].getQuantity());

System.out.print("RentalAmount: "+availableToys[i].getRentalAmount());

break;

public void display()

public class Source {

public static void main( String[] args )

Scanner sc=new Scanner(System.in);

int toyId=sc.nextInt();

CustomerService cs=new CustomerServiceImpl();

cs.rent(toyId);

}
Question 3
Java- Admin -Service 4.6 7
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

1. Create an interface AdminService. Add CRUD methods such as


addToy(Toy toy) , updateToy(Toy toy), deleteToy(int toyId) and getToys() (will
return array of toys) . These methods will be for adding new toys, modifying
existing toys, deleting toys, retrieving the details of all the toys (you should use
the same Toy class with all the attributes as mentioned in the given structure)

2. AdminsServiceImpl class with the static array named toyArray and


provide implementation AdminService interfaces (override all the methods
listed in interface).

3,Override toString method which will return the concatenated String


(toyname,type,price and rentalAmount)

4.user must be asked to give their choice to perform different operations as


below

 1 . Add Toys
 2. UpdateToys
 3 Delete Toys
 4 Display Toys

Note:Below is the sample input for adding a toy to a toyArray and the output is
a toy object since only one object is added hence the output is for a single toy
. when multiple toys are added the getToys method should return all the toys

Sample Input
120
Rubber Ducky
Toy
1
3
200
20
20
200
30

Sample output
Toy Name: Rubber Ducky
Toy Type: Toy
Quantity: 200
RentalAmount: 20

Code:
import java.util.*;

interface AdminService{

void addToy(Toy toy);

void updateToy(Toy toy);

void deleteToy(Toy toy);

Toy[] getToys();

class AdminServiceImpl implements AdminService{

public static Toy[] toyArray=new Toy[5];

public static int count=0;

@Override

public void addToy(Toy toy) {

toyArray[count]=toy;

count++;

public void updateToy(Toy toy) {


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

if(toyArray[i].getToyId()==toy.getToyId()) {

toyArray[i]=toy;

break;

public void deleteToy(Toy toy) {

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

if(toyArray[i].getToyId()==toy.getToyId()) {

toyArray[i]=null;

break;

public Toy[] getToys() {

return toyArray;

class Toy{

private int toyId,minAge,maxAge,quantity;

private String toyName,toyType;

private double price,rentalAmount,refundableDeposit;

Toy(int id,String name,String type,int min,int max,double price,int quant,double rent,double refund){

this.toyId=id;

this.toyName=name;

this.toyType=type;

this.minAge=min;
this.maxAge=max;

this.price=price;

this.quantity=quant;

this.rentalAmount=rent;

this.refundableDeposit=refund;

public int getToyId() {

return toyId;

public void setToyId(int toyId) {

this.toyId = toyId;

public int getMinAge() {

return minAge;

public void setMinAge(int minAge) {

this.minAge = minAge;

public int getMaxAge() {

return maxAge;

public void setMaxAge(int maxAge) {

this.maxAge = maxAge;

}
public int getQuantity() {

return quantity;

public void setQuantity(int quantity) {

this.quantity = quantity;

public String getToyName() {

return toyName;

public void setToyName(String toyName) {

this.toyName = toyName;

public String getToyType() {

return toyType;

public void setToyType(String toyType) {

this.toyType = toyType;

public double getPrice() {

return price;

public void setPrice(double price) {

this.price = price;

}
public double getRentalAmount() {

return rentalAmount;

public void setRentalAmount(double rentalAmount) {

this.rentalAmount = rentalAmount;

public double getRefundableDeposit() {

return refundableDeposit;

public void setRefundableDeposit(double refundableDeposit) {

this.refundableDeposit = refundableDeposit;

public String toString() {

return("Toy Name: "+this.toyName+"\nToy Type: "+this.toyType+"\nQuantity:


"+this.quantity+"\nRentalAmount: "+this.rentalAmount);

class Source{

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

int choice=s.nextInt();

int id,min,max,quantity;

String name,type;

double price,rent,refund;
Toy[] toys;

switch(choice) {

case 1:

if(s.hasNext()){

id=s.nextInt();

name = s.next();

type=s.next();

min=s.nextInt();

max=s.nextInt();

price=s.nextDouble();

quantity=s.nextInt();

rent=s.nextDouble();

refund=s.nextDouble();

Toy t= new Toy(id,name,type,min,max,price,quantity,rent,refund);

AdminService adm = new AdminServiceImpl();

adm.addToy(t);

toys=adm.getToys();

for(int i=0;i<toys.length;i++) {

System.out.println(adm.toString());

break;

case 2:

id=s.nextInt();

name = s.next();

type=s.next();

min=s.nextInt();

max=s.nextInt();

price=s.nextDouble();

quantity=s.nextInt();

rent=s.nextDouble();
refund=s.nextDouble();

Toy t1= new Toy(id,name,type,min,max,price,quantity,rent,refund);

AdminService adm1 = new AdminServiceImpl();

adm1.updateToy(t1);

System.out.println(adm1.toString());

break;

case 3:

id=s.nextInt();

name = s.next();

type=s.next();

min=s.nextInt();

max=s.nextInt();

price=s.nextDouble();

quantity=s.nextInt();

rent=s.nextDouble();

refund=s.nextDouble();

Toy t2= new Toy(id,name,type,min,max,price,quantity,rent,refund);

AdminService adm2 = new AdminServiceImpl();

adm2.deleteToy(t2);

break;

case 4:

AdminService adm3 = new AdminServiceImpl();

toys=adm3.getToys();

for(int i=0;i<toys.length;i++) {

System.out.println(adm3.toString());

s.close();

}
Question 6
Java- Search Customer and Toy 4.10
bookmark_border
 subject Coding
 casino 100 points

DESCRIPTION

1. Search method should be overloaded with different parameters id and


name for both CustomerService and AdminService to get the customer/toy
Object

2.override toString method to and return the concatenated values such as


name of the custmerId/toyId and customerName/toyName

3 get the console input from the user 1.for search customer 2 for search toys

and return the object.

Sample Input
2
120
Sample output
Id: 120
Toy Name: Rubber Ducky
Sample Input
1
100
Sample Output
Id: 100
Name: Chandrav
Code:
import java.util.Scanner;

interface AdminService {

public Toy searchToy(int id);

interface CustomerService {

public Customer searchCustomer(int id);

class Customer {

private int customerId;

private String customerName;

private String email;

private String password;

private String address;

public Customer() {

public Customer(int customerId, String customerName, String email, String password, String
address) {

super();

this.customerId = customerId;

this.customerName = customerName;

this.email = email;

this.password = password;

this.address = address;
}

public int getCustomerId() {

return this.customerId;

public String getCustomerName() {

return this.customerName;

public String toString() {

return "Id: " + this.customerId + "\nName: " + this.customerName;

class Toy {

private int toyId;

private String toyName;

private String toyType;

private int minAge;

private int maxAge;

private double price;

private int quantity;

private int rentalAmt;

private int refundableDeposit;

public String toString;

public Toy() {

}
public Toy(int toyId, String toyName, String toyType, int minAge, int maxAge, double price, int
rentalAmt,

int refundableDeposit, int quantity) {

this.toyId = toyId;

this.toyName = toyName;

this.toyType = toyType;

this.minAge = minAge;

this.maxAge = maxAge;

this.price = price;

this.rentalAmt = rentalAmt;

this.refundableDeposit = refundableDeposit;

this.quantity = quantity;

this.toString = "Id: " + this.toyId + "\nToyName: " + this.toyName;

public int getToyId() {

return this.toyId;

public String getToyName() {

return this.toyName;

public String toString() {

return "Id: " + this.toyId + "\nToyName: " + this.toyName;

class CustomerServiceImpl extends Customer implements CustomerService {

public static Customer[] customerArray = new Customer[5];


public CustomerServiceImpl() {

customerArray[0] = new Customer(100, "Krithick","[email protected]","krithi","Bangalore");

customerArray[1] = new Customer(101, "Rajan","[email protected]","rajan#345","Bangalore");

customerArray[2] = new Customer(102, "Chandrav","[email protected]","wel$234","Chennai");

customerArray[3] = new Customer(103, "Ankit","[email protected]","kit@56","Mumbai");

customerArray[4] = new Customer(104, "Akilan","[email protected]","ak*76","Pune");

public Customer searchCustomer(int id) {

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

if (customerArray[i].getCustomerId() == id) {

return customerArray[i];

return null;

class AdminServiceImpl implements AdminService {

static Toy availableToys[] = new Toy[5];

Toy toy = new Toy();

AdminServiceImpl() {

availableToys[0] = new Toy(120, "Rubber Ducky", "Toy", 1, 3, 200, 20, 20, 200);

availableToys[1] = new Toy(130, "Car", "Toy", 1, 5, 100, 30, 20, 100);

availableToys[2] = new Toy(150, "Kite", "Toy", 3, 8, 100, 50, 20, 50);

availableToys[3] = new Toy(180, "Airplane", "Toy", 4, 7, 500, 30, 50, 20);

public Toy searchToy(int id) {


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

if (availableToys[i].getToyId() == id) {

return availableToys[i];

return null;

public class Source {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int ch=sc.nextInt();

int id=sc.nextInt();

sc.close();

switch(ch) {

case 1: CustomerService customerService=new CustomerServiceImpl();

Customer customer=customerService.searchCustomer(id);

System.out.println(customerService.toString());

break;

case 2: AdminService adminService=new AdminServiceImpl();

Toy toy=adminService.searchToy(id);

System.out.println(toy.toString());

break;

default: System.out.println("Invalid Choice");

You might also like