在学习编程语言的过程中,如果有些得心应手的小项目,往往给人以小小的成就感,可以适当调解初期语法的枯燥,整理出学习中的小项目,以供参考;
一、客房管理
- 简介
共有5层,每层10间客房,以数字101--509标示;
具有入住,退房,搜索,退出四个简单功能;
public class Hotel {
static final int floor = 5;
static final int order = 10;
private static int countFloor;
private static int countOrder;
private static String[][] rooms = new String[floor][order];
//main函数代表酒店的基本功能,入住,退房,查询,其他;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String temp = null;
while(true){
//提醒用户选择输入
System.out.println("请选择输入 in out search quit : ");
temp = scan.next();
int room = 0;
if("in".equals(temp)){
while(true){
System.out.println("请输入房间号:");
room = scan.nextInt();
if(isRightRoom(room,1)){
break;
}
}
System.out.println("请输入名字:");
String name = scan.next();
if(in(room,name)){
System.out.println("恭喜您,入住成功");
System.out.println(room + " : "+name);
}
}else if("out".equals(temp)){
while(true){
System.out.println("请输入房间号:");
room = scan.nextInt();
if(isRightRoom(room,0)){
break;
}
}
if(out(room)){
System.out.println("退房成功,欢迎下次光临");
}
}else if("search".equals(temp)){
System.out.println("请输入查询的房间号(-1表示全部)");
room = scan.nextInt();
search(room);
}else if("quit".equals(temp)){
break;
}else{
System.out.println("您的输入有误,请再次输入!");
}
}
}
//flag:1 检查房间号输入正确 且 无人入住 0 仅检查房间号输入正确
private static boolean isRightRoom(int room,int flag) {
//校验房间号是否输入有误
countFloor = room / 100 - 1;
countOrder = room % 100 - 1;
if(countFloor < 0 || countFloor >= floor || countOrder < 0 || countOrder >= order ){
System.out.println("输入的房间号有误");
return false;
}
if(flag == 1){
if(rooms[countFloor][countOrder] != null){
System.out.println("房间已经入住");
return false;
}
}
return true;
}
//旅馆入住功能实现,in(room,name) countFloor:计算出的楼层 countOrder:计算出的房间顺序
private static boolean in(int room,String name){
rooms[countFloor][countOrder] = name;
return true;
}
//旅馆退房功能实现,out(room),
private static boolean out(int room){
//校验房间号是否有人入住
if(rooms[countFloor][countOrder] == null){
System.out.println("房间未曾入住,退房有误");
return false;
}
rooms[countFloor][countOrder] = null;
return true;
}
//旅馆搜索功能,search(room)
private static void search(int room){
if(room == -1){
int roomNum = 0;
for(int i=0;i<floor;i++){
for(int j=0;j<order;j++){
roomNum = (i + 1) * 100 + j + 1;
System.out.println(roomNum + "->" +
(rooms[i][j] == null ? "empty" : rooms[i][j]));
}
}
}else{
//校验房间号是否正确
if(!isRightRoom(room,0)){
System.out.println("抱歉,房间输入错误!");
}else{
System.out.println(rooms[countFloor][countOrder]);
}
}
}
}
二、微信红包
注意事项
因为金额涉及到分,小数点后保留两位,数值类型的转换操作需要注意;
此中方案相对简陋,并未实现每次红包最大值在0.01-剩余红包均值的2倍间;
/*
* 红包设计要求:
* 1-红包最小值为0.01,最大值为200
* 2-红包的金额面值为小数点后两位
* 3-避免单个红包数值太大,通过系数设定
* 4-红包总量满足金额数
* */
public class RedPacketUtil {
//微信红包的最大值和最小值,和最大红包金额系数
private static final float MINMUM = 0.01f;
private static final float MAXMUM = 200.00f;
private static final float TIMES = 2.1f;
//判断当前金额和数量是否正确
public boolean isRight(float money,int count){
//计算当前平均值
float ave = (float)money / count;
if(ave < MINMUM){
return false;
}else if(ave > MAXMUM){
return false;
}
return true;
}
//生成每个具体红包的金额
public float redPacket(float money, float min, float maxs, int count) {
//判断当前人数
if(count == 1){
//确保红包不小于0.01元
money = money > MINMUM ? money : MINMUM;
return (float)(Math.round(money * 100)) / 100;
}
float max = maxs > money ? money : maxs;
//生成单个红包数量,且保证红包的精度
float one = (float) (Math.random()*(max - min) + min);
one = (float)(Math.round(one * 100)) / 100;
float moneyRest = (money - one);
//判断当前红包数量是否合理
if(isRight(moneyRest,count - 1)){
return one;
}else{
//重新分配红包
float ave = (float)moneyRest / (count-1);
if(ave < MINMUM){
return redPacket(money,min,one,count);
}else if(ave > MAXMUM){
return redPacket(money,one,max,count);
}
}
return one;
}
//拆分红包,生成具体的红包数
public List<Float> splitRedPacket(float money,int count){
//判断当前金额和数量是否正确
if(!isRight(money,count)){
return null;
}
//记录每个红包的数量
List<Float> rpList = new ArrayList<Float>();
//单个红包的最大金额
float max = (float)(money * TIMES)/ count ;
max = max > MAXMUM ? MAXMUM : max;
float one = 0;
//开始记录每个红包的数量
for(int i = 0;i < count;i++){
one = redPacket(money,MINMUM,max,count-i);
rpList.add(one);
money = money - one;
}
return rpList;
}
public static void main(String[] args) {
RedPacketUtil util = new RedPacketUtil();
List<Float> result = util.splitRedPacket(200, 10);
System.out.println(result);
//验证金额总数
float sum = 0;
for(float i : result){
sum = sum + i;
}
System.out.println(sum);
}
}