Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Sunday, 5 October 2014

Collection Examples in Java

 Unknown     09:10     Java     No comments   

package come;

public abstract class Connection implements Comparable<Connection> {

private int connectionId;
private int customerId;
private double balance;
public Connection(int connectionId,int customerId,double balance)
{
this.connectionId=connectionId;
this.customerId=customerId;
this.balance=balance;
}
abstract double recharge(double bal);
abstract double deduct(double bal);
public int getConnectionId() {
return connectionId;
}
public int getCustomerId() {
return customerId;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String toString()
{
return connectionId+" "+customerId+" "+balance;
}
public int compareTo(Connection o) {

int code = 0;
if(o.getConnectionId()< connectionId)
{
code = 1;
}
else if(o.getConnectionId()> connectionId)
{
code = -1;
}
return code;
}
}





package come;
import java.util.*;

public class Inventory {

ArrayList<Connection> connections;
Inventory()
{
connections=new ArrayList<Connection>();
}
boolean addConnection(Connection con)
{
boolean exist=true;
for(Connection c:connections)
{
if(c.getConnectionId()==con.getConnectionId())
{
exist=false;
break;
}
}
if(exist==true)
{
connections.add(con);
}
return exist;
}
double recharge(int conId,double recAmount)
{
double total=-1;
for(Connection con:connections)
{
if(con.getConnectionId()==conId)
{
total=con.getBalance();
total=total+recAmount;
//con.recharge(recAmount);
break;
}
}
return total;
}
double deduct(int conId,double dedAmount) throws ConnectionLockedException
{
double total=-1;
for(Connection con:connections)
{
if(con.getConnectionId()==conId)
{
if(con.getBalance()<0)
{
throw new ConnectionLockedException(con.getBalance());
}
else
{
total=con.getBalance();
total=total-dedAmount;
}
}
}
return total;
}
Set<Integer> getUniqueCustomerIDs()
{
Set<Integer> set=new HashSet<Integer>();
for(Connection con:connections)
{
set.add(con.getConnectionId());
}
return set;
}
HashMap<Integer,Connection> getConnectionHashMap()
{
HashMap<Integer,Connection> hMap=new HashMap<Integer,Connection>();
for(Connection con:connections)
{
hMap.put(con.getConnectionId(), con);
}
return hMap;
}
}



package come;

public class ConnectionLockedException extends Exception {

double currentBalance;
ConnectionLockedException(double currentBalance)
{
this.currentBalance=currentBalance;
}
public String getMessage()
{
return currentBalance+" is invalid balance";
}
}



package come;

import java.util.TreeSet;

public class NewInventory {

TreeSet<Connection>  conTree=null;
public NewInventory()
{
conTree=new TreeSet<>();
}
TreeSet<Connection> getConnections()
{
return conTree;
}
int addConnection(Connection con)
{
conTree.add(con);
return conTree.size();
}
}



package com;

public class Hobby {

String hobbyDescription;
String hobbyName;
public String getHobbyDescription() {
return hobbyDescription;
}
public void setHobbyDescription(String hobbyDescription) {
this.hobbyDescription = hobbyDescription;
}
public String getHobbyName() {
return hobbyName;
}
public void setHobbyName(String hobbyName) {
this.hobbyName = hobbyName;
}
public String toString()
{
return hobbyDescription+hobbyName;
}
}




package come;

public class ConnectionDemo {

public static void main(String[] args) {
Inventory inv=new Inventory();
Prepaid con1=new Prepaid(1,1,100);
Prepaid con2=new Prepaid(2,2,-122);
Prepaid con3=new Prepaid(3,3,300);
System.out.println(inv.addConnection(con1));
System.out.println(inv.addConnection(con2));
System.out.println(inv.addConnection(con3));
System.out.println("Recharge "+inv.recharge(1, 122));
try
{
System.out.println("Deduct1 "+inv.deduct(1, 500));
System.out.println("Deduct "+inv.deduct(2, 1220));
}catch(ConnectionLockedException ce)
{
ce.getMessage();
}
System.out.println("Unique Customer IDs "+inv.getUniqueCustomerIDs());
System.out.println("Hash Map "+inv.getConnectionHashMap().toString());
Prepaid con4=new Prepaid(3,3,300);
System.out.println(inv.addConnection(con4));
NewInventory ni=new NewInventory();
System.out.println("Tree "+ni.addConnection(con1));
System.out.println("Tree "+ni.addConnection(con2));
System.out.println("Tree "+ni.addConnection(con3));
System.out.println("Tree "+ni.addConnection(con4));
System.out.println("Tree output "+ni.getConnections());
}

}




  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ►  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ►  March (20)
    • ►  February (50)
    • ►  January (3)
  • ▼  2014 (339)
    • ►  December (1)
    • ▼  October (55)
      • ServletContext in Servlet
      • sendRedirect() method in Servlet
      • Servlets in Java
      • HashMap in Java
      • HashSet In Java
      • Different ways to delete elements from ArrayList
      • Raw type of ArrayList And its Operations
      • User Defined Exception in Java
      • Exception Handling in Java
      • Best Java Interview Questions
      • ek muskaan: सोलह श्रृंगार से सजूँगी आज मै
      • ek muskaan: हमे तुमसे हुआ है प्यार हम क्या करे आप...
      • ek muskaan: आज दिल बेचैन है तुम बिन
      • ek muskaan: ये जान चली जाएगी तुम बिन
      • ek muskaan: एहसासों की चिलमन भी क्या कमाल होती है
      • ek muskaan: अब मन पे नशा छाने लगा है
      • ek muskaan: हमे तुझसे कितनी मुहब्बत है ये हम जानत...
      • ek muskaan: सहारे आदमी को कमजोर बना देते है
      • बिखरा ख्वाब
      • tujhme fnaa hoon
      • कुछ हौसले टूट रहे है उनकी निगाहों को देख कर कुछ...
      • तोड़ कर दिल वो चले गये एक खिलौना समझकर हम तो रा...
      • ये कैसा अभिशाप है
      • मै पत्थर हु
      • Equals and hashCode method in Java
      • Collection Examples in Java
      • Various File operations in Java
      • FileReader and BufferedReader Example In Java
      • FileReader Example in Java
      • BufferedReader Example In Java
      • Different methods to manage Employee Using File Ha...
      • Using Different methods to manage an Employee deta...
      • है दरिया जो समेट रखा इस दिल ने
      • विजयदशमी की हार्दिक सुभकामना
      • ईद मुबारक
      • जीवन का सत्य
      • Employee Management System using File handling in ...
      • Add an Retrieve data in HashMap using Iterator in ...
      • Add and Retrieve data from HashMap in Java
      • HashMap Collection in Java
      • Removing duplicates from a string in Java using co...
      • Sorting using TreeSet Collection in Java
      • Collections(HashSet) in Java
      • Write Permission of files in java
      • File Paths in Java
      • Directories in Java
      • File Permission in Java
      • File Handling in Java
      • Directory creation in java
      • File Handling in Java
      • String Comparison in Java
      • String sorting in Java
      • String Operations in Java
      • StringBuffer in java
      • Enumeration in Java
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates