# Programming homework, help needed!

• 03-11-2012, 07:16 PM
CarlosGrijalva
Programming homework, help needed!
Hey guys I am taking an introductory course to Java programming because I am a noob and I want to learn. Anyway I have this professor that doesn't really explain well what he wants us to do for our assignments, so i'm a bit confused on this one. I have to write three classes, a class, a test class, and a GetData class. If someone helps me compute the cashier/testcashier classes I will love you forever. Here is the assignment:

Write a class called Cashier that directs a cashier how to cash goods and give change to customers. The typical cashier operations are as follows:
(a) Cashier clears the cash register machine.
(b) Cashier enters the name and the price of each item in the cash registering machine.
(c) The customer tenders an amount of money to pay for the goods (We assume the amount covers the total).
(d) The cash machine computes:
i. The number of items purchased
ii. The total amount of purchase
iii. The average price of each item
iv. The number of coin denominations that the customer should receive. That is, the number of silver dollars, quarters, dimes, nickels, and cents the customer should receive in turn.

Use the following class, TestCashier, as the basis for the test class.

Code:

class TestCashier
{
public static void main(String[] arg)
{
Cashier c = new Cashier();

String name = GetData.getWord(“Enter name of item”);
double price = GetData.getDouble(“Enter price of item”);

name = GetData.getWord(“Enter name of item”);
price = GetData.getDouble(“Enter price of item”);

// Now average the price of the items
c.average();

// Make payment
double amount = GetData.getDouble(“Enter amount of money for payment”);

c.tendered(amount); // For example twenty dollars were tendered
c.makeChange();

generateReceipt(c);
}
static void generateReceipt(Cahier c)
{
// Write the necessary code that will generate a customer’s receipt.
// The output must be displayed in a scrollable pane
}
}

Description of the output:
The output should be displayed in a scrollable pane, and have the following features:
• The first line displays the name of the establishment.
• Second line reads something like this: Welcome – thanks for stopping, followed by the current date
• The list of items displayed, one item per line – That is, the name of the product and price,
• The sum of all the items
• The number of items purchased
• The average price for each item
• The amount of money tendered
• The amount of change in \$ and cents
• The change given in coin denominations

Here is an example of the form of how the output should be ( except that this output must be displayed in a scrollable pane).

Chicken..........6.79
Egg..................3.07
______________
Total ……….\$12.85

The number of items purchased is 3 items
The average price per item is \$4.28

Amount tendered is \$20.00
The change is \$7.15

The change includes
7 dollars
0 quarters
1 dimes
1 nickels
0 cents

NB: Use the class GetData to enter the data. This means that your program MUST consist of three classes: GetData.java, Cashier.java, and TestCashier.

My question is, how do I incorporate the GetData class to the other classes? And do I also need a TestGetData class? Because it clearly states at the bottom that I need only three classes so i'm a bit confused. If someone could please walk me through what I have to do for the GetData/Cashier classes it would be really helpful
• 03-11-2012, 08:10 PM
Fubarable
Re: Programming homework, help needed!
Quote:

Originally Posted by CarlosGrijalva
My question is, how do I incorporate the GetData class to the other classes? And do I also need a TestGetData class? Because it clearly states at the bottom that I need only three classes so i'm a bit confused. If someone could please walk me through what I have to do for the GetData/Cashier classes it would be really helpful

Your GetData class will have only two static methods, and you should probably know by now what those methods are since they're being used in the test code. As for creating a TestGetData class -- that's up to you. Myself, I'd give my GetData class a main method with which I would test out the code to be sure that it works well, and then before turning in the assignment, I'd remove the main method from this class.
• 03-12-2012, 12:21 AM
CarlosGrijalva
Re: Programming homework, help needed!
Quote:

Originally Posted by Fubarable
Your GetData class will have only two static methods, and you should probably know by now what those methods are since they're being used in the test code. As for creating a TestGetData class -- that's up to you. Myself, I'd give my GetData class a main method with which I would test out the code to be sure that it works well, and then before turning in the assignment, I'd remove the main method from this class.

This is what I have so far, and I am running into some compile errors. My test class does not recognize the getWord and getDouble from the getData class, not sure why this is. I will post my code from all three classes.

Code:

