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
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!" );
}
}