我会创建一个Customer
DTO:
public class Customer {
protected int _id;
protected String _name;
protected float _balance;
public Customer() {
init(0, "New Customer", 0.00f);
}
public Customer(int id, String name, float balance) {
init(id, name, balance);
}
private void init(int id, String name, float balance) {
id(id).name(name).balance(balance);
}
public int id() {
return this._id;
}
public Customer id(int id) {
this._id = id;
return this;
}
public String name() {
return this._name;
}
public Customer name(String name) {
this._name = name;
return this;
}
public float balance() {
return this._balance;
}
public Customer balance(float balance) {
this._balance = balance;
return this;
}
public String toString() {
return String.format("#%d: %-5s - $%07.2f", id(), name(), balance());
}
}
接下来,我将创建一个实用程序类来读取文件并生成客户:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Utils {
public static List<Customer> getCustomers(String filename, String delimiters) {
List<Customer> customers = new ArrayList<Customer>();
Customer customer = null;
File file = new File(filename);
Scanner scanner = null;
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if ((customer = createCustomer(line, delimiters)) != null)
customers.add(customer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
scanner.close();
}
return customers;
}
public static Customer createCustomer(String line, String delimiter) {
String[] arr = line.split(delimiter);
if (arr.length < 3)
return null;
int id = Integer.parseInt(arr[0].trim());
String name = arr[1].trim();
float balance = Float.parseFloat(arr[2].trim());
return new Customer(id, name, balance);
}
}
最后我会创建我的Bank
类来存储Customer
s 并可以将它们排序/打印出来。
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static bank.Utils.getCustomers;
public class Bank {
protected List<Customer> customers;
public enum Sort {
ID, NAME, BALANCE
}
public Bank() {
String filename = "bank/Bank.txt";
String delimiters = "[,]";
customers = getCustomers(filename, delimiters);
printCustomers(Sort.ID, false);
printCustomers(Sort.ID, true);
printCustomers(Sort.NAME, false);
printCustomers(Sort.NAME, true);
printCustomers(Sort.BALANCE, false);
printCustomers(Sort.BALANCE, true);
}
public void sortId(boolean descending) {
final int d = descending ? -1 : 1;
Comparator<Customer> comparator = new Comparator<Customer>() {
public int compare(Customer c1, Customer c2) {
return (c1.id() > c2.id() ? 1 :
c1.id() == c2.id() ? 0 : -1) * d;
}
};
Collections.sort(customers, comparator);
}
public void sortName(boolean descending) {
final int d = descending ? -1 : 1;
Comparator<Customer> comparator = new Comparator<Customer>() {
public int compare(Customer c1, Customer c2) {
return (c1.name().compareTo(c2.name())) * d;
}
};
Collections.sort(customers, comparator);
}
public void sortBalance(boolean descending) {
final int d = descending ? -1 : 1;
Comparator<Customer> comparator = new Comparator<Customer>() {
public int compare(Customer c1, Customer c2) {
return (c1.balance() > c2.balance() ? 1 :
c1.balance() == c2.balance() ? 0 : -1) * d;
}
};
Collections.sort(customers, comparator);
}
public void printCustomers(Sort sortBy, boolean descending) {
String header = "";
String desc = descending ? "descending" : "ascending";
switch (sortBy) {
case NAME:
header = "Sorting by Name";
sortName(descending);
break;
case BALANCE:
header = "Sorting by Balance";
sortBalance(descending);
break;
case ID:
header = "Sorting by Id";
default:
sortId(descending);
break;
}
System.out.printf("### %s %s ###\n", header, desc);
for (Customer c : customers)
System.out.println(c);
System.out.println();
}
public static void main(String[] args) {
new Bank();
}
}
输入文件
1234,Joe,436.00
3620,Kim,600.00
4563,Jimmy,1653.00
4571,Billy,72.00
5930,Brian,962.00
6766,Jen,215.00
7092,Adam,8794.00
输出
### Sorting by Id ascending ###
#1234: Joe - $0436.00
#3620: Kim - $0600.00
#4563: Jimmy - $1653.00
#4571: Billy - $0072.00
#5930: Brian - $0962.00
#6766: Jen - $0215.00
#7092: Adam - $8794.00
### Sorting by Id descending ###
#7092: Adam - $8794.00
#6766: Jen - $0215.00
#5930: Brian - $0962.00
#4571: Billy - $0072.00
#4563: Jimmy - $1653.00
#3620: Kim - $0600.00
#1234: Joe - $0436.00
### Sorting by Name ascending ###
#7092: Adam - $8794.00
#4571: Billy - $0072.00
#5930: Brian - $0962.00
#6766: Jen - $0215.00
#4563: Jimmy - $1653.00
#1234: Joe - $0436.00
#3620: Kim - $0600.00
### Sorting by Name descending ###
#3620: Kim - $0600.00
#1234: Joe - $0436.00
#4563: Jimmy - $1653.00
#6766: Jen - $0215.00
#5930: Brian - $0962.00
#4571: Billy - $0072.00
#7092: Adam - $8794.00
### Sorting by Balance ascending ###
#4571: Billy - $0072.00
#6766: Jen - $0215.00
#1234: Joe - $0436.00
#3620: Kim - $0600.00
#5930: Brian - $0962.00
#4563: Jimmy - $1653.00
#7092: Adam - $8794.00
### Sorting by Balance descending ###
#7092: Adam - $8794.00
#4563: Jimmy - $1653.00
#5930: Brian - $0962.00
#3620: Kim - $0600.00
#1234: Joe - $0436.00
#6766: Jen - $0215.00
#4571: Billy - $0072.00