小编给大家分享一下Java如何实现电影院订票系统,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
程序是为了方便用户使用的,因此实现图形化界面的程序编写是所有编程语言发展的必然趋势。。。但是由于时间原因,小编并未在代码中添加可视化图形界面,有兴趣的朋友可自行添加。
可参阅:Java图形化界面设计之容器(JFrame)详解
演示结果:
下面是具体实现代码:
public class Test {public static void main(String[] args) {BookTicket bookTicket = new BookTicket();bookTicket.init();}}
BookTicket主界面类
import java.util.Scanner;public class BookTicket {//主界面类Movie[] movie = new Movie[3];LoginReg loginReg = new LoginReg();Scanner input = new Scanner(System.in);void movieInformation()// 内部电影信息{movie[0] = new Movie();movie[0].number = "S1124";movie[0].movieName = "雷神3:诸神黄昏";movie[0].movieTime = "2017-11-03大陆上映";movie[0].moviePrice = 70;movie[1] = new Movie();movie[1].number = "S1125";movie[1].movieName = "全球风暴";movie[1].movieTime = "2017-10-27大陆上映";movie[1].moviePrice = 80;movie[2] = new Movie();movie[2].number = "S1126";movie[2].movieName = "东方快车谋杀案";movie[2].movieTime = "2017-11-10大陆上映";movie[2].moviePrice = 90;}void interFace()// 订票界面{System.out.println("\t\t\t∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞");System.out.println("\t\t\t 订 票 系 统");System.out.println("\t\t\t∞∞∞∞∞∞∞∞∞∞∞∞∞∞∞");System.out .println("------------------------------------------------------------------------------");System.out.println("电影编号\t\t电影名\t\t上映时间\t\t电影票价");System.out.println(movie[0].number + "\t\t" + movie[0].movieName + "\t\t"+ movie[0].movieTime + "\t" + "$" + movie[0].moviePrice);System.out.println(movie[1].number + "\t\t" + movie[1].movieName + "\t\t"+ movie[1].movieTime + "\t" + "$" + movie[1].moviePrice);System.out.println(movie[2].number + "\t\t" + movie[2].movieName + "\t\t"+ movie[2].movieTime + "\t" + "$" + movie[2].moviePrice);System.out .println("------------------------------------------------------------------------------");}void login()// 登陆、注册{System.out.println("1.登陆\t\t2.注册");Scanner input = new Scanner(System.in);int logReg = input.nextint();switch (logReg) {case 1: if (loginReg.login() == 0) login();break;case 2: loginReg.reg();login();break;default: System.out.println("输入错误");login();}}void booking()// 订票{Boolean con = true;int index = 0;int[] flag = { -1, -1, -1 };do {System.out.println("\t\t欢迎订票");System.out.println("请输入电影编号(注意字母大写)");String odd = input.next();for (int i = 0; i < movie.length; i++) {if (movie[i].number.equals(odd)) {flag[index] = i;break;}}if (flag[index] == -1) {System.out.println("\t\t编号输入错误(找不到您要的电影)");} else {System.out.println("\t\t订票成功");System.out.println("电影编号\t电影名\t\t上映时间\t\t电影票价");System.out.println(movie[flag[index]].number + "\t\t" + movie[flag[index]].movieName + "\t" + movie[flag[index]].movieTime + "\t\t" + "$" + movie[flag[index]].moviePrice);index++;}System.out.println("是否需要继续订票(Y/N)");String yesNo = input.next();if (yesNo.equals("N")) con = false;if (index >= 3) {System.out.println("已经没有可预订的票");con = false;}}while (con);int add = 0;int sign = 0;for (index = 0; index < 3; index++) {if (flag[index] != -1) {add += movie[flag[index]].moviePrice;sign++;}}System.out.println("你一共订了" + sign + "张票" + "一共¥" + add + "元");}void init() {movieInformation();// 电影信息录入interFace();// 界面login();// 用户登陆、注册booking();// 订票}}
登陆,注册类
import java.util.Scanner;public class LoginReg {//登陆,注册类final int INIT = 1024;User[] user = new User[INIT];Scanner input = new Scanner(System.in);void reg()//用户注册{int flag = 0;System.out.println("\t欢 迎 注 册");System.out.println("用户名");for (int i = 0;i < INIT;i++) {if(user[i]==null) {user[i] = new User();user[i].name = input.next();flag = i;break;}}System.out.println("密码");user[flag].password = input.next();System.out.println("\t恭 喜 您!注 册 成 功");}int login()//用户登陆{int flag = 0;System.out.println("\t欢 迎 登 陆");System.out.println("用户名");String n1 = input.next();System.out.println("密码");String n2 = input.next();for (int i = 0;i < INIT;i++) {if(user[i]!=null) {if(user[i].name.equals(n1) && user[i].password.equals(n2)) {flag = 1;break;}}}switch(flag) {case 0: System.out.println("登陆失败");System.out.println("是否重新登录(Y/N)");String instr = input.next();if(instr.equals("Y")) login();break;case 1: System.out.println("登陆成功");break;}return flag;}}
用户类
public class User { //用户类 String name;// 用户名 String password;//密码 }
电影类
public class Movie { //电影类 String number; String movieName; String movieTime; int moviePrice; }
结束。
看完了这篇文章,相信你对“Java如何实现电影院订票系统”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网行业资讯频道,感谢各位的阅读!