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

I know it might look real simple for most of U guys, but I’m really stuck with a

ID: 3604991 • Letter: I

Question

I know it might look real simple for most of U guys, but I’m really stuck with all of it (I am just a beginner in Java (actually in every computer lang)). Please help with the source codes for this simple shopping simulation program.
Moreover, I really don’t need a criticizing phase about ‘why I am not doing this myself?’ or ‘This is real easy!’ right now. So, would you be so kind and skip that part. Thank you very much. 1. Create file products.txt containing names and prices of 5 products. 450 Implement class Product, with at least the following members: cap flag 280 keyring 120 mug t-shirt 720 . . - Variables name and price:values are read from product.txt Variable totalSales: keep the total sales amount of this product Add more variables and methods as needed 550 2. Create file orders.txt containing a log of customer orders. Each line contains customer name and number of each product he/she orders. There must be 20 transactions in this file-for simplicity, there is no duplicate customer Annie e e 4 1 1++Annie orders (positive int): Betty 2 10 e Cathy 3 3 3 David 2 5e 2 Ellen 6 3 e e Frodo e 4 3 1 Ginny 5 12 e e Harry 3 5 e 3 2 e cap, e flog, 4 Reyrings, 1 mug, 1 t-shirt Implement class Customer, with at least the following members: . Variables name and orders (array or ArrayList): values are read from order.txt Variable totalBill : keep the total bill of this customer * . calculate Method calculateBI Add more variables and methods as needed the total bill : 3. Implement dass Shopping as the main class. When the program starts 3.1 3.2 3.3 3.4 Read the data from products.txt into array (or Arraylist) of Product Read the data from orders.txt into array (or ArrayList) of Customer List all customer orders & their total bills. Ask the user to either "commit" or "rollback" In case of rollback, the user must also specify the number of orders to rollback. For example, "rollback 2" means the last 2 customers will be removed. List the remaining customer orders and repeat 3.3 3.5 In case of commit, print summary reports Total bills of all (remaining) customers, sorted in increasing or decreasing order of the bills Total sales of all products, sorted in increasing or decreasing order of the sales Rollbacked transactions must not be included in total bills and total sales 4. The program must be able to handle the following errors/exceptions 4.1 Missing files 4.2 File orders.bxt may contain input errors. You may use 2 separate files, one with clean input and another with errors Missing values, e-g. only 3 products intead of 5 products in a line . Format errors, eg o (oh)" instead of "8 (zero) . Invalid numbers, e-g. -2 for number of products

Explanation / Answer

The following is the code for your question along with the output.

To indent code in Eclipse IDE, you can select the code in each of the java file press "Ctrl + A" and then "Ctrl + i". Make sure if the input files - products.txt and orders.txt are directly under the project and NOT under src folder in Eclipse.


//============== Product.java ===================

