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 3 of 3

Thread: Java stock project help

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java stock project help

    i need help on a java project, i am still a beginner so i havent learnt alot of advanced techniques yet,i have taken a screenshot of the questions as it is hard to explain it.





    and i have done these, i am stuck on if the user enters choice 2 the program will display existing stock information and prompt user to update share price,help is appreciated

    /*Chin Zhi Qiang
     *Computer programming Project
     */
    import java.util.Scanner;
     
    	class ChinZhiQiang {
      		static final double capital = 100000.00;//starting capital
     
    		public static void main(String args[]){
    			/////Variables////
    			Scanner myScanner = new Scanner(System.in);
    			int choice;		
     
    		    /////Menu/////
    			System.out.println("\tStock Portfolio\t");
    			System.out.println("======================================");	
    			System.out.println("1) Select 1 to create stock portfolio.");
    			System.out.println("2) Select 2 to update share price.");
    			System.out.println("3) Select 3 to exit.");
    			/////Prompt Input/////
    			System.out.print("Please enter your choice(1 - 3): ");
    			choice = myScanner.nextInt();
     
    			if(choice == 1){
    				portFolio();
    			}
    			if(choice == 2){
    				updateSharePrice();
    			}
    			if(choice == 3){
    				System.out.println("Thanks for using");
    			}
    		}
     
    	static  void portFolio(){/////portFolio method/////
    		Scanner portFolioScanner = new Scanner(System.in);
    		int stocksToCreate =0;
    		double stockUnitPrice = 0;
    		int stockUnit = 0;
    		String stockName, stockSym;
    		int modify;
     
    			System.out.print("Number of stocks in your portfolio (max 99): ");
    			stocksToCreate = portFolioScanner.nextInt();
    			System.out.println();//line break
    				do
    				{
     
    					System.out.print("Please Enter Stock name ( between 8 and 14 characters ): ");
    					stockName = portFolioScanner.next();
    					System.out.print("Please Enter 3 or 4 letter stock symbol: ");
    					stockSym = portFolioScanner.next();
    					System.out.print("Please Enter unit price: ");
    					stockUnitPrice = portFolioScanner.nextDouble();
    					System.out.print("Please Enter no. of units bought: ");
    					stockUnit = portFolioScanner.nextInt();
    					System.out.println();// line break
     
    					System.out.println("You have entered: ");
    					System.out.println(stockName+"\t"+stockSym+"\t"+stockUnit+" units\t"+"@ "+stockUnitPrice);
     
    					System.out.print("If Ok, enter 1 else enter 0 to modify: ");
    					modify = portFolioScanner.nextInt();
    					System.out.println();
     
    				}while(modify == 0);
     
    			System.out.print("--- Accepted --- Cash Value Left $:");
    			System.out.print(capital - (stockUnit * stockUnitPrice));		
    			System.out.print("\n************************************\n");	
     
     
    	}/////End of portFolio method/////
     
    	static void updateSharePrice(){/////Start of updateSharePrice method/////
    			System.out.println("Here is your current list of stocks : \n");
    			System.out.print("\t\t\t\tBought Current Stock\n");
    			System.out.println("Stock Name\tSymbol\tUnits\tPrice\tPrice\tValue ");
    			System.out.print("----------\t------\t-----\t-----\t-----\t-----\n");
    		//	System.out.print(stockName);
     
     
    	}	
     
    }
    Last edited by lotus; July 4th, 2009 at 09:41 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Java stock project help

    Hello lotus,

    Sorry for the late reply on this.

    To start, you need to make it so the program doesn't terminate after each option is complete.
    To be able to do number 2, you need to take in and store the information from number 1 and continue from there.

    You can stop the program from terminating by putting a while loop in the main method like this:

    /*Chin Zhi Qiang
     *Computer programming Project
     */
    import java.util.Scanner;
     
        class ChinZhiQiang {
              static final double capital = 100000.00;//starting capital
     
            public static void main(String args[]){
                /////Variables////
                Scanner myScanner = new Scanner(System.in);
                int choice;        
     
                /////Menu/////
                System.out.println("\tStock Portfolio\t");
                System.out.println("======================================");    
                System.out.println("1) Select 1 to create stock portfolio.");
                System.out.println("2) Select 2 to update share price.");
                System.out.println("3) Select 3 to exit.");
                /////Prompt Input/////
                System.out.print("Please enter your choice(1 - 3): ");
     
                [B]while(myScanner.hasNext()){[/B]
     
                choice = myScanner.nextInt();
     
                if(choice == 1){
                    portFolio();
                }
                if(choice == 2){
                    updateSharePrice();
                }
                if(choice == 3){
                    System.out.println("Thanks for using");
                }
           [B] }[/B]
        }
        static  void portFolio(){/////portFolio method/////
            Scanner portFolioScanner = new Scanner(System.in);
            int stocksToCreate =0;
            double stockUnitPrice = 0;
            int stockUnit = 0;
            String stockName, stockSym;
            int modify;
     
                System.out.print("Number of stocks in your portfolio (max 99): ");
                stocksToCreate = portFolioScanner.nextInt();
                System.out.println();//line break
                    do
                    {
     
                        System.out.print("Please Enter Stock name ( between 8 and 14 characters ): ");
                        stockName = portFolioScanner.next();
                        System.out.print("Please Enter 3 or 4 letter stock symbol: ");
                        stockSym = portFolioScanner.next();
                        System.out.print("Please Enter unit price: ");
                        stockUnitPrice = portFolioScanner.nextDouble();
                        System.out.print("Please Enter no. of units bought: ");
                        stockUnit = portFolioScanner.nextInt();
                        System.out.println();// line break
     
                        System.out.println("You have entered: ");
                        System.out.println(stockName+"\t"+stockSym+"\t"+stockUnit+" units\t"+"@ "+stockUnitPrice);
     
                        System.out.print("If Ok, enter 1 else enter 0 to modify: ");
                        modify = portFolioScanner.nextInt();
                        System.out.println();
     
                    }while(modify == 0);
     
                System.out.print("--- Accepted --- Cash Value Left $:");
                System.out.print(capital - (stockUnit * stockUnitPrice));        
                System.out.print("\n************************************\n");    
     
     
        }/////End of portFolio method/////
     
        static void updateSharePrice(){/////Start of updateSharePrice method/////
                System.out.println("Here is your current list of stocks : \n");
                System.out.print("\t\t\t\tBought Current Stock\n");
                System.out.println("Stock Name\tSymbol\tUnits\tPrice\tPrice\tValue ");
                System.out.print("----------\t------\t-----\t-----\t-----\t-----\n");
            //    System.out.print(stockName);
     
     
        }    
     
    }
    I'm looking into part 2 now..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java stock project help

    i never thought anyone would reply , but after some modifications and some advice from my school teacher,
    the program is still not complete and has some errors.i do not understand how does your (while myScanner.hasnext()) works. so i have 3 if branches making up the 3 choices,and yea in order to do number 2 i need to have inputs from number 1 .
    /*Chin Zhi Qiang
     *Computer programming Project
     */
    import java.util.Scanner;
    import java.text.*;	
    	class ChinZhiQiang {//////Start of Class/////
      		static double capital = 100000.00;//starting capital
      		static int[] unitBought = new int[99];
    		static double[] unitPrice = new double[99];
    		static String[] stockName = new String[99]; 
    		static String[] stockSym = new String[99];	
      		static String[] portFolioS = new String[99];
      		static Scanner myScanner = new Scanner(System.in);
      		static double cashLeft = 0.00;
      		static double totalStock = 0;
      	public static void main(String args[]){//Main Method
    			/***Variables***/
    		int count1 = 0;//assign value of portFolio() into counter
    		boolean mod = true;
    		int myInt = 0;
    		int choice,i;//choice prompt and for loop init		
     
    		do
    		{
    			/////Menu/////
    			System.out.println("\tStock Portfolio\t");
    			System.out.println("======================================");	
    			System.out.println("1) Select 1 to create stock portfolio.");
    			System.out.println("2) Select 2 to update share price.");
    			System.out.println("3) Select 3 to exit.");
     
    			/////Prompt Choice/////
    			System.out.println();
    			System.out.print("Please enter your choice(1 - 3): ");
    			choice = myScanner.nextInt();
    			System.out.println("-------------------------------");
     
     
     
     
     
    			if(choice == 1){
     
    			count1 = portFolio();	
    			while(mod){
     
    				for( i = 0;i<count1;i++){
     
     
    			stockName[i] =	portFolioStockName();
    			stockSym[i]  =	portFolioStockSym();	
    			unitPrice[i] =	portFolioUnitPrice();
    			unitBought[i] = portFolioUnitBought();
    			portFolioSummary(i);
    			System.out.print("If Ok, enter 0 else enter 1 to modify :");
    			myInt = myScanner.nextInt();
                mod = (myInt != 0);
    			totalStock = unitPrice[i]*unitBought[i];
    			cashLeft = capital - totalStock;
     
     
    						}
     
    					}
    			System.out.println("---Accepted--- cash value left $:"+cashLeft);
     
    				}		
     
     
     
     
     
     
    				if(choice == 2){	
    				System.out.println("Here is your current list of stocks : \n");
    				System.out.print("\t\t\t\tBought Current Stock\n");
    				System.out.println("Stock Name\tSymbol\tUnits\tPrice\tPrice\tValue ");
    				System.out.print("----------\t------\t-----\t-----\t-----\t-----\n");
    				for(i = 0; i<count1;i++){
    					System.out.println(stockName[i]+"\t\t"+stockSym[i]+"\t"+unitBought[i]+"\t"+unitPrice[i]);
     
    				}
     
    			}
     
     
     
     
     
    				if(choice == 3){
    				for(i = 0;i<=count1;i++){
     
    				totalStock = unitBought[i]*unitPrice[i];
    				}
    				System.out.print("Current Stock Portfolio Worth\t"+totalStock);
    				System.out.println();
    				System.out.print("Cash = ");
    				System.out.println();
    				System.out.println("Total Worth = ");
    				System.out.println();
    				System.out.println("Thanks for using!");
    				System.out.println("**************************");
     
     
    			}
     
     
     
     
     
    		}while(choice <=3 && choice !=3);	
     
    }//End of Main method	
     
     
     
    			static int portFolio(){//portFolio method
     
    				int stocks;
    					System.out.print("Number of stocks in your portfolio (max 99): ");
    					stocks = myScanner.nextInt();
    					System.out.println();//line break	
    						return stocks;
    		}//End of portFolio Method
    			static String portFolioStockName(){//Start of portFolioInfo Method
    					boolean again = true;
    					String stockName;
    						do{
     
    						System.out.print("Please Enter Stock name ( between 8 and 14 characters ): ");
    						stockName = myScanner.next();
    						if(stockName.length()>14)
    							System.out.println("Only 14 letter max");
    							else{
     
    								again= false;
    							}
    						}while(again);
    							return stockName;
    			}//End of portFolioInfo Method			
     
    			static String portFolioStockSym(){//Start of portFolioStockSym Method
    					boolean again = true;
    					String stockSym;
    						do{
     
    						System.out.print("Please Enter 3 or 4 letter stock symbol: ");
    						stockSym = myScanner.next();
    						if(stockSym.length()>4)
    							System.out.println("Only 4 letter max");
    							else{
    								again = false;
    							}
    						}while(again);
     
    							return stockSym;
    			}//End of portFolioStockSym method			
     
    			static double portFolioUnitPrice(){//Start of portFolioUnitPrice method
     
    					double unitPrice;
    						System.out.print("Please Enter unit price: ");
    						unitPrice = myScanner.nextDouble();
    							return unitPrice;
    			}//End of portFolioUnitPrice Method
     
    			static int portFolioUnitBought(){//Start of portFolioUnitBought method
     
    					int	unitBought;
    						System.out.print("Please Enter no. of units bought: ");
    						unitBought = myScanner.nextInt();
    							return unitBought;
    			}//End of portFolioUnitBought method
     
    			static void portFolioSummary(int i ){//Start of portFolioSummary method
    				System.out.println("You have entered: ");
    				System.out.println(stockName[i]+"\t"+stockSym[i]+"\t"+unitPrice[i]+" units\t"+"@ "+unitPrice[i]*unitBought[i]);
    			}//End of portFolioSummary method
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    	}/////End of Class/////

Similar Threads

  1. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 06:47 PM
  2. Best way to architect my java and XML project...
    By samiles in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 16th, 2009, 09:43 AM