import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
class Sing_supure {
public static boolean sign() {
String name = "root";
String password = "root";
int a = 3;
for (int i = 0; i < 3; i++) {
System.out.println("请输入账号:");
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
System.out.println("请输入密码:");
Scanner s = new Scanner(System.in);
String s2 = s.next();
if (s1.equals(name) && s2.equals(password)) {
System.out.println("又被你蒙对了密码");
return true;
}
if (a == 0) {
System.out.println("瞎蒙还想登陆?gun");
break;
}
System.out.println("哪个小孩在瞎蒙我密码");
}
return false;
}
}
class Ticket {
String name;
String idCard;
String time;
String ticketType;
public Ticket(String name, String idCard, String time, String ticketType) {
this.name = name;
this.idCard = idCard;
this.time = time;
this.ticketType = ticketType;
}
}
class TicketOrder {
static ArrayList<Ticket> ticketsList = new ArrayList<Ticket>();
static final String[] TICKET_TYPES = {"儿童票", "成人票", "老年票"};
public static void main(String[] args) throws Exception {
System.out.println("--------欢迎来到游乐园售票系统--------");
boolean flag = true;
Scanner scan = new Scanner(System.in);
while (flag) {
System.out.println("1.购买游乐园门票");
System.out.println("2.管理员登陆");
int a = scan.nextInt();
if (a == 1) {
System.out.println("请输入姓名:");
String name = scan.next();
System.out.println("请输入身份证号:");
String idCard = scan.next();
System.out.println("请选择门票类型:1-儿童票 2-成人票 3-老年票");
int typeIndex = scan.nextInt() - 1;
if (typeIndex >= 0 && typeIndex < TICKET_TYPES.length) {
String ticketType = TICKET_TYPES[typeIndex];
String time = getCurrentTime();
Ticket ticket = addTicket(name, idCard, time, ticketType);
FileUtil.saveTickets(ticket);
} else {
System.out.println("输入有误,请重新购买!");
}
} else if (a == 2) {
if (Sing_supure.sign()) {
while (true) {
System.out.println("1查看订单 2退出登陆");
int number = scan.nextInt();
if (number == 1) {
FileUtil.readTickets();
} else {
break;
}
}
}
} else {
System.out.println("输入错误");
}
}
}
private static String getCurrentTime() {
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
public static Ticket addTicket(String name, String idCard, String time, String ticketType) {
Ticket ticket = new Ticket(name, idCard, time, ticketType);
ticketsList.add(ticket);
return ticket;
}
}
class FileUtil {
public static final String TICKET_FILE_PATH = "售票订单.txt";
public static final String SEPARATE_FIELD = "\n";
public static final String SEPARATE_LINE = "\t";
public static synchronized void saveTickets(Ticket ticket) {
if (isTicketFull()) {
System.out.println("今天的门票已经售完了,请明天再来");
return;
}
File file = new File(TICKET_FILE_PATH);
if (!file.exists()) {
createFile(TICKET_FILE_PATH, false, ticket);
} else {
createFile(TICKET_FILE_PATH, true, ticket);
}
}
public static void createFile(String name, boolean label, Ticket ticket) {
BufferedOutputStream out = null;
StringBuffer sbf = new StringBuffer();
try {
if (label) {
out = new BufferedOutputStream(new FileOutputStream(name, true));
} else {
out = new BufferedOutputStream(new FileOutputStream(name));
String fieldSort = "游乐园现场售票系统";
sbf.append(fieldSort).append(SEPARATE_FIELD);
}
sbf.append("购买时间:").append(ticket.time).append(SEPARATE_LINE);
sbf.append("姓名:").append(ticket.name).append(SEPARATE_LINE);
sbf.append("身份证号:").append(ticket.idCard).append(SEPARATE_LINE);
sbf.append("门票类型:").append(ticket.ticketType).append(SEPARATE_FIELD);
String str = sbf.toString();
byte[] b = str.getBytes();
out.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static void readTickets() throws Exception {
FileInputStream in = new FileInputStream(TICKET_FILE_PATH);
byte[] b = new byte[in.available()];
in.read(b);
String str = new String(b);
System.out.println(str);
in.close();
}
private static boolean isTicketFull() {
boolean isFull = false;
int soldNum = 0;
try {
File file = new File(TICKET_FILE_PATH);
if (file.exists()) {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
if (line.contains(getTodayDate())) {
soldNum += 1;
}
}
br.close();
}
if (soldNum >= 500) {
isFull = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return isFull;
}
private static String getTodayDate() {
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
}