public class Product {
private String name = null;
private float price = 0.0f;
private float totalSales = 0.0f;
public Product(String name, float price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public float getPrice() {
return price;
}
public float getTotalSales() {
return totalSales;
}
public void setTotalSales(float salesAmount) {
totalSales = salesAmount;
}
}



//============== Customer.java ===================

import java.util.ArrayList;
public class Customer {
private String name = null;
private ArrayList<Integer> orders = new ArrayList();
private float totalBill = 0.0f;
public Customer(String name, ArrayList<Integer> ordersList) {
this.name = name;
this.orders = ordersList;
}
public ArrayList getOrders() {
return orders;
}
public void calculateBill(ArrayList<Product> products) {
totalBill = 0;
for(int i =0 ; i < orders.size(); i++)
totalBill += orders.get(i) * products.get(i).getPrice();
}
public String getName() {
return name;
}
public float getTotalBill() {
return totalBill;
}
public int getOrder(int index)
{
return orders.get(index);
}
}


//============== Shopping.java ===================

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Shopping {
//Stores the product inventory details
private ArrayList<Product> productsList = new ArrayList();;
//Stores the list of all customers along with their orders
private ArrayList<Customer> customersList = new ArrayList();
//Num of Transactions to be rolled back
private int rollbackTransactionCount = 0;
public void initialize() {
try {
loadProducts();
loadOrders();
listAllCustomerOrders();
showMenuAndAcceptInput();
} catch (Exception ex) {
System.err.println("An error occurred. Message :>[" + ex.getMessage() + "]");
}
}
//Read the product information from the products.txt file
private void loadProducts() throws Exception {
File productsFile = new File("products.txt");
Scanner fileScanner = new Scanner(productsFile);
try {
while(fileScanner.hasNext()) {
String line = fileScanner.nextLine().trim();
if(line.length() > 0) {
Scanner lineScanner = new Scanner(line);
String productName = null, productPrice = null;
//Read the product name
if(lineScanner.hasNext()) {
productName = lineScanner.next();
}
//Read the product size
if(lineScanner.hasNext()) {
productPrice = lineScanner.next();
}
if(productName == null || productName.trim().equals("") || productPrice == null || productPrice.trim().equals("")) {
throw new Exception("Invalid entry in the products.txt");
} else {
//Create a new product object and update the products list
Product newProduct = new Product(productName.trim(), Float.parseFloat(productPrice.trim()));
productsList.add(newProduct);
}
lineScanner.close();
}
}
} catch (Exception ex) {
throw ex;
} finally {
fileScanner.close();
}
}
//Read the customer name and the number of the orders for each of the products
private void loadOrders() throws Exception {
File ordersFile = new File("orders.txt");
Scanner fileScanner = new Scanner(ordersFile);
int totalNumOfProducts = productsList.size();
try {
while(fileScanner.hasNext()) {
String line = fileScanner.nextLine().trim();
if(line.length() > 0) {
Scanner lineScanner = new Scanner(line);
String customerName = null;
ArrayList<Integer> orderCountList = new ArrayList<Integer>();
if(lineScanner.hasNext()) {
customerName = lineScanner.next();
}
int count = 0;
try {
if(customerName == null || customerName.trim().equals("")) {
throw new Error("Invalid customer name");
}
//Read the number of the orders for each of the products
while(count < totalNumOfProducts) {
int productCount = Integer.parseInt(lineScanner.next());
if(productCount < 0) {
throw new Error("Invalid product count [" + productCount + "] for customer [" + customerName + "]" );
}
orderCountList.add(productCount);
count++;
}
} catch (Exception ex) {
System.err.println("Invalid orders entry in orders.txt. Skipping this order ...");
continue;
}
lineScanner.close();
Customer newCustomer = new Customer(customerName, orderCountList);
newCustomer.calculateBill(productsList);
//Update the customers list
customersList.add(newCustomer);
}
}
} catch (Exception ex) {
throw ex;
} finally {
fileScanner.close();
}
}
private void listAllCustomerOrders() {
if(customersList.size() > 0) {
System.out.printf(" %-20s ", "Name ");
for(int i = 0; i < productsList.size(); i++)
System.out.printf("%15s", productsList.get(i).getName());
System.out.printf("%15s ", "Total amount");
for(int i = 0;i < customersList.size(); i++) {
Customer customer = customersList.get(i);
System.out.printf("%-20s ", customer.getName());
for(int j = 0; j < productsList.size(); j++)
System.out.printf("%15d", customer.getOrder(j));
System.out.printf("%15.2f ", customer.getTotalBill());

}
} else {
System.out.println("No customer transaction have been recorded");
}
}
private void showMenuAndAcceptInput() {
System.out.println(" Enter "rollback" or "commit:>");
Scanner inputScanner = new Scanner(System.in);
try {
while(inputScanner.hasNext()) {
System.out.println("Enter "rollback" or "commit:> ");
String input = inputScanner.nextLine();
input = input.trim().toLowerCase();
if(input.startsWith("rollback")) {
doRollback(input);
listAllCustomerOrders();
} else if(input.equals("commit")) {
doCommit();
break;
}else {
System.err.println("Invalid command");
}
}
} finally {
inputScanner.close();
}
}
private void doRollback(String rollbackInput) {
Scanner lineScanner = new Scanner(rollbackInput);
if(lineScanner.hasNext()) {
lineScanner.next(); //skip the rollback keyword
if(lineScanner.hasNext()) {
rollbackTransactionCount = lineScanner.nextInt();
for(int i = 0; i < rollbackTransactionCount; i++)
customersList.remove(customersList.size() - 1);
} else {
System.err.println("Error: Enter valid command - rollback <num of transactions>");
}
}
lineScanner.close();
}
private void doCommit() {
sortCustomers(customersList);
for(int j = 0; j < customersList.size(); j++) {
Customer customer = customersList.get(j);
for(int i = 0; i < productsList.size(); i++) {
Product product = productsList.get(i);
float sales = product.getTotalSales();
sales += customer.getOrder(i) * product.getPrice();
product.setTotalSales(sales);
}
}
sortProducts(productsList);
System.out.println("===== Customer Report =====");
listAllCustomerOrders();
System.out.println("===== Product Report =====");
System.out.printf("%-20s %15s ", "Name", "Total Sales");
System.out.printf("%-20s %15s ", "-----", "-------------");
for(int i = 0; i < productsList.size(); i++) {
Product product = productsList.get(i);
System.out.printf("%-20s %15.2f ", product.getName(), product.getTotalSales());
}
}
//Sort the customers list by the Total bill amount in increasing order
private void sortCustomers(ArrayList<Customer> list)
{
int n = list.size();
for(int i = 0; i < n; i++)
{
int minIdx = i;
for(int j = i + 1; j < n; j++)
{
if(list.get(j).getTotalBill() < list.get(minIdx).getTotalBill())
minIdx = j;
}
if(i != minIdx)
{
Customer temp = list.get(i);
list.set(i, list.get(minIdx));
list.set(minIdx, temp);
}
}
}
//Sort the products list by Total Sales amount in increasing order
private void sortProducts(ArrayList<Product> list)
{
int n = list.size();
for(int i = 0; i < n; i++)
{
int minIdx = i;
for(int j = i + 1; j < n; j++)
{
if(list.get(j).getTotalSales() < list.get(minIdx).getTotalSales())
minIdx = j;
}
if(i != minIdx)
{
Product temp = list.get(i);
list.set(i, list.get(minIdx));
list.set(minIdx, temp);
}
}
}
public static void main(String[] args) {
Shopping shippingApp = new Shopping();
shippingApp.initialize();
}
}



//=============== Input file - products.txt =============
cap 450
flag1 280
keyring 120
mug 550
t-shirt 720

//=============== Input file - orders.txt =============
Annie 0 0 4 1 1
Betty 2 0 10 0 0
Cathy 0 3 0 3 3
David 2 5 0 0 2
Ellen 6 0 3 0 0
Frondo 0 4 0 3 1
Ginny 5 0 12 0 0
Harry 3 5 0 3 2









Output
============



Name cap flag1 keyring mug t-shirt Total amount
Annie 0 0 4 1 1 1750.00
Betty 2 0 10 0 0 2100.00
Cathy 0 3 0 3 3 4650.00
David 2 5 0 0 2 3740.00
Ellen 6 0 3 0 0 3060.00
Frondo 0 4 0 3 1 3490.00
Ginny 5 0 12 0 0 3690.00
Harry 3 5 0 3 2 5840.00

Enter "rollback" or "commit:>
rollback 2
Enter "rollback" or "commit:>
Name cap flag1 keyring mug t-shirt Total amount
Annie 0 0 4 1 1 1750.00
Betty 2 0 10 0 0 2100.00
Cathy 0 3 0 3 3 4650.00
David 2 5 0 0 2 3740.00
Ellen 6 0 3 0 0 3060.00
Frondo 0 4 0 3 1 3490.00
commit
Enter "rollback" or "commit:>
===== Customer Report =====
Name keyring flag1 mug cap t-shirt Total amount
Annie 0 0 4 1 1 1750.00
Betty 2 0 10 0 0 2100.00
Ellen 6 0 3 0 0 3060.00
Frondo 0 4 0 3 1 3490.00
David 2 5 0 0 2 3740.00
Cathy 0 3 0 3 3 4650.00
===== Product Report =====
Name Total Sales
----- -------------
keyring 2040.00
flag1 3360.00
mug 3850.00
cap 4500.00
t-shirt 5040.00