In: Computer Science
PLZ USE ECLIPSE JAVA and show output with explanation
The object of this assignment is to construct a mini-banking system that helps us manage banking data. Notice that you can copy and paste your code from previous assignments. This assignment asks you to allow read from/write to file, as well as search and sorting algorithm. The object of this assignment is to construct a mini-banking system that helps us manage banking data. This assignment asks you to allow read from/write to file, as well as search and sorting algorithm
-The class name should be Checking4, an underscore, and your NetID run together; for example, if your NetID is abcde6, your class name would be Checking4_abcde6. For this assignment, we will create text-based tools for creating, deleting, and sorting checking account objects.
The first object: AccNum: 100, Name: Alice, Balance: 100.00
The second object: AccNum: 120, Name: Bob, Balance: 110.00
The third object: AccNum: 141, Name: Doug, Balance: 90.00
The fourth object: AccNum: 116, Name: Eva, Balance: 100.00
The fifth object: AccNum: 132, Name: Frank, Balance: 80.00
Then store these five objects to AccInfo, data member of this class..
The main method will then call the WriteAcc using AccInfo as parameter, and ReadAcc methods below. In the main method, you should call ReadAcc to read the AccInfo.txt file specified below, and assign the returned value to NewAccInfo array.
Then this main method should call SortBalanceDesc method to sort the NewAccInfo array based on balance in descending order. Then call the SearchBalance method, and display the result of the SearchBalance method.
Hint: You can first identify how many objects you need in an array, then create the array. Then read the file and add objects into this array.
Hint: the binary search method we discuss in class only apply to the scenario in which there are only unique values. For this question, if there are multiple objects with the same search value, it is OK to just return one of them.
Checking.java
public class Checking {
private int accNum;
private String name;
private double balance;
public Checking()
{
this.accNum = 0;
this.name = "";
this.balance = 0.0;
}
public Checking(int accNum, String name, double balance) {
this.accNum = accNum;
this.name = name;
this.balance = balance;
}
public int getAccNum() {
return accNum;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public String toFileStr()
{
return(getAccNum() + "," + getName() + "," + getBalance());
}
@Override
public String toString()
{
return("AccNum: " + getAccNum() + ", Name: " + getName() + ",
Balance: $" + String.format("%.2f", getBalance()));
}
}
AccountsManager.java (Main class)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class AccountsManager {
private static final String FILENAME = "AccInfo.txt";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Checking> NewAccInfo = ReadAcc(FILENAME);
System.out.println("Displaying all accounts read from
file..");
displayAll(NewAccInfo);
System.out.println("Sorting all accounts based on balance in
descending order..");
SortBalanceDesc(NewAccInfo);
displayAll(NewAccInfo);
System.out.println("Searching all accounts having a user defined
balance..");
System.out.print("Please enter a balance to search: $");
double balance = Double.parseDouble(sc.nextLine().trim());
ArrayList<Checking> results = SearchBalance(NewAccInfo,
balance);
if(results.isEmpty())
System.out.println("Sorry, there are no accounts having balance $"
+ String.format("%.2f", balance) + "!\n");
else
{
System.out.println("Matches found: " + results.size());
displayAll(results);
}
System.out.println("Writing the modified accounts data to output
file " + FILENAME + "..");
WriteAcc(NewAccInfo, FILENAME);
}
private static void WriteAcc(ArrayList<Checking> accs, String
filename)
{
if(accs.isEmpty())
{
System.out.println("No accounts in the list!\n");
return;
}
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(filename));
pw = new PrintWriter(fw);
for(Checking c : accs)
pw.write(c.toFileStr() + System.lineSeparator());
pw.flush();
fw.close();
pw.close();
System.out.println("Accounts data has been successully written to "
+ filename + ".\n");
} catch (IOException ex) {
System.out.println("Error in writing data to " + filename + "!
Exiting..");
System.exit(0);
}
}
private static ArrayList<Checking> ReadAcc(String
filename)
{
ArrayList<Checking> accs = new ArrayList<>();
Scanner fileReader;
try
{
System.out.println("Attempting to read data from " + filename +
"..");
fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
String[] data = fileReader.nextLine().trim().split(",");
int accNum = Integer.parseInt(data[0]);
String name = data[1];
double balance = Double.parseDouble(data[2]);
accs.add(new Checking(accNum, name, balance));
}
fileReader.close();
System.out.println("Number of records successfully read = " +
accs.size());
}catch(FileNotFoundException fnfe){
System.out.println(filename + " could not be found!
Exiting..");
System.exit(0);
}
return accs;
}
private static ArrayList<Checking>
SortBalanceDesc(ArrayList<Checking> accs)
{
for(int i = 0; i < (accs.size() - 1); i++)
{
for(int j = 0; j < accs.size() - i - 1; j++)
{
if(accs.get(j).getBalance() < accs.get(j +
1).getBalance())
{
Checking temp = accs.get(j);
accs.set(j, accs.get(j + 1));
accs.set(j + 1, temp);
}
}
}
return accs;
}
private static ArrayList<Checking>
SearchBalance(ArrayList<Checking> accs, double balance)
{
ArrayList<Checking> res = new ArrayList<>();
int l = 0, r = accs.size() - 1;
while(l <= r)
{
int m = l + (r - l) / 2;
if(accs.get(m).getBalance() == balance)
res.add(accs.get(m));
if(balance < accs.get(m).getBalance())
l = m + 1;
else
r = m - 1;
}
return res;
}
private static void displayAll(ArrayList<Checking>
accs)
{
for(Checking ch : accs)
System.out.println(ch);
System.out.println();
}
}
********************************************************* SCREENSHOT *******************************************************
INPUT / OUTPUT FILE(AccInfo.txt) - This file needs to be created before running the code and this file should be created within the same porject directory where the above .java files will be residing.
CONSOLE OUTPUT :