1 public class Test { 2 public static void main(String[] args) { 3 BookTicket bookTicket = new BookTicket(); 4 bookTicket.init(); 5 } 6 7 }
1 import java.util.Scanner; 2 3 public class BookTicket { //主界面类 4 5 Movie[] movie = new Movie[3]; 6 LoginReg loginReg = new LoginReg(); 7 Scanner input = new Scanner(System.in); 8 9 void movieInformation()// 内部电影信息 10 { 11 movie[0] = new Movie(); 12 movie[0].number = "S1124"; 13 movie[0].movieName = "乱世佳人"; 14 movie[0].movieTime = "2013.11.24"; 15 movie[0].moviePrice = 70; 16 movie[1] = new Movie(); 17 movie[1].number = "S1125"; 18 movie[1].movieName = "青春如期"; 19 movie[1].movieTime = "2013.11.25"; 20 movie[1].moviePrice = 80; 21 movie[2] = new Movie(); 22 movie[2].number = "S1126"; 23 movie[2].movieName = "让子弹飞"; 24 movie[2].movieTime = "2013.11.26"; 25 movie[2].moviePrice = 90; 26 } 27 28 void interFace()// 订票界面 29 { 30 31 System.out.println(" ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞"); 32 System.out.println(" 订 票 系 统"); 33 System.out.println(" ∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞"); 34 35 System.out 36 .println("------------------------------------------------------------------------------"); 37 System.out.println("电影编号 电影名 上映时间 电影票价"); 38 System.out.println(movie[0].number + " " + movie[0].movieName + " "+ movie[0].movieTime + " " + "$" + movie[0].moviePrice); 39 System.out.println(movie[1].number + " " + movie[1].movieName + " "+ movie[1].movieTime + " " + "$" + movie[1].moviePrice); 40 System.out.println(movie[2].number + " " + movie[2].movieName + " "+ movie[2].movieTime + " " + "$" + movie[2].moviePrice); 41 System.out 42 .println("------------------------------------------------------------------------------"); 43 } 44 45 46 void login()// 登陆、注册 47 { 48 System.out.println("1.登陆 2.注册"); 49 Scanner input = new Scanner(System.in); 50 int logReg = input.nextInt(); 51 switch (logReg) { 52 case 1: 53 if (loginReg.login() == 0) 54 login(); 55 break; 56 case 2: 57 loginReg.reg(); 58 login(); 59 break; 60 default: 61 System.out.println("输入错误"); 62 login(); 63 } 64 } 65 66 67 void booking()// 订票 68 { 69 boolean con = true; 70 int index = 0; 71 int[] flag = { -1, -1, -1 }; 72 do { 73 System.out.println(" 欢迎订票"); 74 System.out.println("请输入电影编号(注意字母大写)"); 75 String odd = input.next(); 76 for (int i = 0; i < movie.length; i++) { 77 if (movie[i].number.equals(odd)) { 78 flag[index] = i; 79 80 break; 81 } 82 } 83 if (flag[index] == -1) { 84 System.out.println(" 编号输入错误(找不到您要的电影)"); 85 } else { 86 System.out.println(" 订票成功"); 87 System.out.println("电影编号 电影名 上映时间 电影票价"); 88 System.out.println(movie[flag[index]].number + " " 89 + movie[flag[index]].movieName + " " 90 + movie[flag[index]].movieTime + " " + "$" 91 + movie[flag[index]].moviePrice); 92 index++; 93 } 94 System.out.println("是否需要继续订票(Y/N)"); 95 String yesNo = input.next(); 96 if (yesNo.equals("N")) 97 con = false; 98 if (index >= 3) { 99 System.out.println("已经没有可预订的票"); 100 con = false; 101 } 102 } while (con); 103 int add = 0; 104 int sign = 0; 105 for (index = 0; index < 3; index++) { 106 if (flag[index] != -1) { 107 add += movie[flag[index]].moviePrice; 108 sign++; 109 } 110 } 111 System.out.println("你一共订了" + sign + "张票" + "一共¥" + add + "元"); 112 } 113 114 115 116 void init() { 117 movieInformation();// 电影信息录入 118 interFace();// 界面 119 login();// 用户登陆、注册 120 booking();// 订票 121 } 122 123 }
1 import java.util.Scanner; 2 public class LoginReg { //登陆,注册类 3 final int INIT = 1024; 4 User[] user = new User[INIT]; 5 Scanner input = new Scanner(System.in); 6 7 void reg()//用户注册 8 { 9 int flag = 0; 10 System.out.println(" 欢 迎 注 册"); 11 System.out.println("用户名"); 12 for(int i = 0;i < INIT;i++) 13 { 14 if(user[i]==null) 15 { 16 user[i] = new User(); 17 user[i].name = input.next(); 18 flag = i; 19 break; 20 } 21 } 22 System.out.println("密码"); 23 user[flag].password = input.next(); 24 System.out.println(" 恭 喜 您!注 册 成 功"); 25 } 26 int login()//用户登陆 27 { 28 int flag = 0; 29 System.out.println(" 欢 迎 登 陆"); 30 System.out.println("用户名"); 31 String n1 = input.next(); 32 System.out.println("密码"); 33 String n2 = input.next(); 34 for(int i = 0;i < INIT;i++) 35 { 36 if(user[i]!=null) 37 { 38 if(user[i].name.equals(n1) && user[i].password.equals(n2)) 39 { 40 flag = 1; 41 break; 42 } 43 } 44 } 45 switch(flag) 46 { 47 case 0: 48 System.out.println("登陆失败"); 49 System.out.println("是否重新登录(Y/N)"); 50 String instr = input.next(); 51 if(instr.equals("Y")) 52 login(); 53 break; 54 case 1: 55 System.out.println("登陆成功"); 56 break; 57 58 } 59 return flag; 60 61 62 } 63 }
1 public class User { //用户类 2 String name;// 用户名 3 String password;//密码 4 }
1 public class Movie { //电影类 2 String number; 3 String movieName; 4 String movieTime; 5 int moviePrice; 6 }