Results 1 to 2 of 2
  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need help with If and Else statements quick please for Stocks problem!!!

    Another strategy to validate the user input is to test the attribute value in its corresponding mutator method. Use the rules for valid data from exercise 1. If the value of the attribute is valid, save the new value in the appropriate attribute and return a value of true. If the value is invalid, return a value of false. Do not print an error message in the mutator method! Modify the mutators for price and number of shares as described above.

    However there are no mutators for name and symbol as they are set in the constructor. While a constructor can contain if statements, there is no return value.

    Add a default constructor to the Stock class. Set the value of the name and symbol to empty string.

    Modify the code in the main method of TradeStock class to use the default constructor to create an empty object.

    Then use the mutators to validate the user-entered data. Test the return value to determine if the user input is valid. If the return value is false, print the same error messages as in Exercise 1 and terminate the program.

    Exercise 1:

    Java Code:
    
    public class Stock
    {
    	private String name;		//name of company
    	private String symbol;	//Symbol of company on Stock Exchange
    	private double price;	//price per share
    	private int    shares;	//number of shares
    	
    	/**
    		Constructor with name and symbol
    	*/
    	public Stock(String newName, String newSymbol)
    	{
    		name   = newName;
    		symbol = newSymbol;
    		price  = 0.0;
    		shares = 0;
    	}
    	
    	/**
    		Get name of stock
    		@return name of stock
    	*/
    	public String getName( )
    	{
    		return name;
    	}
    	
    	/**
    		Get symbol of stock
    		@return symbol of stock
    	*/
    	public String getSymbol( )
    	{
    		return symbol;
    	}
    	
    	/**
    		Get price of stock
    		@return price of stock
    	*/
    	public double getPrice( )
    	{
    		return price;
    	}
    	
    	/**
    		Get number of shares
    		@return number of shares
    	*/
    	public int getShares( )
    	{
    		return shares;
    	}
    	
    	/**
    		Set price of shares
    		@param newPrice price of shares
    	*/
    	public void setPrice(double newPrice)
    	{
    		price = newPrice;
    	}
    	
    	/**
    		Set number of shares
    		@param newShares number of shares
    	*/
    	public void setShares(int newShares)
    	{
    		shares = newShares;
    	}
    }
    Java Code:
    import java.util.*;
    
    public class TradeStock
    {
    	public static void main(String[] args)
    	{
    	
    		//declarations
    		Scanner in = new Scanner(System.in);
    		String name;	//Name of stock
    		String symbol;	//Symbol of stock
    		double price;	//price per share of stock
    		int    shares;	//number of shares of stock
    		
    	
    				
    		
    		//Get name and symbol
    		System.out.print("Enter name of stock: ");
    		name = in.nextLine( );
    		if (name.length()< 1)
    			{
    				System.out.println("Invalid name - Must be greater than 1 character");
    				System.exit(0);
    			}
    		else
    			{
    				name = name;
    			}
    		System.out.print("Get symbol of stock: ");
    		symbol = in.nextLine( );
    		if ((symbol.length() >= 3)&&(symbol.length() <= 6)) 
    			{
    				symbol = symbol;
    			}
    		else
    			{
    				System.out.println("Invalid symbol of stock - Must be between 3 and 6 charactacter, inclusively");
    				System.exit(0);
    			}	
    
    		
    		//create Stock object with price and symbol
    		Stock myStock = new Stock(name, symbol);
    		
    		//prompt user for price and set it
    		System.out.print("Enter price of stock: ");
    		price = in.nextDouble( );
    		if (price > 0.0)
    			{
    				price = price;
    			}
    		else
    			{
    				System.out.println("Invalid Price - must be greater than 0.");
    				System.exit(0);
    			}
    		myStock.setPrice(price);
    		
    		//prompt user for number of shares and set it
    		System.out.print("Enter number of shares: ");
    		shares = in.nextInt( );
    		if (((shares >= 10)&&(shares <= 1000))&&((shares%10) == 0))
    			{
    				shares = shares;
    			}
    		else
    			{
    				System.out.println("Invalid number of shares - Must be between 10 and 1000, inclusively and a multiple of 10");
    				System.exit(0);
    			}
    		myStock.setShares(shares);
    		
    		//print all stock information
    		//uses space needed for each output value
    		System.out.printf("%nName:   %s%n" ,myStock.getName());
    		System.out.printf("Symbol: %s%n"  , myStock.getSymbol());
    		System.out.printf("Price:  %.2f%n", myStock.getPrice());
    		System.out.printf("Shares: %d%n"  , myStock.getShares());
    	
    	}
    }
    Exercise 2 so far, i have set up the empty strings, added the new get methods for name and symbol and I have tried to use the if and else statement in price and it is not working. I dont understand how to do it:
    Java Code:
    
    public class Stock
    {
    	private String name;		//name of company
    	private String symbol;	//Symbol of company on Stock Exchange
    	private double price;	//price per share
    	private int    shares;	//number of shares
    	
    	/**
    		Constructor with name and symbol
    	*/
    	public Stock()
    	{
    		name   = "";
    		symbol = "";
    		price  = 0.0;
    		shares = 0;
    	}
    	
    	/**
    		Get name of stock
    		@return name of stock
    	*/
    	public String getName( )
    	{
    		return name;
    	}
    	
    	/**
    		Get symbol of stock
    		@return symbol of stock
    	*/
    	public String getSymbol( )
    	{
    		return symbol;
    	}
    	
    	/**
    		Get price of stock
    		@return price of stock
    	*/
    	public double getPrice( )
    	{
    		return price;
    	}
    	
    	/**
    		Get number of shares
    		@return number of shares
    	*/
    	public int getShares( )
    	{
    		return shares;
    	}
    	
    	/**
    		Set price of shares
    		@param newPrice price of shares
    	*/
    	public boolean setPrice(double newPrice)
    	{
    		if (price > 0.0)
    			{
    				price = price;
    				return true;
    			}
    		else
    			{
    				return false;
    			}
    
    	}
    	
    	/**
    		Set number of shares
    		@param newShares number of shares
    	*/
    	public void setShares(int newShares)
    	{
    		shares = newShares;
    	}
    	
    	public void setName(String newName)
    	{
    		name = newName;
    	}
    	
    	public void setSymbol(String newSymbol)
    	{
    		symbol = newSymbol;
    	}
    }
    Java Code:
    import java.util.Scanner;
    
    public class TradeStock
    {
    	public static void main(String[] args)
    	{
    	
    		//declarations
    		Scanner in = new Scanner(System.in);
    		String name;	//Name of stock
    		String symbol;	//Symbol of stock
    		double price;	//price per share of stock
    		int    shares;	//number of shares of stock
    		
    	
    		Stock myStock = new Stock();		
    		
    		//Get name and symbol
    		System.out.print("Enter name of stock: ");
    		name = in.nextLine( );
    		if (name.length()< 1)
    			{
    				System.out.println("Invalid name - Must be greater than 1 character");
    				System.exit(0);
    			}
    		else
    			{
    				name = name;
    			}
    		myStock.setName(name);
    		
    		System.out.print("Get symbol of stock: ");
    		symbol = in.nextLine( );
    		if ((symbol.length() >= 3)&&(symbol.length() <= 6)) 
    			{
    				symbol = symbol;
    			}
    		else
    			{
    				System.out.println("Invalid symbol of stock - Must be between 3 and 6 charactacter, inclusively");
    				System.exit(0);
    			}	
    	myStock.setSymbol(symbol);
    		
    		//create Stock object with price and symbol
    		
    		
    		//prompt user for price and set it
    		System.out.print("Enter price of stock: ");
    		price = in.nextDouble( );
    		myStock.setPrice(price);
    		while(false)
    			{
    				System.out.println("Invalid Price - must be greater than 0.");
    				System.exit(0);
    			}	
    		
    		//prompt user for number of shares and set it
    		System.out.print("Enter number of shares: ");
    		shares = in.nextInt( );
    		if (((shares >= 10)&&(shares <= 1000))&&((shares%10) == 0))
    			{
    				shares = shares;
    			}
    		else
    			{
    				System.out.println("Invalid number of shares - Must be between 10 and 1000, inclusively and a multiple of 10");
    				System.exit(0);
    			}
    		myStock.setShares(shares);
    		
    		//print all stock information
    		//uses space needed for each output value
    		System.out.printf("%nName:   %s%n" ,myStock.getName());
    		System.out.printf("Symbol: %s%n"  , myStock.getSymbol());
    		System.out.printf("Price:  %.2f%n", myStock.getPrice());
    		System.out.printf("Shares: %d%n"  , myStock.getShares());
    	
    	}
    }
    Thank you!!!

  2. #2
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Need help with If and Else statements quick please for Stocks problem!!!

    This looks like you posted a homework assignment for us to do. I'm not clear on what your question is. I would recommend narrowing down exactly what your having trouble with and then post a more focused question. I think this would make it allot easier for people to help you out and would probably be a better way for you to learn.

    Example if you have trouble with a if else statement like the title would suggest. Make a small program with a if else statement that is similar to something you need in your project. If it doesn't seem to work logically or how you thought it would end up post on hear and we might be able to help you understand how it work.
    Last edited by killutch; 10-26-2012 at 09:05 AM.

Similar Threads

  1. Quick sort problem
    By fishy8158 in forum New To Java
    Replies: 0
    Last Post: 02-18-2012, 05:21 AM
  2. If statements and arrays, problem
    By Madlollipop in forum New To Java
    Replies: 3
    Last Post: 11-12-2011, 01:30 PM
  3. quick problem
    By Boomer1 in forum New To Java
    Replies: 4
    Last Post: 11-14-2009, 06:13 AM
  4. Stocks.
    By Alex89 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 11:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •