SHARE
TWEET

Java Cash Register Program

a guest Nov 19th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package prog;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class CashRegister {
  8.  
  9.    
  10.    
  11.     //variables
  12.    
  13.     String yn;
  14.    
  15.         //set 1
  16.         int type;
  17.         int sale;
  18.         String item_name;
  19.         double original_price,discount_amount,sales_price,tax,total;
  20.         double sale_dec;
  21.         String original_price_str,sale_percent,discount_amount_str,sales_price_str,tax_str,total_str;
  22.        
  23.         //set 2
  24.         int type2;
  25.         double money,change;
  26.         double dollars,quarters,dimes,nickels,pennies;
  27.         double dollars_fin,quarters_fin,dimes_fin,nickels_fin,pennies_fin;
  28.         int d,q,di,n,p;
  29.         String change_str;
  30.        
  31.        
  32.        
  33.     //objects
  34.     DecimalFormat dec = new DecimalFormat("$0.00");
  35.     DecimalFormat per = new DecimalFormat("0.00%");
  36.     Scanner scan = new Scanner(System.in);
  37.     Random rand = new Random();
  38.    
  39.     //methods
  40.    
  41.     //run method
  42.     public void run() {
  43.         run_loop();
  44.     }
  45.    
  46.     //loop method
  47.     public void run_loop() {
  48.         run_cash();
  49.         while (type == 1) {
  50.             run_cash();
  51.         }
  52.     }
  53.    
  54.     //compiled method of every method required to run the cash register program
  55.     public void run_cash() {
  56.         type = 0;
  57.         input();
  58.         calc();
  59.         output();
  60.         input2();
  61.         money_loop();
  62.         calc2();
  63.         output2();
  64.         loop();
  65.         while (type == 3) {
  66.             loop();
  67.         }
  68.     }
  69.    
  70.     //asks the user for the first set of variables
  71.     public void input() {
  72.         //prompting user for some values..
  73.         System.out.println("What will you be buying today?");
  74.         item_name = scan.nextLine();
  75.         System.out.println("Okay, how much does it cost?");
  76.         original_price = scan.nextDouble();
  77.     }
  78.    
  79.     //calculates those variables to define other variables
  80.     public void calc() {
  81.         //original price
  82.         original_price_str = dec.format(original_price);
  83.         //discount percent
  84.         //sale = rand.nextInt(((14)+1)+1)*5;
  85.         sale = 65;
  86.         sale_dec = ((double) sale)/100;
  87.         sale_percent = per.format(sale_dec);
  88.         //discount amount
  89.         discount_amount = sale_dec * original_price;
  90.         discount_amount_str = dec.format(discount_amount);
  91.         //sales price
  92.         sales_price = original_price - discount_amount;
  93.         sales_price_str = dec.format(sales_price);
  94.         //tax
  95.         tax = 0.07 * sales_price;
  96.         tax_str = dec.format(tax);
  97.         //total
  98.         total = sales_price + tax;
  99.         total_str = dec.format(total);
  100.     }
  101.    
  102.     //outputs those variables for the user
  103.     public void output() {
  104.         System.out.println("Here is your info:");
  105.         //System.out.println("Original Price of \"" + item_name + "\": " + original_price_str);
  106.         System.out.println("Original price: \t" + original_price_str);
  107.         System.out.println("Discount percent: \t" + sale_percent);
  108.         System.out.println("Discount amount: \t" + discount_amount_str);
  109.         System.out.println("Sales price: \t\t" + sales_price_str);
  110.         System.out.println("Tax: \t\t\t" + tax_str);
  111.         System.out.println("Total: \t\t\t" + total_str);
  112.        
  113.     }
  114.    
  115.     //asks the user for the second set of variables
  116.     public void input2() {
  117.         //prompts user for how much money they will be paying with
  118.         type2 = 0;
  119.         System.out.println(" ");
  120.         System.out.println("How much money will you be paying with?");
  121.         money = scan.nextDouble();
  122.         scan.nextLine();
  123.     }
  124.    
  125.     //in case the user gives an amount of money lower than the total, this stops them.
  126.     public void money_loop() {
  127.         if (money < total) {
  128.             System.out.println("Insufficient funds, try again.");
  129.             type2 = 1;
  130.         }
  131.         while (type2 == 1) {
  132.             input2();
  133.         }
  134.        
  135.     }
  136.    
  137.     //performs the calculations on the second set of variables
  138.     public void calc2() {
  139.         //change
  140.         change = money - total;
  141.         change_str = dec.format(change);
  142.         //dollars
  143.         dollars = change/1;
  144.         dollars_fin = Math.floor(dollars);
  145.         d = (int) dollars_fin;
  146.         //quarters
  147.         quarters = (change - dollars_fin)/.25;
  148.         quarters_fin = Math.floor(quarters);
  149.         q = (int) quarters_fin;
  150.         //dimes
  151.         dimes = (change - (dollars_fin + (quarters_fin * .25)))/.10;
  152.         dimes_fin = Math.floor(dimes);
  153.         di = (int) dimes_fin;
  154.         //nickels
  155.         nickels = (change - (dollars_fin + (quarters_fin * .25) + (dimes_fin * .10)))/.05;
  156.         nickels_fin = Math.floor(nickels);
  157.         n = (int) nickels_fin;
  158.         //pennies
  159.         pennies = (change - (dollars_fin + (quarters_fin * .25) + (dimes_fin * .10) + (nickels_fin * .05)))/0.01;
  160.         System.out.println(pennies);
  161.         pennies_fin = Math.floor(pennies);
  162.         p = (int) pennies_fin;
  163.     }
  164.  
  165.     //outputs the calculated variables
  166.     public void output2() {
  167.         System.out.println("Your change is: " + change_str);
  168.         System.out.println("Dollars: \t" + d);
  169.         System.out.println("Quarters: \t" + q);
  170.         System.out.println("Dimes: \t\t" + di);
  171.         System.out.println("Nickels: \t" + n);
  172.         System.out.println("Pennies: \t" + p);
  173.     }
  174.    
  175.     //actual loop
  176.     public void loop() {
  177.         System.out.println(" ");
  178.         System.out.println("Would you like to buy anything else? (y/n)");
  179.         yn = scan.nextLine();
  180.         yn = yn.toLowerCase();
  181.         if (yn.contentEquals("y")) {
  182.             type = 1;
  183.         }
  184.         else if (yn.contentEquals("n")) {
  185.             type = 2;
  186.             System.out.println("Bye!");
  187.         }
  188.         else {
  189.             System.out.println("Invalid arguments.");
  190.             type = 3;
  191.         }
  192.     }
  193.    
  194.     public static void main(String[] args) {
  195.         CashRegister prog = new CashRegister();
  196.         prog.run();
  197.        
  198.     }
  199.  
  200. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top