发布网友 发布时间:2023-09-20 07:57
共4个回答
热心网友 时间:2024-12-14 23:39
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException{
Map<String, Room> rooms =initRooms();
BufferedReader ctrl=new BufferedReader(new InputStreamReader(System.in));
getRoomState(rooms);
boolean exit = true;
while(exit){
println("");
print("菜单: ");
print(" 1:订房");
print(" 2:退房");
print(" 3:查询");
print(" 4:退出");
println("");
print(" 请输入:");
String num =ctrl.readLine();
cls();
switch (num) {
case "1":
print("房间号:");
String rid = ctrl.readLine();
print("帐号:");
String user = ctrl.readLine();
print("密码:");
String pwd = ctrl.readLine();
print("住天数:");
String day = ctrl.readLine();
Room r = rooms.get(rid);
if(r==null){
println("没有此房间");
}else{
r.setDay(Integer.parseInt(day));
r.setName(user);
r.setPwd(pwd);
rooms.put(r.getrId(),r);
println("订房成功!!");
getRoomState(rooms);
}
break;
case "2":
print("帐号:");
String user2 = ctrl.readLine();
print("密码:");
String pwd2 = ctrl.readLine();
Room r2 =getRoowByUserAndPwd(rooms,user2,pwd2);
if(r2==null){
println("你没有订房!!");
}else{
r2.setName("");
r2.setPwd("");
r2.setDay(0);
rooms.put(r2.getrId(),r2);
println("退订成功");
getRoomState(rooms);
}
break;
case "3":
print("帐号:");
String user3 = ctrl.readLine();
print("天数:");
String day3 = ctrl.readLine();
Room r3 =getRoowByUserAndDay(rooms,user3,day3);
if(r3==null){
println("你没有订房!!");
}else{
println(r3.toString());
}
break;
case "4":exit = false ;break;
}
getRoomState(rooms);
}
}
private static void getRoomState(Map<String, Room> rooms) {
println("房间情况:");
for (Room r : rooms.values()) {
println("房号:"+r.getrId()+"=>" + r.getPrice()+" 状态:"+r.isEmptyRoom());
}
}
private static Room getRoowByUserAndDay(Map<String, Room> rooms,String user,String day) {
for (Room r : rooms.values()) {
if(r.getName().equals(user) && day.equals(r.getDay()+""))
return r;
}
return null;
}
private static Room getRoowByUserAndPwd(Map<String, Room> rooms,String user,String pwd) {
for (Room r : rooms.values()) {
if(r.getName().equals(user) && pwd.equals(r.getPwd()+""))
return r;
}
return null;
}
/**
* 初始化房间
*/
private static Map<String, Room> initRooms() {
Map<String, Room> result = new HashMap<String, Room>();
result.put("8001", new Room("8001",100.0));
result.put("8002", new Room("8002",100.0));
result.put("8003", new Room("8003",100.0));
result.put("8004", new Room("8004",200.0));
result.put("8005", new Room("8005",200.0));
result.put("8006", new Room("8006",200.0));
result.put("8007", new Room("8007",300.0));
result.put("8008", new Room("8008",300.0));
result.put("8009", new Room("8009",300.0));
return result;
}
public static void cls() throws IOException{
Runtime.getRuntime().exec("clear");
}
public static void println(Object str){
System.out.println(str);
}
public static void print(Object str){
System.out.print(str);
}
}
class Room{
private String rId="";
private String name="";
private String pwd="";
private int day=0;
private double price=0.0;
public String isEmptyRoom(){
return this.name.equals("")?"空房间":"已出租";
}
public Room(String rId, double price) {
this.rId = rId;
this.price = price;
}
@Override
public String toString() {
return "房号=" + rId + ", 用户=" + name + ", 天数=" + day
+ ", 租金=" + price + ", 总金额=" + (price*day);
}
public Room(String name, String pwd, int day) {
this.name = name;
this.pwd = pwd;
this.day = day;
}
public Room() {
}
public String getrId() {
return rId;
}
public void setrId(String rId) {
this.rId = rId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
比较粗糙将就
热心网友 时间:2024-12-14 23:39
程序蛮简单的,自己写吧。只是要用数据库存数据热心网友 时间:2024-12-14 23:39
这基本就是输入输出。。。。。。。。。。。。热心网友 时间:2024-12-14 23:40
这是一个钱进的社会哦。