Problem: Use the monthly_sales.txt that has monthly sale amount. Create a progra
ID: 3869199 • Letter: P
Question
Problem:
Use the monthly_sales.txt that has monthly sale amount. Create a program that will import, view, edit and sort the sales amounts for each month of the current year and rewrite the sorted data back to another file.
Specifications
• Use the monthly_sales.txt file that has monthly sales data, the first column holds threeletter abbreviations for each month and the second column holds the sale amount for the corresponding month.
• Your program should use the three-letter abbreviation for month as an input keyword to read the records and import monthly data from the ‘monthly_sales.txt’ file and perform the following operations.
1. View - Views sales for specified month
2. Edit - EditS sales for specified month
3. Totals - View total sale for the year
4. Sort – sorts the sales report in a descending or ascending order
5. Save - re-write the sorted data to ‘sortedd_sales.txt’ file
6. Exit - Exit the program
Monthly Sales:
The Example of command line should look like this:
Type: View
Enter three-letter Month: jan
Sales amount for Jan is 14,317.00.
Type: Edit
Enter three-letter Month: jan
Sales Amount: 15293
Sales amount for Jan is 15,293.00.
Type: Total
Yearly total: 67,855.00
Monthly average: 5,654.58
Type: View
Enter Three-letter Month: july
Invalid three-letter month. Handle exception
Command: exit Bye!
Explanation / Answer
Below is your code.. I have used following assumption: -
1. in your text file there are five spaces between month and sales value.
2. User knows the types of commands.User will enter valid commands
3. edit option will not change the content of file until and unless user enter save command.
Let me know you face any issue in comemnts!
MonthSales.java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MonthSales {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("monthly_sales.txt"));
HashMap<String, Integer> sales = new HashMap<>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] word = line.split(" ");
sales.put(word[0].toLowerCase(), Integer.parseInt(word[1]));
}
Scanner inp = new Scanner(System.in);
String option = "";
int sortChoice = 0;
while (!option.equalsIgnoreCase("Exit")) {
System.out.println();
System.out.print("Type: ");
option = inp.next();
int op = 0;
if (option.equalsIgnoreCase("view")) {
op = 1;
} else if (option.equalsIgnoreCase("edit")) {
op = 2;
} else if (option.equalsIgnoreCase("total")) {
op = 3;
} else if (option.equalsIgnoreCase("sort")) {
op = 4;
} else if (option.equalsIgnoreCase("save")) {
op = 5;
} else if (option.equalsIgnoreCase("Exit")) {
op = 6;
}
switch (op) {
case 1:
System.out.print("Enter three-letter Month: ");
String mon = inp.next();
if (mon.length() > 3) {
System.out.println("Invalid three-letter month. Handle exception");
} else {
System.out.println("Sales amount for " + mon + " is " + sales.get(mon.toLowerCase()).doubleValue());
}
break;
case 2:
System.out.print("Enter three-letter Month: ");
mon = inp.next();
if (mon.length() > 3) {
System.out.println("Invalid three-letter month. Handle exception");
} else {
System.out.print("Sales Amount: ");
Integer sale = Integer.parseInt(inp.next());
sales.put(mon.toLowerCase(), sale);
System.out.println("Sales amount for " + mon + " is " + sales.get(mon.toLowerCase()).doubleValue());
}
break;
case 3:
Iterator<?> it = sales.entrySet().iterator();
double sum = 0;
int count = 0;
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
sum = sum + (int) pair.getValue();
count++;
}
System.out.println("Yearly total: " + sum);
System.out.println("Monthly average: " + sum / count);
break;
case 4:
System.out.println("Select 1 for ascending and 0 for descending .. ");
int choice = Integer.parseInt(inp.next());
sortChoice = choice;
Set<Entry<String, Integer>> set = sales.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
if (choice == 0) {
return (o2.getValue()).compareTo(o1.getValue());
} else {
return (o1.getValue()).compareTo(o2.getValue());
}
}
});
for (Map.Entry<String, Integer> entry : list) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
break;
case 5:
File file = new File("monthly_sales.txt");
FileWriter writer = new FileWriter(file);
Set<Entry<String, Integer>> set1 = sales.entrySet();
List<Entry<String, Integer>> list1 = new ArrayList<Entry<String, Integer>>(set1);
int choice1 = sortChoice;
Collections.sort(list1, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
if (choice1 == 0) {
return (o2.getValue()).compareTo(o1.getValue());
} else {
return (o1.getValue()).compareTo(o2.getValue());
}
}
});
for (Map.Entry<String, Integer> entry : list1) {
writer.write(entry.getKey() + " " + entry.getValue()+" ");
}
writer.flush();
writer.close();
System.out.println("File Saved");
break;
case 6:
System.out.print(" Bye!");
break;
default:
System.out.println("Type a valid command.");
break;
}
}
}
}
Sample Run: -
Type: view
Enter three-letter Month: july
Invalid three-letter month. Handle exception
Type: jul
Type a valid command.
Type: view
Enter three-letter Month: jul
Sales amount for jul is 3232.0
Type: edit
Enter three-letter Month: jul
Sales Amount: 32
Sales amount for jul is 32.0
Type: total
Yearly total: 57149.0
Monthly average: 4762.416666666667
Type: sort
Select 1 for ascending and 0 for descending ..
0
aug 15578
jan 14317
oct 6735
jun 4324
feb 4176
apr 3463
dec 2497
sep 2437
may 2429
mar 1073
nov 88
jul 32
Type: save
File Saved
Type: exit
Bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.