The Artima Developer Community
Sponsored Link

Java Answers Forum
How to remain at the main screen after perform some functions

4 replies on 1 page. Most recent reply: Feb 29, 2004 2:05 AM by leonglkw

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

How to remain at the main screen after perform some functions Posted: Feb 28, 2004 8:28 AM
Reply to this message Reply
Advertisement
Hi guys, i have almost completed my code but i still have some problem to tackle.
Pls consult and advise.

My problem is that the below program only allow the user to perform the function for one time only, after that it will automatically terminate the program.

Actually, what i want is that the program will go back to main screen again the first transaction and to allow the user to choose another transaction such as Sales or Daily Order or Collection.
And only when the user select E for Exit, then the program will terminate.

This is sample main screen:
ComputerMart, Madanne Amanda Smith
Category Favourite Qty Prepared Qty Sold Selling Price($)

--------------------------------------------------------------------- -----------

Computer [c1]DELL 50 0 1700
Computer [c2]Compaq 50 0 1200
Computer [c3]HP 50 0 1500
Printer [p1]Digitech 60 0 200
Printer [p2]HP 60 0 300
Scanner [s1]HP 70 0 150
Scanner [s2]Digitech 70 0 120
--------------------------------------------------------------------------- -----

[S]ales [D]aily Order [C]ollection [E]xit


>Option:


import java.io.*;
 
class Product {
	String[] category;
	String[] favourite;
	int[] qtyPrepared;
	int[] qtySold;
	int[] costPrice;
	int[] sellingPrice;
	int[] totalSellingPrice;
	int[] totalCostPrice;
	int[] difference;
	
	
}
 
 
class ComputerMart {
	public static void main(String[] args) throws IOException{
 
 
	// Declare computer product array and allocate storage
	String[] category = { "Computer", "Printer", "Scanner" };
	String[] favourite = { "[c1]DELL", "[c2]Compaq", "[c3]HP", "[p1]Digitech", "[p2]HP", "[s1]HP", "[s2]Digitech" };
	int[] qtyPrepared = { 50, 50, 50, 60, 60, 70, 70 };
	int[] qtySold = { 0, 0, 0, 0, 0, 0, 0 };
	int[] costPrice = { 1000, 900, 1200, 150, 180, 100, 80 };
	int[] sellingPrice = { 1700, 1200, 1500, 200, 300, 150, 120 };
	int[] totalSellingPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] totalCostPrice = { 0, 0, 0, 0, 0, 0, 0 };
	int[] difference = { 0, 0, 0, 0, 0, 0, 0 };
 
 
	// Display the main screen
	System.out.println("\n\n");
	System.out.println("\t\tComputerMart, Madanne Amanda Smith");
	System.out.println("\n");
	System.out.println("Category\tFavourite\tQty Prepared\tQty Sold\tSelling Price($)");
	System.out.println("--------------------------------------------------------------------------------");
	System.out.println( category[0] + "\t" + favourite[0] + "\t" + qtyPrepared[0] + "\t\t" + qtySold[0] + "\t\t" + sellingPrice[0]);
	System.out.println( category[0] + "\t" + favourite[1] + "\t" + qtyPrepared[1] + "\t\t" + qtySold[1] + "\t\t" + sellingPrice[1]);
	System.out.println( category[0] + "\t" + favourite[2] + "\t\t" + qtyPrepared[2] + "\t\t" + qtySold[2] + "\t\t" + sellingPrice[2]);
	System.out.println( category[1] + "\t\t" + favourite[3] + "\t" + qtyPrepared[3] + "\t\t" + qtySold[3] + "\t\t" + sellingPrice[3]);
	System.out.println( category[1] + "\t\t" + favourite[4] + "\t\t" + qtyPrepared[4] + "\t\t" + qtySold[4] + "\t\t" + sellingPrice[4]);
	System.out.println( category[2] + "\t\t" + favourite[5] + "\t\t" + qtyPrepared[5] + "\t\t" + qtySold[5] + "\t\t" + sellingPrice[5]);
	System.out.println( category[2] + "\t\t" + favourite[6] + "\t" + qtyPrepared[6] + "\t\t" + qtySold[6] + "\t\t" + sellingPrice[6]);
	System.out.println("--------------------------------------------------------------------------------");
        System.out.println("[S]ales\t\t[D]aily Order\t\t[C]ollection\t\t[E]xit");
        System.out.println("\n");
 
	
 
	// Get user input to make option of Sales function, Daily Order function, Collection function and Exit function
	InputStreamReader reader = new InputStreamReader(System.in);
	BufferedReader input = new BufferedReader(reader);
	System.out.print(">Option: ");
	String text = input.readLine();
	System.out.println("Your choice: " + text);
 
