package cn.edu;
public class manager {
public static void main(String srg[])
{
function fun=new function();
for(;5!=function.getChoose();)
{
switch(function.getChoose())
{
case 1:
fun.add();
break;
case 2:
fun.seek();
break;
case 3:
fun.delete();
break;
case 4:
fun.showAll();
break;
}
}
System.out.println("您已经退出该系统,感谢您的使用!");
}
}
package cn.edu;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Vector;
public class function {
//显示系统界面并获取用户的选择
public static int getChoose()
{
System.out.println("*************************************************");
System.out.println("*********************歌曲管理********************");
System.out.println("*************************************************");
System.out.println("---------------------1.添加歌曲------------------");
System.out.println("---------------------2.查找歌曲------------------");
System.out.println("---------------------3.删除歌曲------------------");
System.out.println("---------------------4.显示所有歌曲--------------");
System.out.println("---------------------5.退出系统------------------");
System.out.println("*************************************************");
System.out.println("请输入您的选择:");
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
String str;
try {
str = bufferedReader.readLine();
int i=Integer.parseInt(str);
if(i<1 || i>5)
{
System.out.println("您的输入有误,请重新输入:");
}
return i;
}
catch (Exception e)
{
System.out.println("您的输入有误,请重新输入:");
}
return getChoose();
}
//定义向量保存歌曲信息
Vector<song> v=new Vector<song>(0,1);
song s=new song();
//添加歌曲
public boolean add()
{
try
{
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要添加的是第"+(v.capacity()+1)+"首歌曲!");
s.setsId(v.capacity()+1);
System.out.println("请输入您要添加歌曲的名字:");
s.setsName(bufferedReader.readLine());
System.out.println("请输入您要添加歌曲的语言:");
s.setsLan(bufferedReader.readLine());
System.out.println("请输入您要添加歌曲的类型:");
s.setsType(bufferedReader.readLine());
System.out.println("请输入您要添加歌曲的演唱者:");
s.setsSinger(bufferedReader.readLine());
v.addElement(s);
System.out.println("歌曲添加成功!");
}
catch(Exception e)
{
System.out.println("歌曲添加失败,请重新执行添加操作!");
}
return true;
}
//按歌曲名查找歌曲
public boolean seek()
{
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要查询的歌曲名:");
try
{
String str=bufferedReader.readLine();
Iterator<song> it=v.iterator();
if(it.hasNext())
{
song s=it.next();
if(str.equals(s.getsName()))
{
System.out.println("歌曲编号"+"\t"+"歌曲名"+"\t"+"歌曲语言"+"\t"+"歌曲类型"+"\t"+"歌曲演唱者");
System.out.println(s.getsId()+"\t"+s.getsName()+"\t"+s.getsLan()+"\t"+s.getsType()+"\t"+s.getsSinger());
}
else
System.out.println("没有您要查找的歌曲,请检出您的输入是否有误!");
}
}
catch(Exception e)
{
System.out.println("歌曲查找失败,请重新执行查找操作!");
}
return true;
}
//删除歌曲
public boolean delete()
{
try
{
System.out.println("请输入您要删除的歌曲名:");
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
String str1=bufferedReader.readLine();
Iterator<song> iter = v.iterator();
while(iter.hasNext()){
song s =iter.next();
if(str1.equals(s.getsName())){
iter.remove();
System.out.println("删除歌曲成功!");
}
else
{
System.out.println("没有您要删除的歌曲,请检出您的输入是否有误!");
}
}
}
catch(Exception e)
{
System.out.println("歌曲删除失败,请重新执行删除操作!");
}
return true;
}
//显示所有歌曲
public boolean showAll()
{
Iterator<song> ite = v.iterator();
if(ite.hasNext()){
song s = ite.next();
System.out.println("歌曲编号"+"\t"+"歌曲名"+"\t"+"歌曲语言"+"\t"+"歌曲类型"+"\t"+"歌曲演唱者");
System.out.println(s.getsId()+"\t"+s.getsName()+"\t"+s.getsLan()+"\t"+s.getsType()+"\t"+s.getsSinger());
}
return true;
}
}
package cn.edu;
public class song {
private int sId;
private String sName;
private String sLan;
private String sType;
private String sSinger;
public void setsId(int sId) {
this.sId = sId;
}
public int getsId() {
return sId;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsLan() {
return sLan;
}
public void setsLan(String sLan) {
this.sLan = sLan;
}
public String getsType() {
return sType;
}
public void setsType(String sType) {
this.sType = sType;
}
public String getsSinger() {
return sSinger;
}
public void setsSinger(String sSinger) {
this.sSinger = sSinger;
}
public song(int sId,String sName,String sLan,String sType,String sSinger)
{
this.sId=sId;
this.sName=sName;
this.sLan=sLan;
this.sType=sType;
this.sSinger=sSinger;
}
public song() {
// TODO Auto-generated constructor stub
}
}
代码如上,不知道哪里错了,无论添加多少首歌曲,查找和删除的时候都只能找到最后的一首?知道的烦请解答一下,谢谢!