All pastes #766709 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-11-09 02:14 UTC
rendered paste body
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

public class TicketCounter {
	public static void main( String[] args ){
		try {
			
			String line;	
			File fileName = new File("tickets.txt");
			boolean onPerson = false;
			boolean ticketTime = false;
			boolean paid = false;
			double amount = 0;
			ArrayList<Person> personList = new ArrayList<Person>();		
			DecimalFormat money = new DecimalFormat("0.00");
			
	        BufferedReader fileIn = new BufferedReader(new FileReader(fileName));
	        
	        Person currPer = new Person();
	        while (( line = fileIn.readLine()) != null){
	        	line = line.trim();
	        	if( !line.trim().equals("")) {
	        		if( line.equals("<person>") )
	        			onPerson = true;
	        		else if( line.equals("</person>") ) {
	        			onPerson = false;
	        			personList.add(currPer);
	        			currPer = new Person();
	        		}else if(onPerson) {
	        			if(line.contains("<last>")) {
	        				String last = line.replaceAll("<last>", "");
        					last = last.replaceAll("</last>", "").trim();
        					currPer.setLast(last);
	        			}
	        			if(line.contains("<first>")) {
	        				String first = line.replaceAll("<first>", "");
        					first = first.replaceAll("</first>", "").trim();
        					currPer.setFirst(first);
	        			}
	        			if(line.contains("<number>")) {
	        				String number = line.replaceAll("<number>", "");
        					number = number.replaceAll("</number>", "").trim();
        					currPer.setNumber(number);
	        			}
	        			if(line.equals("<ticket>"))
	        				ticketTime = true;
	        			if(line.equals("</ticket>")) {
	        				ticketTime = false;
	        				currPer.addTicket(amount, paid);
	        			}
	        			if(ticketTime) {
	        				if(line.contains("<paid>")) {
	        					String paidSymbol = line.replaceAll("<paid>", "");
	        					paidSymbol = paidSymbol.replaceAll("</paid>", "").trim();
	        					paid = paidSymbol.equals("Y");
	        				}
	        				if(line.contains("<amount>")) {
	        					String amt = line.replaceAll("<amount>", "");
	        					amt = amt.replaceAll("</amount>", "").trim();
	        					amount = Double.parseDouble(amt);
	        				}
	        			}
	        		}
	        	}
	        }
	        double totalPaid = 0;
	        double total = 0;
	        for( int i = 0; i < personList.size(); i++ ) {
	        	System.out.println( personList.get(i).toString());
	        	totalPaid = totalPaid + personList.get(i).getPaid();
	        	total = total + personList.get(i).getTotal();
	        }
	        System.out.println("Total paid: " + money.format(totalPaid) + ", Total unpaid: " + money.format((total-totalPaid)));
		}
		catch(Exception e) {
			System.err.println("Something went wrong.");
		}
	}
}