Requirement:Please try your best to DESIGN an application that
1. Reads the products from the file named data.txt,
2. Sort the products by Name, and print them on screen,
3. Sort the products by Supplier, and print them on screen.The format of the on screen output should looks like the content in the data.txt.In this application, you can show off your skill/understanding in OOP, pattern and architecture.
Category=Fruit
Name=Orange
Supplier=Orange Fruit Corp.
Price=12.00
LastUpdate=2006-03-15
Color=YellowCategory=Software
Name=Doors
Supplier=Macrosoft
Price=2000.00
LastUpdate=2005-07-03Category=Office
Name=Notebook
Supplier=Office Mate
Price=2.00
LastUpdate=2007-09-30
Spec=B5Category=Fruit
Name=Apple
Supplier=Apple Fruit Corp.
Price=10.00
LastUpdate=2006-01-01
Color=RedCategory=Software
Name=Linux
Supplier=Free Linux
Price=0.00
LastUpdate=2006-05-02
1. Reads the products from the file named data.txt,
2. Sort the products by Name, and print them on screen,
3. Sort the products by Supplier, and print them on screen.The format of the on screen output should looks like the content in the data.txt.In this application, you can show off your skill/understanding in OOP, pattern and architecture.
Category=Fruit
Name=Orange
Supplier=Orange Fruit Corp.
Price=12.00
LastUpdate=2006-03-15
Color=YellowCategory=Software
Name=Doors
Supplier=Macrosoft
Price=2000.00
LastUpdate=2005-07-03Category=Office
Name=Notebook
Supplier=Office Mate
Price=2.00
LastUpdate=2007-09-30
Spec=B5Category=Fruit
Name=Apple
Supplier=Apple Fruit Corp.
Price=10.00
LastUpdate=2006-01-01
Color=RedCategory=Software
Name=Linux
Supplier=Free Linux
Price=0.00
LastUpdate=2006-05-02
package sort;import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.*;
class ProductNameComparator implements Comparator<Product> {
public int compare(Product p1, Product p2) {
return p1.getName().compareTo(p2.getName());
}
}class ProductSupplierComparator implements Comparator<Product> {
public int compare(Product p1, Product p2) {
return p1.getSupplier().compareTo(p2.getSupplier());
}
}public class TestSortProduct { public static void main(String[] args) throws IOException {
ArrayList<Product> products = read("c:/data.txt");
Collections.sort(products, new ProductNameComparator());
System.out
.println("-------------------------sort by name---------------------------");
display(products);
Collections.sort(products, new ProductSupplierComparator());
System.out
.println("-------------------------sort by supplier-----------------------");
display(products);
} public static void display(ArrayList<Product> products) {
if (products == null)
return;
for (int i = 0; i < products.size(); i++) {
System.out.println(products.get(i));
;
}
} public static ArrayList<Product> read(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String s;
ArrayList<Product> products = new ArrayList<Product>();
StringBuffer sb = new StringBuffer(); Product product = new Product();
while ((s = br.readLine()) != null) { Matcher matcher = Pattern.compile("(.+?)=(.+)").matcher(s); if (matcher.find()) {
String key = matcher.group(1);
String value = matcher.group(2);
if (key.equals("Category")) {
product.setCategory(value);
} else if (key.equals("Name")) {
product.setName(value);
} else if (key.equals("Supplier")) {
product.setSupplier(value);
} else if (key.equals("Price")) {
product.setPrice(Float.parseFloat(value));//暂时不做异常处理
} else if (key.equals("LastUpdate")) { Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(value.substring(0, 4)),
Integer.parseInt(value.substring(5, 7)), Integer
.parseInt(value.substring(8, 10)));
product.setLastUpdate(calendar.getTime()); } else if (key.equals("Color")) {
product.setColor(value);
} else if (key.equals("Spec")) {
product.setSpec(value);
}
} if (s.trim().equals("")) {
products.add(product);
product = new Product(); }
} products.add(product);
br.close();
return products;
}}
class Product {
private String category;
private String name;
private String supplier;
private float price;// float是不精确的,这里练习暂时用double
private Date lastUpdate;//
private String color;
private String spec; public String toString() {// 控制输出格式
String temp = "\nCategory=" + category + "\nName=" + name
+ "\nSupplier=" + supplier
+ String.format("\nPrice=%.2f", price)
+ String.format("\nLastUpdate=")
+ new SimpleDateFormat("yyyy-MM-dd").format(lastUpdate);
if (color != null)
temp += "\nColor=" + color;
if (spec != null)
temp += "\nSpec=" + spec;
return temp;
} public String getCategory() {
return category;
} public void setCategory(String category) {
this.category = category;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSupplier() {
return supplier;
} public void setSupplier(String supplier) {
this.supplier = supplier;
} public double getPrice() {
return price;
} public void setPrice(float price) {
this.price = price;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public String getSpec() {
return spec;
} public void setSpec(String spec) {
this.spec = spec;
} public Date getLastUpdate() {
return lastUpdate;
} public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}}/*output:-------------------------sort by name---------------------------Category=Fruit
Name=Apple
Supplier=Apple Fruit Corp.
Price=10.00
LastUpdate=2006-02-01
Color=RedCategory=Software
Name=Doors
Supplier=Macrosoft
Price=2000.00
LastUpdate=2005-08-03Category=Software
Name=Linux
Supplier=Free Linux
Price=0.00
LastUpdate=2006-06-02Category=Office
Name=Notebook
Supplier=Office Mate
Price=2.00
LastUpdate=2007-10-30
Spec=B5Category=Fruit
Name=Orange
Supplier=Orange Fruit Corp.
Price=12.00
LastUpdate=2006-04-15
Color=Yellow
-------------------------sort by supplier-----------------------Category=Fruit
Name=Apple
Supplier=Apple Fruit Corp.
Price=10.00
LastUpdate=2006-02-01
Color=RedCategory=Software
Name=Linux
Supplier=Free Linux
Price=0.00
LastUpdate=2006-06-02Category=Software
Name=Doors
Supplier=Macrosoft
Price=2000.00
LastUpdate=2005-08-03Category=Office
Name=Notebook
Supplier=Office Mate
Price=2.00
LastUpdate=2007-10-30
Spec=B5Category=Fruit
Name=Orange
Supplier=Orange Fruit Corp.
Price=12.00
LastUpdate=2006-04-15
Color=Yellow
*/
1. Reads the products from the file named data.txt,
读取data.txt文件,其实你下面的那些数据应该是保存在一个名为data.txt文件中。
你把里面的信息读取出来,并解析成对象。(最终应该会是一个集合,因为里面有很多的数据)
2. Sort the products by Name, and print them on screen,
根据Name排序,然后打印到屏幕上面。
写一个类实现Comparator接口,然后按照name排序。打印就不用说了。
3. Sort the products by Supplier, and print them on screen.
这个和2是一样的,只是按照Supplier字段排序而已。