我们要做一个图书管理系统,我建立了一个类,包括书名,作者名和价格。我想实现一个功能,就是在查询图书时通过作者名找图书。我编了一个,很死板,大家教我怎样该灵活一些,谢谢。
import java.io.*;
import java.util.*;
class process
{
void step1()
{
System.out.println("do what?");
System.out.println("a.search    b.exit");
char ch=0;
try
{
ch=(char)System.in.read();
System.in.skip(2);
}catch(IOException z){}
if(ch=='a')
{
System.out.println("a.by name    b.by author");

}
char ch2=0;
try
{
ch2=(char)System.in.read();
System.in.skip(2);
}catch(IOException z){}
if(ch2=='a')
{

    InputDate fuck=new InputDate();
    
    
    fuck.findbookname();
}

if(ch2=='b')
{
InputDate fuck2=new InputDate();
fuck2.findauthorname();
}
}
}
class InputDate 
{
static private String s;
static public void input()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
s=br.readLine();
}catch(IOException e){}

}
static public String getString()
{
input();
return s;

}
 public void findbookname()
{
String[] Names={"Java","C#","Delphi","Oracle"};
Vector names=new Vector();
     String s;
     for(int i=0;i<Names.length;i++)
     names.add(Names[i]);
     System.out.print("plz inupt the book's name:");
     s=InputDate.getString();
     if(names.indexOf(s)!=-1)
     System.out.println("there is a book as ur wish");
     else
     System.out.println("there is no one called this name,body!");
}
public void findauthorname()
{
    String[] Names2={"Johnny.R","Bill Gates","motherfucker","zy"};
Vector names2=new Vector();
     String s2;
     for(int i=0;i<Names2.length;i++)
     names2.add(Names2[i]);
     System.out.print("plz inupt the author's name:");
     s=InputDate.getString();
     if(names2.indexOf(s)!=-1)
     System.out.println("there is a book as ur wish");
     else
     System.out.println("there is not such a book!");
}
}
class stock
{
String name;
String author;
int price;
stock(String a,String b,int c)
{
name=a;
author=b;
price=c;
}
void print()
{
System.out.println("name is "+name);
System.out.println("author is "+author);
System.out.println("price is "+price);
}
}
class test
{
public static void main(String args[])
{
stock d=new stock("Java","Johnny.R",20);
//d.print();
stock e=new stock("C#","Bill Gates",30);
//e.print();
stock f=new stock("Delphi","motherfucker",40);
//f.print();
stock g=new stock("Oracle","zy",50);
//g.print();
process a1=new process();
a1.step1(); }
}

解决方案 »

  1.   

    看一下MVC的相关资料,把你要写实现的业务设计为Model、View和Control几个层次,再具体去实现好了。当然也可以利用一些已经现成的框架,如Struts之类。
    注意:MVC不是Struts或者其他什么框架,只是那些框架的实现是遵从了MVC的设计理念而已。你可以根据你业务的复杂程度来判定是自己写个简单的MVC架构还是使用已经存在、功能强大但也很复杂的东西。
      

  2.   

    建议使用MVC设计模式,把图书写成一个book类,它有编号,书名、作者等属性;可以通过逻辑类去实现一些业务,你的这个业务就是通过作者名来找图书。