ismohney-and-curlie

java练习---简单电影购票系统项目

  介绍:购票系统项目,整个项目分商家和用户两块,比ATM系统稍微复杂,集合循环的嵌套运用更灵活。

商家功能:上架、下架、查询

用户功能:购票、退票、查询、浏览

一、Bean类

1、Fare 电影票类

 1 /**
 2  * 电影票Bean:电影名、类型、票价
 3  * */
 4 public class Fare {
 5     private String MovName ;
 6     private String type;
 7     private int price;
 8     private int account; //卖出的数量
 9 
10     public Fare() {
11     }
12 
13     public Fare(String movName, String type, int price,int account) {
14         this.MovName = movName;
15         this.type = type;
16         this.price = price;
17         this.account=account;
18     }
19 
20     @Override
21     public String toString() {
22         return "Fare{" +
23                 "MovName='" + MovName + '\'' +
24                 ", type='" + type + '\'' +
25                 ", price=" + price +
26                 ", account=" + account +
27                 '}';
28     }
29 
30     public void setAccount(int account) {
31         this.account = account;
32     }
33 
34     public int getAccount() {
35         return account;
36     }
37 
38     public void setMovName(String movName) {
39         MovName = movName;
40     }
41 
42     public void setType(String type) {
43         this.type = type;
44     }
45 
46     public void setPrice(int price) {
47         this.price = price;
48     }
49 
50     public String getMovName() {
51         return MovName;
52     }
53 
54     public String getType() {
55         return type;
56     }
57 
58     public int getPrice() {
59         return price;
60     }
61 }

2、Deal 商家类

 1 public class Deal {
 2     private String id;
 3     private String passwd_deal;
 4     private Collection<Fare[]> mov; //初始null
 5 
 6     public Deal() {
 7     }
 8 
 9     public Deal(String id, String passwd_deal, Collection mov) {
10         this.id = id;
11         this.passwd_deal = passwd_deal;
12         this.mov = mov;
13     }
14 
15     public void setId(String id) {
16         this.id = id;
17     }
18 
19     public void setPasswd_deal(String passwd_deal) {
20         this.passwd_deal = passwd_deal;
21     }
22 
23     public void setMov(Collection<Fare[]> mov) {
24         this.mov = mov;
25     }
26 
27     public String getId() {
28         return id;
29     }
30 
31     public String getPasswd_deal() {
32         return passwd_deal;
33     }
34 
35     public Collection getMov() {
36         return mov;
37     }
38 }

 

3、User 用户类

 1 public class User {
 2     private String id ;
 3     private String passwd_user;
 4     private int cost;  //初始0
 5     private Collection<Fare[]> fare; //初始null
 6 
 7     public User() {
 8     }
 9 
10     public User(String id, String passwd_user, int cost, Collection fare) {
11         this.id = id;
12         this.passwd_user = passwd_user;
13         this.cost = cost;
14         this.fare = fare;
15     }
16 
17     public void setId(String id) {
18         this.id = id;
19     }
20 
21     public void setPasswd_user(String passwd_user) {
22         this.passwd_user = passwd_user;
23     }
24 
25     public void setCost(int cost) {
26         this.cost = cost;
27     }
28 
29     public void setFare(Collection<Fare[]> fare) {
30         this.fare=fare;
31     }
32 
33     public String getId() {
34         return id;
35     }
36 
37     public String getPasswd_user() {
38         return passwd_user;
39     }
40 
41     public int getCost() {
42         return cost;
43     }
44 
45     public Collection getFare() {
46         return fare;
47     }
48 }

 

二、功能实现

1、main方法【主要写整个系统的一套逻辑】

 1 //先有main提供整体运行逻辑
 2     public static void main(String[] args) {
 3         //前提准备
 4         Scanner sc = new Scanner(in);//交互
 5         Deal deal = new Deal(); //创建商家
 6         User user = new User();//创建用户
 7         Fare m1 = new Fare(); //创建电影票对象
 8         Collection<Deal> list_deal = new ArrayList<>();
 9         Collection<User> list_user = new ArrayList<>();
10         Collection<Fare> list_fare = new ArrayList<>();//集合存电影票Fare
11         String id = null; //标识符
12         int a = 0; //判断符
13         int opt; //选择
14         // 循环switch整体选择
15         while (true) {
16             out.println("=======登录电影系统========");
17             while (true) {
18                 out.println("请选择首页操作:1:登录用户模式  2:登录商家模式   3:注册账号   4:退出系统");
19                 opt = sc.nextInt();
20                 switch (opt) {
21                     case 1:
22                         id = User_login(user, sc);
23                         if (id != null) {
24                             a = 2;
25                         }
26                         break;
27                     case 2:
28                         id = Deal_login(deal, sc);
29                         if (id != null) {
30                             a = 3;
31                         }
32                         break;
33                     case 3:
34                         register(sc, list_deal, list_user);
35                         break;
36                     case 4:
37                         out.println("正在退出系统......");
38                         a = 1;
39                         break;
40                     default:
41                         out.println("你的输入有误!请重新输入!");
42                         continue;
43                 }
44                 if (a == 0) { //a=0,登录循环
45                     continue;
46                 } else { //a!=0 ,表示操作成功,
47                     break;
48                 }
49             }
50             if ( a == 3) { //a=3 是商家版
51                 out.println("请选择你要做什么:1、上架电影   2、下架电影   3、查看售票");
52                 int selection = sc.nextInt();
53                 switch (selection) {
54                     case 1:
55                         update(list_fare,list_deal, id, sc);
56                         break;
57                     case 2:
58                         undermine(list_fare,list_deal, id, sc);
59                         break;
60                     case 3:
61                         view(id,list_deal);
62                         break;
63                     default:
64                         out.println("你的输入有误!退出系统");
65                         break;
66                 }
67             } else if (a == 2) { //用户版
68                 while (true) {
69                     out.println("请选择你要做什么:1、查看电影票信息   2、指定购票   3、查看个人购票信息");
70                     int selection = sc.nextInt();
71                     switch (selection) {
72                         case 1:
73                             looking(list_fare,sc,list_user,id);
74                             break;
75                         case 2:
76                             grave(sc, list_fare,id,list_user);
77                             break;
78                         case 3:
79                             personBuy(id,list_user);
80                             break;
81                         default:
82                             out.println("输入有误,退出登录!!!");
83                             break;
84                     }
85                 }
86             } else {
87                 out.println("已退出购票系统!欢迎下次再来");
88             }
89         }
90     }

 

