您的位置:首页 > 编程语言 > Java开发

Java MVC设计模式与封装完整示例与介绍 订票系统

2015-09-01 18:58 676 查看
功能介绍:本示例模拟一个剧院的订票系统,剧院拥有东、西、南、北四个方位的座位,每个方位有十排座位,每一排有二十个座位。每个方位第一排座位价格为800元,2--4排                      为600元,5--7排为400元,8--10排为200元。普通游客进入系统可以查询余票、注册。注册用户可以订票、退票、充值。本示例属于控制台应用程序,旨在体现Java的                    MVC设计模式与封装思想。适合Java初学者学习和理解Java的MVC设计模式和封装的思想,希望对您有所帮助。

Model层:

用户类:

import java.util.ArrayList;
import com.bijienet.ticket.Ticket;

public class Account {
private String accountName;//用户名
private String accountPwd;//密码
private double balance;//账户余额
private ArrayList<Ticket> ticketList;//已订列表

public Account(){}
public Account(String accountName, String accountPwd){
this.accountName = accountName;
this.accountPwd = accountPwd;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountPwd() {
return accountPwd;
}
public void setAccountPwd(String accountPwd) {
this.accountPwd = accountPwd;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public ArrayList<Ticket> getTicketList() {
return ticketList;
}
public void setTicketList(ArrayList<Ticket> ticketList) {
this.ticketList = ticketList;
}

}
票类:

public class Ticket {
private String direction;//方位
private int row;//行
private int line;//列
private double price;//价格
private boolean state;//预订状态,false为未预定

public Ticket(){}
public Ticket(String direction,int row,int line,double price,boolean state){
this.direction = direction;
this.row = row;
this.line = line;
this.price = price;
this.state = state;
}

public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getLine() {
return line;
}
public void setLine(int line) {
this.line = line;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}

}


View层:

import java.util.ArrayList;
import java.util.Scanner;

import com.bijienet.account.Account;
import com.bijienet.account.controller.AccountController;
import com.bijienet.ticket.Ticket;
import com.bijienet.ticket.controller.TicketController;
import com.bijienet.tool.Tips;
import com.bijienet.tool.Tool;

public class BiJieView {
public static void main(String[] args){
ArrayList<Account> accountList = new ArrayList<Account>();
ArrayList<Ticket> ticketList = Tool.EstablishTicket();
boolean flag = true;
while(flag){
System.out.println(Tips.WelcomeForHall);
System.out.println(Tips.ChoiceForHall);
char choiceForHall = new Scanner(System.in).next().charAt(0);
switch(choiceForHall){
case '1':
boolean previewFlag = true;
while(previewFlag){
//选择查询方式
System.out.println(Tips.WayOfPreview);
char previewWayChoice = new Scanner(System.in).next().charAt(0);
switch(previewWayChoice){
case '1':
ArrayList<Ticket> surplusTicketList = TicketController.previewTicketBySurplus(ticketList);
Tool.ShowTicket(surplusTicketList);
previewFlag = Tool.ContinueOrExit();
break;
case '2':
System.out.println(Tips.InputForTicketDirection);
String direction = new Scanner(System.in).next();
//把用户输入的方位转化为对象拥有的属性
String choiceDirection = Tool.TransformationInputDirection(direction);
if(!choiceDirection.equals("")){
ArrayList<Ticket> ticketListByDirection = TicketController.previewTicketByDirection(ticketList, choiceDirection);
Tool.ShowTicket(ticketListByDirection);
previewFlag = Tool.ContinueOrExit();
}else{
System.out.println(Tips.ChoiceError);
}
break;
case '3':
System.out.println(Tips.InputForTicketRow);
int row = new Scanner(System.in).nextInt();
if(row>=1 &&row<=10){
ArrayList<Ticket> ticketListByRow = TicketController.previewTicketByRow(ticketList, row);
Tool.ShowTicket(ticketListByRow);
previewFlag = Tool.ContinueOrExit();
}else{
System.out.println(Tips.InputForTicketRowError);
}
break;
case '4':
System.out.println(Tips.InputForTicketLittleRow);
int littleRow = new Scanner(System.in).nextInt();
System.out.println(Tips.InputForTicketBigRow);
int bigRow = new Scanner(System.in).nextInt();
if(littleRow>0 && littleRow<=10 && bigRow > 0 && bigRow <= 10 && littleRow <= bigRow){
ArrayList<Ticket> ticketListByRows = TicketController.previewTicketByRows(ticketList, littleRow, bigRow);
Tool.ShowTicket(ticketListByRows);
previewFlag = Tool.ContinueOrExit();
}else{
System.out.println(Tips.InputForTicketRowsError);
}
break;
case '5':
previewFlag = false;
break;
default:
System.out.println(Tips.ChoiceError);
break;
}
}
break;

case '2':
System.out.println(Tips.InputForAcountName);
String accountName = new Scanner(System.in).next();
System.out.println(Tips.InputForAcountPwd);
String accountPwd = new Scanner(System.in).next();
if(!accountName.equals("") && !accountPwd.equals("")){
boolean registerFlag = AccountController.accountRegister(accountList, accountName, accountPwd);
if(registerFlag){
System.out.println(Tips.RegisterSuccess);
}else{
System.out.println(Tips.RegisterFailure);
}
}else{
System.out.println(Tips.RegisterFailure);
}
break;

case '3':
boolean loginFlag = true;
while(loginFlag){
System.out.println(Tips.InputForAcountName);
String loginAccountName = new Scanner(System.in).next();
System.out.println(Tips.InputForAcountPwd);
String loginAccountPwd = new Scanner(System.in).next();
if(!loginAccountName.equals("") && !loginAccountPwd.equals("")){
Account loginAccount = AccountController.accountLogin(accountList, loginAccountName, loginAccountPwd);
if(loginAccount != null){
System.out.println(Tips.LoginSuccess);
boolean operationFlag = true;
while(operationFlag){
System.out.println(Tips.ChoiceForOperation);
char choiceForOperation = new Scanner(System.in).next().charAt(0);
switch(choiceForOperation){
case '1':
System.out.println(Tips.InputTicketDirection);
String bookTicketDirection = Tool.TransformationInputDirection(new Scanner(System.in).next()) ;
System.out.println(Tips.InputTicketRow);
int bookTicketRow = new Scanner(System.in).nextInt();
System.out.println(Tips.InputTicketLine);
int bookTicketLine = new Scanner(System.in).nextInt();
if(!bookTicketDirection.equals("") && bookTicketRow>0 && bookTicketRow<=10 && bookTicketLine>0 &&bookTicketLine<=20){
boolean bookTicketResult = AccountController.bookTicket(ticketList, loginAccount, bookTicketDirection, bookTicketRow, bookTicketLine);
if(bookTicketResult){
System.out.println(Tips.BookTicketSuccess);
}else{
System.out.println(Tips.BookTicketFailure);
}
}else{
System.out.println(Tips.InputTicketInformationError);
}
break;
case '2':
System.out.println(Tips.InputTicketDirection);
String unsubscribeTicketDirection = Tool.TransformationInputDirection(new Scanner(System.in).next()) ;
System.out.println(Tips.InputTicketRow);
int unsubscribeTicketRow = new Scanner(System.in).nextInt();
System.out.println(Tips.InputTicketLine);
int unsubscribeTicketLine = new Scanner(System.in).nextInt();
if(!unsubscribeTicketDirection.equals("") && unsubscribeTicketRow>0 && unsubscribeTicketRow<=10 && unsubscribeTicketLine>0 && unsubscribeTicketLine<=20){
boolean unsubscribeFlag = AccountController.unsubscribeTicket(ticketList, loginAccount, unsubscribeTicketDirection, unsubscribeTicketRow, unsubscribeTicketLine);
if(unsubscribeFlag){
System.out.println(Tips.UnsubscribeTicketSuccess);
}else{
System.out.println(Tips.UnsubscribeTicketFailure);
}
}else{
System.out.println(Tips.InputTicketInformationError);
}
break;
case '3':
System.out.println(Tips.RechargeAmount);
double amount = new Scanner(System.in).nextDouble();
if(amount>0 && amount%100==0){
boolean rechargeFlag = AccountController.Recharge(loginAccount, amount);
if(rechargeFlag){
System.out.println(Tips.RechargeSuccess);
}else{
System.out.println(Tips.RechargeFailure);
}
}else{
System.out.println(Tips.CheckRechargeAmount);
}
break;
case '4':
ArrayList<Ticket> accountTicketList = AccountController.CheckBookedTickets(loginAccount);
Tool.ShowTicket(accountTicketList);
break;
case '5':
operationFlag = false;
loginFlag = false;
System.out.println(Tips.ExitForPersonalCenter);
break;
default:
System.out.println(Tips.ChoiceError);
break;
}
}

}else{
loginFlag = false;
System.out.println(Tips.LoginFailure);
}
}else{
loginFlag = false;
System.out.println(Tips.InputForAccountError);
}
}
break;

case '4':
flag = false;
System.out.println(Tips.ExitForHall);
break;

default:
System.out.println(Tips.ChoiceError);
break;
}
}

}

}


Controller层:
用户的Controller:

import java.util.ArrayList;
import com.bijienet.account.Account;
import com.bijienet.ticket.Ticket;

public class AccountController {
/**
* 注册
* @param accountList
* @param accountName
* @param accountPwd
* @return
*/
public static boolean accountRegister(ArrayList<Account> accountList,String accountName,String accountPwd){
if(!accountName.equals("") && !accountPwd.equals("")){
for(Account account : accountList){
if(account.getAccountName().equals(accountName)){
return false;
}
}
Account account = new Account(accountName,accountPwd);
account.setTicketList(new ArrayList<Ticket>());
accountList.add(account);
}
return true;
}

/**
* 登陆
* @param accountList
* @param accountName
* @param accountPwd
* @return
*/
public static Account accountLogin(ArrayList<Account> accountList,String accountName,String accountPwd){
if(!accountName.equals("") && !accountPwd.equals("")){
for(Account account : accountList){
if(account.getAccountName().equals(accountName) && account.getAccountPwd().equals(accountPwd)){
return account;
}
}
}
return null;
}

/**
* 订票
* @param ticketList
* @param account
* @param direction
* @param row
* @param line
* @return
*/
public static boolean bookTicket(ArrayList<Ticket> ticketList,Account account,String direction,int row,int line){
ArrayList<Ticket> accountTicketList = new ArrayList<Ticket>();
if(account!=null && !direction.equals("") && row>0 && row<=10 && line>0 && line<=20){
int ticketIndex = 0;
for(Ticket ticket : ticketList){
if(ticket.getDirection().equals(direction) && ticket.getRow()==row && ticket.getLine()==line && ticket.isState()==false && ticket.ge
c06d
tPrice()<=account.getBalance()){
ticket.setState(true);
accountTicketList = account.getTicketList();
accountTicketList.add(ticket);
account.setTicketList(accountTicketList);
//account.getTicketList().add(ticket);
//foreach循环中的迭代变量修改属性值并不能修改到集合中的值,必须从集合中取出对象再修改
ticketList.get(ticketIndex).setState(true);

account.setBalance(account.getBalance() - ticket.getPrice());
return true;
}
ticketIndex = ticketIndex + 1;
}
}
return false;
}

/**
* 退票
* @param ticketList
* @param account
* @param direction
* @param row
* @param line
* @return
*/
public static boolean unsubscribeTicket(ArrayList<Ticket> ticketList,Account account,String direction,int row,int line){
if(account != null && !direction.equals("") && row>0 && row<=10 && line>0 && line<=20){
int ticketIndex = 0;
for(Ticket ticket : account.getTicketList()){
if(ticket.getDirection().equals(direction) && ticket.getRow()==row && ticket.getLine()==line){
account.getTicketList().remove(ticketIndex);
account.setBalance(account.getBalance() + ticket.getPrice());
ticketList.get(ticketIndex).setState(false);
return true;
}
}
}
return false;
}

/**
* 充值
* @param account
* @param amount
* @return
*/
public static boolean Recharge(Account account,double amount){
if(account != null && amount>0 && amount%100==0){
account.setBalance(account.getBalance() + amount);
return true;
}
return false;
}

/**
* 查询已定票
* @param account
* @return
*/
public static ArrayList<Ticket> CheckBookedTickets(Account account){
if(account != null){
return account.getTicketList();
}
return null;
}

}


票的Controller层:

import java.util.ArrayList;
import com.bijienet.ticket.Ticket;

public class TicketController {
public static ArrayList<Ticket> tickets = new ArrayList<Ticket>();

/**
* 总体查询余票
* @param ticketList 列表
* @return
*/
public static ArrayList<Ticket> previewTicketBySurplus(ArrayList<Ticket> ticketList){
if(ticketList != null){
for(Ticket ticket : ticketList){
if(ticket.isState()==false){
tickets.add(ticket);
}
}
}else{
return null;
}
return tickets;
}

/**
* 按方位查询
* @param ticketList 列表
* @param direction 区域
* @return
*/
public static ArrayList<Ticket> previewTicketByDirection(ArrayList<Ticket> ticketList,String direction){
if(ticketList != null && !direction.equals("")){
for(Ticket ticket : ticketList){
if(ticket.getDirection().equals(direction)){
tickets.add(ticket);
}
}
}else{
return null;
}
return tickets;
}

/**
* 按单行查询
* @param ticketList 列表
* @param row 行
* @return
*/
public static ArrayList<Ticket> previewTicketByRow(ArrayList<Ticket> ticketList,int row){
if(ticketList != null && row>0 && row<=10){
for(Ticket ticket : ticketList){
if(ticket.getRow()== row){
tickets.add(ticket);
}
}
}else{
return null;
}
return tickets;
}

/**
* 按多行查询
* @param ticketList 列表
* @param row 行
* @return
*/
public static ArrayList<Ticket> previewTicketByRows(ArrayList<Ticket> ticketList,int littleRow,int bigRow){
if(ticketList != null && littleRow>0 && littleRow<=10 && bigRow > 0 && bigRow <= 10 && littleRow <= bigRow){
for(Ticket ticket : ticketList){
if(ticket.getRow()>= littleRow && ticket.getRow() <= bigRow){
tickets.add(ticket);
}
}
}else{
return null;
}
return tickets;
}

}


封装的工具类:

import java.util.ArrayList;
import java.util.Scanner;

import com.bijienet.ticket.Ticket;

public class Tool {
/**
* 初始化所有的票
* @return
*/
public static ArrayList<Ticket> EstablishTicket(){
ArrayList<Ticket> ticketList = new ArrayList<Ticket>();
String ticketDirection = "";
double ticketPrice = 0;
for(int direction=1; direction<=4; direction++){
for(int row=1; row<=10; row++){
for(int line=1; line<=20; line++){
if(direction==1){ticketDirection = "east";}
else if(direction==2){ticketDirection = "west";}
else if(direction==3){ticketDirection = "south";}
else{ticketDirection = "north";}

if(row==1){ticketPrice = 800;}
else if(row>1 && row<=4){ticketPrice = 600;}
else if(row>4 && row<=7){ticketPrice = 400;}
else{ticketPrice = 200;}

Ticket ticket = new Ticket(ticketDirection,row,line,ticketPrice,false);
ticketList.add(ticket);
}
}
}
return ticketList;
}

/**
* 显示票信息方法
* @param ticketList
*/
public static void ShowTicket(ArrayList<Ticket> ticketList){
for(Ticket ticket : ticketList){
System.out.println("Direction: " + ticket.getDirection() + "  Row: " + ticket.getRow() + "  Line: " + ticket.getLine() + "  Price: " + ticket.getPrice() + "  State: " + ticket.isState());
}
}

/**
* 票务查询时选择继续还是退出
* @return
*/
public static boolean ContinueOrExit(){
System.out.println(Tips.ContinueOrExit);
char continueOrExit = new Scanner(System.in).next().charAt(0);
if(continueOrExit == 'y'){
return true;
}else{
return false;
}
}

/**
* 用户输入区域输入转换为对象的属性值
* @param inputDirection
* @return
*/
public static String TransformationInputDirection(String inputDirection ){
String direction = "";
if(inputDirection.equals("1")){
direction = "east";
}else if(inputDirection.equals("2")){
direction = "west";
}else if(inputDirection.equals("3")){
direction = "south";
}else if(inputDirection.equals("4")){
direction = "north";
}else{
direction = "";
}
return direction;
}

}


提示信息类:

public class Tips {
public static String WelcomeForHall = "welcome to #####";
public static String ExitForHall = "Thanks for your visit, exit success";
public static String ChoiceForHall = "Plase choice your operation:  1.PreviewTicket  2.Register  3.Login  4.Exit";
public static String ChoiceError = "Choice Error! Plase check you choice!";

//选择查询方式
public static String WayOfPreview = "Plase choice the way of preview:  1.SurplusOfTheTicket  2.ByTheDirection  3.ByRow  4.ByRows  5.Exit";
public static String InputForTicketDirection = "Plase input direction for the ticket: 1.east  2.west  3.south  4.north";
public static String InputForTicketRow = "Plase input row for the ticket: 1 <= row <= 10";
public static String InputForTicketRowError = "Plase check your input row for the ticket,the row must in: 1 <= row <= 10";
public static String InputForTicketLittleRow = "Plase input littleRow for the Section of the ticket: 1<=littleRow<=10";
public static String InputForTicketBigRow = "Plase input bigRow for the Section of the ticket: 1<=Bigrow<=10 and bigRow>=littleRow";
public static String InputForTicketRowsError = "Plase check your input rows for the ticket,the row must: 1<=littleRow<=10 1<=Bigrow<=10 and bigRow>=littleRow";
public static String ContinueOrExit = "Plase choice continue preview or exit: y->continue  n->exit";

//注册
public static String InputForAcountName = "Plase input the account name";
public static String InputForAcountPwd = "Plase input the account password";
public static String RegisterSuccess = "Congratulate register success!";
public static String RegisterFailure = "Sorry register Failure! Plase check your input";

//登陆
public static String LoginSuccess = "Login success!";
public static String LoginFailure = "Login failure!";
public static String InputForAccountError = "plase check your input for login accountName or accountPwd is error";

//登陆后操作
public static String WelcomeForPersonalCenter = "Welcome to personal center!";
public static String ExitForPersonalCenter = "Exit personal center success!";
public static String ChoiceForOperation = "Plase choice for your operation:  1.BookTicket  2.Unsubscribe  3.Recharge  4.CheckBookTickets  5.Exit";

//订票
public static String InputTicketDirection = "Plase intput the direction for the ticket: 1.east  2.west  3.south  4.north";
public static String InputTicketRow = "Plase intput the direction for the ticket: 1<=row<=10";
public static String InputTicketLine = "Plase intput the direction for the ticket: 1<=line<=20";
public static String InputTicketInformationError = "Plase check your input infromation for the ticket, there are some error in the information";
public static String BookTicketSuccess = "Congratulate the ticket booking success";
public static String BookTicketFailure = "Sorry the ticket booking failure,maybe the ticket is already booked or your balance is not enough";

//退票
public static String UnsubscribeTicketSuccess = "Unsubscribe ticket success";
public static String UnsubscribeTicketFailure = "Unsubscribe ticket failure";

//充值
public static String RechargeAmount = "Plase input the amount of recharge";
public static String RechargeSuccess = "Recharge success!";
public static String RechargeFailure = "Recharge failure!";
public static String CheckRechargeAmount = "Plase check your input recharge amount: amount>0 and amount is the multiple of hundred";

}


建议从视图层通过各层的功能以及调用的方法,逐一查找对应封装的方法,以便理解。本项目经过测试,能够正常运行

项目的结构如下图所示:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: