0% found this document useful (0 votes)
103 views14 pages

Practice PDF

This document defines classes for renting different types of properties (buildings, houses, shops) and calculating rental amounts. It includes: - A BuildingForRent class that stores building dimensions and calculates a base rental amount based on dimensions. - HouseForRent and ShopForRent subclasses that extend BuildingForRent and add additional fields (facilities, bedrooms, shop type) to calculate total rental amounts. - Methods to validate input, calculate totals by summing base amounts and additional fees. - A Tester class with a main method that creates instances and tests the rental amount calculations.

Uploaded by

Sakshi Wankhade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views14 pages

Practice PDF

This document defines classes for renting different types of properties (buildings, houses, shops) and calculating rental amounts. It includes: - A BuildingForRent class that stores building dimensions and calculates a base rental amount based on dimensions. - HouseForRent and ShopForRent subclasses that extend BuildingForRent and add additional fields (facilities, bedrooms, shop type) to calculate total rental amounts. - Methods to validate input, calculate totals by summing base amounts and additional fees. - A Tester class with a main method that creates instances and tests the rental amount calculations.

Uploaded by

Sakshi Wankhade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

import java.util.

Arrays;

class BuildingForRent {
private int buildingDimensions;
private float advanceAmount;

public BuildingForRent(int buildingDimensions){


this.buildingDimensions=buildingDimensions;
}

public float getAdvanceAmount(){


return this.advanceAmount;
}

public void setAdvanceAmount(float advanceAmount){


this.advanceAmount = advanceAmount;
}

public int getBuildingDimensions(){


return this.buildingDimensions;
}

public void calculateAdvanceAmount(){


float advanceAmount = -1.0f;
if(this.buildingDimensions>0){
advanceAmount=this.buildingDimensions*10;
}
this.advanceAmount=advanceAmount;
}

@Override
public String toString() {
return "BuildingForRent (buildingDimensions=" + this.buildingDimensions
+ ")";
}

class HouseForRent extends BuildingForRent{

private static String[][] facilitiesDetailsArr = {{"Security","Parking","Amusement-


Park"},{"2000","3400","2900"}};
private String[] facilities;
private int noOfBedrooms;

public HouseForRent(int buildingDimensions,int noOfBedrooms,String[] facilities){


super(buildingDimensions);
this.noOfBedrooms=noOfBedrooms;
this.facilities = facilities;
}

public int getNoOfBedrooms(){


return this.noOfBedrooms;
}

public String[] getFacilities(){


return this.facilities;
}

//To_Trainee
public Boolean validateNoOfBedrooms(){
//write your code here
if(this.noOfBedrooms>0 && this.noOfBedrooms<6 ){
return true;
}
//change return statement accordingly
return false;
}

//To_Trainee
@Override
public void calculateAdvanceAmount(){
//write your code here
super.calculateAdvanceAmount();
float totalcharge=0.0f;
float basicAmount=super.getAdvanceAmount();
if(basicAmount==-1.0f || validateNoOfBedrooms()==false || facilities ==null){
super.setAdvanceAmount(-1);
}
else{
for(String f:facilities){
for(int i=0;i<=facilitiesDetailsArr[0].length-1;i++){
if(facilitiesDetailsArr[0][i].equalsIgnoreCase(f)){
totalcharge+=Float.parseFloat(facilitiesDetailsArr[1][i]);
}
else{
super.setAdvanceAmount(-1);
}
}
}
if (totalcharge != 0.0f) {
super.setAdvanceAmount(basicAmount+totalcharge+(noOfBedrooms*20000));
}
}
}

@Override
public String toString() {
return "HouseForRent (BuildingForRent (buildingDimensions=" + this.getBuildingDimensions()
+ ")facilities=" + Arrays.toString(this.facilities)
+ ", noOfBedrooms=" + this.noOfBedrooms + ")";
}
}

class ShopForRent extends BuildingForRent{


char shopType;

public ShopForRent(int buildingDimensions,char shopType){


super(buildingDimensions);
this.shopType=shopType;
}

//To_Trainee
public int identifyShopRent(){
int shopRent = -1;

if(this.shopType=='A' ||this.shopType=='a' ){
shopRent=45000;
}
if(this.shopType=='B' ||this.shopType=='b' ){
shopRent=30000;
}
if(this.shopType=='C' ||this.shopType=='c' ){
shopRent=25000;
}
return shopRent;

//To_Trainee
@Override
public void calculateAdvanceAmount(){
//write your code here
super.calculateAdvanceAmount();
float basicsalary=super.getAdvanceAmount();
float x=(float)this.identifyShopRent();
if(basicsalary==-1.0f|| x==-1.0f){
super.setAdvanceAmount(-1);
}
else{

super.setAdvanceAmount(basicsalary+x);
}
}

@Override
public String toString() {
return "ShopForRent (BuildingForRent (buildingDimensions=" + this.getBuildingDimensions()
+ ")shopType=" + this.shopType + ")";
}
}

class Tester{
public static void main(String[] args){
String[] facilities = {"PArking","SecUrity"};
HouseForRent obj1 = new HouseForRent(2000,5,facilities);
obj1.calculateAdvanceAmount();
System.out.println("House Rent Amount:" + obj1.getAdvanceAmount());

ShopForRent obj2 = new ShopForRent(10000,'A');


obj2.calculateAdvanceAmount();
System.out.println("Shop Rent Amount:" +obj2.getAdvanceAmount());
}
}
PP 3
class Mart {
public static String [] itemNameArr= {"Chocolate","Perfume","Bouquet","Apparel"};
public static int [] itemPriceArr= {200,400,150,300};
public static int [] itemQuantityArr= {10,20,30,40};

public float findPricePerItem(String itemName) {


float priceItem = -1.0f;
for(int index=0; index<Mart.itemNameArr.length;index++) {
if(itemName.equals(Mart.itemNameArr[index])) {
priceItem=Mart.itemPriceArr[index];
}
}
return priceItem;
}
}
class OnlineMart extends Mart{
private int onlineDiscountPercentage;
private Order order;

public OnlineMart(Order order){


this.order=order;
}

public Order getOrder(){


return this.order;
}

public void identifyOnlineDiscount() {


if (this.order.getPaymentMode().equals("Prepaid")){
this.onlineDiscountPercentage=5;
}else if (this.order.getPaymentMode().equals("COD")) {
this.onlineDiscountPercentage=2;
}
else {
this.onlineDiscountPercentage=-1;
}
}

//To_Trainee
@Override
public float findPricePerItem(String itemName) {
float pricePerItem = 0.0f;
//write your code here
float r=super.findPricePerItem(itemName);
if(r==-1.0f){
pricePerItem=-1.0f;
}
else{
this.identifyOnlineDiscount();
if(onlineDiscountPercentage==-1){
pricePerItem=-1.0f;
}
else{
pricePerItem=r-r*onlineDiscountPercentage/100;
}
}
return pricePerItem;
}

@Override
public String toString() {
return "OnlineMart (Order ( itemName=" + this.order.getItemName()
+ ", quantityRequired=" + this.order.getQuantityRequired() + ", paymentMode="
+ this.order.getPaymentMode() + "))";
}

//To_Trainee
public int checkItemAvailability() {
//write your code here
for(int i = 0; i <= Mart.itemNameArr.length - 1;i++) {
if(Mart.itemNameArr[i].equals(this.order.getItemName())) {
if (Mart.itemQuantityArr[i] >= getOrder().getQuantityRequired()) {
Mart.itemQuantityArr[i] = Mart.itemQuantityArr[i] - this.order.getQuantityRequired();//10
return getOrder().getQuantityRequired();
}
}
}
return -1;
}
//To_Trainee
public void shipOrder() {
//write your code here
int ca=checkItemAvailability();
float fn=findPricePerItem(getOrder().getItemName());
if(ca==-1 ||fn==-1.0f){
getOrder().setOrderPrice(-1.0);
getOrder().setTrackingId("NA");
}
else{
double c=ca* fn;
getOrder().setOrderPrice(c);
getOrder().generateTrackingId();
}
}
}
class Order extends Mart{
private static int counter=1000;
private String trackingId;
private String itemName;
private int quantityRequired;
private String paymentMode;
private double orderPrice;

public Order(String itemName, int quantityRequired, String paymentMode){


this.itemName = itemName;
this.quantityRequired = quantityRequired;
this.paymentMode = paymentMode;
}

public void setTrackingId(String trackingId) {


this.trackingId = trackingId;
}

public String getTrackingId() {


return this.trackingId;
}
public String getItemName(){
return this.itemName;
}
public int getQuantityRequired(){
return this.quantityRequired;
}

@Override
public String toString() {
return "Order (trackingId=" + this.trackingId + ", itemName=" + this.itemName
+ ", quantityRequired=" + this.quantityRequired + ", paymentMode="
+ this.paymentMode + ", orderPrice=" + this.orderPrice + ")";
}

public String getPaymentMode(){


return this.paymentMode;
}

public double getOrderPrice(){


return this.orderPrice;
}
public void setOrderPrice(double orderPrice){
this.orderPrice=orderPrice;
}

//To_Trainee
public void generateTrackingId(){
//write your code here
this.trackingId="TR"+counter++;
}
}
class Tester {
public static void main(String[] args) {
Order orderObj = new Order("Chocolate", 4, "COD");
OnlineMart onlineMartObject = new OnlineMart(orderObj);
onlineMartObject.shipOrder();
System.out.println("Tracking ID :" + onlineMartObject.getOrder().getTrackingId());
System.out.println("Order Price :" + onlineMartObject.getOrder().getOrderPrice());
}
}

pp 2 Vehicle
abstract class VehicleRental {
private Customer customer;
private int noOfKms;
private int journeyDays;
public VehicleRental(Customer customer, int noOfKms) {
this.customer = customer;
this.noOfKms = noOfKms;
this.journeyDays = 0;
}
public int getJourneyDays() {
return this.journeyDays;
}
public Customer getCustomer() {
return this.customer;
}
public int identifyJourneyDays() {
this.journeyDays = this.noOfKms/300;
return this.noOfKms % 300;
}
public abstract double calculateFinalAmount();
}
class Customer {
public static String[] memberCustIdArr = {"1001P", "1051R", "1072P", "2019R", "2913R", "2931P"};
public static int[] memberBillAmountArr = {2050, 5345, 6896, 9100, 4500, 3234};
private String custId;
private String custName;
public Customer(String custId, String custName) {
this.custId = custId;
this.custName = custName;
}
public String getCustId() {
return this.custId;
}
public String getCustName() {
return this.custName;
}
//To_Trainee
public float calculateDiscount(float rentalAmount) {
float discountAmount = -1.0f;
for(int i=0;i<=memberCustIdArr.length-1;i++){
if(memberCustIdArr[i].equals(this.custId)){
this.upgradeCustomer(rentalAmount);
char lc=this.custId.charAt(this.custId.length()-1);
if(lc=='P'){
discountAmount=(15.0f/100.0f)*rentalAmount;
}
else if(lc=='R'){
discountAmount=(10.0f/100.0f)*rentalAmount ;
}
else{
discountAmount=-1.0f;
}
}
}
return discountAmount;
}
public void upgradeCustomer(float rentalAmount) {
if (rentalAmount >= 15000) {
int index = 0;
for (int ind=0; ind<Customer.memberCustIdArr.length; ind++) {
if (Customer.memberCustIdArr[ind].equals(this.custId)) {
index = ind;
break;
}
}
this.custId = this.custId.substring(0,4)+"P";
Customer.memberCustIdArr[index] = this.custId;
}
}
}
class CarRental extends VehicleRental {
private String carType;
public CarRental(Customer customer, int noOfKms, String carType) {
super(customer, noOfKms);
this.carType = carType;
}
//To_Trainee
public double calculateFinalAmount() {
double finalAmount = 0.0;
int rentPerDay=CarDetails.identifyPerDayRent(carType);
int excessKms=super.identifyJourneyDays();
if(rentPerDay==-1 || excessKms<0 || super.getJourneyDays()<0){
finalAmount=-1.0;
}
else{
float rentalAmount=(super.getJourneyDays()*rentPerDay);
int excessKmsAmount=excessKms*12;
rentalAmount=rentalAmount+excessKmsAmount;
float discountAmount= getCustomer().calculateDiscount(rentalAmount);
if(discountAmount==-1.0f){
finalAmount=-1.0;
}
else{
finalAmount=(rentalAmount-discountAmount);
for(int i=0;i<=getCustomer().memberCustIdArr.length-1;i++){
if(getCustomer().memberCustIdArr[i].equals(getCustomer().getCustId())){
getCustomer().memberBillAmountArr[i]+=finalAmount;
}
}
}
}
return finalAmount;
}
}
class CarDetails {
private static String[] carTypesArr = {"Hatch-back","Sedan","SUV"};
private static int[] perDayRentsArr = {3500,5000,6000};
//To_Trainee
public static int identifyPerDayRent(String carType) {
int rentPerDay = 0;
for(int i=0;i<=carTypesArr.length-1;i++){
if (carTypesArr[i].equalsIgnoreCase(carType)){
rentPerDay=perDayRentsArr[i];
return rentPerDay;
}
else{
rentPerDay=-1;
}
}
return rentPerDay;
}
}
class Tester {
public static void main(String[] args) {
CarDetails c1=new CarDetails();
Customer custObj = new Customer("1051R","Wilch");
CarRental carRentalObj = new CarRental(custObj,10,"SUV");
for (int ind=0; ind<Customer.memberCustIdArr.length; ind++) {
System.out.print(Customer.memberCustIdArr[ind]+":"+Customer.memberBillAmountArr[ind]+" ");
}
System.out.println();
double finalAmount = carRentalObj.calculateFinalAmount();
System.out.println("Final bill amount: "+finalAmount);
for (int ind=0; ind<Customer.memberCustIdArr.length; ind++) {
System.out.print(Customer.memberCustIdArr[ind]+":"+Customer.memberBillAmountArr[ind]+" ");
}
}
}
PP 4
class Player {
private int noOfMatches;
private int pointsEarned;

public Player(int noOfMatches){


this.noOfMatches=noOfMatches;
}

public int getPointsEarned(){


return this.pointsEarned;
}

public void setPointsEarned(int pointsEarned){


this.pointsEarned = pointsEarned;
}

public int getNoOfMatches(){


return this.noOfMatches;
}

//To_Trainees
public Boolean validateNoOfMatches(){
//write your logic here
if(noOfMatches>0 && noOfMatches<=100){
return true;
}
//change return statement accordingly
return false;
}

@Override
public String toString() {
return "Player (noOfMatches=" + this.noOfMatches + ")";
}
}

class Batsman extends Player {


private int runsScored;
private int centuryCount;
private int batsmanRank;

public Batsman(int noOfMatches,int runsScored,int centuryCount){


super(noOfMatches);
this.runsScored = runsScored;
this.centuryCount = centuryCount;
}
public int getRunsScored(){
return this.runsScored;
}

public int getCenturyCount(){


return this.centuryCount;
}

public int getBatsmanRank(){


return this.batsmanRank;
}

@Override
public String toString() {
return "Batsman (Player (noOfMatches=" + this.getNoOfMatches() + ")+runsScored=" + this.runsScored + ",
centuryCount="
+ this.centuryCount + ")";
}

//To_Trainee
public Boolean validateBatsmanRecord(){
//write your logic here
if(centuryCount>=0 && centuryCount<=getNoOfMatches() && centuryCount*100<=runsScored){
return true;
}
//change return statement accordingly
return false;
}

//To_Trainees
public void calculatePoints(int index){
//write your logic here
if(validateNoOfMatches()&& validateBatsmanRecord()){
int pointsforruns=2*runsScored;
int pointsforcentury=25*centuryCount;
setPointsEarned(pointsforcentury+pointsforruns);
int oo=PlayerDetails.rankPlayer(getPointsEarned(),index);
batsmanRank=oo;
}
else{
setPointsEarned(-1);
batsmanRank=-1;
}
}
}

class PlayerDetails {
static int [] playersPointsArr={1000,934,800,550};

public static void swap(int[] numbers, int firstIndex, int secondIndex) {


int temp = numbers[firstIndex];
numbers[firstIndex] = numbers[secondIndex];
numbers[secondIndex] = temp;
}

public static int[] sort(int[] pointsArr){


for(int index2=0;index2<PlayerDetails.playersPointsArr.length;index2++) {
boolean swapped = false;
for(int index3=0;index3<(PlayerDetails.playersPointsArr.length- index2 - 1);index3++) {
if (PlayerDetails.playersPointsArr[index3] < PlayerDetails.playersPointsArr[index3 + 1]) {
swap(PlayerDetails.playersPointsArr, index3, index3 + 1);
swapped = true;
}
}
if (swapped == false)
break;
}
return pointsArr;
}
public static Integer rankPlayer(int pointsEarned, int index){
int playerRank=-1;
playersPointsArr[index]=pointsEarned;
sort(playersPointsArr);
for(int i=0;i<=playersPointsArr.length-1;i++){
if(pointsEarned==playersPointsArr[i]){
playerRank=i;
break;
}
}
return playerRank+1;
}
}
class Tester {
public static void main(String[] args){
Batsman obj1 = new Batsman(10, 420, 3);
obj1.calculatePoints(0);

System.out.println("Batsman Points:" + obj1.getPointsEarned());


System.out.println("Batsman Rank:"+ obj1.getBatsmanRank());
System.out.println("Player Points Array:");
for(int index=0; index < PlayerDetails.playersPointsArr.length;index++){
System.out.print(PlayerDetails.playersPointsArr[index]+" ");
}

}
}

You might also like