Lab: Allow List Checking

For this lab you will be making a program to simulates the search for fraudulent transactions. Your program will be given two command line arguments. One for the allow list and one for the transactions you are to check. The allow file will have valid 10 digit account numbers, one per line (note that the file may not be sorted). For example:

3333333333
1111111111
2222222222
0000000000
Each line of the transactions file will have an account number followed by a positive integer amount in cents (note that the faile may not be sorted in any way). For example:

1234567890 10000
1111111111 23456
2222222222 54321
1212121212 20000
Some of the transactions will be valid (i.e. from an account number on the allow list) and some will be invalid (from an account not on the allow list).

You are to build a program that efficiently calculates the value of all of the valid transactions, the value of the invalid transactions and prints the difference as dollars and cents. For example, the output for the above files should be:

$477.77
Use only Scanner, ArrayList, arrays, and other basic Java functionality to solve this problem. In particular, you are to build your own solution using your own binary search method. In other words, do not use TreeSet, HashSet, Java’s built-in binary search, or other collections. As we have not discussed sorting in this class yet, you may use Java’s built-in sorting functions. Also, the values of transactions will be small enough that overflow of an int will not be an issue.

Because of the time needed to run the tests, you will have to wait at least 5 minutes between submissions for grading. You can still run your own tests as fast as you want.

import java.io.BufferedReader;
import java.io.FileReader;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;

public class Main
{
    public static void main(String[] args) throws Exception {
        final String allowFilename = args[0];
        final String transactionsFilename = args[1];
        ArrayList<BigInteger> a1 = new ArrayList<>();
        ArrayList<BigInteger> a2 = new ArrayList<>();
        ArrayList<Integer> a3 = new ArrayList<>();
        ArrayList<String> var1 = new ArrayList<>();
        String s = "";
        String s2 = "";
        String var2 ="";
        double var4 = 0;

        BufferedReader b1 = new BufferedReader(new FileReader(allowFilename));
        BufferedReader b2 = new BufferedReader(new FileReader(transactionsFilename));
        while ((s = (b1.readLine())) != null) {
            String[] s1 = s.split(" ");
            for (int n = 0; n < s1.length; n++) {
                a1.add(new BigInteger(s1[n]));
            }
        }
        Collections.sort(a1);
        while ((s2 = (b2.readLine())) != null) {
            var1.add(s2);
            Collections.sort(var1);
        }
        for(int n =0;n<var1.size();n++){
                String var3 = var1.get(n);
                var2 +=var3;
                var2 +=" ";
        }
        String[] s3 = var2.split(" ");
        for (int n = 0; n < s3.length; n++) {
            if(n%2==0){
                a2.add(new BigInteger(s3[n]));
            }
            else
                a3.add(new BigInteger(s3[n]).intValue());
        }


        for(int var5 = 0; var5<a2.size();var5++){
            BigInteger var3 = a2.get(var5);
            int n1 = Search(var3,a1,0,a1.size()-1);
            if(n1 == -1){
                var4 -= a3.get(var5);
            }
            else
            {
                var4 += a3.get(var5);
            }
        }
        double d = var4*0.01;
        DecimalFormat P = new DecimalFormat(".00");

            System.out.println("$"+P.format(d));
    }

    public static int Search(BigInteger key,ArrayList<BigInteger> a4,int low, int high){
        if(key.compareTo(a4.get(low))<0||key.compareTo(a4.get(high))>0||low>high){
            return -1;
        }
        int middle =(low+high)/2;
        if(key.compareTo(a4.get(middle))<0){
            return Search(key,a4,low,middle-1);
        }else if (key.compareTo(a4.get(middle))>0){

            return Search(key,a4,middle+1,high);
        }
        else{
            return middle;

        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值