列表实现选秀投票代码
时间: 2024-10-15 20:07:36 浏览: 17
在Python中,我们可以使用列表和简单的循环结构来实现一个基本的选秀投票系统。这里是一个简化的例子,假设我们有一个候选人列表(list of candidates),每个元素代表一位候选人,包含名字和初始票数:
```python
# 候选人列表
candidates = [
{"name": "候选人A", "votes": 0},
{"name": "候选人B", "votes": 0},
# 添加更多候选人...
]
def vote(candidate_name):
for candidate in candidates:
if candidate["name"] == candidate_name:
candidate["votes"] += 1
break # 只需增加一次投票
def show_results():
print("投票结果:")
for candidate in sorted(candidates, key=lambda x: x["votes"], reverse=True):
print(f"{candidate['name']} - {candidate['votes']}票")
# 用户投票
print("请输入你想投票给哪位候选人(按名字输入):")
user_input = input()
vote(user_input)
# 显示投票结果
show_results()
#
相关问题
基于ssm框架的出道选秀管理系统的功能模块代码观众(普通用户) (能看到艺人表演的视频,能投票,留言功能(能给自己喜欢的明星留言),可以对自己喜的艺人进行打赏,增加选手的人气))代码
以下是基于SSM框架的出道选秀管理系统的观众(普通用户)功能模块的Java代码示例:
1. 观众DAO接口及其实现类:
```
public interface AudienceDao {
// 根据观众ID获取观众信息
Audience getAudienceById(int audienceId);
// 观众投票
void vote(int audienceId, int artistId);
// 观众留言
void leaveMessage(int audienceId, int artistId, String message);
// 观众打赏
void reward(int audienceId, int artistId, int amount);
}
@Repository
public class AudienceDaoImpl implements AudienceDao {
@Autowired
private JdbcTemplate jdbcTemplate;
// 根据观众ID获取观众信息
@Override
public Audience getAudienceById(int audienceId) {
String sql = "SELECT * FROM audience WHERE audience_id=?";
return jdbcTemplate.queryForObject(sql, new Object[]{audienceId}, new AudienceRowMapper());
}
// 观众投票
@Override
public void vote(int audienceId, int artistId) {
String sql = "INSERT INTO vote(audience_id, artist_id) VALUES (?,?)";
jdbcTemplate.update(sql, audienceId, artistId);
}
// 观众留言
@Override
public void leaveMessage(int audienceId, int artistId, String message) {
String sql = "INSERT INTO message(audience_id, artist_id, content) VALUES (?,?,?)";
jdbcTemplate.update(sql, audienceId, artistId, message);
}
// 观众打赏
@Override
public void reward(int audienceId, int artistId, int amount) {
String sql = "UPDATE artist SET popularity=popularity+?, income=income+? WHERE artist_id=?";
jdbcTemplate.update(sql, amount, amount, artistId);
sql = "INSERT INTO reward(audience_id, artist_id, amount) VALUES (?,?,?)";
jdbcTemplate.update(sql, audienceId, artistId, amount);
}
}
class AudienceRowMapper implements RowMapper<Audience> {
@Override
public Audience mapRow(ResultSet rs, int rowNum) throws SQLException {
Audience audience = new Audience();
audience.setAudienceId(rs.getInt("audience_id"));
audience.setUsername(rs.getString("username"));
audience.setPassword(rs.getString("password"));
audience.setNickname(rs.getString("nickname"));
//...
return audience;
}
}
```
2. 观众Service接口及其实现类:
```
public interface AudienceService {
// 观众登录
Audience login(String username, String password);
// 观众投票
void vote(int audienceId, int artistId) throws VoteException;
// 观众留言
void leaveMessage(int audienceId, int artistId, String message);
// 观众打赏
void reward(int audienceId, int artistId, int amount) throws RewardException;
}
@Service
public class AudienceServiceImpl implements AudienceService {
@Autowired
private AudienceDao audienceDao;
// 观众登录
@Override
public Audience login(String username, String password) {
Audience audience = audienceDao.getAudienceByUsername(username);
if (audience == null || !audience.getPassword().equals(password)) {
return null;
}
return audience;
}
// 观众投票
@Override
public void vote(int audienceId, int artistId) throws VoteException {
// 判断是否已经投过票
if (hasVoted(audienceId, artistId)) {
throw new VoteException("已经投过票了");
}
// 更新选手的票数
updateVoteCount(artistId);
// 记录投票记录
audienceDao.vote(audienceId, artistId);
}
// 观众留言
@Override
public void leaveMessage(int audienceId, int artistId, String message) {
audienceDao.leaveMessage(audienceId, artistId, message);
}
// 观众打赏
@Override
public void reward(int audienceId, int artistId, int amount) throws RewardException {
// 判断余额是否足够
Audience audience = audienceDao.getAudienceById(audienceId);
if (audience.getBalance() < amount) {
throw new RewardException("余额不足");
}
// 更新选手的人气和收入
audienceDao.reward(audienceId, artistId, amount);
}
// 判断是否已经投过票
private boolean hasVoted(int audienceId, int artistId) {
String sql = "SELECT COUNT(*) FROM vote WHERE audience_id=? AND artist_id=?";
Integer count = audienceDao.queryForObject(sql, new Object[]{audienceId, artistId}, Integer.class);
return count != null && count > 0;
}
// 更新选手的票数
private void updateVoteCount(int artistId) {
String sql = "UPDATE artist SET vote_count=vote_count+1 WHERE artist_id=?";
audienceDao.update(sql, artistId);
}
}
```
3. 观众Controller:
```
@Controller
@RequestMapping("/audience")
public class AudienceController {
@Autowired
private AudienceService audienceService;
// 观众登录
@PostMapping("/login")
@ResponseBody
public Result login(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpSession session) {
Audience audience = audienceService.login(username, password);
if (audience == null) {
return Result.fail("用户名或密码错误");
}
session.setAttribute("audience", audience);
return Result.success();
}
// 观众投票
@PostMapping("/vote")
@ResponseBody
public Result vote(@RequestParam("artistId") int artistId, HttpSession session) {
Audience audience = (Audience) session.getAttribute("audience");
if (audience == null) {
return Result.fail("请先登录");
}
try {
audienceService.vote(audience.getAudienceId(), artistId);
return Result.success();
} catch (VoteException e) {
return Result.fail(e.getMessage());
}
}
// 观众留言
@PostMapping("/leaveMessage")
@ResponseBody
public Result leaveMessage(@RequestParam("artistId") int artistId,
@RequestParam("message") String message,
HttpSession session) {
Audience audience = (Audience) session.getAttribute("audience");
if (audience == null) {
return Result.fail("请先登录");
}
audienceService.leaveMessage(audience.getAudienceId(), artistId, message);
return Result.success();
}
// 观众打赏
@PostMapping("/reward")
@ResponseBody
public Result reward(@RequestParam("artistId") int artistId,
@RequestParam("amount") int amount,
HttpSession session) {
Audience audience = (Audience) session.getAttribute("audience");
if (audience == null) {
return Result.fail("请先登录");
}
try {
audienceService.reward(audience.getAudienceId(), artistId, amount);
return Result.success();
} catch (RewardException e) {
return Result.fail(e.getMessage());
}
}
}
```
基于ssm框架的出道选秀管理系统的功能模块代码
由于出道选秀管理系统的功能模块比较多,这里我只能给你提供一个大致的代码框架,具体实现还需要根据你的具体需求进行开发。
1. 用户管理模块代码:
```
//UserMapper.java
public interface UserMapper {
//添加用户
int addUser(User user);
//更新用户
int updateUser(User user);
//删除用户
int deleteUser(int id);
//查询用户
User selectUserById(int id);
//查询所有用户
List<User> selectAllUsers();
}
//UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
@Override
public int deleteUser(int id) {
return userMapper.deleteUser(id);
}
@Override
public User selectUserById(int id) {
return userMapper.selectUserById(id);
}
@Override
public List<User> selectAllUsers() {
return userMapper.selectAllUsers();
}
}
```
2. 选秀活动管理模块代码:
```
//ActivityMapper.java
public interface ActivityMapper {
//添加选秀活动
int addActivity(Activity activity);
//更新选秀活动
int updateActivity(Activity activity);
//删除选秀活动
int deleteActivity(int id);
//查询选秀活动
Activity selectActivityById(int id);
//查询所有选秀活动
List<Activity> selectAllActivities();
}
//ActivityServiceImpl.java
@Service
public class ActivityServiceImpl implements ActivityService {
@Autowired
private ActivityMapper activityMapper;
@Override
public int addActivity(Activity activity) {
return activityMapper.addActivity(activity);
}
@Override
public int updateActivity(Activity activity) {
return activityMapper.updateActivity(activity);
}
@Override
public int deleteActivity(int id) {
return activityMapper.deleteActivity(id);
}
@Override
public Activity selectActivityById(int id) {
return activityMapper.selectActivityById(id);
}
@Override
public List<Activity> selectAllActivities() {
return activityMapper.selectAllActivities();
}
}
```
3. 选手管理模块代码:
```
//PlayerMapper.java
public interface PlayerMapper {
//添加选手
int addPlayer(Player player);
//更新选手
int updatePlayer(Player player);
//删除选手
int deletePlayer(int id);
//查询选手
Player selectPlayerById(int id);
//查询所有选手
List<Player> selectAllPlayers();
}
//PlayerServiceImpl.java
@Service
public class PlayerServiceImpl implements PlayerService {
@Autowired
private PlayerMapper playerMapper;
@Override
public int addPlayer(Player player) {
return playerMapper.addPlayer(player);
}
@Override
public int updatePlayer(Player player) {
return playerMapper.updatePlayer(player);
}
@Override
public int deletePlayer(int id) {
return playerMapper.deletePlayer(id);
}
@Override
public Player selectPlayerById(int id) {
return playerMapper.selectPlayerById(id);
}
@Override
public List<Player> selectAllPlayers() {
return playerMapper.selectAllPlayers();
}
}
```
4. 投票管理模块代码:
```
//VoteMapper.java
public interface VoteMapper {
//添加投票记录
int addVote(Vote vote);
//更新投票记录
int updateVote(Vote vote);
//查询投票记录
Vote selectVoteById(int id);
//查询某个选手的所有投票记录
List<Vote> selectAllVotesByPlayerId(int playerId);
//查询某个用户对某个选手的投票记录
Vote selectVoteByUserIdAndPlayerId(int userId, int playerId);
}
//VoteServiceImpl.java
@Service
public class VoteServiceImpl implements VoteService {
@Autowired
private VoteMapper voteMapper;
@Override
public int addVote(Vote vote) {
return voteMapper.addVote(vote);
}
@Override
public int updateVote(Vote vote) {
return voteMapper.updateVote(vote);
}
@Override
public Vote selectVoteById(int id) {
return voteMapper.selectVoteById(id);
}
@Override
public List<Vote> selectAllVotesByPlayerId(int playerId) {
return voteMapper.selectAllVotesByPlayerId(playerId);
}
@Override
public Vote selectVoteByUserIdAndPlayerId(int userId, int playerId) {
return voteMapper.selectVoteByUserIdAndPlayerId(userId, playerId);
}
}
```
阅读全文