Question

Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...

Please answer in JAVA

IDS 401

Assignment 4

Deadline

In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted.

Assignment

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 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. All requirements from the previous assignment remain in effect (meaning you should still keep the two data members and three methods from last assignment). The new features for this application are:

  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. However, if you want to return all objects with the search value, you need to slightly change the

Homework Answers

Answer #1
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package checkingaccount;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author sysadmin
 */
public class CheckingAccount {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ArrayList<Checking> AccInfo = new ArrayList<Checking>();
        CheckingAccount checkAccount = new CheckingAccount();
       ArrayList<Checking> newAccInfo = new ArrayList<Checking>();
        Checking acc1 = new Checking(100, "Alice", 100.0);
        Checking acc2 = new Checking(120, "Bob", 110.0);
        Checking acc3 = new Checking(141, "Doug", 90.0);
        Checking acc4 = new Checking(116, "Eva", 100.0);
        Checking acc5 = new Checking(132, "Frank", 80.0);
        AccInfo.add(acc1);
        AccInfo.add(acc2);
        AccInfo.add(acc3);
        AccInfo.add(acc4);
        AccInfo.add(acc5);
        checkAccount.writeAcc(AccInfo);
        newAccInfo=checkAccount.readAcc("AccInfo.txt");
        checkAccount.sortBalanceDesc(newAccInfo);
        checkAccount.searchBalance(newAccInfo);
        // TODO code application logic here
    }

    public void writeAcc(ArrayList<Checking> accountInfo) {
        String fileName = "AccInfo.txt";
        try {
            PrintWriter fw = new PrintWriter(new BufferedWriter(new FileWriter(fileName, false)));
            for (int i = 0; i < accountInfo.size(); i++) {
                Checking acc = (Checking) accountInfo.get(i);
                fw.println(acc.account_no + "," + acc.name + "," + acc.balance);

            }
            fw.close();
        } catch (Exception e) {
            System.out.println("Exception" + e);
        }
    }

    public ArrayList<Checking> readAcc(String filename) {
         ArrayList<Checking> checkingInput = new ArrayList<Checking>();
        try {
            FileInputStream fs = new FileInputStream(filename);

           
            DataInputStream inpt = new DataInputStream(fs);
            BufferedReader brread = new BufferedReader(new InputStreamReader(inpt));
            String str;
            while ((str = brread.readLine()) != null) {
                String token[] = str.split(",");
                checkingInput.add(new Checking(Long.parseLong(token[0]), token[1], Double.parseDouble(token[2])));

            }
        } catch (IOException e) {
            System.out.println("Error in reading");
        }
        return checkingInput;
    }

    public ArrayList<Checking> sortBalanceDesc(ArrayList<Checking> accountInfo) {

        double temp;
        String name;
        long id;
        for (int i = 0; i < accountInfo.size(); i++) {
            for (int j = 0; j < (accountInfo.size() - i - 1); j++) {

                if ((accountInfo.get(j).getBalance()) < (accountInfo.get(j + 1).getBalance())) {
                    temp = accountInfo.get(j).getBalance();
                    name = accountInfo.get(j).getName();
                    id = accountInfo.get(j).getAccount_no();
                    accountInfo.get(j).setBalance(accountInfo.get(j + 1).getBalance());
                    accountInfo.get(j + 1).setBalance(temp);
                    accountInfo.get(j).setName(accountInfo.get(j + 1).getName());
                    accountInfo.get(j + 1).setName(name);
                    accountInfo.get(j).setAccount_no(accountInfo.get(j + 1).getAccount_no());
                    accountInfo.get(j + 1).setAccount_no(id);
                }
            }

        }
        for (int i = 0; i < accountInfo.size(); i++) {
            System.out.println("Account No." + accountInfo.get(i).account_no + "\t:Balance" + accountInfo.get(i).getBalance());

        }
        return accountInfo;

    }

   

    public ArrayList<Checking> searchBalance(ArrayList<Checking> accountInfo) {

        Scanner input = new Scanner(System.in);
        ArrayList<Checking> account = new ArrayList<Checking>();
        System.out.println("Please enter balance to search");
        double balance = input.nextDouble();
        int first = 0;
        int last = accountInfo.size();
        int mid = (first + last) / 2;
        while (first <= last) {
            if (accountInfo.get(mid).getBalance() < balance) {
                first = mid + 1;
            }
            if (accountInfo.get(mid).getBalance() == balance) {
                account.add(new Checking(accountInfo.get(mid).account_no,accountInfo.get(mid).name,accountInfo.get(mid).balance));
                System.out.println("Account_No\t"+accountInfo.get(mid).account_no);
                System.out.println("Name of Customer\t"+accountInfo.get(mid).name);
                 System.out.println("Balance\t"+accountInfo.get(mid).balance);
                break;
            } else {
                last = mid + 1;
            }
            mid = (first + last) / 2;
        }
        if (first > last) {
            System.out.println("Element not found");
        }
        return account;
    }
    
}







/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package checkingaccount;

/**
 *
 * @author sysadmin
 */
public class Checking {
    long account_no;
    String name;
    double balance;

    public Checking(long account_no, String name, double balance) {
        this.account_no = account_no;
        this.name = name;
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public long getAccount_no() {
        return account_no;
    }

    public void setAccount_no(long account_no) {
        this.account_no = account_no;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are passed to...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
My assignment is listed below. I already have the code complete, but I cannot figure out...
My assignment is listed below. I already have the code complete, but I cannot figure out how to complete this portion: You must create a makefile to compile and build your program. I can't figure out if I need to create a makefile, and if I do, what commands do I use for that? Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber...
For this assignment, you'll create a Player class that captures information about players on a sports...
For this assignment, you'll create a Player class that captures information about players on a sports team. We'll keep it generic for this assignment, but taking what you learn with this assignment, you could create a class tailored to your favorite sport. Then, imagine using such a class to track the stats during a game. Player Class Requirements Your class should bet set up as follows: Named Player Is public to allow access from other object and assemblies Has a...
Java assignment: You are tasked with building a BookStore. Your Architect has told you that the...
Java assignment: You are tasked with building a BookStore. Your Architect has told you that the BookStore class has the following members : long id; String name; Address address; Book book; The Address class has the following attributes : String streetName; String houseNumber; String apartmentNumber; String zipCode; The book class has the following members String name; String isbn; Author author; The Author class within the book class has the following member : String firstName; String lastName; Date dateOfBirth; Address address;...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT