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
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,...
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...
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...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
the coding language is java The start of Polymorphism Assignment Outcome: Student will demonstrate the ability...
the coding language is java The start of Polymorphism Assignment Outcome: Student will demonstrate the ability to apply polymorphism in a Java program. Program Specifications: Design the following program: A person has a name, and age. An Employee is a person. An Employee has a company and position. A SoftwareEngineer is an employee and a person. A SoftwareEngineer has a rank (Junior, Middle, and Senior) A SoftwareEngineer either C programmer, Jave programmer or both. A Manager is an Employee and...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
You must use Windows Programming(Do NOT use Console) to complete this assignment. Use any C# method...
You must use Windows Programming(Do NOT use Console) to complete this assignment. Use any C# method you already know. Create a Windows application that function like a banking account register. Separate the business logic from the presentation layer. The graphical user interface should allow user to input the account name, number, and balance. Provide TextBox objects for withdrawals and deposits. A button object should be available for clicking to process withdrawal and deposit transactions showing the new balance. Document and...