设计Appliances类,包含品牌brand和电器的价格price。定义getter/setter方法,brand要求品牌至少4位,否则抛出异常,价格price要求价格必须大于等于0, 否则抛出异常,定义测试类Test, 利用File类建立goodsInfo.xls文件,利用jxl.jar包将商品信息存入goodsInfo.xls文件,实现数据的增删改查。
时间: 2024-03-03 22:49:41 浏览: 61
以下是一个完整的 Java 实现,并实现了数据的增删改查:
```
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class Appliances {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) throws Exception {
if (brand.length() < 4) {
throw new Exception("Brand must be at least 4 characters.");
}
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) throws Exception {
if (price < 0) {
throw new Exception("Price cannot be negative.");
}
this.price = price;
}
public static void main(String[] args) {
List<Appliances> goods = new ArrayList<>();
Appliances a1 = new Appliances();
try {
a1.setBrand("Brand1");
a1.setPrice(100.0);
} catch (Exception e) {
e.printStackTrace();
}
goods.add(a1);
Appliances a2 = new Appliances();
try {
a2.setBrand("Brand2");
a2.setPrice(200.0);
} catch (Exception e) {
e.printStackTrace();
}
goods.add(a2);
File file = new File("goodsInfo.xls");
try {
WritableWorkbook workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet("Goods", 0);
sheet.addCell(new Label(0, 0, "Brand"));
sheet.addCell(new Label(1, 0, "Price"));
int row = 1;
for (Appliances a : goods) {
sheet.addCell(new Label(0, row, a.getBrand()));
sheet.addCell(new Label(1, row, String.valueOf(a.getPrice())));
row++;
}
workbook.write();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
// Read and modify data using jxl.jar
try {
Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook writableWorkbook = Workbook.createWorkbook(file, workbook);
WritableSheet sheet = writableWorkbook.getSheet(0);
// Add a new appliance
Appliances a3 = new Appliances();
a3.setBrand("Brand3");
a3.setPrice(300.0);
goods.add(a3);
int row = sheet.getRows();
sheet.addCell(new Label(0, row, a3.getBrand()));
sheet.addCell(new Label(1, row, String.valueOf(a3.getPrice())));
// Modify an existing appliance
for (int i = 1; i < sheet.getRows(); i++) {
Cell brandCell = sheet.getCell(0, i);
if (brandCell.getContents().equals("Brand2")) {
Cell priceCell = sheet.getCell(1, i);
double price = Double.parseDouble(priceCell.getContents());
price *= 1.1;
sheet.addCell(new Label(1, i, String.valueOf(price)));
a2.setPrice(price);
break;
}
}
// Delete an appliance
for (int i = 1; i < sheet.getRows(); i++) {
Cell brandCell = sheet.getCell(0, i);
if (brandCell.getContents().equals("Brand1")) {
sheet.removeRow(i);
goods.remove(a1);
break;
}
}
writableWorkbook.write();
writableWorkbook.close();
} catch (Exception e) {
e.printStackTrace();
}
// Print all appliances
for (Appliances a : goods) {
System.out.println(a.getBrand() + ": " + a.getPrice());
}
}
}
```
这个实现在 `main` 方法中,首先创建了两个 `Appliances` 对象,并将它们添加到一个 `ArrayList` 中,然后使用 `jxl.jar` 库将商品信息写入 Excel 文件 `goodsInfo.xls` 中。接着,使用 `jxl.jar` 库读取并修改了文件中的数据,包括增加一个新的电器、修改一个现有的电器和删除一个电器。最后,打印了所有的电器。
阅读全文