Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Project help, kinda lost

  1. #1
    Junior Member OpX316's Avatar
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Project help, kinda lost

    I am fairly new to Java and have been working on a cash register project for a computer science class I am in. I am new to Java and a little lost here. We have 3 separate classes: Item, Transaction and Cashregister. Item and transaction were easy. You have names, item IDs, price, tax for the item class and the transaction class is basically a few ArrayLists that take all those things from the Item class and make them a transaction. What is stumping me is how I would code the Cashregister class and how I would test it in a main method. Could someone help me out a little or give some example code for a main method? That would be great. I am not asking you to do my stuff for me, but I am honestly stumped . I'll post my progress so far along with my instructions given to me by my instructor. FYI, I was given some example code to go off of for the item and transaction classes.

    All 3 classes in zip form(or look below): attachments.zip - FileSmelt File and Image Hosting

    Instructions:
    You will not be writing a user interface for the Cash Register project. (Unless you write one to test your class.) What you are doing is writing a class which could serve as the underlying code that makes a cash register work. Your CashRegister class must have the following methods:
     
     public double getStartingCash() - returns the amount of cash in the register when it was instantiated
     public double getCurrentCash() - returns the amount of cash currently in the register
     public double getTotalChangeDispensed() - returns the total amount of change dispensed from this register
     public ArrayList<Transaction> getAllTransactions() - returns an ArrayList<Transaction> of all of the Transactions for this register
     public double getTotalChecks() - gets the total value of all checks in the register
     public int getNumberOfChecks() - gets the total number of check transactions in the register
     public double getTotalCreditTransactions() - gets the total value of all credit card transactions in the register
     public int getNumberOfCreditTransactions() - gets the total number of credit card transactions in the register
     public double getTotalDebitTransactions() - gets the total value of all credit card transactions in the register
     public int getNumberOfDebitTransactions() - gets the total number of credit card transactions in the register
     public double getTotalCashTransactions() - gets the total value of all cash transactions in the register
     public int getNumberOfCashTransactions() - gets the total number of cash transactions in the register
     public void processTransaction(Transaction t) - adds the Transaction t to the register and updates any instance variables as required
     public String receipt(int id) - returns a String with a receipt for the Transaction with the given id number, returns null if no such transaction is in the cash register
     public Transaction voidTransaction(int id) - returns the Transaction with the given ID number and voids it from the register - voiding means the transaction is removed from the collection of transactions and any instance variables which need to be updated as a result are updated (for instance the amount of cash in the register may change)

    Item class:
     
     
    // Note, if you are using the default package you will have to
    // eliminate this line about the package.
    package cashregister;
     
    public class Item 
    {
    	private String name;
    	private String idCode;
    	private double price;
    	private double taxRate;
     
    	/**
    	 * Constructs a new Item
    	 * @param nm name of Item
    	 * @param id id code of Item
    	 * @param pr price of Item
    	 * @param tr tax rate of Item
    	 */
    	public Item(String nm, String id, double pr, double tr)
    	{
    		name = nm;
    		idCode = id;
    		price = pr;
    		taxRate = tr;
    	}
     
    	/**
    	 * Returns the name of the Item
    	 * @return name
    	 */
    	public String getName()
    	{
    		return name;
    	}
     
    	/**
    	 * Returns the ID code of the Item
    	 * @return ID code
    	 */
    	public String getID()
    	{
    		return idCode;
    	}
     
    	/**
    	 * Returns the price of the Item
    	 * @return price
    	 */
    	public double getPrice()
    	{
    		return price;
    	}
     
    	/**
    	 * Returns the tax rate for this Item (as a decimal number,
    	 * for example 6.5% = .065)
    	 * @return tax rate 
    	 */
    	public double getTaxRate()
    	{
    		return taxRate;
    	}
     
    	/**
    	 * Returns the amount of tax paid for the Item
    	 * @return tax paid
    	 */
    	public double getTax()
    	{
    		return price*taxRate;
    	}
     
    	/**
    	 * Returns the total cost (price + tax) of the Item
    	 * @return total cost
    	 */
    	public double getCost()
    	{
    		return price + price*taxRate;
    	}
     
    	/**
    	 * Sets the price of the Item
    	 * @param pr new price
    	 */
    	public void setPrice(double pr)
    	{
    		price = pr;
    	}
     
    	/**
    	 * Sets the tax rate of the Item (as a decimal number,
    	 * for example 6.5% should be .065)
    	 * @param tx new tax rate
    	 */
    	public void setTax(double tx)
    	{
    		taxRate = tx;
    	}
     
    	// Verifies the the methods work
    	public static void main(String[] args)
    	{
    		Item it = new Item("SoBe","PN123456",1.59,.065);
    		System.out.println("Name = " + it.getName());
    		System.out.println("ID = " + it.getID());
    		System.out.printf("Price = $%.2f\n",it.getPrice());
    		System.out.printf("Tax = $%.2f\n",it.getTax());
    		System.out.printf("Cost = $%.2f\n",it.getCost());
    		System.out.printf("Tax Rate = %.3f\n",it.getTaxRate());
     
    		it.setPrice(1.89);
    		it.setTax(0.09);
    		System.out.printf("New Values:\nPrice = $%.2f\n",it.getPrice());
    		System.out.printf("Tax = $%.2f\n",it.getTax());
    		System.out.printf("Cost = $%.2f\n",it.getCost());
    		System.out.printf("Tax Rate = %.3f\n",it.getTaxRate());
    	}
    }

    Transaction class:
    package cashregister;
    import java.util.ArrayList;
     
    public class Transaction 
    {
    	private ArrayList<Item> items;
    	private double totalIn;
    	private String type;
    	private int idNumber;
     
    	private static int IDPOOL = 100000000;
     
    	/**
    	 * Constructor takes the amount paid in and the payment type, initializes
    	 * an empty list of items.
    	 * @param tp type of the payment (credit, cash, debit, check)
    	 * @param in amount of payment
    	 */
    	public Transaction(String tp, double in)
    	{
    		totalIn = in;
    		type = tp;
    		idNumber = IDPOOL;
    		IDPOOL += 1;
    		items = new ArrayList<Item>();
    	}
     
    	/**
    	 * Constructor takes the amount paid in and the payment type, as well
    	 * as the ArrayList of items
    	 * @param tp type of the payment (credit, cash, debit, check)
    	 * @param in amount of payment
    	 * @param list ArrayList of Items purchased
    	 */
    	public Transaction(ArrayList<Item> list, String tp, double in)
    	{
    		totalIn = in;
    		type = tp;
    		idNumber = IDPOOL;
    		IDPOOL += 1;
    		items = list;
    	}
     
    	/**
    	 * Returns an ArrayList of all items in the transaction
    	 * @return ArrayList<Item> of all Items in transaction
    	 */
    	public ArrayList<Item> getItems()
    	{
    		return items;
    	}
     
    	/**
    	 * Returns the payment in for this transaction
    	 * @return payment in
    	 */
    	public double paymentIn()
    	{
    		return totalIn;
    	}
     
    	/**
    	 * Returns the change returned in this transaction
    	 * @return change returned
    	 */
    	public double changeOut()
    	{
    		double temp = totalIn;
    		temp -= getTaxPaid();
    		temp -= subTotal();
    		return temp;
    	}
     
    	/**
    	 * Returns the subtotal for the transaction (the total of all
    	 * the prices, without taxes)
    	 * @return subtotal of transaction
    	 */
    	public double subTotal()
    	{
    		double sum = 0;
    		for(Item temp : items)
    		{
    			sum += temp.getPrice();
    		}
    		return sum;
    	}
     
    	/**
    	 * Returns the total taxes paid for this transaction
    	 * @return taxes paid
    	 */
    	public double getTaxPaid()
    	{
    		double sum = 0;
    		for(Item temp : items)
    		{
    			sum += temp.getPrice()*temp.getTaxRate();
    		}
    		return sum;
    	}
     
    	/**
    	 * Returns the payment type (cash, credit, debit or check)
    	 * @return payment type
    	 */
    	public String getPaymentType()
    	{
    		return type;
    	}
     
    	/**
    	 * Returns the number of Items in the transaction
    	 * @return number of items in transaction
    	 */
    	public int getTotalNumberOfItems()
    	{
    		return items.size();
    	}
     
    	/**
    	 * Returns the ID number of the transaction
    	 * @return ID number
    	 */
    	public int getID()
    	{
    		return idNumber;
    	}
     
    	/**
    	 * Adds an Item to the transaction
    	 * @param it Item to be added to transaction
    	 */
    	public void addItem(Item it)
    	{
    		items.add(it);
    	}
     
    	/**
    	 * Removes an Item from the transaction, and returns it (returns null
    	 * if the Item is not in the Transaction)
    	 * @param it Item to be removed
    	 * @return the Item removed, or null if the Item is not in transaction
    	 */
    	public Item voidItem(Item it)
    	{
    		for(Item temp : items)
    		{
    			if(temp.getID().compareTo(it.getID())==0)
    			{
    				items.remove(temp);
    				return temp;
    			}
    		}
    		return null;
    	}
     
    	/**
    	 * Sets the payment amount and type
    	 * @param in payment in
    	 * @param tp type of payment
    	 */
    	public void setPaymentIn(double in, String tp)
    	{
    		totalIn = in;
    		type = tp;
    	}
     
     
     
    	public static void main(String[] args)
    	{
    		Item it1 = new Item("SoBe","PN123456",1.59,.065);
    		Item it2 = new Item("Twinkie","AR23471",0.99,.065);
    		Item it3 = new Item("Apples","PROD33456",3.57, 0.00);
     
    		Transaction trans = new Transaction("Cash",10.00);
    		trans.addItem(it1);
    		trans.addItem(it2);
    		trans.addItem(it3);
     
    		System.out.printf("\nPayment In = $%.2f",trans.paymentIn());
    		System.out.printf("\nTaxes Paid = $%.2f",trans.getTaxPaid());
    		System.out.printf("\nSubtotal = $%.2f",trans.subTotal());
    		System.out.printf("\nTotal = $%.2f",trans.subTotal()+trans.getTaxPaid());
    		System.out.printf("\nChange Out = $%.2f",trans.changeOut());
    		System.out.printf("\nPayment Type:  %s",trans.getPaymentType());
    		System.out.printf("\nTransaction Number:  %s",trans.getID());
    		System.out.printf("\nNumber of Items = %i",trans.getTotalNumberOfItems());
     
     
    		ArrayList<Item> list = new ArrayList<Item>();
    		list.add(it1);
    		list.add(it2);
    		list.add(it3);
    		trans = new Transaction(list, "credit", 6.32);
    		System.out.println("\nNew Transaction:");
    		System.out.printf("\nPayment In = $%.2f",trans.paymentIn());
    		System.out.printf("\nTaxes Paid = $%.2f",trans.getTaxPaid());
    		System.out.printf("\nSubtotal = $%.2f",trans.subTotal());
    		System.out.printf("\nTotal = $%.2f",trans.subTotal()+trans.getTaxPaid());
    		System.out.printf("\nChange Out = $%.2f",trans.changeOut());
    		System.out.printf("\nPayment Type:  %s",trans.getPaymentType());
    		System.out.printf("\nTransaction Number:  %s",trans.getID());
    		System.out.printf("\nNumber of Items = %i",trans.getTotalNumberOfItems());
     
     
    		trans.voidItem(it2);
    		System.out.println("\nNew Transaction:");
    		System.out.printf("\nPayment In = $%.2f",trans.paymentIn());
    		System.out.printf("\nTaxes Paid = $%.2f",trans.getTaxPaid());
    		System.out.printf("\nSubtotal = $%.2f",trans.subTotal());
    		System.out.printf("\nTotal = $%.2f",trans.subTotal()+trans.getTaxPaid());
    		System.out.printf("\nChange Out = $%.2f",trans.changeOut());
    		System.out.printf("\nPayment Type:  %s",trans.getPaymentType());
    		System.out.printf("\nTransaction Number:  %s",trans.getID());
    		System.out.printf("\nNumber of Items = %i",trans.getTotalNumberOfItems());
    	}
    }

    Incomplete CashRegister class:
    package cashregister;
     
    import java.util.ArrayList;
     
    public class Cashregister {
    	private double startinCash;
    	private double currentcash;
    	private double totalchange;
    	private ArrayList<Transaction> transList;
     
    	public Cashregister(double startCash, double curCash, ArrayList<Transaction> tran)
    	{
    		startinCash = startCash;
    		currentcash = curCash;
    		transList = tran;
    	}
    	public double getStartingCash()
    	{
    		return startinCash;
    	}
    	public double getCurrentCash()
    	{
    		return currentcash;
    	}
    	public double getTotalChangeDispensed()
    	{
    		return totalchange;
    	}
    	public static void main(String[] args)
    	{
    		Cashregister cr1 = new Cashregister(25, 23.4, transList);
     
    		System.out.println(cr1);
    	}
     
     
     
    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    10
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Project help, kinda lost

    Just google "java main" -- you'll find an example of a main method straight away!

    For example, if I've got a class called "Car" with a method called start() that I want to test, I could save the following in Application.java and run it:

    public class Application {

    public static void main(String[] args) {
    Car car = new Car();
    car.start();
    }

    }
    Cave of Programming -- programming info and help, exercises, tutorials and other stuff.

Similar Threads

  1. Kinda different question
    By crazed8s in forum AWT / Java Swing
    Replies: 0
    Last Post: December 5th, 2010, 01:13 PM
  2. Im lost....
    By brale76578 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 31st, 2010, 08:09 AM
  3. lost
    By nyny in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2010, 07:32 AM
  4. I'm looking for some getting started kinda help
    By Matress in forum Member Introductions
    Replies: 2
    Last Post: April 14th, 2010, 10:58 AM
  5. help: union of two array,.. kinda problem
    By sanfor in forum Collections and Generics
    Replies: 2
    Last Post: November 29th, 2009, 01:33 PM