Question

In: Computer Science

Write a java program that does the following: a) The user will enter data such as...

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"

Solutions

Expert Solution

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


Related Solutions

Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Write a simple JAVA program to understand natural language. The user will enter the input following...
Write a simple JAVA program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Chris came to Bangkok, Thailand in 2009. The user will follow the exactly the same formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay in City for X year(s). City is in...
Write a Java program to do the following: Ask the user to enter 10 first names...
Write a Java program to do the following: Ask the user to enter 10 first names (one word - no hyphen or apostrophe) using the keyboard. 1) Display the list of names one per line on the Console. 2) Display the names again, after eliminating duplicates using a HashSet (Your code MUST use HashSet).
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT