电影院
package com.zhou.com.cinema;
public class Movie {
private String name;//电影名称
private String daoyan;//导演
private String actor;//主演
private int price;//价格
private String time;//上映时间
//创建get和set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDaoyan() {
return daoyan;
}
public void setDaoyan(String daoyan) {
this.daoyan = daoyan;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
//创建默认构造
public Movie() {
}
//创建带参构造
public Movie(String name, String daoyan, String actor, int price, String time) {
this.name = name;
this.daoyan = daoyan;
this.actor = actor;
this.price = price;
this.time = time;
}
//创建toString
@Override
public String toString() {
return "Movie{" +
"电影名称='" + name + '\'' +
", 导演='" + daoyan + '\'' +
", 主演='" + actor + '\'' +
", 票价=" + price +
", 上映时间='" + time + '\'' +
'}';
}
}
普通票
package com.zhou.com.cinema;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
//普通票
public class Ticket implements IPrict{
private Movie movie;//票有电影的属性,所以创建父类的类型
private String setNum;//座位
//随机产生座位号
private String createNum() {
Random ran = new Random();
int m = ran.nextInt(20) + 1;
int x = ran.nextInt(30) + 1;
return m + "排" + x + "号";
}
//普通票的价格就是原价
protected int jiage() {
return this.movie.getPrice();
}
//创建默认构造和带参构造,默认构造把座位号的方法赋值到座位上
public Ticket() {
this.setNum = this.createNum();
}
//本类带参构造必须放在第一
public Ticket(Movie movie) {
this();
this.movie = movie;
}
//创建get和set
public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
public String getSetNum() {
return setNum;
}
public void setSetNum(String setNum) {
this.setNum = setNum;
}
//重写toString
@Override
public String toString() {
return "Ticket{" +
"movie=" + movie +
", setNum='" + setNum + '\'' +
'}';
}
//重写打印信息
@Override
public void print() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
System.out.println("=====欢迎来到佳辉电影院=====【普通票】=====");
System.out.printf("===电影名称:【"+this.movie.getName()+"】\t ===座位号:【"+this.setNum+"】===\n");
System.out.printf("===导演:【"+this.movie.getDaoyan()+"】\t===主演:【"+this.movie.getActor()+"】===\n");
System.out.printf("===票价:【"+this.jiage()+"】\t ===放映时间:【"+this.movie.getTime()+"】===\n");
System.out.println("=====票的类型【普通票】");
System.out.println("=====购票时间【"+sdf.format(new Date())+"】=====");
System.out.println("=====佳辉电影院=====");
}
}
学生票
package com.zhou.com.cinema;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StudentTicket extends Ticket{//继承父类的普通票
private int zhekou;//学生票的特有属性折扣
//创建get和set
public int getZhekou() {
return zhekou;
}
public void setZhekou(int zhekou) {
this.zhekou = zhekou;
}
//创建默认构造和带参构造
public StudentTicket() {
}
public StudentTicket(Movie movie) {
super(movie);
}
//重写toString
@Override
public String toString() {
return super.toString();
}
//重写价格
@Override
protected int jiage() {
//价格是普通票+价格 /10 * 折扣
return this.getMovie().getPrice()/10 * this.zhekou;
}
//重写打印信息
@Override
public void print() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
System.out.println("=====欢迎来到佳辉电影院=====【学生票】=====");
System.out.printf("===电影名称:【"+this.getMovie().getName()+"】\t ===座位号:【"+this.getSetNum()+"】===\n");
System.out.printf("===导演:【"+this.getMovie().getDaoyan()+"】\t===主演:【"+this.getMovie().getActor()+"】===\n");
System.out.printf("===票价:【"+this.jiage()+"】\t ===放映时间:【"+this.getMovie().getTime()+"】===\n");
System.out.println("=====票的类型【学生票】\t===折扣:【"+this.zhekou+"】折");
System.out.println("=====购票时间【"+sdf.format(new Date())+"】=====");
System.out.println("=====佳辉电影院=====");
}
}
赠票
package com.zhou.com.cinema;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FreeTicket extends Ticket{//继承父类的普通票
private String kehu;//赠票的特有属性客户姓名
//重写价格
@Override
protected int jiage() {
//赠票的价格是0元
return 0;
}
//创建默认构造和带参构造
public FreeTicket() {
}
public FreeTicket(Movie movie) {
super(movie);
}
//创建get和set
public String getKehu() {
return kehu;
}
public void setKehu(String kehu) {
this.kehu = kehu;
}
//重写toString
@Override
public String toString() {
return super.toString();
}
//重写打印信息
@Override
public void print() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
System.out.println("=====欢迎来到佳辉电影院=====【赠票】=====");
System.out.printf("===电影名称:【"+this.getMovie().getName()+"】\t ===座位号:【"+this.getSetNum()+"】===\n");
System.out.printf("===导演:【"+this.getMovie().getDaoyan()+"】\t===主演:【"+this.getMovie().getActor()+"】===\n");
System.out.printf("===票价:【"+this.jiage()+"】\t ===放映时间:【"+this.getMovie().getTime()+"】===\n");
System.out.println("=====票的类型【赠票】\t===客户姓名:【"+this.kehu+"】");
System.out.println("=====购票时间【"+sdf.format(new Date())+"】=====");
System.out.println("=====佳辉电影院=====");
}
}
创建一个异常
package com.zhou.com.cinema;
public class MyException extends RuntimeException{//创建一个异常继承RuntimeException父类
//创建默认构造和带参构造
public MyException() {
}
public MyException(String message) {
super(message);
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(Throwable cause) {
super(cause);
}
public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
电影数组
package com.zhou.com.cinema;
import java.util.Scanner;
public class Cinema {
//创建电影的数组用来存放电影票的信息
private static Movie[] movies = new Movie[4];
//创建一个静态的电影票的信息
static {
Movie m1 = new Movie("变形金刚1","迈尔克","张三",80,"8:00");
Movie m2 = new Movie("速9","迈尔克","张三",70,"9:00");
Movie m3 = new Movie("消失的她","迈尔克","张三",100,"10:00");
Movie m4 = new Movie("我爱你","迈尔克","张三",90,"7:00");
//把电影票的信息存放进去数组
movies[0] = m1;
movies[1] = m2;
movies[2] = m3;
movies[3] = m4;
}
//创建一个浏览电影
public void show(){
for(Movie movie:movies){
System.out.println(movie);
}
}
//创建一个输入电影跟电影票匹配的方法
public Movie findByName(String cname) throws MyException{
Movie m = null;
for(Movie m1:movies){
if(cname.equals(m1.getName())){
m = m1;
break;
}
}
//声明方法抛出异常
if(m == null){
throw new MyException("对不起,你的电影不存在!");
}
return m;
}
//创建一个用户登录系统
public boolean logo(){
Scanner in = new Scanner(System.in);
System.out.println("=====欢迎来到佳辉电影院=====");
System.out.println("=====系统登录=====");
System.out.println("请输入账号:");
String zhang = in.next();
System.out.println("请输入密码:");
String mi = in.next();
if(zhang.equalsIgnoreCase("abcd") && mi.equals("1234")){
System.out.println("欢迎【"+zhang+"】用户登录成功");
return true;
}
return false;
}
}
工厂
package com.zhou.com.cinema;
public class TicketFactory {
//创建工厂
public static Ticket choose(int m,Movie movie){
Ticket ticket = null;
switch (m){
//普通票
case 1: ticket = new Ticket(movie); break;
//学生票
case 2:ticket = new StudentTicket(movie); break;
//赠票
case 3:ticket = new FreeTicket(movie); break;
}
return ticket;
}
}
继承的方法
package com.zhou.com.cinema;
public interface IPrict {
//接口的方法
void print();
}
测试
package com.zhou.com.cinema;
import java.util.Scanner;
public class View {
public static void main(String[] args) {
Movie movie = null;
//接收用户输入的信息
Scanner in = new Scanner(System.in);
int num = 0;
//创建一个电影的数组
Cinema cinema = new Cinema();
//判断登录系统
if (cinema.logo() == true) {
//循环
do {
System.out.println("=====欢迎来到佳辉电影院=====");
System.out.println("请选择电影:1:浏览电影 2:购票 3:退出");
num = in.nextInt();
switch (num){
case 1:
//输出浏览的电影
cinema.show();
break;
case 2:
System.out.println("请输入你要购买的票:");
String gou = in.next();
try {
//通过输入的电影名称匹配到数组的名称
movie = cinema.findByName(gou);
//输出数组里面的电影票信息
System.out.println(movie);
}catch (RuntimeException e){
//输出异常的信息
System.out.println(e.getMessage());
return;
}
System.out.println("请选择购票的类型:1:普通票 2:学生票 3:赠票");
num = in.nextInt();
//调用工厂
Ticket ticket = TicketFactory.choose(num,movie);
if(ticket instanceof StudentTicket){
System.out.println("请输入折扣:");
int zhe = in.nextInt();
((StudentTicket) ticket).setZhekou(zhe);
}
if(ticket instanceof FreeTicket){
System.out.println("请输入客户姓名:");
String ke = in.next();
((FreeTicket) ticket).setKehu(ke);
}
//计算价格
int jiage = ticket.jiage();
//打印信息
ticket.print();
break;
case 3:
//退出
System.out.println("确认退出吗?(y/n)");
String y= in.next();
//用户输入y退出
if(y.equals("y")){
System.out.println("欢迎光临,下次再来!");
System.exit(0);
break;
//否则继续进入循环
}else {
System.out.println("请继续购票!");
}
}
} while (num != 0);
}else{
System.out.println("账号密码输入错误,请重新输入!");
}
}
}