Question

In: Computer Science

PLZ USE ECLIPSE JAVA and show output with explanation The object of this assignment is to...

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.

  1. In the Checking class from last assignment, add a new data member Name, type string, representing customer name.
  2. In Checking4_abcde6 class, keep data member to be the same. In the main method, first create five objects in Checking class with the following information.

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.

  1. Create a WriteAcc method. This method receives a parameter, and the type of this parameter is array of Checking class. This method should write the information stored in objects within the parameter to a delimiter file, separated using comma. Each line in this delimiter file is an object, and comma separates the three fields of an object. Write this file to the work directory of your java code with the name AccInfo.txt
  2. Create a ReadAcc method. This method receives a parameter with type String. This string represents the file name in your work directory. In this method, read information contained in the file (corresponding to the parameter value), and generate Checking objects based on the information from the file. This method should return an array with Checking objects.

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.

  1. Create a SortBalanceDesc method. This method receives a parameter with array with Checking objects, and return an array with Checking objects. The returned array should be a sorted array with Checking objects, with the value of Balance in descending order. You should adapt the Bubble Sort algorithm from slides in this method. You should also print this array out in console.
  2. Create a SearchBalance method. This method receives a parameter with array with Checking objects, and return an array with Checking objects. In addition, this method should also request input from user to specify a Balance value. The returned array should contain objects with user-specified Balance value. You should adapt the Binary Search algorithm from slides in this method.

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.

Solutions

Expert Solution

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 :


Related Solutions

Please create a Java Eclipse code and show output and explanation. MUST USE EXCEPTION! Step 1....
Please create a Java Eclipse code and show output and explanation. MUST USE EXCEPTION! Step 1. Ask users how much money was spent on an orange. Step 2. Then ask the user how many oranges they purchased Step 3. Lastly calculate the price for each orange purchased.
PLZ USE JAVA ECLIPSE AND EXPLAIN Create a GUI which works as an accumulator:  There...
PLZ USE JAVA ECLIPSE AND EXPLAIN Create a GUI which works as an accumulator:  There is a textfield A which allows user to enter a number  There is also another textfield B with value start with 0.  When a user is done with entering the number in textfield A and press enter, calculate the sum of the number in textfield B and the number user just entered in textfield A, and display the updated number in textfield...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
Demonstrate the following: Usage of Eclipse Variable Assignment Looping (for, while etc) ArrayList (using object) Object...
Demonstrate the following: Usage of Eclipse Variable Assignment Looping (for, while etc) ArrayList (using object) Object based programming User Interaction with multiple options Implementing Methods in relevant classes Instructions Start with below java files: Item.java Pages //add package statement here public class Item { private int id; private double price; public Item(int id, double price){ this.id = id; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double...
Create a Java Program to show a savings account balance. using eclipse IDE This can be...
Create a Java Program to show a savings account balance. using eclipse IDE This can be done in the main() method. Create an int variable named currentBalance and assign it the value of 0. Create an int variable named amountToSaveEachMonth. Prompt "Enter amount to save each month:" and assign the result to the int variable in step 2. Create an int variable name numberOfMonthsToSave. Prompt "Enter the number of months to save:" and store the input value into the variable...
use eclipse give me java codes Task III: Write a classCarInsurancePolicy The CarInsurancePolicy class will...
use eclipse give me java codes Task III: Write a class CarInsurancePolicy The CarInsurancePolicy class will describe an insurance policy for a car. 1. The data members should include all the data members of an Insurance Policy, but also the driver’s license number of the customer, whether or not the driver is considered a “good” driver (as defined by state law) , and the car being insured (this should be a reference to a Car object -- write a separate...
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression...
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives •...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives • Be able to write methods • Be able to call methods Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct...
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT