Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package cashregister;
  7. import java.util.Scanner;
  8. import java.util.ArrayList;
  9. import java.io.*;
  10.  
  11. /**
  12.  *
  13.  * @author gregf
  14.  */
  15. public class CashRegister {
  16.     static Scanner in = new Scanner(System.in);
  17.     static FileWriter fw;      
  18.     static PrintWriter outputFile;
  19.    
  20.     /**
  21.      * @param args the command line arguments
  22.      */
  23.     public static void main(String[] args)
  24.     throws IOException {
  25.         fw = new FileWriter("transactions.txt", true);
  26.         outputFile = new PrintWriter(fw);
  27.         ArrayList<Double> productList = new ArrayList<>();
  28.        
  29.         int option;
  30.         double totalCost = 0;
  31.         double totalRevenue = 0;
  32.         String date = null;
  33.  
  34.         do {
  35.             System.out.println("Main Menu. Pick an option:\n"
  36.                              + "1. Open Day\n"
  37.                              + "2. Register Transaction\n"
  38.                              + "3. Do Payment\n"
  39.                              + "4. Close Day");
  40.             option = in.nextInt();
  41.            
  42.             switch(option) {
  43.                 case 1:
  44.                   System.out.println("Opening Day... ");
  45.                   date = openDay();
  46.                   break;
  47.                 case 2:
  48.                     System.out.println("Register Transactions: ");
  49.                     if(date != null) {
  50.                        totalCost = registerTransactions(productList);
  51.                     } else {
  52.                         System.out.println("You must open day first.\n");
  53.                     }
  54.                     break;
  55.                 case 3:
  56.                     System.out.println("Do Payment: ");
  57.                     if(totalCost > 0) {
  58.                         totalRevenue = doPayment(totalCost);
  59.                     } else {
  60.                         System.out.println("You must register transactions first.\n");
  61.                     }
  62.                     break;
  63.                 case 4:
  64.                         System.out.println("Closing Day...");
  65.                         closeDay(totalRevenue, date, productList);                        
  66.                     break;
  67.             }
  68.         } while(option != 4);
  69.     }    
  70.    
  71.     public static String openDay() {
  72.         String date;
  73.         System.out.println("Enter the date (mm/dd/yy): ");
  74.         date = in.next();
  75.        
  76.         return date;
  77.     }    
  78.    
  79.     /**
  80.      * Remember you must OPEN DAY before you can register transaction
  81.      * @param productList
  82.      * @return
  83.      * @throws java.io.IOException
  84.      */
  85.     public static double registerTransactions(ArrayList<Double> productList)
  86.     throws IOException {
  87.         int option;
  88.         double totalCost = 0;
  89.  
  90.         do {
  91.             System.out.println("Enter Product Code (numbers only): ");
  92.             double productCode = in.nextDouble();
  93.  
  94.             System.out.println("Enter Price: ");
  95.             double price = in.nextDouble();
  96.  
  97.             System.out.println("Enter quantity: ");
  98.             double quantity = in.nextDouble();
  99.            
  100.             totalCost = totalCost + (price * quantity);
  101.  
  102.             productList.add(productCode);
  103.             productList.add(price);
  104.             productList.add(quantity);
  105.            
  106.             System.out.println("Register Transaction Menu\n"
  107.                              + "1. Enter Transaction\n"
  108.                              + "2. Main Menu");
  109.             option = in.nextInt();
  110.         } while(option != 2);
  111.         return totalCost;
  112.     }
  113.    
  114.     public static double doPayment(double totalCost) {
  115.         double totalRevenue = 0;
  116.        
  117.         do {
  118.             totalCost = Math.round(totalCost * 100) / 100.0;
  119.             System.out.println("You owe: " + totalCost);
  120.  
  121.             System.out.println("You give: ");
  122.             double amountPaid = in.nextDouble();
  123.             amountPaid = Math.round(amountPaid * 100) / 100.0;
  124.  
  125.             if(totalCost > amountPaid) {
  126.                 totalCost -= amountPaid;
  127.                 totalRevenue += amountPaid;
  128.             } else if(totalCost <= amountPaid) {
  129.                 amountPaid -= totalCost;
  130.                 totalRevenue += totalCost;
  131.                 totalCost = 0;
  132.                 System.out.printf("Your change is: %4.2f\n", amountPaid);
  133.             } else {
  134.                 System.out.println("Else condition met in doPayment(). Something went wrong.");
  135.             }    
  136.         } while(totalCost > 0);
  137.        
  138.         totalRevenue = Math.round(totalRevenue * 100) / 100.0;
  139.         return totalRevenue;
  140.     }
  141.    
  142.     public static void closeDay(double totalRevenue, String date, ArrayList<Double> productList)
  143.     throws IOException {
  144.         outputFile.println("Date: " + date);
  145.         for(int i = 2; i < productList.size(); i += 3) {
  146.             outputFile.printf("Product Code: %.0f\n", productList.get(i - 2));
  147.             outputFile.println("Price: " + productList.get(i - 1));
  148.             outputFile.printf("Quantity: %.0f\n", productList.get(i));
  149.             outputFile.println();
  150.         }
  151.  
  152.         System.out.println();
  153.         System.out.println("-------------------------");
  154.         System.out.println("Date: " + date);
  155.        
  156.         //Create new ArrayList with only productCode and quantity
  157.         ArrayList<Double> productsSold = new ArrayList<>();
  158.         for(int i = 2; i < productList.size(); i += 3) {
  159.            productsSold.add(productList.get(i - 2));
  160.            productsSold.add(productList.get(i));
  161.         }      
  162.  
  163.         for(int i = 2; i < productList.size(); i += 3) {
  164.             System.out.printf("%.0f" + " x " + "%.0f\n", productList.get(i - 2), productList.get(i));
  165.         }
  166.  
  167.         System.out.println("\nTotal Revenue: " + totalRevenue);
  168.         System.out.println("-------------------------");
  169.         System.out.println();
  170.         outputFile.close();        
  171.     }
  172.    
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement