我的Java学习之路(一) -- 简易酒店管理控制台程序

简易酒店管理控制台程序


一个简单的控制台酒店订房、退房、查看房间状态的程序,是我学习Java过程中做的一个小练习,拿出来分享给和我一样刚开始学习Java的伙伴们。
废话不多说,直接上代码:

package com.feonix;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class Hotel {
	static String[][] rooms = new String[9][9];

	public static void main(String[] args) {
		Scanner key = new Scanner(System.in);
		boolean flag = true;
		initRooms();
		while (flag) {
			menu();
			String command = key.next();
			switch (command) {
				case "search":
					search();
					break;
				case "in":
					in(key);
					break;
				case "out":
					out(key);
					break;
				case "exit":
					flag = false;
					break;
				default:
					System.out.println("无效的指令,请重新输入");
			}
		}
		key.close();
	}

	/**
	 * 	操作菜单的方法
	 */
	private static void menu() {
		System.out.println("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓");
		System.out.println("┃           欢迎使用酒店订房系统                        ┃");
		System.out.println("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫");
		System.out.println("┃ 请选择操作                                                             ┃");
		System.out.println("┃      输入 search 查询房间入住状态                   ┃");
		System.out.println("┃      输入 in     预订房间           ┃");
		System.out.println("┃      输入 out    退订房间           ┃");
		System.out.println("┃      输入 exit   退出系统           ┃");
		System.out.println("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
		System.out.println("请输入指令:");
	}

	/**
	 * 	查询房间入住状态的方法
	 */
	private static void search() {
		for (int i = 0; i < rooms.length; i++) {
			for (int j = 0; j < rooms[i].length; j++) {
				System.out.print(((i + 1) * 1000 + j + 1) + "\t");
			}
			System.out.println();
			for (int j = 0; j < rooms[i].length; j++) {
				String name = "empty".equals(rooms[i][j]) ? "empty": "****";
				System.out.print(name + "\t");
			}
			System.out.println("\n-----------------------------------------------------------------------");
		}
	}

	/**
	 * 	预订房间的方法
	 * @param key
	 */
	private static void in(Scanner key) {
		boolean flag = true;
		int num = 0;
		while (flag) {
			System.out.println("请输入房间号:");
			num = key.nextInt();
			if (num < 1001 || num > 9009) {
				System.out.println(num + "号房间不存在,请重新输入");
			} else {
				int i = num / 1000 - 1;
				int j = num % 1000 - 1;
				if (!"empty".equals(rooms[i][j])) {
					System.out.println(num + "号房间已被入住,请重新选择房间");
				} else {
					System.out.println("请输入姓名:");
					String name = key.next();
					rooms[i][j] = name;
					System.out.println(name + "成功入住" + num + "号房间");
					
					flag = false;
				}
			}
		}
		saver();
	}

	/**
	 * 	退房的方法
	 * @param key
	 */
	private static void out(Scanner key) {
		boolean flag = true;
		int num = 0;
		while (flag) {
			System.out.println("请输入房间号:");
			num = key.nextInt();
			if (num < 1001 || num > 9009) {
				System.out.println(num + "号房间不存在,请重新输入");
			} else {
				int i = num / 1000 - 1;
				int j = num % 1000 - 1;
				if ("empty".equals(rooms[i][j])) {
					System.out.println(num + "号房间没有人入住,请确认后重新输入");
				} else {
					rooms[i][j] = "empty";
					System.out.println(num + "号房间退房成功");
					flag = false;
				}
			}
		}
		saver();
	}

	/**
	 * 	初始化房间的方法
	 */
	private static void initRooms() {
		File file = new File("./data/data.csv");
		try {
			File dir = file.getParentFile();
			if (!dir.exists()) {
				dir.mkdirs();
			}
			if (!(file.isFile() && file.exists())) {
				for (int i = 0; i < rooms.length; i++) {
					for (int j = 0; j < rooms[i].length; j++) {
						rooms[i][j] = "empty";
					}
				}
			} else {
				BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
				String linestr = null;
				int i = 0;
				while((linestr = bufferedReader.readLine()) != null) {
					String[] room = linestr.split(",");
					rooms[i] = room;
					i++;
				}
				bufferedReader.close();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 	保存数据到文件
	 */
	private static void saver() {
		File file = new File("./data/data.csv");
		try {
			if (!(file.isFile() && file.exists())) {
				file.createNewFile();
			}
			BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			for (int i = 0; i < rooms.length; i++) {
				StringBuffer linestr = new StringBuffer();
				for (int j = 0; j < rooms[i].length; j++) {
					linestr.append(rooms[i][j]);
					if (j != rooms[i].length - 1) {
						linestr.append(",");
					}
				}
				bufferedWriter.write(linestr.toString());
				bufferedWriter.newLine();
			}
			bufferedWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

数据结构用的9*9的数组,数据持久化使用本地文件保存

猜你喜欢

转载自blog.csdn.net/u012587568/article/details/106153162