Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prog;
- import java.text.DecimalFormat;
- import java.util.Random;
- import java.util.Scanner;
- public class CashRegister {
- //variables
- String yn;
- //set 1
- int type;
- int sale;
- String item_name;
- double original_price,discount_amount,sales_price,tax,total;
- double sale_dec;
- String original_price_str,sale_percent,discount_amount_str,sales_price_str,tax_str,total_str;
- //set 2
- int type2;
- double money,change;
- double dollars,quarters,dimes,nickels,pennies;
- double dollars_fin,quarters_fin,dimes_fin,nickels_fin,pennies_fin;
- int d,q,di,n,p;
- String change_str;
- //objects
- DecimalFormat dec = new DecimalFormat("$0.00");
- DecimalFormat per = new DecimalFormat("0.00%");
- Scanner scan = new Scanner(System.in);
- Random rand = new Random();
- //methods
- //run method
- public void run() {
- run_loop();
- }
- //loop method
- public void run_loop() {
- run_cash();
- while (type == 1) {
- run_cash();
- }
- }
- //compiled method of every method required to run the cash register program
- public void run_cash() {
- type = 0;
- input();
- calc();
- output();
- input2();
- money_loop();
- calc2();
- output2();
- loop();
- while (type == 3) {
- loop();
- }
- }
- //asks the user for the first set of variables
- public void input() {
- //prompting user for some values..
- System.out.println("What will you be buying today?");
- item_name = scan.nextLine();
- System.out.println("Okay, how much does it cost?");
- original_price = scan.nextDouble();
- }
- //calculates those variables to define other variables
- public void calc() {
- //original price
- original_price_str = dec.format(original_price);
- //discount percent
- //sale = rand.nextInt(((14)+1)+1)*5;
- sale = 65;
- sale_dec = ((double) sale)/100;
- sale_percent = per.format(sale_dec);
- //discount amount
- discount_amount = sale_dec * original_price;
- discount_amount_str = dec.format(discount_amount);
- //sales price
- sales_price = original_price - discount_amount;
- sales_price_str = dec.format(sales_price);
- //tax
- tax = 0.07 * sales_price;
- tax_str = dec.format(tax);
- //total
- total = sales_price + tax;
- total_str = dec.format(total);
- }
- //outputs those variables for the user
- public void output() {
- System.out.println("Here is your info:");
- //System.out.println("Original Price of \"" + item_name + "\": " + original_price_str);
- System.out.println("Original price: \t" + original_price_str);
- System.out.println("Discount percent: \t" + sale_percent);
- System.out.println("Discount amount: \t" + discount_amount_str);
- System.out.println("Sales price: \t\t" + sales_price_str);
- System.out.println("Tax: \t\t\t" + tax_str);
- System.out.println("Total: \t\t\t" + total_str);
- }
- //asks the user for the second set of variables
- public void input2() {
- //prompts user for how much money they will be paying with
- type2 = 0;
- System.out.println(" ");
- System.out.println("How much money will you be paying with?");
- money = scan.nextDouble();
- scan.nextLine();
- }
- //in case the user gives an amount of money lower than the total, this stops them.
- public void money_loop() {
- if (money < total) {
- System.out.println("Insufficient funds, try again.");
- type2 = 1;
- }
- while (type2 == 1) {
- input2();
- }
- }
- //performs the calculations on the second set of variables
- public void calc2() {
- //change
- change = money - total;
- change_str = dec.format(change);
- //dollars
- dollars = change/1;
- dollars_fin = Math.floor(dollars);
- d = (int) dollars_fin;
- //quarters
- quarters = (change - dollars_fin)/.25;
- quarters_fin = Math.floor(quarters);
- q = (int) quarters_fin;
- //dimes
- dimes = (change - (dollars_fin + (quarters_fin * .25)))/.10;
- dimes_fin = Math.floor(dimes);
- di = (int) dimes_fin;
- //nickels
- nickels = (change - (dollars_fin + (quarters_fin * .25) + (dimes_fin * .10)))/.05;
- nickels_fin = Math.floor(nickels);
- n = (int) nickels_fin;
- //pennies
- pennies = (change - (dollars_fin + (quarters_fin * .25) + (dimes_fin * .10) + (nickels_fin * .05)))/0.01;
- System.out.println(pennies);
- pennies_fin = Math.floor(pennies);
- p = (int) pennies_fin;
- }
- //outputs the calculated variables
- public void output2() {
- System.out.println("Your change is: " + change_str);
- System.out.println("Dollars: \t" + d);
- System.out.println("Quarters: \t" + q);
- System.out.println("Dimes: \t\t" + di);
- System.out.println("Nickels: \t" + n);
- System.out.println("Pennies: \t" + p);
- }
- //actual loop
- public void loop() {
- System.out.println(" ");
- System.out.println("Would you like to buy anything else? (y/n)");
- yn = scan.nextLine();
- yn = yn.toLowerCase();
- if (yn.contentEquals("y")) {
- type = 1;
- }
- else if (yn.contentEquals("n")) {
- type = 2;
- System.out.println("Bye!");
- }
- else {
- System.out.println("Invalid arguments.");
- type = 3;
- }
- }
- public static void main(String[] args) {
- CashRegister prog = new CashRegister();
- prog.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement