Making a Cash Register Program in Java: So this is what my problem is; REGULAR l
ID: 3721382 • Letter: M
Question
Making a Cash Register Program in Java:
So this is what my problem is;
REGULAR level: Complete this level correctly for a maximum score of 82%.
Use the CashRegister class to demonstrate its functionality. This means that you should write a main() method that creates at least two instances of the class, calls the class methods and prints output to show how the method works.
Take a look at the main method in 8.2 and see how we did something similar for the Counter class.
CHALLENGE level: Complete one item at this level in addition to the REGULAR level for a max score of 100%.
Read input (prices) using one of these methods:
From popup windows like you did in 6.8. <-- This one is the one i'm trying to do for the Challenge portion
From a text file.
From an array.
From an ArrayList.
I have the code below so far: My main issue is taking the input values from the dialog box and implementing them into the addItem() Method i have. I left notes on the key problems.
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class CashRegaster {
private int itemCount;
private double totalPrice;
public CashRegaster() {//Sub-method of the CashRegaster Class
itemCount = 0;//Counts number of prices entered by the user
totalPrice = 0;//Final value reset for later step
}
public void addItem(double price) {
itemCount ++;//Count increases by one everytime this loop is run
totalPrice = totalPrice + price;//TotalPrice reset to the next current value for user to know total cost later.
}
public double getTotal() {
return totalPrice;//Returns totalPrice for later use
}
public int getCount() {
return itemCount;//Returns itemCount for later use
}
public void clear() { //Resets main values.
itemCount = 0;
totalPrice = 0;
}
public static void main(String[] args) {
//This program runs a cash register program that totals up the cost of the items entered by the user.
CashRegaster register1 = new CashRegaster();//Call to begin the program
Scanner in = new Scanner(System.in);
ArrayList<String> prices = new ArrayList<>();
boolean done = false;
do {//Parameter of Do Statement
String input;//Input variable is called.
input = JOptionPane.showInputDialog("Enter Item Price(click Cancel or the X): ");//Box collects info from user
if(input == null) {//Parameter of If Loop
break;//Breaks from Dialog box loop
}
//register1.addItem(input); // <-- This is where i'm having issues.
//Taking the input and plugging it into the addItem() Method.
prices.add(input);//Stores input variable for testing
}while(!done);//While Statement to satisfy Java
System.out.println(prices);//Tests to see if input is accepted by program.
System.out.println(register1.getCount());//Returns number of items in Register1
System.out.printf("%.2f ", register1.getTotal());//Returns the total cost of the items in Register1
CashRegaster register2 = new CashRegaster();//Call to begin the program
Scanner src = new Scanner(System.in);
ArrayList<String> price = new ArrayList<>();
boolean complete = false;
do {//Parameter of Do Statement
String enter;//Input variable is called.
enter = JOptionPane.showInputDialog("Enter Item Price(click Cancel or the X): ");//Box collects info from user
if(enter == null) {//Parameter of If Loop
break;//Breaks from Dialog box loop
}
//register1.addItem(input); // <-- This is where i'm having issues.
//Taking the input and plugging it into the addItem() Method.
prices.add(enter);//Stores input variable for testing
}while(!complete);//While Statement to satisfy Java
System.out.println(prices);//Tests to see if input is accepted by program.
System.out.println(register1.getCount());//Returns number of items in Register2
System.out.printf("%.2f ", register1.getTotal());//Returns the total cost of the items in Register2
}
}
Explanation / Answer
import javax.swing.*; import java.util.ArrayList; import java.util.Scanner; public class CashRegaster { private int itemCount; private double totalPrice; public CashRegaster() {//Sub-method of the CashRegaster Class itemCount = 0;//Counts number of prices entered by the user totalPrice = 0;//Final value reset for later step } public void addItem(double price) { itemCount ++;//Count increases by one everytime this loop is run totalPrice = totalPrice + price;//TotalPrice reset to the next current value for user to know total cost later. } public double getTotal() { return totalPrice;//Returns totalPrice for later use } public int getCount() { return itemCount;//Returns itemCount for later use } public void clear() { //Resets main values. itemCount = 0; totalPrice = 0; } public static void main(String[] args) { //This program runs a cash register program that totals up the cost of the items entered by the user. CashRegaster register1 = new CashRegaster();//Call to begin the program Scanner in = new Scanner(System.in); ArrayList prices = new ArrayList(); boolean done = false; do {//Parameter of Do Statement String input;//Input variable is called. input = JOptionPane.showInputDialog("Enter Item Price(click Cancel or the X): ");//Box collects info from user if(input == null) {//Parameter of If Loop break;//Breaks from Dialog box loop } register1.addItem(Double.parseDouble(input)); //Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.