[go: up one dir, main page]

0% found this document useful (0 votes)
94 views7 pages

Codejava107: 1 .Banking Scenario, Currentaccount & Savingsaccount

The document contains code for 3 Java programs that demonstrate different object oriented programming concepts: 1. Banking classes that model current and savings accounts and calculate interest. 2. Item classes that model different item types (wooden, electronics) and calculate total amounts. 3. A Stall class that overloads methods to compute rental costs based on stall type and additional parameters like TVs.

Uploaded by

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

Codejava107: 1 .Banking Scenario, Currentaccount & Savingsaccount

The document contains code for 3 Java programs that demonstrate different object oriented programming concepts: 1. Banking classes that model current and savings accounts and calculate interest. 2. Item classes that model different item types (wooden, electronics) and calculate total amounts. 3. A Stall class that overloads methods to compute rental costs based on stall type and additional parameters like TVs.

Uploaded by

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

CODEJAVA107

1 .Banking Scenario, CurrentAccount & SavingsAccount

import java.util.Scanner;

import java.util.Date;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

abstract class Account{

String name;

String number;

int balance;

Date startDate;

Account(String name, String no, int bal, Date date){

this.name=name;

this.number=no;

this.balance=bal;

this.startDate=date;

abstract double calculateInterest(Date dueDate);

public int monthsDifference(Date startDate, Date endDate){

Calendar c1 = new GregorianCalendar();

c1.setTime(startDate);

Calendar c2 = new GregorianCalendar();

c2.setTime(endDate);

int ans = (c2.get(c2.YEAR) - c1.get(c1.YEAR))*12;

ans += c2.get(c2.MONTH)-c1.get(c1.MONTH);
CODEJAVA107

return ans;

class CurrentAccount extends Account{

CurrentAccount(String nam,String no,int bal,Date date){

super(nam,no,bal,date);

double calculateInterest(Date duedate){

return balance*(0.05/12)*monthsDifference(startDate,duedate);

class SavingsAccount extends Account{

SavingsAccount(String nam,String no,int bal,Date date){

super(nam,no,bal,date);

double calculateInterest(Date duedate){

return balance*(0.12/12)*monthsDifference(startDate,duedate);

class Codejava107{

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

String name=sc.next();

String accno=sc.next();

int amount=sc.nextInt();
CODEJAVA107

String strt=sc.next();

String end=sc.next();

Account a;

if(n==1){

try{

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

Date startDate=df.parse(strt);

Date endDate=df.parse(end);

a=new SavingsAccount(name,accno,amount,startDate);

System.out.print(a.calculateInterest(endDate));

catch (Exception ex ){

System.out.println(ex);

else if(n==2){

try{

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

Date strtdate=df.parse(strt);

Date enddate=df.parse(end);

a=new CurrentAccount(name,accno,amount,strtdate);

System.out.print(a.calculateInterest(enddate));

catch (Exception ex ){

System.out.println(ex);

}}
CODEJAVA107

2 .Item details-Abstract class, ItemType

//ItemType

//wooden

//elsectronics

//calculateAmount()

import java.util.Scanner;

abstract class ItemType{

abstract double caluculateAmount();

class Wooden extends ItemType{

private int noOfItems;

private double cost;

Wooden(int noOfItems,double cost){

this.noOfItems=noOfItems;

this.cost=cost;

double caluculateAmount(){

return noOfItems*cost;

class Electronics extends ItemType{

private String type;

private double amount;

Electronics(String type,double amount){

this.type=type;

this.amount=amount;
CODEJAVA107

double caluculateAmount(){

if(this.type.equals("Commercial")) return amount;

else return 0.95*amount;

class Codejava107{

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

ItemType i;

if(n==1) {

int noof = sc.nextInt();

double cst=sc.nextDouble();

i=new Wooden(noof,cst);

System.out.print(i.caluculateAmount());

else{

String ty=sc.next();

double amt=sc.nextDouble();

i=new Electronics(ty,amt);

System.out.print(i.caluculateAmount());

}
CODEJAVA107

3.Overloading-Stall

//public Double computeCost(String stallType, Integer squareFeet)

//public Double computeCost(String stallType, Integer squareFeet, Integer numberOfTV)

import java.util.Scanner;

class Stall{

protected String name;

protected String detail;

private String ownerName;

Stall(String name,String detail,String ownername){

this.name=name;

this.detail=detail;

this.ownerName=ownername;

public double computeCost(String stallType,int sqaureFeet){

if(stallType.equals("Platinum")){

return 200*sqaureFeet;

else if(stallType.equals("Diamond")){

return 150*sqaureFeet;

else{

return 100*sqaureFeet;

public double computeCost(String stallType,int sqaureFeet,int numberOfTv){

if(stallType.equals("Platinum")){

return 200*sqaureFeet+10000*numberOfTv;
CODEJAVA107

else if(stallType.equals("Diamond")){

return 150*sqaureFeet+10000*numberOfTv;

else{

return 100*sqaureFeet+10000*numberOfTv;

class Codejava107{

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

String naam=sc.nextLine();

String details=sc.nextLine();

String oname=sc.next();

Stall st=new Stall(naam,details,oname);

String sttpe=sc.next();

int sqfeet=sc.nextInt();

if (sc.hasNext()){

int nooftv=sc.nextInt();

System.out.print(st.computeCost(sttpe,sqfeet,nooftv));

else{

System.out.print(st.computeCost(sttpe,sqfeet));

You might also like