无目录
执行体
import com.txy.bean.Customer;
import com.txy.bean.Merchant;
import com.txy.bean.Movie;
import com.txy.bean.User;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class CinemaSystem {
/**
* 定义一个list容器存储用户数据,包括客人,和商家
* 加上final关键字,表示这个容器的地址不要再被修改了
*/
public static final List<User> ALL_USERS = new ArrayList<>();
/**
* 商家及其拍片信息也需要一个集合来存,因为拍片是复数信息,所以用一个映射集合
*/
public static final Map<Merchant,List<Movie>> ALL_MOVIES = new HashMap<>();
public static final Map<Customer,List<Movie>> WATCHED_MOVIES = new HashMap<>();
public static final Map<Movie,Double> iSCORE = new HashMap<>();
public static final Map<Movie,List<Double>> MOVIES_SCORE = new HashMap<>();
public static final Scanner SC_ALL = new Scanner(System.in);
public static User loggedInUser;
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
/*
在开始进入main方法,常理上,要初始构建几个user信息
用静态代码块,让类加载时最早生成
*/
static {
Customer c = new Customer();
c.setLoginName("Angel1");
c.setPassword("1234");
c.setUserName("Leon");
c.setGender('M');
c.setMoney(10000.0);
c.setPhone("98765");
ALL_USERS.add(c);
List<Movie> cWatch = new ArrayList<>();
c.setMyWatched(cWatch);
WATCHED_MOVIES.put(c, c.getMyWatched());
Customer c1 = new Customer();
c1.setLoginName("Angel2");
c1.setPassword("1234");
c1.setUserName("Ashley");
c1.setGender('F');
c1.setMoney(3000.0);
c1.setPhone("87654");
ALL_USERS.add(c1);
List<Movie> c1Watch = new ArrayList<>();
c.setMyWatched(c1Watch);
WATCHED_MOVIES.put(c1, c1.getMyWatched());
Merchant me = new Merchant();
me.setLoginName("Heaven1");
me.setPassword("1234");
me.setUserName("MarsBoy");
me.setMoney(0);
me.setGender('F');
me.setPhone("98979");
me.setAddress("MarsTowerNum2BFloor3");
me.setShopName("HelloMars");
/*商家跟拍片要存入映射里去,所以这里要做一个拍片的集合,凑成键值对 */
List<Movie> mov = new ArrayList<>();
ALL_MOVIES.put(me,mov);
ALL_USERS.add(me);
Merchant me1 = new Merchant();
me1.setLoginName("Heaven2");
me1.setPassword("1234");
me1.setUserName("VenusGirl");
me1.setMoney(0);
me1.setGender('M');
me1.setPhone("97989");
me1.setAddress("VenusTowerNum1BFloor2");
me1.setShopName("HelloVenus");
/*商家跟拍片要存入映射里去,所以这里要做一个拍片的集合,凑成键值对 */
List<Movie> mov1 = new ArrayList<>();
ALL_MOVIES.put(me1,mov1);
ALL_USERS.add(me1);
}
public static void main(String[] args) {
showHomePage();
}
private static void showHomePage() {
while (true) {
System.out.println("=========Gable Cinema Home Page==========");
System.out.println("1.User Login");
System.out.println("2.Customer registration");
System.out.println("3.Merchant registration");
System.out.println("4.Quit System");
System.out.println("Chose your operation: ");
String command = SC_ALL.nextLine();
switch (command){
case "1":
login();
break;
case "2":
// cusRegister();
break;
case "3":
// merRegister();
break;
case "4":
return;
default:
System.out.println("Incorrect input, make a correct choice please.");
}
}
}
private static void login() {
while (true) {
System.out.println("Input your login name please: ");
String loginName = SC_ALL.nextLine();
System.out.println("Input your password please: ");
String password = SC_ALL.nextLine();
User u = getUserByLoginName(loginName);
if (u != null){
if (password.equals(u.getPassword())){
loggedInUser = u;
// LOGGER.info(u.getUserName()+"has logged on system.");
if (u instanceof Customer){
showCustomerHomePage();
}else {
showMerchantHomePage();
}
return;
}else {
System.out.println("The password is wrong, try again please.");
}
}else {
System.out.println("Oop! Seems like you are not exist yet.");
}
}
}
private static void showMerchantHomePage() {
while (true) {
System.out.println("=========Gable Cinema Merchant Page==========");
System.out.println("Welcome back! "+(loggedInUser.getGender() == 'F'? "Mr " : "Mrs ")+loggedInUser.getUserName()
+", chose what do you gonna do here:");
System.out.println("1.Display movies info");
System.out.println("2.Update a new movie");
System.out.println("3.Delete a movie");
System.out.println("4.Edit a movie's info");
System.out.println("5.Exit");
System.out.println("Input your command: ");
String command = SC_ALL.nextLine();
switch (command) {
case "1" -> showMovies();
case "2" -> updateNewMovie();
case "3" -> deleteMovie();
case "4" -> modifyMovie();
case "5" -> {
System.out.println((loggedInUser.getGender() == 'F' ? "Mr " : "Mrs ") + loggedInUser.getUserName()
+ ", you have exit successfully\n");
return;
}
default -> System.out.println("That choice doesn't exit!");
}
}
}
private static void modifyMovie() {
System.out.println("========Modify==Movie========");
List<Movie> movies = ALL_MOVIES.get(loggedInUser);
if (movies.size() == 0){
System.out.println("There's no movie on screening right now.");
return;
}
while (true) {
System.out.println("Enter name of movie you want to modify:");
String name = SC_ALL.nextLine();
Movie movie = getMovieByName(name);
if (movie != null){
while (true) {
System.out.println("Enter which info you gonna change:");
System.out.println("1.Movie's name");
System.out.println("2.Movie's cast");
System.out.println("3.Movie's duration");
System.out.println("4.Ticket's price");
System.out.println("5.Ticket's stock");
System.out.println("6.Movie's release time");
System.out.println("0.Back");
String command = SC_ALL.nextLine();
switch (command){
case "1":
System.out.println("Enter the new name:");
String newName = SC_ALL.nextLine();
System.out.println("Are you sure about your change: y/n?");
String deter1 = SC_ALL.nextLine();
if ("y".equals(deter1)) {
movie.setName(newName);
System.out.println("Change succeed.\n");
} else {
System.out.println("Well, change is stop.\n");
}
break;
case "2":
System.out.println("Enter the new cast:");
String newCast = SC_ALL.nextLine();
System.out.println("Are you sure about your change: y/n?");
String deter2 = SC_ALL.nextLine();
if ("y".equals(deter2)) {
movie.setActor(newCast);
System.out.println("Change succeed.\n");
} else {
System.out.println("Well, change is stop.\n");
}
break;
case "3":
System.out.println("Enter the new duration:");
String newDuration = SC_ALL.nextLine();
System.out.println("Are you sure about your change: y/n?");
String deter3 = SC_ALL.nextLine();
if ("y".equals(deter3)) {
movie.setDuration(Double.parseDouble(newDuration));
System.out.println("Change succeed.\n");
} else {
System.out.println("Well, change is stop.\n");
}
break;
case "4":
System.out.println("Enter the new price:");
String newPrice = SC_ALL.nextLine();
System.out.println("Are you sure about your change: y/n?");
String deter4 = SC_ALL.nextLine();
if ("y".equals(deter4)) {
movie.setPrice(Double.parseDouble(newPrice));
System.out.println("Change succeed.\n");
} else {
System.out.println("Well, change is stop.\n");
}
break;
case "5":
System.out.println("Enter the new stock:");
String newStock = SC_ALL.nextLine();
System.out.println("Are you sure about your change: y/n?");
String deter5 = SC_ALL.nextLine();
if ("y".equals(deter5)) {
movie.setStock(Integer.parseInt(newStock));
System.out.println("Change succeed.\n");
} else {
System.out.println("Well, change is stop.\n");
}
break;
case "6":
while (true) {
try {
System.out.println("Enter the new release time:");
String newRelease = SC_ALL.nextLine();
System.out.println("Are you sure about your change: y/n?");
String deter6 = SC_ALL.nextLine();
if ("y".equals(deter6)) {
movie.setStartTime(sdf.parse(newRelease));
System.out.println("Change succeed.\n");
} else {
System.out.println("Well, change is stop.\n");
}
break;
} catch (ParseException e) {
e.printStackTrace();
}
}
break;
case "0":
System.out.println("Back to previous\n");
return;
}
System.out.println("You changed <<"+movie.getName()+">> successfully. Your shop's info" +
" update to:\n");
showMovies();
// return;
}
}else {
System.out.println("This movie is not on your screen, are you continue modify: y/n?");
String command = SC_ALL.nextLine();
if ("y".equals(command)) {
System.out.println("everything goes on~~~");
} else {
System.out.println("Well.");
return;
}
}
}
}
private static void deleteMovie() {
System.out.println("========Remove==Movie========");
List<Movie> movies = ALL_MOVIES.get(loggedInUser);
if (movies.size() == 0){
System.out.println("There's no movie on screening right now.");
return;
}
while (true) {
System.out.println("Enter name of movie you want to remove:");
String name = SC_ALL.nextLine();
Movie movie = getMovieByName(name);
if (movie != null){
movies.remove(movie);
System.out.println("You removed <<"+movie.getName()+">> successfully. Your shop's info" +
" update to:");
showMovies();
return;
}else {
System.out.println("This movie is not on your screen, are you continue removing: y/n?");
String command = SC_ALL.nextLine();
if ("y".equals(command)) {
System.out.println("Thing goes on~~~");
} else {
System.out.println("Well.");
return;
}
}
}
}
private static Movie getMovieByName(String name) {
List<Movie> movies = ALL_MOVIES.get(loggedInUser);
for (Movie movie : movies) {
if (movie.getName().contains(name)){
return movie;
}
}
return null;
}
private static void updateNewMovie() {
List<Movie> movies = ALL_MOVIES.get(loggedInUser);
System.out.println("Please enter the Movie's name:");
String name = SC_ALL.nextLine();
System.out.println("Please enter the Movie's main cast:");
String cast = SC_ALL.nextLine();
System.out.println("Please enter the Movie's duration:");
String duration = SC_ALL.nextLine();
System.out.println("Please enter the Movie's ticket price:");
String price = SC_ALL.nextLine();
System.out.println("Please enter the Movie's ticket stock:");
String stock = SC_ALL.nextLine();
while (true) {
System.out.println("Please enter the Movie's release time:");
String release = SC_ALL.nextLine();
try {
Movie mov = new Movie(name, cast, 0.0, Double.parseDouble(duration),Double.parseDouble(price),Integer.parseInt(stock),
sdf.parse(release));
movies.add(mov);
List<Double> scoreList = new ArrayList<>();
MOVIES_SCORE.put(mov,scoreList);
System.out.println("Update movie: <<"+mov.getName()+">> successfully!");
return;
} catch (ParseException e) {
e.printStackTrace();
System.out.println("Date parsing process has catch an error!");
}
}
}
private static void showMovies() {
Merchant merchant = (Merchant) loggedInUser;
System.out.println(merchant.getShopName()+"\t\tTel: "+merchant.getPhone()+"\t\tAddress: "+merchant.getAddress()+"\n");
System.out.println(loggedInUser.getUserName()+", here is your shop's current movie info:");
List<Movie> movies = ALL_MOVIES.get(merchant);
System.out.println("Movie's name\t\tCast\t\tDuration\t\tScore\t\tPrice\t\tStock\t\tReleaseTime\n");
if (movies.size() > 0){
for (Movie movie : movies) {
System.out.println(movie.getName()+"\t\t\t"+movie.getActor()+"\t\t\t"+movie.getDuration()+"\t\t"+
movie.getScore()+"\t\t"+movie.getPrice()+"\t\t"+movie.getStock()+"\t\t\t"+
sdf.format(movie.getStartTime())+"");
}
System.out.println();
}else {
System.out.println("Your shop have no movie on screen yet.\n");
}
}
private static void showCustomerHomePage() {
while (true) {
System.out.println("=========Gable Cinema Customer Page==========");
System.out.println("Welcome back! "+(loggedInUser.getGender() == 'F'? "Mr " : "Mrs ")+loggedInUser.getUserName()
+", chose what do you gonna do here ");
System.out.println("1.Display all movies info");
System.out.println("2.Query movie by name");
System.out.println("3.Score movie");
System.out.println("4.Buy ticket");
System.out.println("5.Exit");
System.out.println("Input the command: ");
String command = SC_ALL.next();
switch (command) {
case "1" -> showAllMovies();
case "2" -> queryMovie();
case "3" -> scoreMovie();
case "4" -> buyTicket();
case "5" -> {
System.out.println((loggedInUser.getGender() == 'F' ? "Mr " : "Mrs ") + loggedInUser.getUserName()
+ ", you have exit successfully");
return;
}
default -> {
System.out.println("That choice doesn't exit!\n");
}
}
}
}
/** HAVE BUG */
private static void buyTicket() {
/*
分别用 (店名:商家) 和 (商家:电影)预先生成两个映射
为后来买票时,消费者输入信息,定位方便
*/
Map<Merchant,Movie> screening = new HashMap<>();
Map<String,Merchant> merByShopName = new HashMap<>();
while (true) {
System.out.println("====Ticket==Purchase==Service====");
System.out.println("Enter the movie you like to watch:");
String name = SC_ALL.next();
ALL_MOVIES.forEach((merchant, movies) -> {
for (Movie movie : movies) {
if (movie.getName().contains(name)) {
screening.put(merchant, movie);
merByShopName.put(merchant.getShopName(), merchant);
}
}
});
if (screening.size() != 0) {
System.out.println("This movie's screening info is under:");
screening.forEach((merchant1, movie) -> {
System.out.println("\t" + (merchant1.getShopName() + "\t\tTel: " + merchant1.getPhone() +
"\t\tAddress: " + merchant1.getAddress()));
System.out.println("Movie's name\t\tCast\t\tDuration\t\tScore\t\tPrice\t\tStock\t\tReleaseTime");
System.out.println(movie.getName() + "\t\t\t" + movie.getActor() + "\t\t\t" + movie.getDuration() + "\t\t" +
movie.getScore() + "\t\t" + movie.getPrice() + "\t\t" + movie.getStock() + "\t\t\t" +
sdf.format(movie.getStartTime()));
});
System.out.println("\nEnter which cinema you would like to visit:");
String cinemaName = SC_ALL.next();
while (true) {
System.out.println("Enter the amount you wanna buy for this movie:");
int num = SC_ALL.nextInt();
Merchant willVisit = merByShopName.get(cinemaName);
Movie willSee = screening.get(willVisit);
if (num > 0 && num <= willSee.getStock()) {
double payment = willSee.getPrice() * num;
System.out.println("Your should pay: " + payment);
System.out.println("Are you sure you wanna deal: Y/N ?");
String decide = SC_ALL.next();
if (decide.equals("y")) {
loggedInUser.setMoney(loggedInUser.getMoney() - payment);
willVisit.setMoney(willVisit.getMoney() + payment);
willSee.setStock(willSee.getStock() - num);
Customer myself = (Customer) loggedInUser;
myself.getMyWatched().add(willSee);
WATCHED_MOVIES.put(myself, myself.getMyWatched());
/**为下次买票清空临时映射表*/
// merByShopName.remove(willVisit);
screening.remove(willVisit);
System.out.println("Your purchase completed successfully!\nThanks for your support!");
break;
} else {
System.out.println("Well.");
break;
}
} else {
System.out.println("That amount is illegal, try again please.\n");
}
}
} else {
System.out.println("Sorry! That movie is not on screen anywhere.");
}
System.out.println("would you like to quit or continue purchase: Y/N ?");
String command = SC_ALL.next();
if ("y".equals(command)) {
System.out.println("Continue purchase...");
} else {
System.out.println("Well.");
return;
}
}
}
private static void scoreMovie() {
List<Movie> iMovies = WATCHED_MOVIES.get(loggedInUser);
if (iMovies != null){
while (true) {
System.out.println("Input the name of the movie you want to score:");
String name = SC_ALL.next();
for (Movie iMovie : iMovies) {
if (iMovie.getName().contains(name)) {
System.out.println("Enter your score for this movie");
double scoreFor = SC_ALL.nextDouble();
if (scoreFor >= 0 && scoreFor <= 10) {
iSCORE.put(iMovie, scoreFor);
List<Double> scoreAllIn = MOVIES_SCORE.get(iMovie);
scoreAllIn.add(scoreFor);
System.out.println("Your score for this movie update to:" + scoreFor);
Double totalScore = 0.0;
for (Double aDouble : scoreAllIn) {
totalScore += aDouble;
}
double finalScoreForAMovie = BigDecimal.valueOf(totalScore).divide
(BigDecimal.valueOf(scoreAllIn.size())).doubleValue();
iMovie.setScore(finalScoreForAMovie);
} else {
System.out.println("Sorry, here only accept a number between 0 to 10.");
}
System.out.println("would you like to quit or continue query: Y/N ?");
String command = SC_ALL.next();
if ("y".equals(command)) {
System.out.println("Score one more time...");
break;
} else {
System.out.println("Well.");
return;
}
}else {
System.out.println("Your input isn't correct,or you haven't watch it yet.");
break;
}
}
}
}else {
System.out.println("You don't watched any movie yet, no movie for you to score");
}
}
/** HAVE BUG */
private static void queryMovie() {
Map<Merchant,Movie> screenPerShop = new HashMap<>();
while (true) {
System.out.println("====Customer==Movie==Query====");
System.out.println("Input the name of movie you like to know:");
String name = SC_ALL.next();
ALL_MOVIES.forEach((merchant, movies) -> {
for (Movie movie : movies) {
if (movie.getName().contains(name)){
screenPerShop.put(merchant,movie);
}
}
});
if (screenPerShop.size() > 0){
System.out.println("The movie's info you queried like under:\n");
screenPerShop.forEach((merchant1, movie) -> {
// if (movie.getName().contains(name)){
System.out.println((merchant1.getShopName()+"\t\tTel: "+merchant1.getPhone()+
"\t\tAddress: "+merchant1.getAddress()));
System.out.println("Movie's name\t\tCast\t\tDuration\t\tScore" +
"\t\tPrice\t\tStock\t\tReleaseTime");
System.out.println("\n"+movie.getName()+"\t\t\t"+movie.getActor()+"\t\t\t"+movie.getDuration()+"\t\t"+
movie.getScore()+"\t\t"+movie.getPrice()+"\t\t"+movie.getStock()+"\t\t\t"+
sdf.format(movie.getStartTime())+"\n");
// }
});
}else {
System.out.println("Seems like that movie isn't on any screen by now.\n");
}
System.out.println("Would you like to quit or continue query: Y/N ?");
String command = SC_ALL.next();
if ("y".equals(command)) {
System.out.println("To query next...");
} else {
System.out.println("Well.");
return;
}
}
}
private static void showAllMovies() {
System.out.println("=====Display==all==shop's==screen==info====");
ALL_MOVIES.forEach((merchant, movies) -> {
System.out.println(merchant.getShopName()+"\t\tTel: "
+merchant.getPhone()+"\t\tAddress: "+merchant.getAddress());
System.out.println("Movie's name\t\t\tCast\t\t\tDuration\t\tScore\t\tPrice\t\tStock\t\tReleaseTime\n");
for (Movie movie : movies) {
System.out.println(movie.getName()+"\t\t\t"+movie.getActor()+"\t\t\t"+movie.getDuration()+"\t\t"+
movie.getScore()+"\t\t"+movie.getPrice()+"\t\t"+movie.getStock()+"\t\t\t"+
sdf.format(movie.getStartTime()));
}
System.out.println();
});
}
private static User getUserByLoginName(String loginName) {
for (User user : ALL_USERS) {
if (loginName.equals(user.getLoginName())){
return user;
}
}
return null;
}
}
电影类
package com.gable.Bean;
import java.util.Date;
public class Movie {
private String name;
private String actor;
private double score;
private double duration;
private double price;
private int stock;//余票
private Date startTime;//放映开始时间
public Movie(String name, String actor, double score, double duration, double price, int stock, Date startTime) {
this.name = name;
this.actor = actor;
this.score = score;
this.duration = duration;
this.price = price;
this.stock = stock;
this.startTime = startTime;
}
//不需要重写toString方法,本例中
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public double getDuration() {
return duration;
}
public void setDuration(double duration) {
this.duration = duration;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
}
顾客类
package com.gable.Bean;
public class Customer extends User{
private List<Movie> myWatched;
public List<Movie> getMyWatched() {
return myWatched;
}
public void setMyWatched(List<Movie> myWatched) {
this.myWatched = myWatched;
}
}
商家类
package com.gable.Bean;
public class Merchant extends User{
private String shopName;
private String address;
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
用户父类
package com.gable.Bean;
public class User {
private String loginName;
private String userName;
private String password;
private char gender;
private String phone;
private double money;
public User() {
}
public User(String loginName, String userName, String password, char gender, String phone, double money) {
this.loginName = loginName;
this.userName = userName;
this.password = password;
this.gender = gender;
this.phone = phone;
this.money = money;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
本文介绍了CinemaSystem类,包含了用户登录、注册、商户操作(展示电影、添加/更新/删除电影)、顾客功能(查看电影、评分、购票)等核心功能。通过实例展示了如何初始化用户和商家信息,以及处理购票和评分流程。
207

被折叠的 条评论
为什么被折叠?



