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:
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. However, if you want to return all objects with the search value, you need to slightly change the
/* * 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; } }
Get Answers For Free
Most questions answered within 1 hours.