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;
}
}
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());
}
0 comments:
Post a Comment