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: Code problems

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Code problems

    I'm having a bit of trouble fixing some things in my program. I can't figure out how to get the menu to repeat itself after a selection has been made. If I lock the first register I cannot display its state. My math is wrong. By this I believe that when I enter an amount it stores both cash register one and two in the same location. So when I add one, I'm actually adding 2. I also can't figure out how to fix my toString method. It displays the same thing for register one and two.

    Cash Register Class
    package proj1;
     
    public class CashRegister {
     
    	private int numOnes;
    	private int numFives;
    	private int numTens;
    	private int numTwenties;
    	boolean isLocked;
     
    	public CashRegister(int numOnes, int numFives,int numTens, int numTwenties, boolean isLocked )
    	{
    		this.numOnes = numOnes;
    		this.numFives = numFives;
    		this.numTens = numTens;
    		this.numTwenties = numTwenties;
    		this.isLocked = isLocked;
    	}
     
    	public CashRegister(int numOnes, int numFives,int numTens, int numTwenties)
    	{
    		this.numOnes = numOnes;
    		this.numFives = numFives;
    		this.numTens = numTens;
    		this.numTwenties = numTwenties;
    	}
     
    	/*public CashRegister(CashRegister original)
    	{
    		this.numOnes = original.numOnes;
    		this.numFives = original.numFives;
    		this.numTens = original.numTens;
    		this.numTwenties = original.numTwenties;
    	}*/
     
    	public void CashReg(int startOnes,int startFives,int startTens,int startTwenties)
    	{
    		numOnes = startOnes;
    		numFives = startFives;
    		numTens = startTens;
    		numTwenties = startTwenties;
    	}
     
    	public int getOnes()
    	{
    		return numOnes;
    	}
     
    	public int getFives()
    	{
    		return numFives;
    	}
     
    	public int getTens()
    	{
    		return numTens;
    	}
     
    	public int getTwenties()
    	{
    		return numTwenties;
    	}
     
    	public void addMoney(int ones, int fives, int tens, int twenties)
    	{
    		if(isLocked == false)
    		{
    			numOnes += ones;
    			numFives += fives;
    			numTens += tens;
    			numTwenties += twenties;
    		}
     
    		else
    		{
    			throw new RuntimeException("The register is locked.");
    		}
     
    		if(numOnes < 0 || numFives < 0 || numTens < 0 || numTwenties < 0)
    		{
    			throw new RuntimeException("You have a negative amount.");
    		}
    	}
     
    	public void removeMoney(int ones, int fives, int tens, int twenties)
    	{
    		if(isLocked == false)
    		{
    			numOnes -= ones;
    			numFives -= fives;
    			numTens -= tens;
    			numTwenties -= twenties;
    		}
     
    		else
    		{
    			throw new RuntimeException("The register is locked.");
    		}
     
    		if(numOnes < 0 || numFives < 0 || numTens < 0 || numTwenties < 0)
    		{
    			throw new RuntimeException("You have a negative amount.");
    		}
    	}
     
    	public void lock()
    	{
    		isLocked = true;
    	}
     
    	public void unlock()
    	{
    		isLocked = false;
    	}
     
    	public String toString()
    	{
    		String str = "Register One has:\n" +  "Ones: " +numOnes +"\nFives: ";
    		str += numFives + "\nTens: " + numTens + "\nTwenties: " + numTwenties;
    		str += "\nRegister Two has:\n"  +  "Ones: " + numOnes +"\nFives: ";
    		str += numFives + "\nTens:" + numTens + "\nTwenties: " + numTwenties;
    		str += "\nRegister One is " + isLocked;
    		str += "\nRegister Two is " + isLocked;
    		return str;	
    	}
     
    }

    Main Class
    package proj1;
    import java.util.Scanner;
    public class Project1{
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String selection = "";
    		String registerState = "";
    		int register = 0;
    		int registerTransfer = 0;
    		int registerLocked = 0;
    		int registerUnlock = 0;
    		Scanner scan = new Scanner(System.in);
     
    		System.out.println("Enter the amount of ones: ");
    		int ones = scan.nextInt();
     
    		System.out.println("Enter the amount of fives: ");
    		int fives = scan.nextInt();
     
    		System.out.println("Enter the amount of tens: ");
    		int tens = scan.nextInt();
     
    		System.out.println("Enter the amount of twenties: ");
    		int twenties = scan.nextInt();
     
    		CashRegister cr1 = new CashRegister(ones, fives, tens, twenties);
    		CashRegister cr2 = new CashRegister(ones, fives, tens ,twenties);
     
    		System.out.println("What would you like to do:");
     
     
    		do
    		{
    			System.out.println("A - Add money");
    			System.out.println("R - Remove money");
    			System.out.println("T - Transfer money");
    			System.out.println("L - Lock register");
    			System.out.println("U - Unlock register");
    			System.out.println("S - Display register state");
    			System.out.println("C - Close store and quit");
    			selection = scan.next();
     
    			cr1.addMoney(ones, fives, tens, twenties);
     
    			if( selection.equalsIgnoreCase("A"))
    			 {
    				 System.out.println("Which Cash Register do you want to add to (1,2)");
    				 register = scan.nextInt();
     
    				 System.out.println("How many ones do you want to add?");
    				 ones = scan.nextInt();
     
    				 System.out.println("How many fives do you want to add?");
    				 fives = scan.nextInt();
     
    				 System.out.println("How many tens do you want to add?");
    				 tens = scan.nextInt();
     
    				 System.out.println("How many twenties do you want to add?");
    				 twenties = scan.nextInt();
     
    				 if(register == 1)
    				 {
    					 cr1.addMoney(ones, fives, tens, twenties);
    				 }
    				 else if (register == 2)
    				 {
    					 cr2.addMoney(ones, fives, tens, twenties);
    				 }
     
    				 else
    				 {
    					 throw new RuntimeException("Choose either register one or two");
    				 }
    			 }//ends selection A
     
    			 if(selection.equalsIgnoreCase("R"))
    			 {
    				 System.out.println("Which Cash Register do you want to subtract from (1,2)");
    				 register = scan.nextInt();
     
    				 System.out.println("How many ones do you want to remove?");
    				 ones = scan.nextInt();
     
    				 System.out.println("How many fives do you want to remove?");
    				 fives = scan.nextInt();
     
    				 System.out.println("How many tens do you want to remove?");
    				 tens = scan.nextInt();
     
    				 System.out.println("How many twenties do you want to remove?");
    				 twenties = scan.nextInt();
     
    				 if(register == 1)
    				 {
    					 cr1.removeMoney(ones, fives, tens, twenties);
    				 }
     
    				 else if (register == 2)
    				 {
    					 cr2.removeMoney(ones, fives, tens, twenties);
    				 }
     
    				 else
    				 {
    					 throw new RuntimeException("Choose either register one or two");
    				 }
    			 }
     
    			 if(selection.equalsIgnoreCase("T"))
    			 {
    				 System.out.println("Which register do you want to transfer money from (1,2)");
    				 registerTransfer = scan.nextInt();
     
    				 System.out.println("Enter the amount of ones:");
    				 ones = scan.nextInt();
     
    				 System.out.println("Enter the amount of fives:");
    				 fives = scan.nextInt();
     
    				 System.out.println("Enter the amount of tens:");
    				 tens = scan.nextInt();
     
    				 System.out.println("Enter the amount of twenties:");
    				 twenties = scan.nextInt();
     
    				 if(registerTransfer == 1)
    				 {
    					cr1.removeMoney(ones, fives, tens, twenties);
    					cr2.addMoney(ones, fives, tens, twenties);
    				 }
     
    				 else if(registerTransfer == 2)
    				 {
    					 cr2.removeMoney(ones, fives, tens, twenties);
    					 cr1.addMoney(ones, fives, tens, twenties);
    				 }
     
    				 else
    				 {
    					 throw new RuntimeException("Choose either register one or two");
    				 }
    			 }//ends selection T
     
    			 if(selection.equalsIgnoreCase("L"))
    			 {
    				 System.out.println("Which register do you want to lock (1,2)");
    				 registerLocked = scan.nextInt();
     
    				 if(registerLocked == 1)
    				 {
    					 cr1.lock();
    				 }
     
    				 else if(registerLocked == 2)
    				 {
    					 cr2.lock();
    				 }
     
    				 else
    				 {
    					 throw new RuntimeException("Choose either register one or two");
    				 }
     
    			 }//ends selection L
     
    			 if(selection.equalsIgnoreCase("U"))
    			 {
    				 System.out.println("Which register do you want to unlock (1,2)");
    				 registerUnlock = scan.nextInt();
     
    				 if(registerUnlock == 1)
    				 {
    					 cr1.unlock();
    				 }
     
    				 else if (registerUnlock == 2)
    				 {
    					 cr2.unlock();
    				 }
     
    				 else
    				 {
    					 throw new RuntimeException("Choose either register one or two");
    				 }
     
    			 }//ends selection U
     
    			 if(selection.equalsIgnoreCase("S"))
    			 {
    				 System.out.println(cr1.toString());
    				 registerState= scan.next();
    				 System.out.println(cr2.toString());
    				 registerState= scan.nextLine();
    			 }//ends selection S
     
    			 else if(selection.equalsIgnoreCase("C"))
    			 {
    				 cr1.lock();
    				 cr2.lock();
    				 cr1.CashReg(0, 0, 0, 0);
    				 cr2.CashReg(0, 0, 0, 0);
    				 System.out.println("The store is closed:\n"+ cr1.toString());	 
    			 }//ends selection C
     
    		} while(!selection.equalsIgnoreCase("C"));
     
    	}
     
    }


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Code problems

    D

Similar Threads

  1. problems with my code
    By poppe in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2012, 09:10 AM
  2. Problems with my code
    By poppe in forum Member Introductions
    Replies: 1
    Last Post: January 26th, 2012, 04:53 AM
  3. Problems with a code
    By oriol in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2011, 07:02 PM
  4. phone book code problems
    By mu'min in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 16th, 2010, 10:36 AM
  5. Problems with Code?
    By Sean137 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2010, 02:15 PM