In: Computer Science
Write a java program that does the following:
a)
The user will enter data such as client name and client balance. The user can stop inputting data by entering "stop". The program should store the user inputs as a data member of an array. The type of this array is a class named ClientData.
Below the output should be displayed by the program. The parts in bold are inputs from the user and not hard coded in the program
Client Name: Aaron
Client Balance: 100
Client Name: Barry
Client Balance: 200
Client Name: stop
b) The program will then ask the user to choose a function: Average or Sort or Stop. The program should ask the user "Please choose the next function: Average"
c) The Average program will call another method called BalanceAverage that will display the average of the Client's Balances. The program should output "The average client balance is : 100"
Here ArrayList is used to strore the different objects of ClientData..
Also to sort the data Comparator interface is implemented ...
Here BalanceAverage function prints the average of the balances of all the clients ...
The Print function prints the data of the Clients which is present in list...
The code snippet and screenshots are attached...
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package manage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @author vijay
*/
class ClientData//class to store client information
{
private String name;//to store client name
private int balance;//to store client balance
ClientData(String name,int balance)//parameterised constructor
{
this.name=name;//initialize the name
this.balance=balance;//initialize the balance
}
String getName()//getter method for name
{
return name;
}
int getBalance()//getter method for balance
{
return balance;
}
void setName(String name)//setter method for name
{
this.name=name;
}
void setBalance(int balance)//setter method for balance
{
this.balance=balance;
}
}
//Compare class to implement Comparator class which will be useful in sorting the data array
class Compare implements Comparator<ClientData>{
@Override
public int compare(ClientData o1, ClientData o2)//method which tells how to sort the array on which parameter or variable
{
return o1.getBalance()-o2.getBalance();
}
}
public class Manage {
/**
* @param args the command line arguments
*/
//function to calculate Avaerage of the data array
private static void BalanceAverage(ArrayList<ClientData> data)
{
double sum=0;//initialize the sum to 0
for(int i=0;i<data.size();i++)//iterate the data list
{
sum+=data.get(i).getBalance();//add the balance to sum
}
//print the average
System.out.println("The average client balance is: "+sum/data.size());
}
//function to print the data list
private static void Print(ArrayList<ClientData> data)
{
//iterate in forward direction and print info of each client
for(int i=0;i<data.size();i++)
{
System.out.println("Client Name : "+data.get(i).getName()+" , Client Balance : "+data.get(i).getBalance());
}
}
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System.in);//intialize the scanner object
ArrayList<ClientData> data=new ArrayList<ClientData>();//intialize the data list
while(true)//iterate until stop is enetered
{
System.out.print("Client Name: ");
String name=sc.next();//get the input
if(name.equals("stop"))//check whether the stop is enetered or not
{
break;
}
System.out.print("Client Balance: ");
int balance=sc.nextInt();
ClientData client=new ClientData(name,balance);//make the ClientData object
data.add(client);//add the object to the list
}
while(true)//iterate until user enters Stop
{
System.out.print("Please choose the next function: ");
String task=sc.next();//get the task
if(task.equals("Average"))
{
BalanceAverage(data);//call the function to calculate the average
}
else if(task.equals("Sort"))
{
data.sort(new Compare());//sort the data with the help of object of Compare class
Print(data);//print the data
}
else if(task.equals("Stop"))
{
break;//break the loop
}
else
{
System.out.println("Enter the correct function");//notify the user to enter correct data
}
}
}
}
Screenshot