Code for Connection Class
package javaapplication1;
public class Connection {
private int connId;
private int customerId;
private String customerEmail;
private double balance;
public Connection(int connId,int customerId,String customerEmail,double balance)
{
this.connId = connId;
this.customerId = customerId;
this.customerEmail = customerEmail;
this.balance = balance;
}
public double getBalance() {
return balance;
}
public int getConnId() {
return connId;
}
public int getCustomerEmail() {
return customerEmail;
}
public int getCustomerId() {
return customerId;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setCustomerEmail(int customerEmail) {
this.customerEmail = customerEmail;
}
}
Code for ConnectionDemo Class in java which contains the main method
package javaapplication1;
import java.util.Scanner;
public class ConnectionDemo {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Scanner sc1=new Scanner(System.in);
Connection[] connections=new Connection[3];
for(int i=0;i<connections.length;i++)
{
System.out.println("Enter connection id:");
int connId = sc.nextInt();
System.out.println("Enter customer Id:");
int custId = sc.nextInt();
System.out.println("Enter customer email:");
String email = sc1.nextLine();
System.out.println("Enter customer balance:");
double balance = sc.nextDouble();
connections[i]=new Connection(connId,custId,email,balance);
}
double avgBalance = getAverageBalance(connections,1);
System.out.println("average balance for customer with id 1 is " + avgBalance);
}
public static double getAverageBalance(Connection[] connections, int custId)
{
double balance = 0;
int custCount = 0;
for(int i = 0;i<connections.length;i++)
{
if(custId == connections[i].getCustomerId())
{
custCount++;
balance = balance + connections[i].getBalance();
}
}
balance = balance/custCount;
return balance;
}
}
Output
0 comments:
Post a Comment