《Java语言程序设计》课程
实训教学实习报告
一.问题描述和设计思想
1.问题描述
本设计所开发实现的是一个迷你超市管理系统,主要用于购买超市物品。简述系统主要的功能或作用;简述系统的主要特点。
主要功能分为三大模块:
对管理员进行添加信息,删除信息,修改信息,查询信息等。
对商品的增添商品,修改商品,删除商品,查询商品等。
购买已经添加的商品进行购买,以及对订单的查询
2.设计思想
先设置一个商品类,其中里面包括商品的所有属性,在主类中创建一个商品类ArrayList集合,把商品的所有属性添加到集合中。分别创建增删改查的方法。
设置会员管理类。该类数据依旧是根据ArrayList的集合把会员所有的属性值进行储存。再次基础上,依次创建增删改查的方法,对会员的属性值进行操作。
在前两个基础上,登录会员系统对商品进行操作,并记录
根据记录,把全部的消费记录打印到控制台上
1.主要功能
超市管理系统分为三个模块:第一个模块是商品的操作,第二个是会员的管理,第三个是商品的购买以及购买的记录。
2.系统结构
图1 学生信息管理系统功能结构图
三.采用的类设计、相关技术和核心代码
1.主要的类设计
姓名系统共定义了三个类,前两个类是定义商品和会员的属性值,最后一个是主类,是程序的执行入口
public class Users {
int id;//用户id
String name;//名称
String password;//密码
int num;//卡号
double balance;//余额
}
public class Item {
int id;//商品id编号
String name;//商品名称
int count;//数量
double price;//单价
}
public class Shop {
public static void main(String [] args) throws IOException {
ArrayList<Item> goods=new ArrayList<>();
initGoods(goods);
ArrayList<Users> users=new ArrayList<>();
FileWriter fw=new FileWriter("D:\\杂文件\\interview\\c.txt");
fw.write("商品编号 商品名称 商品单价 商品数量\n");
fw.close();
System.out.println("=====超市管理系统=====");
while(true){
System.out.println("1.商品管理");
System.out.println("2.会员管理");
System.out.println("3.商品购买及查询");
System.out.println("4.退出");
Scanner sc=new Scanner(System.in);
System.out.println();
System.out.println("请选择主功能:");
int choice = sc.nextInt();
.....
2.关键操作/技术及实现代码
商品初始化操作
文字描述:先在商品的arraylist的集合中初始化一些元素
代码实现:public static void initGoods( ArrayList<Item> goods){
Item i1=new Item();
//第一个商品
i1.id=202201;
i1.name="苹果";
i1.count=100;
i1.price=10;
Item i2=new Item();
i2.id=202202;
i2.name="香蕉";
i2.count=30;
i2.price=15;
goods.add(i1);
goods.add(i2);
}
(1)商品信息增加操作
文字描述:键盘录入商品的信息,并把输入的信息存储到集合中
代码实现:
public static void addGoods(ArrayList<Item> goods){//添加商品
System.out.println("选择的是添加商品");
Scanner sc=new Scanner(System.in);
System.out.println("请输入商品的编号id:");
int id=sc.nextInt();
System.out.println("请输入商品的名称:");
String name=sc.next();
System.out.println("请输入商品的单价:");
double price=sc.nextInt();
System.out.println("请输入商品的数量:");
int count=sc.nextInt();
//创建商品对象
Item i=new Item();
i.id=id;
i.name=name;
i.price=price;
i.count=count;
goods.add(i);
System.out.println("添加成功");
}//end add
(2)商品信息删除操作
文字描述:先遍历集合,输入需要删除的id,如果集合存在,直接利用remove删除
代码实现:public static void delGoods(ArrayList<Item> goods){
System.out.println();
System.out.println("选择的是要删除的编号");
Scanner sc=new Scanner(System.in);
int id=sc.nextInt();
for (int i = 0; i < goods.size(); i++) {
Item item= goods.get(i);
if(item.id==id){
goods.remove(item);
System.out.println("删除成功");
break;
}
}//end if
}//end del
(3)商品信息查询操作
文字描述:遍历集合,分别把信息打印到控制台上,并利用IO流保存到文件中
代码实现:public static void showGoods(ArrayList<Item> goods) throws IOException {
System.out.println();
System.out.println("=====商品清单=====");
System.out.println("商品编号 商品名称 商品单价 商品数量");
for (int i = 0; i < goods.size(); i++) {
Item item= goods.get(i);
System.out.println(item.id+" "+item.name+" "+ item.price+" "+item.count);
}
Item item = null;
for (int i = 0; i < goods.size(); i++) {
item = goods.get(i);
}
FileWriter fw=new FileWriter("D:\\杂文件\\interview\\c.txt",true);
//fw.write("商品编号 商品名称 商品单价 商品数量\n" );
fw.write(item.id+" "+item.name+" "+ item.price+" "+item.count+"\n");
fw.close();
}
(4)商品信息修改操作
文字描述:输入要修改的商品,并判断是否存在。存在则遍历集合,依次把信息进行修改
代码实现:public static void updateGoods(ArrayList<Item> goods){
System.out.println();
System.out.println("选择的是修改功能");
System.out.println("请选择你要修改的商品的编号:");
Scanner sc=new Scanner(System.in);
int id=sc.nextInt();
for (int i = 0; i < goods.size(); i++) {
Item t= goods.get(i);
if(t.id==id){
System.out.println("请输入新的商品编号:");
t.id=sc.nextInt();
System.out.println("请输入新的商品名称:");
t.name=sc.next();
System.out.println("请输入新的商品单价:");
t.price=sc.nextInt();
System.out.println("请输入新的商品数量:");
t.count=sc.nextInt();
System.out.println("修改成功");break;
}
}//end for
}//end update
(5)用户添加操作
文字描述:键盘录入,依次进行添加,并保存到集合中
代码实现:public static void addUsers(ArrayList<Users> users){
System.out.println("选择的是添加用户");
System.out.println();
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户的id:");
int id=sc.nextInt();
System.out.println("请输入用户的名称:");
String name=sc.next();
System.out.println("请输入用户的卡号:");
int num=sc.nextInt();
System.out.println("请输入用户密码:");
String password=sc.next();
System.out.println("请输入用户的余额");
double balance=sc.nextInt();
Users u=new Users();
u.id=id;
u.name=name;
u.num=num;
u.password=password;
u.balance=balance;
users.add(u);
System.out.println("添加成功");
}//end addusers
(6)商品修改操作
文字描述:输入要遍历的集合,如果存在,则遍历用户集合,并依次进行修改,再保存到用户集合中。
代码实现:public static void updateUsers( ArrayList<Users> users){
System.out.println();
System.out.println("选择的是修改功能");
Scanner sc=new Scanner(System.in);
System.out.println();
System.out.println("请输入要修改的用户的id:");
int id=sc.nextInt();
for (int i = 0; i < users.size(); i++) {
Users s=users.get(i);
if(s.id==id){
System.out.println("请输入新的用户id:");
s.id=sc.nextInt();
System.out.println("请输入新的用户名称:");
s.name=sc.next();
System.out.println("请输入新的用户密码:");
s.password=sc.next();
System.out.println("请输入新的用户卡号:");
s.num=sc.nextInt();
System.out.println("请输入新的用户余额");
s.balance=sc.nextDouble();
System.out.println("修改成功");
break;
}//edn if
if(s.id!=id){
System.out.println("该用户不存在");break;
}
}//end for
}//end updateusers
(7)用户查询操作
文字描述:遍历用户集合,并打印所需要的信息
代码实现:public static void showUsers(ArrayList<Users> users){
System.out.println();
System.out.println("用户列表");
System.out.println("用户id 用户名称 用户密码 用户卡号 用户余额");
for (int i = 0; i < users.size(); i++) {
Users u= users.get(i);
System.out.println(u.id+" "+u.name+" "+u.password+" "+u.num+" "+u.balance);
}
}
(8)用户删除操作
文字描述:键盘录入所删除用户的id,遍历集合是否存在。存在,则删除成功。不存在,则要删除的用户不存在
代码实现:
public static void delUsers(ArrayList<Users> users){
System.out.println();
System.out.println("选择的是删除用户功能");
System.out.println();
System.out.println("请输入要删除用户的id");
Scanner sc=new Scanner(System.in);
int id =sc.nextInt();
for (int i = 0; i < users.size() ; i++) {
Users u= users.get(i);
if(u.id==id){
users.remove(u);
System.out.println("删除成功");break;
}//end if
if(u.id!=id){
System.out.println("该用户不存在");break;
}
}//end for
购买商品和查询操作
文字描述:先后遍历两个集合,然后输入已经存在的用户,进行登录操作,再进行购买商品,并计算出商品的最终数量以及用户的余额,再把商品的记录利用IO流存储到文件。最后输入yes进行查询用户的消费记录
代码实现:
public static void buyGoods(ArrayList<Users> users,ArrayList<Item> goods,int code) throws IOException {
System.out.println();
Users u=null;Item m=null;
FileWriter fw2 = new FileWriter("D:\\杂文件\\interview\\d.txt");
fw2.write("下单编号:" +code+"\t"+"下单日期:"+System.currentTimeMillis()+"\n\r");
fw2.write("--商品--"+"\n\r");
fw2.close();
for (int i = 0; i < users.size(); i++) {
u= users.get(i); }//end for
System.out.println("--登录系统--");
System.out.println();
System.out.println("请输入要登录用户的id:");
Scanner sc=new Scanner(System.in);
int id= sc.nextInt();
if(u.id==id){
System.out.println("登录成功");
System.out.println();
System.out.println("请输入商品的id:");
Scanner sc4=new Scanner(System.in);
int idGoods= sc4.nextInt();
if(u.id!=id){
System.out.println("不存在该商品的id");
}
for (int j = 0; j < goods.size(); j++) {
m= goods.get(j);Users us=null;
for (int k = 0; k < users.size(); k++) {
us=users.get(k);
}//end forusers
if(idGoods==m.id){
showGoods(goods);
System.out.println();
System.out.println("请输入商品购买的数量:");
Scanner sc5=new Scanner(System.in);
int shu=sc5.nextInt();
m.count=m.count-shu;
us.balance= us.balance-shu*m.price;
if(us.balance<0){
System.out.println("余额不足");
}
showGoods(goods);
showUsers(users);
code++;
FileWriter fw3=new FileWriter("D:\\杂文件\\interview\\d.txt",true);
fw3.write("商品名:"+m.name+"\t"+"数量:"+m.count+"\t"+m.price);
fw3.close();
}//end if
}//end forgoods
}//end if
if(u.id!=id){
System.out.println("该用户不存在");
}
System.out.println("是否查询订单(yes:查询 no:不查询):");
Scanner sc6=new Scanner(System.in);
String justice= sc.next();
if(justice.equals("yes")){
System.out.println( "商品名:"+m.name+"\t"+"数量:"+m.count+"\t"+m.price);
}
}//end buyGoods
四.系统运行结果截图
1. 商品功能
(1)注册/登录前界面截图
(2)添加商品面截图
用户功能
(1)用户添加
(3)购买商品
查询订单
五.遇到的问题和解决方法
1. 遇到的问题
IO流的使用,把数据保存到文件中,会覆盖上面数据以及文件换行。
添加下单日期时,保存到文件里,出现很多的字符
3.思想上单一,考虑问题不周到,在用户和商品添加和删除时少考虑不再存在的情况,接下来该怎么办,输入非法字符怎么办
2. 解决方法
1.在filerwriter后面添加true,在writer中添加/n/r(windows系统)
2.删除下单日期,不保存在文件中
3.用if来单独列出不存在的情况,进行单独考虑
六.总结和体会
1. 总结
超市管理系统主要是为了方便超市管理者对超市的商品、用户及销售情况进行实时监控和管理。通过超市管理系统,可以实现商品和用户的增删改查等功能,也可以实现购买记录的记录和查询。超市管理系统需要包括商品管理和用户管理两个模块,以及购买记录的记录和查询功能。可以根据实际需求选择相应的功能进行设计。对集合和类的把握,对IO流文件的储存商品,用户购买记录等信息来保证数据的可观性。通过Java实现了商品和用户管理,购买记录的记录和查询等功能,提高了超市的管理效率和服务质量。
体会
自己创建的超市管理系统功能设计需要尽可能满足实际的需求:超市管理系统必须满足管理者对商品、用户、销售情况等方面的需要,同时也需要具备方便用户购买的功能。也会从系统的易用性非常重要:界面要清晰直观,操作要简单易懂,否则会增加管理员的工作量,降低系统的使用效率的角度上考虑其价值的实用性。同时
系统的数据备份要做好,记录管理员的操作过程,有利于管理者及时发现问题并进行处理。
总的来说,超市管理系统的设计尽可能达到全面、合理、易用和安全,只有如此才能真正满足超市的管理需求,提高管理效率和服务质量。