java 用eclipse做简单的学生选课
import.java.util.*;
武邑ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
public static void Main(String [] args)
{
String [] classes=new [] String[4]{"A","B","C","D"};
int a=0,b=0,c=0,d =0;
Scanner input =new Scanner(System.in);
for(int i = 0 ;i30;i++)
{
System.out.println("请输入你选的课程号:");
String choice=input.next();
if(choice.equals("A"))
{
a++;
}
else if(choice.equals("B"))
{
b++;
}
else if(choice.equals("A"))
{
c++;
}
else if(choice.equals("A"))
{d++;
}
}
System.out.println("A门课程被选中"+a+"次");
System.out.println("B门课程被选中"+b+"次");
System.out.println("C门课程被选中"+c+"次");
System.out.println("D门课程被选中"+d+"次");
}
java代码
帮你改了一下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class logon extends JFrame {
public static void main(String args[]) {
logon a=new logon();
a.show();
}
CheckboxGroup ShenFen=new CheckboxGroup();
Checkbox ShenFen1;
Checkbox ShenFen2;
Checkbox ShenFen3;
JButton LogonButton,Exit;
JTextField id;
JPasswordField password;
public logon(){
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLayout(null);
setTitle("学生选课系统");
setSize(600,600);
ShenFen1=new Checkbox("学生",ShenFen,true);
ShenFen2=new Checkbox("教师",ShenFen,false);
ShenFen3=new Checkbox("管理员",ShenFen,false);
add(ShenFen1);
add(ShenFen2);
add(ShenFen3);
ShenFen1.setBounds(300, 350, 50, 50);
ShenFen2.setBounds(350, 350, 50, 50);
ShenFen3.setBounds(400, 350, 50, 50);
LogonButton=new JButton("登录");
add(LogonButton);
Exit=new JButton("退出");
add(Exit);
Exit.setBounds(400, 400,60, 60);
id=new JTextField();
add(id);
id.setBounds(300, 200, 200, 30);
password=new JPasswordField();
add(password);
password.setBounds(300, 250, 200, 30);
}
}
java学生选课系统
学生选课系统的设计与实现摘要本文以一个具体的应用系统—学生选课信息系统的设计与实现来说明如何利用UML和EJB组件来设计和构建分布式软件系统平台。UML和组件技术结合使用能提高开发效率,增加系统的易维护性。关键词UML;EJB;实例1引言现在信息管理系统软件的开发,采用组件技术可以提高效率,信息管理系统的分析设计也采用UML来进行。两者的结合可以极大的提高开发效率,保证系统开发的易维护性。本文用UML这种设计方法和EJB这种组件技术来设计和实现一个系统。2系统分析本系统设计为学生通过网页登陆学校网站,进行选课。下面用用例图来说明该系统要实现的功能。2.1用例图2.2系统总体结构图本系统采用三层体系结构,分为表示层,事务处理层,数据存储层。三层结构层次清晰,易维护。图3类图学生选课系统涉及到三个实体类学生,课程,以及学生和课程之间的一个一对多关系类。对每一个类,映射到一张表。学生类和课程类用Container-ManagedEntityBean实现,学生和课程间的一对多关系类,用Bean-ManagedPersistence的EntityBean实现。再设计一个SessionBean对学生选课过程进行控制。页面显示部分用JSP实现。3数据库设计学生表对应学生实体,详细内容如下表1学生表关联表对应学生和课程间的一对多关系,详细内容如下研究开发4实现4.1SessionBean的设计4.1.1定义HomeInterface4.1.2定义RemoteInterfacepublicinterfaceEnrollSessionextendsEJBObject{//-----//这是一个基于SessionBean的Remote接口,这个SessionBean是基于//Stateful的SessionBean,用来对特定学生选课的登记过程进行操作//-----------------------------publicStringgetStudentName()throwsRemoteException;publicvoidenroll(ArrayListcourseItems)throwsRemoteExcep-tion;publicvoidunenroll()throwsRemoteException;publicvoiddeleteStudent()throwsFinderException,RemoteEx-ception;publicvoiddeleteCourse(Stringcourse_id)throwsRemoteExcep-tion;}4.1.3Client获取HomeInterface和RemoteInterface的参考方式,我们使用JNDI机制来获取Home接口和Remote接口的对象参考。4.1.4定义回调方法4.1.5实现远程数据库的连接使用JNDI机制,通过数据库的JNDI名称javacomp/env/jdbc/StudentCourseDB来连接后台数据库。4.1.6SessionBean方法实现//定义变量publicStudentHomesHome;publicEnrollHomeeHome;publicStringstudent_id;publicStringname;//回调方法实现publicvoidejbCreate(Stringstudent_id)throwsCreateException{try{Studentstudent=sHome.findByPrimaryKey(student_id);name=student.getName();}catch(ObjectNotFoundExceptione){thrownewCreateException(Student+student_id+notfoundinStudentTBL!);}catch(Exceptione){thrownewEJBException(e.getMessage());}this.student_id=student_id;}//商业方法实现publicvoidenroll(ArrayListcourseItems){Enrollenroll=null;try{enroll=eHome.findByPrimaryKey(student_id);}catch(Exceptione){}try{if(enroll!=null){enroll.replaceCourseItems(courseItems);}else{eHome.create(student_id,courseItems);}}catch(Exceptione){thrownewEJBException(e.getMessage());}}publicvoidunenroll(){try{Enrollenroll=eHome.findByPrimaryKey(student_id);enroll.remove();}catch(Exceptione){thrownewEJBException(e.getMessage());}}//涉及到对两张表的删除。publicvoiddeleteStudent()throwsFinderException{try{Enrollenroll=eHome.findByPrimaryKey(student_id);Studentstudent=sHome.findByPrimaryKey(student_id);enroll.remove();student.remove();}catch(Exceptione){thrownewEJBException(e.getMessage());}}publicvoiddeleteCourse(Stringcourse_id){PreparedStatementps=null;try{getConnection();StringdeleteStatement=deletefromEnrollTBL+wherestudent_id=?andcourse_id=?;ps=con.prepareStatement(deleteStatement);ps.setString(1,student_id);ps.setString(2,course_id);ps.executeUpdate();}catch(Exceptione){thrownewEJBException(e.getMessage());}finally{try{ps.close();con.close();}catch(Exceptione){thrownewEJBException(e.getMessage());}}}4.2EntityBean的设计我们以关联表(EnrollTBL)对应的实体Bean为例进行说明,它涉及到两个表的一对多关系。4.2.1定义Home接口4.2.2定义RemoteInterfacepublicinterfaceEnrollextendsEJBObject{//---------------//这是一个基于EntityBean的Remote接口,这个EntityBean是基于//Bean-ManagedPersistence的EntityBean,用来对EnrollTBL表进行操作//--------------------------publicArrayListgetCourseItems()throwsRemoteException;publicStringgetStudent_id()throwsRemoteException;publicvoidreplaceCourseItems(ArrayListcourseItems)throwsRemoteException;}4.2.3变量定义publicStringstudent_id;publicArrayListcourseItems;4.2.4增加数据记录实现publicStringejbCreate(Stringstudent_id,ArrayListcourseItems)throwsCreateException{if(courseItems==null||courseItems.size()==0){thrownewCreateException(ejbCreateexception!);}this.student_id=student_id;try{enroll(courseItems);}catch(Exceptione){thrownewEJBException(ejbCreateexception+e.getMessage());}this.courseItems=courseItems;returnstudent_id;}//根据学生ID,插入课程项。privatevoidenroll(ArrayListcourseItems)throwsSQLException{StringinsertStatement=insertintoEnrollTBLvalues(?,?);PreparedStatementps=con.prepareStatement(insertStatement);try{//------------//依次将所有的课程项目插入EnrollTBL表//------------------ps.setString(1,this.student_id);for(inti=0;icourseItems.size();i++){Stringcourse_id=(String)courseItems.get(i);ps.setString(2,course_id);ps.executeUpdate();}}finally{ps.close();}}//根据学生ID,删除课程项privatevoidunenroll()throwsSQLException{StringdeleteStatement=deletefromEnrollTBL+wherestudent_id=?;PreparedStatementps=con.prepareStatement(deleteStatement);try{ps.setString(1,student_id);ps.executeUpdate();}finally{ps.close();}}5部署和运行5.1部署程序5.1.1部署StudentEntityBean,设置事务属性,生成部署文件。5.1.2同样,再部署其他三个EJB组件EnrollSessionBean,CourseEntityBean,EnrollEntityBean。5.1.3部署web组件5.1.4部署整个应用程序5.2运行启动J2EE服务器,启动数据库服务器,打开浏览器。学生登录,即可选课。6结论组件技术使得复杂的多层结构应用系统开发变得容易。采用组件技术能提高开发人员的效率,降低软件的开发和维护成本,提高软件的质量,控制所构建系统的复杂性。UML设计方法的使用能提高软件设计的效率和保证设计的规范性。参考文献[1]CraigLarman.UML和模式应用-面向对象分析与设计导论[M].北京机械工业出版社,2003.10-100.[2]刘特.J2EEEJB应用编程实例[M].北京清华大学出版社,2003.90-150.[3]GeriSchneider.用例分析技术[M].北京机械工业出版社,2003.1-98.
-----------------------------------------------
谢谢请采纳:
不能发连接啊,请在百度
搜索‘珍惜知识网’点进去就是了[ORDA]
Java帮我写下代码~
项目结构如下:
代码如下
package main;
public class Course {
private String classNum;
private String credit;
private String startClassDate;
private String endClassDate;
private String teacher;
private String assessment;
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public String getCredit() {
return credit;
}
public void setCredit(String credit) {
this.credit = credit;
}
public String getStartClassDate() {
return startClassDate;
}
public void setStartClassDate(String startClassDate) {
this.startClassDate = startClassDate;
}
public String getEndClassDate() {
return endClassDate;
}
public void setEndClassDate(String endClassDate) {
this.endClassDate = endClassDate;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getAssessment() {
return assessment;
}
public void setAssessment(String assessment) {
this.assessment = assessment;
}
@Override
public String toString() {
return " 课程号:" + classNum + ", 学分:" + credit
+ ", 上课时间:" + startClassDate + ", 下课时间:"
+ endClassDate + ", 教师:" + teacher + ", 考核方式:"
+ assessment + "";
}
}
package main;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("创建学生请输入姓名:");
Student stu1 = new Student();
stu1.setName(sc.nextLine());
Scanner sc1 = new Scanner(System.in);
System.out.println("创建学生请输入学号:");
stu1.setNumber(sc1.nextLine());
System.out.println("请选择课程,课程如下:");
ListCourse list2 = new ArrayListCourse();
stu1.setCourses(list2);
ListCourse list = readTxt();
for (Course course : list) {
System.out.println(course.toString());
}
for (int i = 1; i 5; i++) {
System.out.println("请输入第"+i+"个课程号:");
//输入第一个课程号
list2 = handleClass(list, list2);
System.err.println("已选择成功!");
list = notSelectClass(list, list2);
System.out.println("请选择课程,课程如下:");
if(i==4){
break;
}
for (Course course : list) {
System.out.println(course.toString());
}
}
System.out.println("课程已选完!结果如下:");
System.err.println("学生:"+stu1.getName()+":");
for (Course course : list2) {
System.err.println(course.toString());
}
}
/**
*
* 查看未选择可选课程
* @param list 全部可选课程
* @param list2 已选课程
* @return
*/
private static ListCourse notSelectClass(ListCourse list,ListCourse list2){
for (int i = 0; i list.size(); i++) {
for (Course course : list2) {
//把已选课程剔除
if(course.getClassNum().equals(list.get(i).getClassNum())){
list.remove(i);
continue;
}
//把时间重合课程剔除 startdate1 =enddate2 and enddate1=startdate2
if(daYuDengYu(course.getEndClassDate(),list.get(i).getStartClassDate())daYuDengYu(list.get(i).getEndClassDate(), course.getStartClassDate())){
list.remove(i);
}
}
}
return list;
}
public static boolean daYuDengYu(String first, String second){
BigDecimal bd1 = new BigDecimal(first);
BigDecimal bd2 = new BigDecimal(second);
return bd1点抗 pareTo(bd2)=0?true:false;
}
/**
* 处理选择课程
* @param list
* @param list2
* @return
*/
private static ListCourse handleClass(ListCourse list,ListCourse list2){
while (true) {
Scanner sssi = new Scanner(System.in);
String num = sssi.nextLine().trim();
for (Course course : list) {
if(num.equals(course.getClassNum())){
list2.add(course);
return list2;
}
}
System.out.println("课程号输入错误,请重新输入:");
}
}
/**
* 读取txt获取课程 每个课程按;分割每个属性按,分割
* @return
* @throws Exception
*/
private static ListCourse readTxt() throws Exception{
/* 读入TXT文件 */
String pathname = Main.class.getResource("/").toString();
pathname = pathname.substring(6, pathname.length()-4)+"/src/classs/class.txt"; //获取绝对路径
File filename = new File(pathname); // 要读取以上路径的txt文件
ListCourse list = new ArrayListCourse();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
//读取文件内容
byte[] b = new byte[bis.available()];
bis.read(b);
if(b!=null){
String [] strs= new String(b).split(";");
if (strs==null) {
return null;
}
for (String string : strs) {
String[] str = string.split(",");
Course course = new Course();
course.setAssessment(str[5].trim());
course.setClassNum(str[0].trim());
course.setCredit(str[1].trim());
course.setEndClassDate(str[3].trim());
course.setStartClassDate(str[2].trim());
course.setTeacher(str[4].trim());
list.add(course);
}
}
bis.close();//关闭流(关闭bis就可以了)
return list;
}
}
package main;
import java.util.List;
public class Student {
private String name;
private String number;
private ListCourse courses;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public ListCourse getCourses() {
return courses;
}
public void setCourses(ListCourse courses) {
this.courses = courses;
}
}
001,1,7,8,Joyce,assignment;
002,1,8,9,Joyce,assignment;
003,1,12,15,Joyce,assignment;
004,1,18,19,Joyce,assignment;
005,1,7,8,LI,assignment;
006,1,8,9,LI,assignment;
007,1,12,15,LI,assignment;
008,1,18,19,LI,assignment;
名称栏目:网上选课系统java代码 学生选课系统javaweb
文章位置:http://lswzjz.com/article/ddigjsh.html