设计Appliances类,包含品牌brand和电器的价格price。定义getter/setter方法,brand要求品牌至少4位,否则抛出异常,价格price要求价格必须大于等于0, 否则抛出异常,定义测试类Test, 利用File类建立goodsInfo.xls文件,利用jxl.jar包将商品信息存入goodsInfo.xls文件,实现数据的增删改查。
时间: 2024-03-03 17:49:20 浏览: 59
好的,这是一个关于Java编程的问题。下面是我的回答:
首先,我们需要设计一个Appliances类,包含品牌brand和电器的价格price属性,代码如下:
```java
public class Appliances {
private String brand;
private double price;
public Appliances(String brand, double price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) throws Exception {
if (brand.length() < 4) {
throw new Exception("品牌名必须至少为4个字符!");
}
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) throws Exception {
if (price < 0) {
throw new Exception("价格必须大于等于0!");
}
this.price = price;
}
}
```
然后,我们需要编写一个测试类Test,实现数据的增删改查。
首先,我们需要引入jxl.jar包,然后利用File类创建goodsInfo.xls文件。代码如下:
```java
import java.io.File;
import jxl.Workbook;
public class Test {
public static void main(String[] args) {
try {
File file = new File("goodsInfo.xls");
if (!file.exists()) {
file.createNewFile();
Workbook workbook = Workbook.createWorkbook(file);
workbook.createSheet("商品信息", 0);
workbook.write();
workbook.close();
System.out.println("创建文件成功!");
} else {
System.out.println("文件已存在!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
接下来,我们需要实现数据的增删改查。我们可以使用jxl.jar包提供的API,从goodsInfo.xls文件中读取数据,然后进行增删改查操作。代码如下:
```java
import java.io.File;
import java.util.ArrayList;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Test {
public static void main(String[] args) {
try {
File file = new File("goodsInfo.xls");
if (!file.exists()) {
file.createNewFile();
Workbook workbook = Workbook.createWorkbook(file);
workbook.createSheet("商品信息", 0);
workbook.write();
workbook.close();
System.out.println("创建文件成功!");
} else {
System.out.println("文件已存在!");
}
// 读取商品信息
Workbook workbook = Workbook.getWorkbook(file);
Sheet sheet = workbook.getSheet(0);
ArrayList<Appliances> list = new ArrayList<>();
for (int i = 1; i < sheet.getRows(); i++) {
Cell[] row = sheet.getRow(i);
String brand = row[0].getContents();
double price = Double.parseDouble(row[1].getContents());
Appliances appliances = new Appliances(brand, price);
list.add(appliances);
}
workbook.close();
// 添加商品信息
list.add(new Appliances("Haier", 3999));
list.add(new Appliances("Midea", 2999));
list.add(new Appliances("Gree", 4999));
// 修改商品信息
list.get(0).setBrand("Hisense");
list.get(0).setPrice(4999);
// 删除商品信息
list.remove(1);
// 保存商品信息
WritableWorkbook writableWorkbook = Workbook.createWorkbook(file);
WritableSheet writableSheet = writableWorkbook.createSheet("商品信息", 0);
writableSheet.addCell(new Label(0, 0, "品牌"));
writableSheet.addCell(new Label(1, 0, "价格"));
for (int i = 0; i < list.size(); i++) {
Appliances appliances = list.get(i);
writableSheet.addCell(new Label(0, i + 1, appliances.getBrand()));
writableSheet.addCell(new Label(1, i + 1, Double.toString(appliances.getPrice())));
}
writableWorkbook.write();
writableWorkbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上代码实现了数据的增删改查,并将修改后的数据保存到goodsInfo.xls文件中。
阅读全文