JAVA练习-继承-发红包案例
要求#
老板给员工发红包,红包金额为小数点两位,并且红包随机分配金额。
User.java#
package com.inherit;
public class User {
private String username;
private Double remainMoney;
public User() {
}
public User(String username, Double remainMoney) {
this.username = username;//角色
this.remainMoney = remainMoney;//剩余金额
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Double getRemainMoney() {
return remainMoney;
}
public void setRemainMoney(Double remainMoney) {
this.remainMoney = remainMoney;
}
public void show() {
System.out.println("username:"+username+"-----"+"remainMoney:"+remainMoney);
}
}
Boss.java#
package com.inherit;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Boss extends User{
/**
* 如果发送的金额大于余额,返回null
*
*/
public Boss() {
}
public Boss(String username, Double remainMoney) {
super(username, remainMoney);
}
public List<Double> send(double money, int count){
double remainMoney = super.getRemainMoney();
List<Double> list = new ArrayList<Double>();
if(money > remainMoney || count == 0){
return null;
}else{
//设置老板新的余额
super.setRemainMoney(remainMoney-money);
//如果红包个数只有一个,全部给他
if(count == 1){
list.add(Double.parseDouble(String.format("%.2f",money)));
}else{
//红包个数为n,前n-1个随机分配,最后一个得到剩余全部金额
while(count > 1){
//最小的红包金额0.01
double num = Math.random() * (money-0.01)+0.01;
//System.out.println("num"+num);
num = Double.parseDouble(String.format("%.2f",num));
money -= num;
//System.out.println(money);
list.add(num);
count--;
}
//最后一个红包
list.add(Double.parseDouble(String.format("%.2f",money)));
}
}
return list;
}
}
Employee.java#
package com.inherit;
import java.util.List;
import java.util.Random;
public class Employee extends User{
public Employee(String username, Double remainMoney) {
super(username, remainMoney);
}
public Employee() {
}
public double receive(List<Double> list){
Random random = new Random();
int i = random.nextInt(list.size());
Double remainMoney = list.get(i);
list.remove(i);
//设置余额
super.setRemainMoney(remainMoney);
return remainMoney;
}
}
Test.java#
package com.inherit;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Boss boss = new Boss("老板", 200.00);
Scanner scan = new Scanner(System.in);
System.out.println("请输入金额:");
double money = scan.nextDouble();
System.out.println("请输入个数:");
int count = scan.nextInt();
List<Double> send = boss.send(money, count);
if(send == null){
System.out.println("金额不足!");
return ;
}else{
Employee one = new Employee("one",0.0);
Employee two = new Employee("two",0.0);
Employee three = new Employee("three",0.0);
double a = one.receive(send);
double b = two.receive(send);
double c = three.receive(send);
// System.out.println(a);
// System.out.println(b);
// System.out.println(c);
boss.show();
one.show();
two.show();
three.show();
}
}
}
核心语句#
public List<Double> send(double money, int count){
double remainMoney = super.getRemainMoney();
List<Double> list = new ArrayList<Double>();
if(money > remainMoney || count == 0){
return null;
}else{
//设置老板新的余额
super.setRemainMoney(remainMoney-money);
//如果红包个数只有一个,全部给他
if(count == 1){
list.add(Double.parseDouble(String.format("%.2f",money)));
}else{
//红包个数为n,前n-1个随机分配,最后一个得到剩余全部金额
while(count > 1){
//最小的红包金额0.01
double num = Math.random() * (money-0.01)+0.01;
//System.out.println("num"+num);
num = Double.parseDouble(String.format("%.2f",num));
money -= num;
//System.out.println(money);
list.add(num);
count--;
}
//最后一个红包
list.add(Double.parseDouble(String.format("%.2f",money)));
}
}
return list;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理