水了一点代码,记录一下
Product类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ProductOrder;

public class Product {
private String code;
private String description;
private double price;

public String getCode() {
return code;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
public Product() {

}
public Product(String initialCode,String initialDescription,double initialPrice) {
this.code = initialCode;
this.description = initialDescription;
this.price = initialPrice;
}
public boolean equals(Object object) {
if(object instanceof Product) {
return ((Product)object).getCode().equals(this.getCode());
}
else
return false;
}
public String toString() {
return code + "_" + description + "_" + price;
}
}

OrderItem类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package ProductOrder;

public class OrderItem {
private int quantity;
private Product product;
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int newQuantity) {
this.quantity = newQuantity;
}
public double getValue() {
return product.getPrice() * quantity;
}
public String toString() {
return quantity + " " + product.getCode() + " " + product.getPrice();
}
public OrderItem(Product initialProduct,int initialQuantity) {
this.product = initialProduct;
this.quantity = initialQuantity;
}
}

Coffee类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package ProductOrder;

public class OrderItem {
private int quantity;
private Product product;
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int newQuantity) {
this.quantity = newQuantity;
}
public double getValue() {
return product.getPrice() * quantity;
}
public String toString() {
return quantity + " " + product.getCode() + " " + product.getPrice();
}
public OrderItem(Product initialProduct,int initialQuantity) {
this.product = initialProduct;
this.quantity = initialQuantity;
}
}

CoffeeBrewer类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package ProductOrder;

public class CoffeeBrewer extends Product{
String model;
String waterSupply;
int numberOfCups;
public CoffeeBrewer(String iniCode,String iniDescription,double iniPrice
,String iniModel,String iniWaterSupply,int iniNumberOfCups) {
super(iniCode,iniDescription,iniPrice);
model = iniModel;
waterSupply = iniWaterSupply;
numberOfCups = iniNumberOfCups;
}
public String getModel() {
return model;
}
public String getWaterSupply() {
return waterSupply;
}
public int getNumberOfCups() {
return numberOfCups;
}
public String toString() {
return super.getCode() + "_" + super.getDescription() + "_" + super.getPrice() + "_"
+ model + "_" + waterSupply + "_" + numberOfCups;
}
}

TestProduct类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ProductOrder;
import java.io.*;
import java.util.*;

public class TestProduct {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String str1,str2,str3;
double dou1 = 0;
Product pro1 = new Product("001", "Apple", 3.99);
Product pro2 = new Product("002", "Banana", 5.99);
Product pro3 = new Product("003", "Canyon", 21.99);
Product pro4 = new Product("004", "Dove", 6.99);
Product pro5 = new Product("004", "Eggs", 10.99);

OrderItem bill1 = new OrderItem(pro1, 5);
OrderItem bill2 = new OrderItem(pro1, 20);
OrderItem bill3 = new OrderItem(pro2, 25);
OrderItem bill4 = new OrderItem(pro5, 3);
OrderItem bill5 = new OrderItem(pro4, 7);
OrderItem bill6 = new OrderItem(pro1, 0);
OrderItem bill7 = new OrderItem(pro5, 999);
OrderItem bill8 = new OrderItem(pro5, 12);
OrderItem bill9 = new OrderItem(pro2, 2);
OrderItem bill10 = new OrderItem(pro3, 100);

Coffee cof1 = new Coffee("01","Cappuccino",15.2,"American","Cure","Sweet","Heavy","Small","Oval");

CoffeeBrewer ord1 = new CoffeeBrewer("001","SUNNY",169.99,"Abrasive","PrueWater",3);
BufferedReader buf = null;//声明 BufferedReader 对象
buf = new BufferedReader(new InputStreamReader(System.in));//实例化 BufferedReader
String str = null;//接收输入内容
System.out.print("请输入新建Product,以“,”分开:");
str = buf.readLine();//读取输入内容
StringTokenizer st=new StringTokenizer(str,",");
str1 = st.nextToken();
str2 = st.nextToken();
str3 = st.nextToken();
dou1 = Double.parseDouble(str3);
Product pro6 = new Product(str1,str2,dou1);
if(pro4.equals(pro5)) {
System.out.println("Yes!");
}
else {
System.out.println("No!");
}

System.out.println(bill10);
System.out.println(cof1);
System.out.println(pro6);
System.out.println(ord1.getPrice());
System.out.println(bill1.getQuantity());
System.out.println(bill2.getProduct().getDescription());

}

}

本次作业考察了java基础类、类的继承的基础语法,以及tokenizer、bufferreader等库的使用,总体来说难度不大。