import java.text.NumberFormat;
import java.text.DecimalFormat;

public class Cashier
{
//Variables
private static int numberOfItems;
private static double totalSum;
private String name, s;
private double amount, price, tendered, change, dollars, quarters, dimes, nickels, pennies;

NumberFormat nf = NumberFormat.getInstance();
DecimalFormat df = (DecimalFormat)nf;

//Constructor
public Cashier()
{
this.name = name;
this.price = price;
price = 0;
this.s = "";
}
public double average()
{
}
public void Add(String name, double price)
{
totalSum = totalSum + price;
s = s + name + "........" + price + "\n";
numberOfItems++;
}
public void tendered(double amount);
{
f.format(tendered = amount);
}

Code:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Locale;
import java.util.Date;
import java.text.DateFormat;

class TestCashier
{
public static void main(String[] arg)
{
Cashier c = new Cashier();

String name = GetData.getWord(“Enter name of item”);
double price = GetData.getDouble(“Enter price of item”);

name = GetData.getWord(“Enter name of item”);
price = GetData.getDouble(“Enter price of item”);
name = GetData.getWord(“Enter name of item”);
price = GetData.getDouble(“Enter price of item”);

// Now average the price of the items
c.average();

// Make payment
double amount = GetData.getDouble("Enter amount of money for payment");

tendered(amount); // Twenty dollars were tendered

c.makeChange();

generateReceipt(c);

}
static void generateReceipt(Cashier c)
{

String r = "ABC Groceries Shop \n";

Code:

import javax.swing.JOptionPane;
public class GetData
{
static String str;
static double getDouble(String s)
{
str = JOptionPane.showInputDialog(s);
return Double.parseDouble(str);
}
static int getInt(String s)
{
str = JOptionPane.showInputDialog(s);
return Integer.parseInt(str);
}
static String getWord(String s)
{
return JOptionPane.showInputDialog(s);
}
}

If you can please point out what I am doing wrong it would help alot thank you!
• 03-12-2012, 12:32 AM
Fubarable
Re: Programming homework, help needed!
Quote:

Originally Posted by CarlosGrijalva
This is what I have so far, and I am running into some compile errors. My test class does not recognize the getWord and getDouble from the getData class, not sure why this is. I will post my code from all three classes.

I've edited your post and have added code tags to your posted code with the tag [code] above a code block and [/code] below a code block, but still your code is hard to read as it has no formatting to begin with. Please edit your post above and post formatted code so that we will be able to read it. Also please post any error text the code generates, and indicate some way to us which lines are causing these errors.
• 03-12-2012, 02:16 AM
CarlosGrijalva
Re: Programming homework, help needed!
Quote:

Originally Posted by Fubarable
I've edited your post and have added code tags to your posted code with the tag [code] above a code block and [/code] below a code block, but still your code is hard to read as it has no formatting to begin with. Please edit your post above and post formatted code so that we will be able to read it. Also please post any error text the code generates, and indicate some way to us which lines are causing these errors.

I am sorry as you can see I am new to these forums and not quite sure of the rules. I will post the formatted lines that are giving me errors. All the errors occur in my TestCashier class, as my GetData and Cashier classes have no compile errors as of now.

Code:

String name = GetData.getWord(“Enter name of item”);
double price = GetData.getDouble(“Enter price of item”);

name = GetData.getWord(“Enter name of item”);
price = GetData.getDouble(“Enter price of item”);
name = GetData.getWord(“Enter name of item”);
price = GetData.getDouble(“Enter price of item”);

Here the error shows as "name and price already defined in main(java.lang.String[])
and for c.add(name,price) it says it has not been initialized.

Also for this single line of coding, tendered(amount);
it says it cannot find symbol.

I am confused as to how to put everything working together, if you could point out my mistakes it would be greatly appreciated!
• 03-12-2012, 03:35 AM
CarlosGrijalva
Re: Programming homework, help needed!
I'm sorry for double posting, but pleaseeeee could anyone help me? I am in desperate need of some assistance :(
• 03-12-2012, 06:30 AM
waiheng1986
Re: Programming homework, help needed!
your cashier's class constructor should be

//initialize the value for name and price but not point back the global variable back to itself.
this.name = "";
this.price = ''";
price = 0;
this.s = "";

change it and try it again.