in JAVA JAVA PLEASE JAVA********************* PLEASE READ ******************** p
ID: 3801414 • Letter: I
Question
in JAVA JAVA PLEASE JAVA********************* PLEASE READ ******************** please follow instructions and do as asked in JAVA JAVA JAVA JAVA
I have this program that my professor assigned to us. He has not taught us how to save files and how to do arrays as complicated as this. I need help finding how to do the code required for this. If only you can help me find the correct code to use and how to set the attributes. I need like a general code that can work as i will be imputing the movies as elements. Thank you, hope you can help me in this time of need
Assignment Description:
For this assignment, you will write a program that keeps track of your expenses, taking the information from a text file and displaying it back to you. A skeleton of the program is provided to you, which will also contain code relevant to taking data from your text data.
This purpose of this assignment is for you to understand both methods and loops fully; you will also learn how to access data from files and how to manipulate this data in various ways to display the required output. At the end, you will be given a .class file which you can run from the terminal to see the expected output and compare to your own program. We will explain how you can run the .class file this later.
Please read through all of the instructions first, before starting.
When starting the program, the user is given the following choices:
Make a log
View all logs for my previous expenses
View all logs for deposits
View all logs for a month
View Customized logs for a month
View all logs for a day
View Customized logs for a day
View all logs for a previous expenses
View all logs for a previous deposits
View all logs till date
Quit
Each option in this menu will have its own method. If the method is not already provided in the skeleton code, you will need to make it yourself. After the user chooses one option, other than to Quit, the menu will be displayed and the user will get to pick another option. This should continue looping until the user chooses to quit.
Each menu option is described below:
Option A:
For this method, the user will be able to create a log. First, ask the user if he or she would like to log (A) a deposit, or (B) an expense. Based on that choice, they will next be asked to enter a category for that expense/deposit. For example, if one chose option ‘B’, they could then type in ‘food’ as the expense category.
Next, ask for the amount for that expense or deposit. If it was an expense, this amount will be subtracted from the current balance. If it was a deposit, it will be added.
Once the user enters everything, this information must be added to the text file. The program should also automatically determine the current date, as well as the current balance, and save all of this to the text file.
Note that to get and update the current balance, you will need to obtain the last item in the text file. This will probably be taken in as a String, so you must also parse it to convert to an integer. For example:
int number = Integer.ParseInt(someString);
Again, once we have all five variables (date, type, category, amount, and balance), they will be added into the text file in that order and separated by spaces. Finally, display back to the user that a log has been successfully added.
Option B:
The program will read the given text file and print every log where the user has recorded an expense. Make sure to print all the information:
Date
Type (Expense or Deposit)
Category (such as Food, etc…)
Amount
Balance
Option C:
The program will read the given text file and print the logs where the user has recorded a deposit. Make sure to display all the same information as described above in Option B.
Option D:
The program will first ask the user to enter the month he or she would like to look up. Next, read in the given text file and print all the logs where the month recorded is the same as that of the user’s input.
Option E:
This option is like Option D, but more specific. Again, ask the user to enter the month they would like to look up. Next, also ask them to select either “Expenses” or “Deposits.” Finally, ask the user for the deposit or expense category (food, salary, etc.). Once you have all this information, read the given text file and print all the logs that contain only the information that the user has set.
Option F:
Ask the user to enter a specific month and day he or she would like to look up. Next, read in the given text file and print all the logs where that month and day correspond to that which the user inputted.
Option G:
Like option F, ask the user to enter a specific month and day. Then, ask them to also enter whether it was an expense or deposit, as well as the category. Once you have all this information, read the given text file and print all the logs that contain only the information that the user has set.
Option H:
The program will ask the user what type of expense they would like to look up. Provide a menu listing the possible types of expenses they may choose from (such as food, entertainment, etc.). Once the user makes a choice, read the text file and print all the logs based on the inputted type.
Option I:
The program will ask the user what type of deposit they would like to look up. Provide a menu listing the possible types of deposits they may choose from (such as food, entertainment, etc.). Once the user makes a choice, read the text file and print all the logs based on the inputted type.
Option J:
This option will be to print all the logs from the text file.
Based on all this information, complete the code based on the skeleton provided.
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;
public class LogManager {
private List<UserLog> userLogData = new ArrayList<UserLog>();
public static void main(String args[]) throws IOException {
new LogManager("/home/zubair/Desktop/logs.txt");
}
public LogManager(String filePath) throws IOException {
userLogData = loadFromFile(filePath);
start();
writeBackToFile(filePath, userLogData);
}
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
displayMenu();
String choice = scanner.nextLine().toUpperCase();
switch(choice) {
case "A" :
makeLog(scanner);
break;
case "B" :
viewAllLogForExpenses();
break;
case "C" :
viewAllLogForDeposits();
break;
case "D" :
viewAllLogForAMonth(scanner);
break;
case "E" :
viewCustomizedForAMonth(scanner);
break;
case "F" :
viewAllLogsForADay(scanner);
break;
case "G" :
viewCustomizedLogForADay(scanner);
break;
case "H" :
viewLogForAExpenses(scanner);
break;
case "I" :
viewLogForADeposit(scanner);
break;
case "J" :
viewAllLogsTillDate(scanner);
break;
case "K" :
scanner.close();
return;
default :
System.out.println(System.lineSeparator());
System.out.println("Invalid Choice");
System.out.println(System.lineSeparator());
}
}
}
public List<UserLog> loadFromFile(String filePath) throws IOException {
List<UserLog> resultList = new ArrayList<UserLog>();
BufferedReader bufferedInput = new BufferedReader(new FileReader(filePath));
String inputLine;
while ((inputLine = bufferedInput.readLine()) != null) {
String[] logData = inputLine.split(" ");
int date = Integer.valueOf(logData[0]);
String type = logData[1].toUpperCase();
String category = logData[2].toUpperCase();
double amount = Double.valueOf(logData[3]);
double balance = Double.valueOf(logData[4]);
UserLog userLog = new UserLog(date, type, category, amount, balance);
resultList.add(userLog);
}
bufferedInput.close();
return resultList;
}
public void writeBackToFile(String filePath, List<UserLog> userLogs) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
for (UserLog log : userLogs) {
bufferedWriter.write(log.toOutputString());
}
bufferedWriter.close();
}
public void displayMenu() {
System.out.println(System.lineSeparator());
System.out.println(System.lineSeparator());
System.out.println("Menu");
System.out.println("A. Make a log");
System.out.println("B. View all logs for expenses");
System.out.println("C. View all logs for deposits");
System.out.println("D. View all logs for a month");
System.out.println("E. View customized logs for a month");
System.out.println("F. View all logs for a day");
System.out.println("G. View customized logs for a day");
System.out.println("H. View all logs for a previous expenses");
System.out.println("I. View all logs for a previous deposit");
System.out.println("J. View all logs till date");
System.out.println("K. Quit");
System.out.print("Enter your choice : " );
}
public void makeLog(Scanner sc) {
System.out.println("Enter the log data [TYPE CATEGORY AMOUNT] : ");
/*
* Get the User Data
*/
String type = sc.next().toUpperCase();
String category = sc.next().toUpperCase();
double amount = Double.valueOf(sc.next());
int date = getModelDateFromTs(System.currentTimeMillis());
/*
* Find out the current balance form the last record
*/
double balance = 0.0;
int lastIndex = userLogData.size()-1;
if (lastIndex >= 0) {
UserLog lastLog = userLogData.get(lastIndex);
balance = lastLog.balance;
}
/*
* If it is a deposit account, add the amount to the balance, otherwise substract
*/
if (category.equals("DEPOSIT")) {
balance += amount;
}
else {
balance -= amount;
}
UserLog userLog = new UserLog(date, type, category, amount, balance);
userLog.toOutputString();
userLogData.add(userLog);
}
public void viewAllLogForExpenses() {
for (UserLog log : userLogData) {
if (log.type.equalsIgnoreCase("EXPENSES")) {
System.out.print(log.toOutputString());
}
}
}
public void viewAllLogForDeposits() {
for (UserLog log : userLogData) {
if (log.type.equalsIgnoreCase("DEPOSIT")) {
System.out.print(log.toOutputString());
}
}
}
public void viewAllLogForAMonth(Scanner scanner) {
System.out.println("Enter the month (1-12) : ");
int month = Integer.valueOf(scanner.nextLine());
for (UserLog log : userLogData) {
/* Date is in YYYYMMdd format
*/
if ((log.date%10000)/100 == month) {
System.out.print(log.toOutputString());
}
}
}
public void viewAllLogsForADay(Scanner scanner) {
System.out.println("Enter the date (YYMMdd) : ");
int date = Integer.valueOf(scanner.nextLine());
for (UserLog log : userLogData) {
/* Date is in YYYYMMdd format
*/
if (log.date == date) {
System.out.print(log.toOutputString());
}
}
}
public void viewAllLogsTillDate(Scanner scanner) {
System.out.println("Enter the date (YYMMdd) : ");
int date = Integer.valueOf(scanner.nextLine());
for (UserLog log : userLogData) {
/* Date is in YYYYMMdd format
*/
if (log.date <= date) {
System.out.print(log.toOutputString());
}
}
}
public void viewCustomizedForAMonth(Scanner scanner) {
System.out.println("Enter the month (1-12) : ");
int month = Integer.valueOf(scanner.nextLine());
System.out.println("Enter the type : ");
String type = scanner.nextLine();
System.out.println("Enter the category : ");
String category = scanner.nextLine();
for (UserLog log : userLogData) {
/* Date is in YYYYMMdd format
*/
if (( log.date%10000)/100 == month
&& log.type.equalsIgnoreCase(type)
&& log.category.equalsIgnoreCase(category)) {
System.out.print(log.toOutputString());
}
}
}
public void viewCustomizedLogForADay(Scanner scanner) {
System.out.println("Enter the date (YYMMdd) : ");
int month = Integer.valueOf(scanner.nextLine());
System.out.println("Enter the type : ");
String type = scanner.nextLine();
System.out.println("Enter the category : ");
String category = scanner.nextLine();
for (UserLog log : userLogData) {
/* Date is in YYYYMMdd format
*/
if ( log.date == month
&& log.type.equalsIgnoreCase(type)
&& log.category.equalsIgnoreCase(category)) {
System.out.print(log.toOutputString());
}
}
}
public void viewLogForAExpenses(Scanner scanner) {
System.out.println("Enter the category : ");
String category = scanner.nextLine();
for (UserLog log : userLogData) {
/* Date is in YYYYMMdd format
*/
if ( log.type.equalsIgnoreCase("EXPENSES")
&& log.category.equalsIgnoreCase(category)) {
System.out.print(log.toOutputString());
}
}
}
public void viewLogForADeposit(Scanner scanner) {
System.out.println("Enter the category : ");
String category = scanner.nextLine();
for (UserLog log : userLogData) {
/* Date is in YYYYMMdd format
*/
if ( log.type.equalsIgnoreCase("DEPOSIT")
&& log.category.equalsIgnoreCase(category)) {
System.out.print(log.toOutputString());
}
}
}
/**
* Converts the timestamp in millis, to a integer date of form YYYYMMDD
*/
public static Integer getModelDateFromTs(long referenceTs) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(referenceTs);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String label = dateFormat.format(cal.getTimeInMillis());
return Integer.parseInt(label);
}
}
class UserLog {
public int date;
public String type;
public String category;
public double amount;
public double balance;
public UserLog(int date, String type, String category, double amount, double balance) {
super();
this.date = date;
this.type = type;
this.category = category;
this.amount = amount;
this.balance = balance;
}
@Override
public String toString() {
return "date=" + date + ", type=" + type + ", category=" + category + ", amount=" + amount + ", balance=" + balance + "]";
}
public String toOutputString() {
return date + " " + type + " " + category + " " + amount + " " + balance + " ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.