Skip to content

Instantly share code, notes, and snippets.

@iamricard
Created May 27, 2013 09:53
Show Gist options
  • Save iamricard/5656218 to your computer and use it in GitHub Desktop.
Save iamricard/5656218 to your computer and use it in GitHub Desktop.
examen.java
import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
//author: lion
public class main {
private static String paths[] = {"coches.txt", "Ventas.txt", "vendedores.txt"}; // default files are these, assumed to be in the same directory
//new String[3];
private static String cars[][]; // bidimensional array to hold the cars data
private static String sales[][]; // bidimensional array to hold the sales data
private static String salesmen[][]; // bidimensional array to hold the salesmen data
private static String total_incomes[][] = {{"January", "0"}, // Array of Arrays in the form of {Month, Income}
{"February", "0"},
{"March", "0"},
{"April", "0"},
{"May", "0"},
{"June", "0"},
{"July", "0"},
{"August", "0"},
{"September", "0"},
{"Octover", "0"},
{"November", "0"},
{"December", "0"}};
private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
public static void getpaths() throws IOException { // allows the user to change the paths and names of the data files
for (byte i = 0; i < 3; i++) {
switch (i) {
case 0:
System.out.println("cars file path:"); // everything else is pretty obvious
System.out.print("\t");
paths[i] = stdin.readLine();
break;
case 1:
System.out.println("sales file path:");
System.out.print("\t");
paths[i] = stdin.readLine();
break;
case 2:
System.out.println("salesmans file path:");
System.out.print("\t");
paths[i] = stdin.readLine();
break;
}
}
}
public static void parsecars() throws IOException { // this parses the cars data file into IDs, Make -cars_car-, Models and Prices ArrayList
ArrayList<String> cars_id = new ArrayList<String>();
ArrayList<String> cars_car = new ArrayList<String>();
ArrayList<String> cars_model = new ArrayList<String>();
ArrayList<String> cars_price = new ArrayList<String>();
String line,
darray[] = null; // Array for each line of BufferedReader
File data = new File (paths[0]); // opens the file for cars -in the path paths[0]-
FileReader fr = new FileReader(data); // reads
BufferedReader br = new BufferedReader(fr); // lines
while ((line = br.readLine()) != null) { // appends to each ArrayList
darray = line.split(";");
cars_id.add(darray[0]);
cars_car.add(darray[1]);
cars_model.add(darray[2]);
cars_price.add(darray[3]);
}
cars = new String[cars_id.size()][4];
for (byte i = 0; i < cars_id.size(); i++) {
cars[i][0] = cars_id.get(i);
cars[i][1] = cars_car.get(i);
cars[i][2] = cars_model.get(i);
cars[i][3] = cars_price.get(i);
}
}
public static void parsesales() throws IOException { // parses data from the sales data file into IDs, Salesmen IDs, Cars IDs, amount of cars sold, date of the cars sold.
ArrayList<String> sales_id = new ArrayList<String>();
ArrayList<String> sales_salesman = new ArrayList<String>();
ArrayList<String> sales_cars = new ArrayList<String>();
ArrayList<String> sales_cars_amount = new ArrayList<String>();
ArrayList<String> sales_date = new ArrayList<String>();
String line,
darray[] = null;
File data = new File (paths[1]);
FileReader fr = new FileReader(data);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
darray = line.split(";");
sales_id.add(darray[0]);
sales_salesman.add(darray[1]);
sales_cars.add(darray[2]);
sales_cars_amount.add(darray[3]);
sales_date.add(darray[4]);
}
sales = new String[sales_id.size()][5];
for (byte i = 0; i < sales_id.size(); i++) {
sales[i][0] = sales_id.get(i);
sales[i][1] = sales_salesman.get(i);
sales[i][2] = sales_cars.get(i);
sales[i][3] = sales_cars_amount.get(i);
sales[i][4] = sales_date.get(i);
}
}
public static void parsesalesmans() throws IOException { //parses data from the salesmen data file into IDs and names
ArrayList<String> salesmans_id = new ArrayList<String>();
ArrayList<String> salesmans_name = new ArrayList<String>();
String line,
darray[] = null;
File data = new File (paths[2]);
FileReader fr = new FileReader(data);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
darray = line.split(";");
salesmans_id.add(darray[0]);
salesmans_name.add(darray[1]);
}
salesmen = new String[salesmans_id.size()][2];
for (byte i = 0; i < salesmans_id.size(); i++) {
salesmen[i][0] = salesmans_id.get(i);
salesmen[i][1] = salesmans_name.get(i);
}
}
public static void parseincomes() throws IOException { // fills in the total_incomes array
int car,
car_amount,
car_price = 0,
current_income = 0,
sale_total,
current_month;
String current_date[] = null;
for (byte i = 0; i < sales.length; i++) {
current_date = sales[i][4].split("/");
current_month = Integer.parseInt(current_date[1]);
car = Integer.parseInt(sales[i][2]);
car_amount = Integer.parseInt(sales[i][3]);
for (byte j = 0; j < cars.length; j++) {
if (car == Integer.parseInt(cars[j][0])) {
car_price = Integer.parseInt(cars[j][3]);
}
}
sale_total = car_amount * car_price;
current_income = Integer.parseInt(total_incomes[current_month][1]);
current_income = current_income + sale_total;
total_incomes[current_month][1] = Integer.toString(sale_total);
}
}
public static void mostsold() throws IOException { // looks for the most sold model
int index = 0,
highest_amount = 0,
total_sold[] = new int[cars.length];
byte count = 0;
String most_sold_car;
for (byte i = 1; i <= cars.length; i++) {
for (byte j = 0; j < sales.length; j++) {
if (i == Integer.parseInt(sales[j][2])) {
total_sold[count] = total_sold[count] + Integer.parseInt(sales[j][3]);
}
}
count++;
}
for (int i = 0; i < total_sold.length; i++) {
if (highest_amount < total_sold[i]) {
highest_amount = total_sold[i];
index = i;
}
}
most_sold_car = cars[index][1] + " " + cars[index][2];
System.out.println("Most sold car:\n"+most_sold_car+". With "+highest_amount+" units sold.");
}
public static void bestsalesman() { // looks for the best salesman
int index = 0,
highest_amount = 0,
total_sales[] = new int[salesmen.length];
byte count = 0,
car_count = 0;
for (byte i = 1; i <= salesmen.length; i++) {
for (byte j = 0; j < sales.length; j++) {
if (i == Integer.parseInt(sales[j][1])) {
for (byte k = 0; k < cars.length; k++) {
if (Integer.parseInt(sales[j][2]) == Integer.parseInt(cars[k][0])) { // if sales[j] ID equals cars[k] ID
total_sales[count] = total_sales[count] + ((Integer.parseInt(sales[j][3]))*(Integer.parseInt(cars[k][3]))); // we add the value of carks[k]price * sales[j]amount to current total sold
}
car_count++;
}
total_sales[count] = total_sales[count] + Integer.parseInt(sales[j][3]);
car_count = 0;
}
}
count++;
}
for (int i = 0; i < total_sales.length; i++) {
if (highest_amount < total_sales[i]) {
highest_amount = total_sales[i];
index = i;
}
}
System.out.println("Best Salesman:\n"+salesmen[index][1]+". With a total of "+highest_amount+"$ cashed in.");
}
public static void monthlyincome() { // looks for the month with the highest income
int index = 0,
highest_amount = 0;
for (byte i = 0; i < 12; i++) {
if (highest_amount < Integer.parseInt(total_incomes[i][1])) { // if current highest_amount is lower than the income found in total_incomes[i][1],
index = i-1; // we store i in the index (minus 1) to know which is the current max
highest_amount = Integer.parseInt(total_incomes[i][1]); // and highest amount is replaced
}
}
System.out.println("Highest grossing month is:\n"+total_incomes[index][0]+". With a total amount of "+total_incomes[index][1]+"$.");
}
public static void start() throws IOException { // main loop
String input;
byte choice;
System.out.println("0-Most sold model.\n1-Best Salesman\n2-Best Month Ever\n3-Income Per Month\n4-Change path for files\n5-Reparse\nAny other number will quit");
System.out.println();
System.out.print(">");
input = stdin.readLine();
choice = Byte.parseByte(input);
System.out.println();
switch (choice) {
case 0:
mostsold();
break;
case 1:
bestsalesman();
break;
case 2:
monthlyincome();
break;
case 3:
System.out.println(Arrays.deepToString(total_incomes));
break;
case 4:
getpaths();
break;
case 5:
parsecars();
parsesales();
parsesalesmans();
parseincomes();
break;
default:
System.exit(0);
}
}
public static void main(String[] args) throws IOException {
parsecars();
parsesales();
parsesalesmans();
parseincomes();
System.out.println(Arrays.deepToString(cars));
System.out.println(Arrays.deepToString(salesmen));
System.out.println(Arrays.deepToString(sales));
while(true) {
start();
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment