1.管路员登录(账号密码固定admin/admin)
2.修改管理员密码
3.商品添加
4.商品列表
5.查询指定id商品
6.根据商品id删除商品
7.根据id修改商品价格
8.根据id修改指定商品库存
9.根据商品类别查询所有商品
10.查询指定价格区间的商品信息
根据项目要求,我们首先分析题目,知道此项目至少四个类:管理员类、商品类、系统类、测试类。
由于我们决定对此系统进行优化,要求能够对数据进行保存,所以我们用流的相关知识,将数据写入已经创建好的文档中,使其自动读写,以便管理,三个文档分别用于保存商品信息、id、user。
首先是user类,
package com01;
import java.io.Serializable;
@SuppressWarnings("serial")
public class User implements Serializable{
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + "]";
}
}
然后是Goods类
package com01;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Goods implements Serializable {
private int id;
private String name;
private String typeName;
private double price;
private int count;
// 产地
private String placeOrigin;
// 单位
private String units;
public Goods() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getPlaceOrigin() {
return placeOrigin;
}
public void setPlaceOrigin(String placeOrigin) {
this.placeOrigin = placeOrigin;
}
public String getUnits() {
return units;
}
public void setUnits(String units) {
this.units = units;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", typeName=" + typeName + ", price=" + price + ", count=" + count
+ ", placeOrigin=" + placeOrigin + ", units=" + units + "]";
}
}
GoodsSystem类:
package com01;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class GoodsSystem {
private User user;
private Scanner scanner;
private ArrayList<Goods> goodss;
private int maxId;
public GoodsSystem() {
// TODO Auto-generated constructor stub
getSystemUser();
readMaxId();
readGoodss();
this.scanner = new Scanner(System.in);
}
// 登录
public void login() {
// TODO Auto-generated method stub
String userName;
String password;
System.out.println("欢迎来到德莱Store!");
System.out.print("请输入用户名:");
userName = scanner.nextLine();
System.out.print("请输入密码:");
password = scanner.nextLine();
if (user.getUserName().equals(userName) && user.getPassword().equals(password)) {
System.out.println("登录成功!!!");
menuFromSelect();
} else {
System.out.println("用户名和密码不匹配,请重新输入!");
login();
}
}
// 得到账户中的信息
public void getSystemUser() {
// TODO Auto-generated method stub
File file = new File("E:\\软帝培训\\user.txt");
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
if (obj instanceof User) {
this.user = (User) obj;
}
} catch (EOFException e) {
this.user = new User();
user.setUserName("admin");
user.setPassword("admin");
writeUserToLocal();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 把内存中的用户信息写入本地
public void writeUserToLocal() {
// TODO Auto-generated method stub
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("E:\\软帝培训\\user.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(user);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 打印菜单
public String printMenu() {
// TODO Auto-generated method stub
System.out.println("\t");
System.out.println("\t欢迎来到商城管理员界面");
System.out.println("---------------------------");
System.out.println("1.商品列表");
System.out.println("2.商品添加");
System.out.println("3.根据id查询指定商品");
System.out.println("4.根据商品类别查询所有商品");
System.out.println("5.查询指定价格区间的商品信息");
System.out.println("6.根据id删除商品根据");
System.out.println("7.根据id指定商品价格");
System.out.println("8.根据id修改商品库存");
System.out.println("9.修改密码");
System.out.println("10.退出系统");
System.out.println(">");
return scanner.nextLine();
}
// 菜单查询界面
public void menuFromSelect() {
// TODO Auto-generated method stub
String result = printMenu();
switch (result) {
case "1":
goodsList();
menuFromSelect();
break;
case "2":
addGoods();
menuFromSelect();
break;
case "3":
findListById();
menuFromSelect();
break;
case "4":
findListBytypeName();
menuFromSelect();
break;
case "5":
findListByPrice();
menuFromSelect();
break;
case "6":
deleteGoods();
menuFromSelect();
break;
case "7":
setPrice();
menuFromSelect();
break;
case "8":
reCountById();
menuFromSelect();
break;
case "9":
rePsw();
menuFromSelect();
break;
case "10":
break;
}
}
// 读取本地ID
public void readMaxId() {
// TODO Auto-generated method stub
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("E:\\软帝培训\\id.txt");
br = new BufferedReader(fr);
this.maxId = Integer.parseInt(br.readLine());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 把最大id写入本地
public void writeMaxId() {
FileWriter fw = null;
try {
fw = new FileWriter("E:\\软帝培训\\id.txt");
fw.write(this.maxId + "");
// fw.write(this.maxId);
fw.flush();// 刷新
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 添加商品
public void addGoods() {
// TODO Auto-generated method stub
Goods goods = new Goods();
goods.setId(maxId + 1);
System.out.print("请输入商品名称:");
goods.setName(scanner.nextLine());
System.out.print("请输入商品分类:");
goods.setTypeName(scanner.nextLine());
System.out.print("请输入商品价格:");
goods.setPrice(Double.parseDouble(scanner.nextLine()));
System.out.print("请输入商品库存:");
goods.setCount(Integer.parseInt(scanner.nextLine()));
System.out.print("请输入商品产地:");
goods.setPlaceOrigin(scanner.nextLine());
System.out.print("请输入商品计量单位:");
goods.setUnits(scanner.nextLine());
goodss.add(goods);
writeGoodssToLocal();
maxId++;
writeMaxId();
System.out.println("商品添加成功!!!");
}
// 读取商品信息
public void readGoodss() {
// TODO Auto-generated method stub
File file = new File("E:\\软帝培训\\goodss.txt");
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
if (obj instanceof ArrayList) {
this.goodss = (ArrayList<Goods>) obj;
}
} catch (EOFException e) {
// 读取失败,第一次创建,所以需要创建一个默认账户
this.goodss = new ArrayList<Goods>();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 将商品信息写入本地
public void writeGoodssToLocal() {
// TODO Auto-generated method stub
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("E:\\软帝培训\\goodss.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(goodss);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 根据价格查询商品信息
public void findListByPrice() {
System.out.println("请输入最低价格:");
double minPrice = Double.parseDouble(scanner.nextLine());
System.out.println("请输入最高价格:");
double maxPrice = Double.parseDouble(scanner.nextLine());
boolean b = false;
for (Goods goods : goodss) {
if (goods.getPrice() > minPrice && goods.getPrice() < maxPrice) {
b = true;
System.out.println(goods);
}
}
if (!b) {
System.out.println("没有在此区间的商品!");
}
}
// 根据id寻找商品信息
private int findListById() {
System.out.print("请输入您要查询的ID:");
int id = Integer.parseInt(scanner.nextLine());
int index = findListById(id);
if (index != -1) {
System.out.println(goodss.get(index));
} else {
System.out.println("没有查询到id为:" + id + "的商品");
}
return index;
}
private int findListById(int id) {
int index = Utils.halfQuery(goodss, id, new HalfQueryInter<Goods>() {
@Override
public double getNumber(Goods g) {
// TODO Auto-generated method stub
return g.getId();
}
});
return index;
}
// 根据类别寻找商品信息
public void findListBytypeName() {
// TODO Auto-generated method stub
boolean b = false;
System.out.println("请输入你要寻找的类别:");
String type = scanner.nextLine();
for (Goods goods : goodss) {
if (goods.getTypeName().equals(type)) {
System.out.println(goods);
b = true;
}
}
if (!b) {
System.out.println("查无此商品!");
}
}
// 根据id修改库存
public void reCountById() {
// TODO Auto-generated method stub
System.out.println("请输入你要查找的商品id:");
int id = Integer.parseInt(scanner.nextLine());
int index = findListById(id);
if (index == -1) {
System.out.println("没有你要找的商品!");
} else {
System.out.println("请输入你要修改的库存:");
int count = Integer.parseInt(scanner.nextLine());
goodss.get(index).setCount(count);
System.out.println("修改成功!");
writeGoodssToLocal();
}
}
// 根据id修改商品价格
public void setPrice() {
// TODO Auto-generated method stub
System.out.print("请输入你要查找的商品id");
int id = Integer.parseInt(scanner.nextLine());
int index = findListById(id);
if (index == -1) {
System.out.println("查无此商品");
} else {
System.out.println("请输入你要修改的价格:");
double price = Double.parseDouble(scanner.nextLine());
goodss.get(index).setPrice(price);
writeGoodssToLocal();
System.out.println("价格修改成功!");
}
}
// 删除商品
public void deleteGoods() {
// TODO Auto-generated method stub
System.out.println("请输入你要删除的商品id:");
int id = Integer.parseInt(scanner.nextLine());
int index = findListById(id);
if (index == -1) {
System.out.println("没有你找的商品!");
} else {
goodss.remove(index);
System.out.println("删除成功!");
writeGoodssToLocal();
for (Goods goods : goodss) {
System.out.println(goods);
}
}
}
// 修改密码
public void rePsw() {
// TODO Auto-generated method stub
String lastPsw;
String newPsw1;
String newPsw2;
System.out.print("请输入原始密码:");
lastPsw = scanner.nextLine();
if (!this.user.getPassword().equals(lastPsw)) {
System.out.println("密码错误请重新输入!");
return;
}
System.out.print("请输入新密码:");
newPsw1 = scanner.nextLine();
System.out.print("请再次输入新密码:");
newPsw2 = scanner.nextLine();
if (newPsw1.equals(newPsw2)) {
this.user.setPassword(newPsw2);
writeUserToLocal();
System.out.println("修改成功!!!");
} else {
System.out.println("两次密码输入不一致,修改失败!!!");
return;
}
}
// 商品列表
public void goodsList() {
// TODO Auto-generated method stub
if (goodss.size() == 0) {
System.out.println("没有任何商品!");
return;
}
for (Goods goods : goodss) {
System.out.println(goods);
}
}
}
因为我们需要查找id这个功能能运用到折半查询,所以我们创建了一个工具类和一个接口,将折半查询封装起来以便调用。
工具 Utils类:
package com01;
import java.util.List;
public class Utils {
public static int query(int[] buf, int n) {
// TODO Auto-generated method stub
for (int i = 0; i < buf.length; i++) {
if (buf[i] == 0) {
return i;
}
}
return -1;
}
public static <T> int halfQuery(List<T> buf, int n,HalfQueryInter<T> h) {
return halfQuerySys(buf,n,0, buf.size(),h);
}
private static <T> int halfQuerySys(List<T> buf, int n, int sta, int end, HalfQueryInter<T> h) {
// TODO Auto-generated method stub
if (sta > end) {
return -1;
}
int half = (sta + end) / 2;
if(h.getNumber(buf.get(half)) == n) {
return half;
}else if(h.getNumber(buf.get(half)) > n) {
return halfQuerySys(buf,n,sta,half-1,h);
}else {
return halfQuerySys(buf,n,half+1,end,h);
}
}
}
接口HalfQueryInter
package com01;
public interface HalfQueryInter<T> {
public double getNumber(T t);
}
最后测试类:
package com01;
public class Test {
public static void main(String[] args) {
GoodsSystem gs = new GoodsSystem();
gs.login();
}
}