Practice PDF
Practice PDF
Arrays;
class BuildingForRent {
private int buildingDimensions;
private float advanceAmount;
@Override
public String toString() {
return "BuildingForRent (buildingDimensions=" + this.buildingDimensions
+ ")";
}
//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 + ")";
}
}
//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());
//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;
@Override
public String toString() {
return "Order (trackingId=" + this.trackingId + ", itemName=" + this.itemName
+ ", quantityRequired=" + this.quantityRequired + ", paymentMode="
+ this.paymentMode + ", orderPrice=" + this.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;
//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 + ")";
}
}
@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};
}
}