java写的关于studentlist的简单类

emm第一次写java,期间查了很多资料啊,,还借鉴了一下别人类的写法,。。
写完赶紧继续acm去了

import java.util.Scanner;

import	java.io.*;

import java.text.DecimalFormat;

class student{
			private String snumber;
			private String sname;
			private int markfm;
			private int markfe;
			private int markfs;
			student(String snumber,String sname)
			{
				this.snumber=snumber;
				this.sname=sname;
				
			}
			String getname()
			{
				return sname;
			}
			String getnumber()
			{
				return snumber;
			}
			void entermarks(int markfm,int markfe,int markfs)
			{
				this.markfe=markfe;
				this.markfm=markfm;
				this.markfs=markfs;
			}
			int getm()
			{
				return markfm;
			}
			int gete()
			{
				return markfe;
			}
			int gets(){
				return markfs;
			}
			double calav()
			{
				return (float)(markfe+markfm+markfs)/3;
			}
			public String toString() {
					return ("Number:"+this.snumber+"\nName:"+this.sname+"\nMath:"+this.markfm+"\nEnglish:"+this.markfe+"\nScience:"+this.markfs+"\nAve:"+String.format("%.2f",calav()));
				}}
			 
 class StudentList{
				student[] list;
				int total=0;
				StudentList(int lenth)
				{
					list=new student[lenth];
				}
				boolean add(student stu)
				{
					if(total>=list.length)
					{
						return false;
					}
					else {
						list[total]=new student(stu.getnumber(),stu.getname());
						list[total++].entermarks(stu.getm(), stu.gete(), stu.gets());
						return true;
					}
				}
				boolean remove(int no)
				{
					if(no>=total)
					{
						return false;
					}
					else {
						for(int j=no;j<total-1;j++)
						{
							list[j]=list[j+1];
						}
						total--;
						return true;
					}
				}
				boolean remove(String number)
				{
					if(!isEmpty())
					{		for(int i=0;i<total;i++)
					{
						if(list[i].getnumber()==number)
						{
							for(int j=i;j<total-1;j++)
							{
								list[j]=list[j+1];
							}
							total--;
							return true;
						}
					}}
					return false;
				}
				boolean isEmpty()
				{
					if(total==0)return true;
					return false;
				}
				student getitem(int no)
				{
					if(no>=total)return null;
					//no--;
					return list[no];
				}
				student getitem(String number)
				{
					for(int i=0;i<total;i++)
					{
						if(list[i].getnumber()==number)
						{
							return list[i];
						}
					}
					return null;
				}
				int gettotal()
				{
					return total;
				}
			}
 
public class students {
	public static void main(String[] args) 
			{
				StudentList list1=new StudentList(500);
				String line;
				Scanner ss=new Scanner(System.in);
				int markForMaths;
				int markForEnglish;
				int markForScience;
				String studentNumber;
				String studentName;
				while(ss.hasNext())
				{
					line=ss.nextLine().trim();
					String[] s1=line.split(" ");
					String s2=s1[0].trim();
					if(Integer.valueOf(s2).intValue()==1)
					{
						studentNumber =s1[1];
						studentName = s1[2];
						markForMaths =Integer.valueOf(s1[3]).intValue();
						markForEnglish=Integer.valueOf(s1[4]).intValue();
						markForScience=Integer.valueOf(s1[5]).intValue();
						student stu = new student(studentNumber,studentName);
						stu.entermarks(markForMaths, markForEnglish, markForScience);
						if(list1.add(stu)) {
							System.out.println(list1.gettotal()+" Students at the moment as described below:");
							for(int i=0;i<list1.gettotal();i++)
								System.out.println(list1.getitem(i).toString());
						}
						else {System.out.println("当前出入有误请重新输入:");}
					}
					if(Integer.valueOf(s2).intValue()==2)
					{
						studentNumber=s1[1];
						if(list1.remove(studentNumber))
						{
							//syso
							System.out.println("Remove successfully");
							System.out.println(list1.total+" Students at the moment as described below:");
							for(int i=0;i<list1.total;i++)
							{
								System.out.println(list1.getitem(i).toString());
							}
						}
						else {
							System.out.println("No such student");
						}
					}
					if(Integer.valueOf(s2).intValue()==3)
					{
						int temp=Integer.valueOf(s1[1]).intValue();
						if(list1.remove(temp-1))
						{
							//syso
							System.out.println("Remove successfully");
							System.out.println(list1.total+" Students at the moment as described below:");
							for(int i=0;i<list1.total;i++)
							{
								System.out.println(list1.getitem(i).toString());
							}
						}
						else {
							System.out.println("No such student");
						}
					}
					if(Integer.valueOf(s2).intValue()==4)
					{
						if(list1.isEmpty())
						{
							System.out.println("为空");
						}
						else {
							{
								System.out.println("不为空");
							}
						}
					}
					if(Integer.valueOf(s2).intValue()==5)
					{
						int temp=Integer.valueOf(s1[1]).intValue();
						if(list1.getitem(temp-1)!=null)
						{
							System.out.println("Student Info:\n"+list1.getitem(temp-1).toString());
						}
						else System.out.println("No such student");
					}
					if(Integer.valueOf(s2).intValue()==6)
					{
						int temp=Integer.valueOf(s1[1]).intValue();
						if(list1.getitem(s1[1])!=null)
							System.out.println("Student Info:\n"+list1.getitem(s1[1]).toString());
						else System.out.println("No such student");
					}
					if(Integer.valueOf(s2).intValue()==7)
					{
						System.out.println(list1.total+" Students at the moment as described below:");
						for(int i=0;i<list1.gettotal();i++)
						{
							System.out.println(list1.getitem(i).toString());
						}
						
					}
					if(Integer.valueOf(s2).intValue()==8)
					{
						System.exit(0);
					}
				}
				
			}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值