Dear Experts, This is Urgent. Please assist. Thanks. Dear Experts, Please help o
ID: 3571569 • Letter: D
Question
Dear Experts,
This is Urgent. Please assist. Thanks.
Dear Experts,
Please help on Java assignment. Question as below.
In this question, the quicksort, are chosen to do the performance test and evaluation.
Implement quicksort of the sorting algorithms above and use it to sort a banking application records as below.
Client records are composed of the following fields:
First Name
Last Name
Bank Account No.
Current Balance
Account Status
Account Type
Your program will need to be able to sort a list of Account Number according to any of the fields, using one of the algorithms listed above.
A user should also be able to specify whether the list will be sorted from smallest to largest, or largest to smallest.
Every account should only be able to be accessed when the account number followed by the (four digit) pin number is entered.
Explanation / Answer
public class QuickSort {
private static Account[] accounts;
public static void main(String args[]) {
// Test data
Account account1 = new Account("Bob", "Maron", "SBI001", "10000", "savings");
Account account2 = new Account("Peter", "Hans", "SBI002", "20000", "current");
Account account3 = new Account("Perry", "Mason", "SBI003", "30000", "savings");
accounts = new Account[3];
accounts[0] = account1;
accounts[1] = account2;
accounts[2] = account3;
/* Make the below variable If you want to sort in descending order */
boolean isAscending = true;
/* Sorting based on firstName */
System.out.println("---------------------------------------");
System.out.println("SORTING BASED ON FIRST NAME");
System.out.println("---------------------------------------");
quicksort(0,accounts.length-1,"firstName",isAscending);
for(int index=0; index<accounts.length; index++){
System.out.println(accounts[index].getData());
}
/* Sorting based on lastName */
System.out.println("---------------------------------------");
System.out.println("SORTING BASED ON LAST NAME");
System.out.println("---------------------------------------");
quicksort(0,accounts.length-1,"lastName",isAscending);
for(int index=0; index<accounts.length; index++){
System.out.println(accounts[index].getData());
}
/* Sorting based on accountNumber */
System.out.println("---------------------------------------");
System.out.println("SORTING BASED ON ACCOUNT NUMBER");
System.out.println("---------------------------------------");
quicksort(0,accounts.length-1,"accountNumber",isAscending);
for(int index=0; index<accounts.length; index++){
System.out.println(accounts[index].getData());
}
/* Sorting based on currentBalance */
System.out.println("---------------------------------------");
System.out.println("SORTING BASED ON CURRENT BALANCE");
System.out.println("---------------------------------------");
quicksort(0,accounts.length-1,"currentBalance",isAscending);
for(int index=0; index<accounts.length; index++){
System.out.println(accounts[index].getData());
}
/* Sorting based on accountType */
System.out.println("---------------------------------------");
System.out.println("SORTING BASED ON ACCOUNT TYPE");
System.out.println("---------------------------------------");
quicksort(0,accounts.length-1,"accountType",isAscending);
for(int index=0; index<accounts.length; index++){
System.out.println(accounts[index].getData());
}
}
/* Quick sort Algorithm */
private static void quicksort(int low, int high,String property, boolean isAscending) {
int i = low, j = high;
Account pivot = accounts[low + (high - low) / 2];
// Divide into two lists
while (i <= j) {
if(isAscending){
while (accounts[i].get(property).compareTo(pivot.get(property)) < 0 ) {
i++;
}
while (accounts[j].get(property).compareTo(pivot.get(property)) > 0) {
j--;
}
}
else{
while (accounts[i].get(property).compareTo(pivot.get(property)) > 0 ) {
i++;
}
while (accounts[j].get(property).compareTo(pivot.get(property)) < 0) {
j--;
}
}
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
// Recursion
if (low < j)
quicksort(low, j,property,isAscending);
if (i < high)
quicksort(i, high,property,isAscending);
}
private static void swap(int i, int j) {
Account temp = accounts[i];
accounts[i] = accounts[j];
accounts[j] = temp;
}
}
/**
* Account model with Getters and setters
*/
class Account {
private String firstName;
private String lastName;
private String accountNumber;
private String currentBalance;
private String accountType;
// Default constructor
public Account() {
}
// Parametrized constructor
public Account(String firstName, String lastName, String accountNumber, String currentBalance, String accountType) {
this.firstName = firstName;
this.lastName = lastName;
this.accountNumber = accountNumber;
this.currentBalance = currentBalance;
this.accountType = accountType;
}
// Getters and setters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getCurrentBalance() {
return currentBalance;
}
public void setCurrentBalance(String currentBalance) {
this.currentBalance = currentBalance;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public String get(String property) {
try{
return this.getClass().getDeclaredField(property).get(this).toString();
}
catch(Exception e){
// handle exception
}
return null;
}
public String getData(){
return this.firstName + ";" + this.lastName + ";" + this.accountNumber + ";" + this.currentBalance + ";" + this.accountType;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.