Skip to content

Instantly share code, notes, and snippets.

@samaddico
Forked from ah052070/BankReader.java
Created October 22, 2019 11:14
Show Gist options
  • Save samaddico/49c074d37e1b36f62c9c49510cb1581c to your computer and use it in GitHub Desktop.
Save samaddico/49c074d37e1b36f62c9c49510cb1581c to your computer and use it in GitHub Desktop.
Java program involving transactions
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Class which reads clients transactions from internet database file and returns for its consumers
*/
/**
* Assignment: Minty Money
* Username: amh11u
* Author: Alex Hunting
* Creation Date: 10/6/11
* Completion time: 10 hrs.
* Honor Code: I pledge that this represents my own program code.
* I worked with Lee in designing and debugging my program.
*/
public class BankReader {
// list to hold the retrieved transactions
private List transactions = new ArrayList();
private Iterator transactionIterator = null;
public void loadData(String url) {
try {
// create new URL object
URL webpageUrl = new url.(url);
// create reader to get stream
BufferedReader in = new BufferedReader(new InputStreamReader(webpageUrl.openStream()));
String inputLine = null;
// retrieve through the file content and store it in the list
while ((inputLine = in.readLine()) != null) {
String strSplit[] = inputLine.split(",");
transactions.add(strSplit);
}
// close the stream
in.close();
} catch(Exception e) {
System.out.println("Exception e"+ e.getMessage());
}
// iterator to hold the retrieved list values
transactionIterator = transactions.iterator();
}
public int getNumberTransactions() {
return transactions.size();
}
public String[] getNext() {
return (String[]) transactionIterator.next();
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* The class which serves for user's needs
*/
/**
* Assignment: Minty Money
* Username: amh11u
* Author: Alex Hunting
* Creation date: 10/6/11
* Completion time: 10 hrs.
* Honor Code: I pledge that this program represents my own program code.
* I worked with Lee in designing and debugging my program.
*/
public class MintyDriver {
// main method to run
public static void main(String args[]) {
// create BankReader object to access the financial data
BankReader br = new BankReader();
br.loadData("http://pic.fsu.edu/courses/2011/summer/cop2258/assignments/mintymoney/bank.csv");
// get the number of transactions the user has
int numberOfTransactions = br.getNumberTransactions();
// print the number of transactions
System.out.println("The number of transactions: " + numberOfTransactions);
// create ArrayList to hold the transactions
List transactionList = new ArrayList();
//create new Transaction object and store into transactionList
for(int i = 0; i < numberOfTransactions; i++) {
// get the transactions from BankReader and assign it to variables
String s[] = br.getNext();
String date = s[0];
String vendor = s[1];
double price = Double.parseDouble(s[2]);
//create new Transaction object for each getNext method
Transaction transaction = new Transaction();
transaction.setDate(date);
transaction.setVendor(vendor);
transaction.setPrice(price);
//add the created transactions into ArrayList
transactionList.add(transaction);
}
double totalAmount = 0;
//Sum the final total transaction amount
for(Iterator iterator = transactionList.iterator(); iterator.hasNext();) {
Transaction transaction = (Transaction) iterator.next();
totalAmount += transaction.getPrice();
}
//print the total transactions amount
System.out.println("Total transaction amount: " + totalAmount);
}
}
/**
* The class used as DTO object for holding the user's transactions
*/
/**
* Assignment: Minty money
* Username: amh11u
* Autor: Alex Hunting
* Creation Date: 10/6/ 11
* Completion time: 10 hrs.
* Honor code: I worked with Lee and in designing and debugging this assignment.
*
*
*/
public class Transaction {
//private instance variables:
private String date;
private String vendor;
private double price;
// public bean methods
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(String date) {
this.date = date;
}
/**
* @return the vendor
*/
public String getVendor() {
return vendor;
}
/**
* @param vendor the vendor to set
*/
public void setVendor(String vendor) {
this.vendor = vendor;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment