The code illustrated on this page was developed from an Assembler Workshop exercise as a bit of fun. (The same problem has now been solved in Assembler, Java, and Rexx.)

The exercise requirement was to read a number of records aggregate amounts to produce a total, then calculate the Value-Added Tax (VAT), subtracting it to give the Net Amount.

import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;

class wshop3 {
      public static void main(String arguments[]) {
      try {       
          FileInputStream ina  = new FileInputStream("wshop1I");
          try {
              DataInputStream in = new DataInputStream(ina); 
         
              String amt = " ";
              String tempname = " ";
              String tempname2 = " ";
              int i = 0;
              int z = 1;
              double gross = 0.00;
              double vat   = 0.00;
              double net   = 0.00;
              int work1  = 0;
              double work2 = 0.00;
              NumberFormat nf = NumberFormat.getNumberInstance();
              nf.setMaximumFractionDigits(2);
              try {
                  for (int n = 0; n < 9; n++) {
                      tempname = (String)in.readLine();
                      tempname2 = tempname.substring(0,4); 
                      z = tempname2.compareTo("****");
                      if (z == 0)
                         break;
                      amt = tempname.substring(9,11);
                      work1 = Integer.parseInt(amt);
                      work2 = (double)work1;
                      gross = gross + work2;
                      i = i + 1;
                      }
                  work2 = gross / 117.5;
                  vat = work2 * 100;
                  net = gross - vat;
                  String rgross = nf.format(gross);
                  String rvat = nf.format(vat);
                  String rnet = nf.format(net);
                    
                  System.out.println("Gross amount = $"+rgross+" VAT at 17.5% = $"+rvat+" net amount = $"+rnet);
                  }
              catch (EOFException e) {
                    System.out.println("Exception 3: " + e.getMessage());
                    }
              }
          catch (IOException e) {
                System.out.println("Exception 2: " + e.getMessage());
                }
          }
      catch (FileNotFoundException e) {
            System.out.println("Exception 1: " + e.getMessage());
            }
      }
}

Copyright © KMS-IT Limited 2003