2、共用功能

2.1 注册

 1  private static void register(Scanner sc, Collection<Deal> list_deal, Collection<User> list_user) {
 2         out.println("请选择注册账号类型:1、商家   2、用户");
 3         int option = sc.nextInt();
 4         String id = null;
 5         String passwd = null;
 6         switch (option) {
 7             case 1://商家
 8                 id = "d" + obtainId();
 9                 out.println("输入你的密码:");
10                 passwd = sc.next();
11                 Deal d = new Deal(id, passwd, null);
12                 list_deal.add(d);
13                 out.println("已注册成功!你的账号id是:" + id);
14                 break;
15             case 2: //用户
16                 id = "u" + obtainId();
17                 out.println("输入你的密码:");
18                 passwd = sc.next();
19                 User u = new User(id, passwd, 0, null);
20                 list_user.add(u);
21                 out.println("已注册成功!你的账号id是:" + id);
22                 break;
23             default:
24                 out.println("你的输入有误!退出注册程序......");
25                 break;
26         }
27     }

 

【获得id】

 1 //获得id
 2     private static String obtainId() {
 3         Random r = new Random();
 4         String id = null;
 5         StringBuilder sb = new StringBuilder();
 6         while (true) {
 7             for (int i = 0; i < 10; i++) { //10位id
 8                 sb.append(r.nextInt(10));
 9             }         // 0 对应的ascii码是 48
10             if (sb.charAt(0) == 48 && sb.length() != 10) {
11                 continue;
12             } else {
13                 id = sb.toString();
14                 return id;
15             }
16         }
17     }

3、用户各功能

3.1用户登录 【登录功能做了两种,稍微有些累赘,还可以优化,但是两种对后续功能实现更方便】

 1  private static String User_login(User user, Scanner sc) {
 2         for (int i = 0; i < 3; i++) {
 3             out.println("请输入账号id:");
 4             String id = sc.next();
 5             out.println("请输入你的密码:");
 6             String passwd = sc.next();
 7             if (user.getId().equals(id) && user.getPasswd_user().equals(passwd)) {
 8                 out.println("登录成功~~~");
 9                 return id;
10             } else {
11                 out.println("输入有误重新输入!还有" + i + 1 + "次机会");
12             }
13         }
14         out.println("机会用完,退出登录系统......");
15         return null;
16     }

 

3.2查询用户个人信息

 1  private static void personBuy(String id, Collection<User> list_user) {
 2         out.println("========以下是"+id+"用户的购票信息========");
 3         for (User user : list_user) {
 4             if (user.getId().equals(id)){
 5                 Collection<Fare[]>massage = user.getFare();
 6                 for (Fare[] fares : massage) {
 7                     int i = 0;
 8                     out.println((i+1)+":\n"+fares[i]);
 9                     i += 1 ;
10                 }
11             }
12         }
13     }

3.3购买和浏览电影信息

 1  private static void looking(Collection<Fare> list_fare, Scanner sc, Collection<User> list_user, String id) {
 2         out.println("========电影信息=========");
 3         for (Fare fare : list_fare) {
 4             fare.toString();
 5         }
 6         out.println("请选择需要购买的电影名:");
 7         String name = sc.next();
 8         out.println("正在购买......");
 9         for (User user : list_user) {
10             if(user.getId().equals(id)){ //锁定用户
11                 for (Fare fare : list_fare) {
12                     if(fare.getMovName().equals(name)){ //锁定电影
13                         fare.setAccount(fare.getAccount()+1); //售出量+1
14                         Fare[] fares = {new Fare(name, fare.getType(), fare.getPrice(), fare.getAccount())};
15                         Collection<Fare[]> list = user.getFare();
16                         int price = fare.getPrice();
17                         int cost = price;
18                         user.setCost(cost);
19                         list.add(fares);
20                         user.setFare(list);
21                         out.println("购买成功~~");
22                         personBuy(id,list_user);
23                     }
24                 }
25             }
26         }
27     }

 

