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

Customer Tabs Write a java program and you can only use the java.util.Scanner; j

ID: 3701063 • Letter: C

Question

Customer Tabs

Write a java program and you can only use the java.util.Scanner; java.util.io.*; java.util.ArrayList;
You run a restaurant where you allow customers to put items on their "tab". When a customer buys something, you write down the name of the customer, the price of the item, and the name of the item on a line in a log file. The customer name, price, and item name are separated by a # sign. At the end of the month, you want to be able to compute total for each customer, and the total sales for each item.

We will model this using a class called Entry that contains two fields: a name, and a price. We will use two separate ArrayLists of type Entry in the class CustomerLog: one list will contain an Entry for each customer, and the other an Entry for each item. As we process the log file, when we read each line (hint: use split () ), we will update the appropriate entry in each of the two lists. Or, if the line includes a new customer or a new item, we will add an Entry to the appropriate list(s).

At the end, we will write the customer results to a file (one customer per line), and the item results to a file (one item per line). The program should take three filename (the log file which is provided, and the two result files) as command line parameters.

Run the program by typing the following command:

java CustomerLog   log.txt CustLog.txt ItemLog.txt

IF the log.txt file contains the following contents:

john smith#2.59#espresso

cindy jones#3.18#turkey sandwich

john smith#6.70#turkey sandwich

abby james#5.18#espresso

john smith#18.40#soup

john smith#9.99#chesseburger

cindy jones#4.44#soup

THEN the result file CustLog.txt should contains:

john smith 37.68

cindy jones 7.62

abby james 5.18

AND the result file ItemLog.txt should contains:

espresso 7.77

turkey sandwich 9.88

soup 22.84

chesseburger 9.99

Explanation / Answer

Solution: Put both java files and input file in same folder.

Entry.java

//Entry class

class Entry{

       //fields

       private String name;

       private double price;

      

       //constructor

       public Entry(String n, double p)

       {

              name = n;

              price = p;

       }

       //getters and setters

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public double getPrice() {

              return price;

       }

       public void setPrice(double price) {

              this.price = price;

       }

       //toString

       @Override

       public String toString() {

              return name + " " + price;

       }

      

}

CustomerLog.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

//CustomerLog class

public class CustomerLog {

       //ArrayLists

       private ArrayList<Entry> customer = new ArrayList<>();

       private ArrayList<Entry> item = new ArrayList<>();

       //to update customer Arraylist

       public void updateCustomer(Entry e)

       {

              this.customer.add(e);

       }

       //to update item Arraylist

       public void updateItem(Entry e)

       {

              this.item.add(e);

       }

       //getters

       public ArrayList<Entry> getCustomer() {

              return customer;

       }

       public ArrayList<Entry> getItem() {

              return item;

       }

      

       //main method

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

              File file = new File(args[0]); //input file

              Scanner sc = new Scanner(file); //Scanner object

              String line; //to read line

              CustomerLog cl = new CustomerLog(); //CustomerLog object

              //read file

              while(sc.hasNextLine())

              {

                     line = sc.nextLine(); //read a line

                     String details[] = line.split("#");//split it

                     int flag = 0; //to check customer or item already exists

                     //check for customer existance

                     for(Entry e: cl.customer)

                     {

                           //if present, update price and break

                           if(e.getName().equals(details[0]))

                           {

                                  e.setPrice(e.getPrice() + Double.parseDouble(details[1]));

                                  flag = 1;

                                  break;

                           }

                     }

                     //if not present add new Entry object

                     if(flag == 0)

                     {

                           Entry e1 = new Entry(details[0], Double.parseDouble(details[1]));

                           cl.updateCustomer(e1);

                     }

                     flag = 0; //set flag to 0

                     //check for item existance

                     for(Entry e: cl.item)

                     {

                           //if present, update price

                           if(e.getName().equals(details[2]))

                           {

                                  e.setPrice(e.getPrice() + Double.parseDouble(details[1]));

                                  flag = 1;

                                  break;

                           }

                     }

                     //if not present add new Entry object

                     if(flag == 0)

                     {

                           Entry e2 = new Entry(details[2], Double.parseDouble(details[1]));

                           cl.updateItem(e2);

                     }

              }

              //write to ouput files

              PrintWriter writer1 = new PrintWriter(args[1]);

              for(Entry e:cl.customer)

                     writer1.println(e);

              writer1.close();

              PrintWriter writer2 = new PrintWriter(args[2]);

              for(Entry e:cl.item)

                     writer2.println(e);

              writer2.close();

             

              sc.close();

       }

}

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