一个简单的java小项目
对于Java初学者来说,实践很重要,下面就举一个简单的小例子:
学生选举班长项目:
// 定义Student类
package com.vote.student;
//import java.util.Comparable;
// 学生类,保存候选人信息
public class Student implements Comparable
{
private int stuNo = 0; // 学生编号
private String name = null; // 学生姓名
private int vote = 0; // 得票数目
public Student()
{
}
public Student(int stuNo, String name, int vote)
{
this.stuNo = stuNo;
this.name = name;
this.vote = vote;
}
public int getNo()
{
return this.stuNo; // 获取编号
}
public String getName()
{
return this.name; // 获取姓名
}
public int getVote()
{
return this.vote; // 获取票数
}
public void setNo(int stuNo)
{
this.stuNo = stuNo; // 设置编号
}
public void setName(String name)
{
this.name = name; // 设在姓名
}
public void setVote(int vote)
{
this.vote = vote; // 设置票数
}
public int compareTo(Student o) // 覆写,实现比较器
{
if (this.vote > o.vote)
{
return -1; // 按降序排列
}
else if (this.vote < o.vote)
{
return 1;
}
else
{
return 0;
}
}
public String toString()// 覆写:1: zhangSan [5]
{
return this.getNo() + “: ” + this.getName() + ” [” + this.getVote() + “]”;
}
}
// 定义InputData类
package com.project.inputdata;
// ———————————————InputData类————————————————————
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class InputData // 实现类型数据输入
{
private BufferedReader buf = null; // 读取数据
public InputData()
{
this.buf = new BufferedReader(new InputStreamReader(System.in)); // 接收标准输入;
}
public String getString(String info) // 获取输入的字符串形式
{
String temp = null; // 接收输入数据
System.out.println(info); // 打印提示信息
try
{
temp = this.buf.readLine(); // 读取标准输入以字符串形式返回;
}
catch (IOException e)
{
e.printStackTrace();
}
return temp;
}
public int getInt(String info, String err) // 获取整型的输入数据
{
int temp = 0; // 接收输入
String str = null; // 获取字符串信息
boolean flag = true; // 监视输入
while (flag)
{
str = this.getString(info); // 打印提示,并等待接收字符串信息
if (str.matches(“^\\d+$”)) // 判断是数字组成
{
temp = Integer.parseInt(str); // String–>Integer–>int
flag = false; // 结束循环
}
else
{
System.out.println(err); // 打印错误信息
}
}
return temp;
}
public float getFloat(String info, String err) // 获取输入的float型小数
{
float temp = 0.0f; // 接收输入
String str = null; // 获取字符串信息
boolean flag = true; // 监视输入
while (flag)
{
str = this.getString(info); // 打印提示,并等待接收字符串信息
if (str.matches(“^\\d+.?\\d+f$”)) // 判断是数字组成
{
temp = Float.parseFloat(str); // String–>Float–>float
flag = false; // 结束循环
}
else
{
System.out.println(err); // 打印错误信息
}
}
return temp;
}
public Date getDate(String info, String err) // 获取输入的Date型日期
{
Date temp = null; // 接收输入
String str = null; // 获取字符串信息
boolean flag = true; // 监视输入
while (flag)
{
str = this.getString(info); // 打印提示,并等待接收字符串信息
if (str.matches(“^\\d{4}-[0-1]\\d-[0-3]\\d$”)) // 判断是数字组成
{
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”); // 实例化日期风格
try
{
temp = sdf.parse(str); // String–>Date
}
catch (ParseException e)
{
e.printStackTrace();
}
flag = false; // 结束循环
}
else
{
System.out.println(err); // 打印错误信息
}
}
return temp;
}
}
// 定义Operate类
package com.vote.operate;
import com.vote.student.*;
import com.vote.inputdata.*;
public class Operate
{
private Student[] stu = null; // 声明候选人数组
private int num = 0; // 候选人个数
private InputData input = new InputData(); // 接收标准输入
public Operate()
{
this.num = this.input.getInt(“请输入候选人数量:”, “输入整数!”);
if (this.num > 1) // 2个以上才有必要投票
{
this.stu = new Student[this.num];
this.init(); // 设置候选人信息
}
else
{
System.out.println(“至少有两个候选者才有必要投票!”);
}
this.vote(); // 投票
// 显示当选者信息
this.getResult();
}
public void init() // 初始化投票环境
{
int temp = 0;
for (int x = 0; x < this.stu.length; x++)
{
this.stu[x] = new Student(); // 实例化候选人
this.stu[x].setNo(x + 1); // 编号由系统维护
this.stu[x].setName(this.input.getString(“请输入第” + (x+1) + “个候选者的姓名:”));
}
this.voteInfo(); // 查看候选人信息
String sel = this.input.getString(“请确认信息,是否提交!(y:提交 n:修改)”);
while (true)
{
if (“y”.equals(sel) || “Y”.equals(sel))
{
break; // 退出循环
}
else if (“n”.equals(sel) || “N”.equals(sel))
{ temp = this.input.getInt(“输入要修改的候选者编号”, “输入整数”);
if (temp > 1 && temp <= this.stu.length)
{
this.stu[temp – 1].setName(this.input.getString(“输入修改姓名:”)); // 修改数据
System.out.println(“修改成功!”);
break;
}
else
{
System.out.println(“输入范围:” + “(1 <= " + "编号 <= " + this.stu.length +")");
}
}
else
{
System.out.println(“请输入正确的选择:( y : 提交, n : 修改)”);
sel = this.input.getString(“重新输入选择:!(y:提交 n:修改)”);
}
}
}
public void vote()
{
this.voteInfo(); // 查看候选人信息
while (true)
{
int sel = this.input.getInt(“请选择候选人编号为其投票” + “(1 <= " + "编号 <= " + this.stu.length + ")" + "\n(输入0退出!)", "编号是整数!");
if (sel < 0 || sel > this.num)
{
System.out.println(“必须输入列出的编号: ” + “(1 <= " + "编号 <= " + this.stu.length +")");
}
else if (sel == 0)
{
String s = this.input.getString(“确认退出:(y|n?)”);
if (“y”.equals(s) || “Y”.equals(s))
{
break; // 退出循环
}
else if (“n”.equals(s) || “N”.equals(s))
{
continue;
}
else
{
System.out.println(“请输入正确的选择:( y : 提交, n : 继续)”);
}
}
else
{
this.stu[sel – 1].setVote(this.stu[sel – 1].getVote() + 1); // 投票成功
System.out.println(“投票成功!”);
this.voteInfo(); // 更新候选人信息
}
}
}
public Student[] getStu()
{
return this.stu; // 获取候选人数组
}
public int getNum()
{
return this.num; // 获取候选人数
}
public void setNum(int num)
{
this.num = num; // 设置候选人数量
}
public void voteInfo() // 候选人信息
{
if (this.getStu() != null)
{
System.out.println(“当前候选人信息:”);
for (Student s : this.stu)
{
System.out.println(s);
}
}
else
{
System.out.println(“候选人信息未设置,请先设置!”);
}
}
private void getResult() // 获得结果
{
java.util.Arrays.sort(this.stu); // 排序票数
System.out.println(“投票结果:” + this.stu[0].getName() + “同学,以” + this.stu[0].getVote() + “票当选班长!”);
}
}
// 定义主类
package com.vote;
import com.vote.operate.*;
public class Vote
{
public static void main(String[] args)
{
new Operate(); // 初始化投票系统
}
}
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《一个简单的java小项目》
本文地址:http://www.xiupu.net/archives-108.html
关注公众号:
微信赞赏
支付宝赞赏