3.4退票【不做展示】

 1   private static void grave(Scanner sc, Collection<Fare> list_fare, String id, Collection<User> list_user) {
 2         out.println("========退票=========");
 3         out.println("请输入想要退票的电影:");
 4         String name = sc.next();
 5         out.println("正在退票........");
 6         for (User user : list_user) {
 7             if (user.getId().equals(id)) {
 8                 if (user.getId().equals(id)) { //锁定用户
 9                     for (Fare fare : list_fare) {
10                         if (fare.getMovName().equals(name)) { //锁定电影
11                             fare.setAccount(fare.getAccount()-1); //售出量-1
12                             int price = fare.getPrice();//修改cost
13                             int cost = user.getCost();
14                             user.setCost(cost-price);
15                             Collection<Fare[]> list = user.getFare();
16                             for (Fare[] fares : list) {
17                                 int i = 0;
18                                 if (fares[i].getMovName().equals(name)){
19                                     list.remove(fares[i]);
20                                 }
21                                 i+=1;
22                             }
23                             user.setFare(list);
24                             out.println("退票成功~~");
25                             personBuy(id, list_user);
26                         }
27                     }
28                 }
29             }
30         }
31     }

 

4、商家各功能

4.1上架电影

 1 //上架电影:先在Fare中添加mov信息,更新到list_fare,再使用id在list_deal定位deal位置,找到mov集合属性,更新mov
 2     private static void update(Collection<Fare> list_fare, Collection<Deal> list_deal, String id, Scanner sc) {
 3         out.println("=========上架电影===========");
 4         boolean flag = true; //标识符
 5         while (true) {
 6             out.println("请输入MovName:");
 7             String name = sc.next();
 8             out.println("请输入MovType:");
 9             String type = sc.next();
10             out.println("请输入MovPrice:");
11             int price = sc.nextInt();
12             int account = 0;
13             Fare f = new Fare(name, type, price, account);
14             for (Fare fare : list_fare) {
15                 if (fare.getMovName().equals(f.getMovName())) {
16                     out.println("该电影已经存在了~请重新添加");
17                     flag = false;
18                     break; //直接重新开始上架输入
19                 }
20             }
21             if (flag == true) {
22                 list_fare.add(f);
23                 for (Deal deal : list_deal) {
24                     if (deal.getId().equals(id)){
25                         //通过id锁定对象,创建新内容,取原内容,对原内容修改后产生新内容,替换set原内容
26                         Fare [] mov = {new Fare(name,type,price,0)};
27                         Collection<Fare[]> mov2 = deal.getMov();
28                         mov2.add(mov);
29                         deal.setMov(mov2);
30                         break;
31                     }
32                 }
33                     out.println("上架电影成功!");
34                     break;
35             }
36         }
37         }

 

4.2下架电影【其实需要在进入之前再做一个判断,如果该商家的电影列表为空的话,就无法进行下架操作】

 1  //下架电影:通过id找到list_deal,删除对应mov信息,遍历list_fare,找到删除
 2     private static void undermine(Collection<Fare> list_fare, Collection<Deal> list_deal, String id, Scanner sc) {
 3         out.println("=========下架电影===========");
 4         boolean flag = true; //标识符
 5         while (true) {
 6             out.println("请输入MovName:");
 7             String name = sc.next();
 8             for (Deal deal : list_deal) {
 9                 if (deal.getId().equals(id)){
10                     Collection<Fare[]> mov1 = deal.getMov();
11                     for (Fare[] fares : mov1) {
12                         int i = 0; //相当于遍历数组,取movname
13                         if (fares[i].getMovName().equals(name)){
14                             out.println(fares[i]);
15                             mov1.remove(fares[i]);
16                         }
17                         i+=1;
18                     }
19                     deal.setMov(mov1); //改变后再替换
20                 }
21             }
22             //遍历电影信息,修改
23             for (Fare fare : list_fare) {
24                 if (fare.getMovName().equals(name)){
25                     out.println(fare);
26                     list_fare.remove(fare);
27                     break;
28                 }
29             }
30             if (flag == true){
31                 out.println("下架电影成功!");
32                 break;
33             }
34         }
35     }

 

4.3查询电影售票情况

 //查看电影售票(name+account)情况:直接显示mov集合
    private static void view(String id, Collection<Deal> list_deal) {
        for (Deal deal : list_deal) {
            if (deal.getId().equals(id)){
                Collection<Fare[]> list = deal.getMov();
                for (Fare[] fares : list) {
                    int i = 0;
                    out.println(i+1+"号:");
                    out.println("电影名:"+fares[i].getMovName()+
                            "\n电影售票量:"+fares[i].getAccount());
                }
                break;
            }
        }
    }

【以上就结束啦,各位在参考的同时可以做更多优化改进】

 

posted on 2023-04-07 23:21  ooyls  阅读(108)  评论(0编辑  收藏  举报

导航