	if(text.equalsIgnoreCase("S")){
		System.out.println("Sales");
 
		try
		{
			String prdCode ="";
			String prdQuantity = "0";
			String morePrd ="";
		
		
			InputStreamReader inReader = new InputStreamReader(System.in);
			BufferedReader BuffReader = new BufferedReader(inReader);
			System.out.println("Items sold:-");
			while(true)
			{
				System.out.print("Enter product code: ");
				prdCode = BuffReader.readLine();
				System.out.print("Enter quantity: ");
				prdQuantity = BuffReader.readLine();
				int prdQty = new Integer(prdQuantity).intValue();
 
				if(prdCode.equalsIgnoreCase("c1")){
					qtyPrepared[0] = qtyPrepared[0] - prdQty;	
					qtySold[0] = qtySold[0] + prdQty;
					System.out.println("Quantity prepared =" + qtyPrepared[0]);
					System.out.print("Quantity Sold =" + qtySold[0]);
				}
				else if (prdCode.equalsIgnoreCase("c2")){
					qtyPrepared[1] = qtyPrepared[1] - prdQty;	
					qtySold[1] = qtySold[1] + prdQty;
					System.out.println("Quantity prepared =" + qtyPrepared[1]);
					System.out.print("Quantity Sold =" + qtySold[1]);
				}
				else if (prdCode.equalsIgnoreCase("c3")){
					qtyPrepared[2] = qtyPrepared[2] - prdQty;	
					qtySold[2] = qtySold[2] + prdQty;
					System.out.println("Quantity prepared =" + qtyPrepared[2]);
					System.out.print("Quantity Sold =" + qtySold[2]);
				}
				else if (prdCode.equalsIgnoreCase("p1")){
					qtyPrepared[3] = qtyPrepared[3] - prdQty;	
					qtySold[3] = qtySold[3] + prdQty;
					System.out.println("Quantity prepared =" + qtyPrepared[3]);
					System.out.print("Quantity Sold =" + qtySold[3]);
				}
				else if (prdCode.equalsIgnoreCase("p2")){
					qtyPrepared[4] = qtyPrepared[4] - prdQty;	
					qtySold[4] = qtySold[4] + prdQty;
					System.out.println("Quantity prepared =" + qtyPrepared[4]);
					System.out.print("Quantity Sold =" + qtySold[4]);
				}
				else if (prdCode.equalsIgnoreCase("s1")){
					qtyPrepared[5] = qtyPrepared[5] - prdQty;	
					qtySold[5] = qtySold[5] + prdQty;
					System.out.println("Quantity prepared =" + qtyPrepared[5]);
					System.out.print("Quantity Sold =" + qtySold[5]);
				}
				else if (prdCode.equalsIgnoreCase("s2")){
					qtyPrepared[6] = qtyPrepared[6] - prdQty;	
					qtySold[6] = qtySold[6] + prdQty;
					System.out.println("Quantity prepared =" + qtyPrepared[6]);
					System.out.print("Quantity Sold =" + qtySold[6]);
				}
				else
					System.out.println("Wrong option!");
				
				System.out.print("\n");
				System.out.print("More items[Y/N]?:");
				morePrd = BuffReader.readLine();
				if(morePrd.equalsIgnoreCase("N"))
			 		break;
				else
			 		continue;
			
			}
		}
		catch(Exception e )
		{
			System.out.println("ERROR " + e);
		}
 
	}
	else if (text.equalsIgnoreCase("D")){
		System.out.println("Daily Order");
		
		try
		{
			String prdCode ="";
			String prdQuantity = "0";
			String morePrd ="";
		
		
			InputStreamReader inReader = new InputStreamReader(System.in);
			BufferedReader BuffReader = new BufferedReader(inReader);
			System.out.println("Items ordered:-");
			while(true)
			{
				System.out.print("Enter product code: ");
				prdCode = BuffReader.readLine();
				System.out.print("Enter quantity: ");
				prdQuantity = BuffReader.readLine();
				int prdQty = new Integer(prdQuantity).intValue();
 
				if(prdCode.equalsIgnoreCase("c1")){
					qtyPrepared[0] = qtyPrepared[0] + prdQty;	
					System.out.println("Quantity prepared =" + qtyPrepared[0]);
				}
				else if (prdCode.equalsIgnoreCase("c2")){
					qtyPrepared[1] = qtyPrepared[1] + prdQty;	
					System.out.println("Quantity prepared =" + qtyPrepared[1]);
				}
				else if (prdCode.equalsIgnoreCase("c3")){
					qtyPrepared[2] = qtyPrepared[2] + prdQty;	
					System.out.println("Quantity prepared =" + qtyPrepared[2]);
				}
				else if (prdCode.equalsIgnoreCase("p1")){
					qtyPrepared[3] = qtyPrepared[3] + prdQty;	
					System.out.println("Quantity prepared =" + qtyPrepared[3]);
				}
				else if (prdCode.equalsIgnoreCase("p2")){
					qtyPrepared[4] = qtyPrepared[4] + prdQty;	
					System.out.println("Quantity prepared =" + qtyPrepared[4]);
				}
				else if (prdCode.equalsIgnoreCase("s1")){
					qtyPrepared[5] = qtyPrepared[5] + prdQty;	
					System.out.println("Quantity prepared =" + qtyPrepared[5]);
				}
				else if (prdCode.equalsIgnoreCase("s2")){
					qtyPrepared[6] = qtyPrepared[6] + prdQty;	
					System.out.println("Quantity prepared =" + qtyPrepared[6]);
				}
				else
					System.out.println("Wrong Product Code Entered!!!");
				
				
				System.out.print("More items[Y/N]?:");
				morePrd = BuffReader.readLine();
				if(morePrd.equalsIgnoreCase("N"))
			 		break;
				else
			 		continue;
			
			}
		}
		catch(Exception e )
		{
			System.out.println("ERROR " + e);
		}		
	}
	else if (text.equalsIgnoreCase("C")){
		System.out.println("Collection");
		
		int sumTotalCostPrice = totalCostPrice[0] + totalCostPrice[1] + totalCostPrice[2] + totalCostPrice[3] + totalCostPrice[4] + totalCostPrice[5] + totalCostPrice[6];		
		int sumTotalSellingPrice = totalSellingPrice[0] + totalSellingPrice[1] + totalSellingPrice[2] + totalSellingPrice[3] + totalSellingPrice[4] + totalSellingPrice[5] + totalSellingPrice[6];
		int sumDifference = sumTotalSellingPrice - sumTotalCostPrice;
 
        	System.out.println("\n\n");
		System.out.println("\t\tComputerMart, Madanne Amanda Smith");
		System.out.println("\t\t\tDaily Sales Collection");
		System.out.println("\n");
		System.out.println("\t\t\t\tQuantity\tTotal Price($)");
		System.out.println("Category\tFavourite   Prepared   Sold\tCost  Selling\tDifference($)");
		System.out.println("--------------------------------------------------------------------------------");
		System.out.println(category[0] + "\t" + favourite[0] + "\t" + qtyPrepared[0] + "\t" + qtySold[0] + "\t" + totalCostPrice[0] + "\t" + totalSellingPrice[0] + "\t" + difference[0]);
		System.out.println(category[0] + "\t" + favourite[1] + "\t" + qtyPrepared[1] + "\t" + qtySold[1] + "\t" + totalCostPrice[1] + "\t" + totalSellingPrice[1] + "\t" + difference[1]);
		System.out.println(category[0] + "\t" + favourite[2] + "\t\t" + qtyPrepared[2] + "\t" + qtySold[2] + "\t" + totalCostPrice[2] + "\t" + totalSellingPrice[2] + "\t" + difference[2]);
		System.out.println(category[1] + "\t\t" + favourite[3] + "\t" + qtyPrepared[3] + "\t" + qtySold[3] + "\t" + totalCostPrice[3] + "\t" + totalSellingPrice[3] + "\t" + difference[3]);
		System.out.println(category[1] + "\t\t" + favourite[4] + "\t\t" + qtyPrepared[4] + "\t" + qtySold[4] + "\t" + totalCostPrice[4] + "\t" + totalSellingPrice[4] + "\t" + difference[4]);
		System.out.println(category[2] + "\t\t" + favourite[5] + "\t\t" + qtyPrepared[5] + "\t" + qtySold[5] + "\t" + totalCostPrice[5] + "\t" + totalSellingPrice[5] + "\t" + difference[5]);
		System.out.println(category[2] + "\t\t" + favourite[6] + "\t" + qtyPrepared[6] + "\t" + qtySold[6] + "\t" + totalCostPrice[6] + "\t" + totalSellingPrice[6] + "\t" + difference[6]);
		System.out.println("--------------------------------------------------------------------------------");
        	System.out.println("Total" + "\t\t\t\t\t\t" + sumTotalCostPrice + "\t" + sumTotalSellingPrice + "\t" + sumDifference);
        	System.out.println("\n");
	}
	else if (text.equalsIgnoreCase("E")){
		System.out.println("Exit");
		System.exit(0);
	}
	else
		System.out.println("Wrong option!");
	
 
	
		
	
	}
}


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How to remain at the main screen after perform some functions Posted: Feb 28, 2004 3:29 PM
Reply to this message Reply
The short answer is to put a loop around the code that you want to repeat. You probably would want a do-while loop, since that will guarantee that the code runs at least once. Simply test to see if the user entered "E".
do{
 
//code that needs to repeat goes in here
 
}while(text.equalsIgnoreCase("E"));


You can also delete the if(text.equalsIgnoreCase("E")) and put the code in its block outside the loop.

The long answer is in the next post.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: How to remain at the main screen after perform some functions Posted: Feb 28, 2004 3:32 PM
Reply to this message Reply
I'm going to move the long answer to my reply to your question about subfunctions.

Sorry for any confusion.

tom

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How to remain at the main screen after perform some functions Posted: Feb 29, 2004 2:02 AM
Reply to this message Reply
Thanks a lot.

leonglkw

Posts: 43
Nickname: leonglkw
Registered: Feb, 2004

Re: How to remain at the main screen after perform some functions Posted: Feb 29, 2004 2:05 AM
Reply to this message Reply
Hi TWC,

i forgot to tell u that, in fact, if possible, i would to have the read the data in the array from I/O file.
But i'm trying how to implement it.

Flat View: This topic has 4 replies on 1 page
Topic: Tomcat 4 --> IIS ? Previous Topic   Next Topic Topic: Tomcat server with IIS

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use