二、音像店管理 1、音像店现有影碟《功夫》《蜘蛛侠》《我机器人》《夏洛特烦恼》等,库存20 2、影碟的购买,查询影碟是否存在,如果存在,判断是否有库存,如果有则库存减1。使用java语言中arraylist
时间: 2023-05-14 07:07:03 浏览: 72
可以使用以下代码实现音像店管理:
import java.util.ArrayList;
public class VideoStore {
private ArrayList<String> movies = new ArrayList<String>();
private ArrayList<Integer> stock = new ArrayList<Integer>();
public VideoStore() {
// 初始化影碟库存
movies.add("功夫");
stock.add(20);
movies.add("蜘蛛侠");
stock.add(20);
movies.add("我机器人");
stock.add(20);
movies.add("夏洛特烦恼");
stock.add(20);
}
public boolean buyMovie(String movie) {
int index = movies.indexOf(movie);
if (index == -1) {
// 影碟不存在
return false;
} else {
int s = stock.get(index);
if (s == 0) {
// 库存不足
return false;
} else {
// 库存减1
stock.set(index, s - 1);
return true;
}
}
}
}
使用时可以这样调用:
VideoStore store = new VideoStore();
if (store.buyMovie("功夫")) {
System.out.println("购买成功");
} else {
System.out.println("购买失败");
}