Skip to content

Instantly share code, notes, and snippets.

@pirogoeth
Created June 26, 2012 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirogoeth/2997112 to your computer and use it in GitHub Desktop.
Save pirogoeth/2997112 to your computer and use it in GitHub Desktop.
Second Lesson assignment for an intermediate Java course.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class SalesWeek {
public static Map<Day, ArrayList<Sale>> total_sales_day = new HashMap<Day, ArrayList<Sale>>(7);
public SalesWeek() { };
public enum Day {
MONDAY("M"),
TUESDAY("Tu"),
WEDNESDAY("W"),
THURSDAY("Th"),
FRIDAY("F"),
SATURDAY("Sa"),
SUNDAY("Su");
public final String abbr;
private static final Map<String, Day> abbr_store = new HashMap<String, Day>(7);
Day(final String abbr) {
this.abbr = abbr;
}
public String getAbbr() {
return this.abbr;
}
public List<Sale> getSales() {
return SalesWeek.total_sales_day.get(this);
}
static {
for (Day day : Day.values()) {
abbr_store.put(day.getAbbr(), day);
}
}
public static Day getDayByAbbr(String abbr) {
return (abbr_store.containsKey(abbr) ? abbr_store.get(abbr) : null);
}
}
public class Sale {
private Day day_sold;
private double price;
public Sale(Day day, double price) {
this.day_sold = day;
this.price = price;
}
public double getPrice() {
return this.price;
}
public Day getDaySold() {
return this.day_sold;
}
}
public void processSale(String info) {
String[] split_data = info.split("\\,");
try {
Sale sale = new Sale(Day.getDayByAbbr(split_data[0]), Double.valueOf(split_data[1]));
processSale(sale);
} catch (java.lang.Exception e) {
System.out.println("Invalid sales data!");
e.printStackTrace();
}
}
public void processSale(final Sale s) {
List<Sale> sales_day = (s.getDaySold().getSales() == null ? new ArrayList<Sale>() : s.getDaySold().getSales());
int idx = (sales_day.size() > 0 ? sales_day.size() - 1 : 0);
sales_day.add(s);
total_sales_day.put(s.getDaySold(), (ArrayList<Sale>) sales_day);
System.out.println("Processed sale for [" + s.getDaySold().getAbbr() + "] for $" + s.getPrice() + ".");
}
public static List<String> getWeekStats() {
List<String> stats = new ArrayList<String>();
int d = 0;
double week_income = 0, week_sales = 0, week_average = 0;
for (Day day : Day.values()) {
List<Sale> total_sales = total_sales_day.get(day);
double total_income = 0;
int total_sale_count = 0;
double average_sale_price = 0;
try {
total_sale_count = total_sales.size();
for (Sale s : total_sales) {
total_income += s.getPrice();
}
average_sale_price = (total_income / (double) total_sale_count);
} catch (java.lang.NullPointerException e) {
// this means that there were no sales on that day. so do nothing.
}
week_income += total_income;
week_sales += total_sale_count;
stats.add(d, String.format("Sales on [%s]: %s\nIncome on [%s]: $%f\nAverage of all sales on [%s]: %f",
day.toString(), total_sale_count, day.toString(), total_income, day.toString(), average_sale_price));
d++;
}
week_average = (week_income / (double) week_sales);
stats.add(d++, String.format("Week Sales: %s\nWeek Income: $%f\nWeek Sale Average: %f", week_sales, week_income, week_average));
return stats;
}
public static void main(String[] args) {
String in = "";
SalesWeek sw = new SalesWeek();
Scanner input = new Scanner(System.in);
do {
System.out.print("Please enter sales info (format: DayAbbr,Price): ");
in = input.nextLine();
if (!(in.equals("*"))) {
sw.processSale(in);
}
} while (!(in.equals("*")));
System.out.println("===================================\n"); // data separator..
try {
for (String sale_data : SalesWeek.getWeekStats()) {
System.out.println(sale_data);
System.out.println("-----");
}
} catch (java.lang.Exception e) {
System.out.println("Caught an exception while getting total sales data!");
e.printStackTrace();
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment