Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java A phone company bills customers as follows: $20/month base fee per account

ID: 3550468 • Letter: J

Question

Java


A phone company bills customers as follows: $20/month base fee per account $0. 12 per minute for calls that started between 8:00 AM and 10:00 PM, inclusive $0. 05 per minute all other times A family will have separate accounts for each member. Your program must read phone data from a file and generates an invoice. Your program must read in a data file that includes all the calls for a month. The file must be called input_data. txt and you should read it from the directory where the program is stored (i. e. , do not use a full path for the filename!). The format for the data file is as follows: The times are given in 24:00 format (1:00 AM is 01:00, 1:00 PM is 13:00, etc). Note - the TA will use a different file for grading, so you should create some test files for yourself to make sure that things are working correctly. Your program should create an invoice text file called invoice. txt with the following format: The is only an example of formatting - you should of course print out the actual account numbers and totals that you calculate. The number of accounts and the length of the input file is not known in advance! Code accordingly. Your program must check to make sure the input file is in the correct format. If a line in the file is not correct, an error message should be displayed and the program should exit. There are many ways to address the problem, but you might consider using two arrays - one to hold the account numbers, and one to hold account totals. Given that you don't know how many accounts you will have, you might consider using ArrayList instead of simple arrays. ArrayList has methods for determining if an object is already in the arrays

Explanation / Answer

Link to Code - https://www.dropbox.com/sh/ytz8ae6pw0xlsmp/U08L9H-njE


Compiling - "javac Solution.java"


Running - "java Solution"


Code

Solution.java

import java.io.FileReader;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;



public class Solution {


/**

* @param args

*/

public static double basefee = 20.0;

public static double daycharge = 0.12;

public static double nightcharge = 0.05;

public static ArrayList<String> accnum;

public static ArrayList<Double> bill;

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

Scanner input = new Scanner(new FileReader("input_data.txt"));

PrintWriter writer = new PrintWriter("invoice.txt", "UTF-8");

accnum = new ArrayList<String>();

bill = new ArrayList<Double>();

String line = input.nextLine();

while(input.hasNextLine())

{

line = input.nextLine();

String toks[] = line.split("[ , ]+");

String time[] = toks[1].split(":");

int hr = Integer.parseInt(time[0]);

int min = Integer.parseInt(time[1]);

if(accnum.contains(toks[0]))

{

int index = accnum.indexOf(toks[0]);

Double temp = bill.get(index);

if((hr>=8) && (hr<=22))

{

temp += Double.parseDouble(toks[2])*daycharge;

}

else

{

temp += Double.parseDouble(toks[2])*nightcharge;

}

bill.set(index, temp);

}

else

{

Double temp = basefee;

if((hr>=8 && hr<22) || (hr==22 && min == 0))

{

temp += Double.parseDouble(toks[2])*daycharge;

}

else

{

temp += Double.parseDouble(toks[2])*nightcharge;

}

accnum.add(toks[0]);

bill.add(temp);

}

}

writer.println("Invoice");

writer.println("------------------------");

writer.println("Account Amount Due");

int len = accnum.size();

Double sum = 0.0;

for(int i = 0; i<len; i++)

{

writer.println(accnum.get(i)+" $"+bill.get(i));

sum+=bill.get(i);

}

writer.println("------------------------");

writer.println("Total $"+sum);

writer.close();

}


}


input_data.txt

account_number start_time_of_call duration

10011A 21:50 20.2

10011A 13:23 12.3

10033C 23:00 34.0

00101B 10:23 45.1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote