Java A phone company bills customers as follows: $20/month base fee per account
ID: 3550468 • Letter: J
Question
Java
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.