⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printandmodify.java

📁 java实现的税务计算器程序。中英文操作界面
💻 JAVA
字号:
//**************************** Begin PrintAndModify.java
import java.util.*;
import java.io.*;
import basicIO.*;
public class PrintAndModify {
   //**** Construction: The no-argument constructor prompts
   //     for input and output file names.
   //     The one-argument constructor prompts for output file name.;
   //     The two-argument constructor tries to create the files.
   public PrintAndModify() {
	  //若没有文件则产生新文件,在本实验中没用,因为刚开始就读出数据文件,要是读取创建的空文件结果不用算都是0了
      inName = promptForFile( "infile" );
      outName = promptForFile( "outfile" );
      allEmps = new Vector();
   }
   public PrintAndModify( String in ) {
      inName = in;
      outName = promptForFile( "outfile" );
      allEmps = new Vector();
   }
   //本实验中用到的构造函数,参数是读取的文件名和修改以后存储的文件名
   public PrintAndModify( String in, String out ) {
      inName = in;
      outName = out;
      allEmps = new Vector();
   }
   //**** Read table records from input file until end-of-file.
   //读文件操作
   public void read() {
      createInfile();
      try {
         while (!infile.eof() ) {
            //String code = infile.getString().toUpperCase();
            //定义Emp对象,用它去调用自己的读取函数,这才体现接口的作用嘛!
            Emp t = null;
            //if(code.equals("T")){
            t = new Emp();
		//}
		//将读取的文件数据一行一行的指针存入一个Vector类型的数组变量
            allEmps.addElement( t );
            t.read( infile );
                         //*** polymorphic
         }
      }
      //异常处理代码
      catch( IOException e ) {
         System.err.println( "Can't read  " + inName );
         System.err.println( e );
      }
      try {
        infile.close();
      }
      catch( IOException e ) {
         System.err.println( "Can't close " +  inName );
         System.err.println( e );
      }
   }
   //写文件,就是将修改后的数据存储
   public void write() {
      createOutfile();
      Enumeration e = allEmps.elements();
      while ( e.hasMoreElements() ) {
        Emp t = (Emp) e.nextElement();
        t.write( outfile );
        outfile.putln();
      }
      try {
        outfile.close();
      } catch( IOException ex ) { System.err.println( ex ); }
   }
   public void writeRecords( Vector v ) {
      createOutfile();
      Enumeration rows = v.elements();
      while ( rows.hasMoreElements() )
         outfile.putln( (String) rows.nextElement() );
      try {
         outfile.close();
      } catch( IOException ex ) { System.err.println( ex ); }
   }
   //**** write records to standard error
   public void dump() {
      Enumeration e = allEmps.elements();
      while ( e.hasMoreElements() )
         System.err.println( e.nextElement() );
   }
   //**** set/get for Vector of Employees
   public Vector getEmps() { return allEmps; }
   public void setEmps( Vector v ) { allEmps = v; }
   //**** private methods to prompt for file names and
   //     create the files.
   //没有文件时产生新文件
   private String promptForFile( String which ) {
      String fname = null;
      try {
         BasicInput stdin = new BasicInput();
         System.out.println( "Please enter the " + which + " file's name: " );
         fname = stdin.getString();
      }
      catch( IOException e ) { System.err.println( e ); }
      finally { return fname; }
   }
   //产生输入文件
   private void createInfile() {
       try {
         infile = new BasicInput( inName );
      }
      catch( IOException e ) {
         System.err.println( "Can't open " + inName + "Please try again." );
      }
   }
   //创建输出文件
   private void createOutfile() {
      try {
         outfile = new BasicOutput( outName );
      }
      catch( IOException e ) {
         System.err.println( "Can't open " + outName + "Please try again." );
      }
   }
   //声明领域
   private String inName;
   private String outName;
   private BasicInput infile;
   private BasicOutput outfile;
   private Vector allEmps;
}
//**************************** End PrintAndModify.java

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -