⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userinterface.java

📁 该程序有17个java源文件。是经典奥斯波特艺术品商人软件工程例子
💻 JAVA
字号:
package Osbert;
import java.io.*;

public class UserInterface {

  public static char getChar()
  //
  // returns a char from the keyboard
  //
  {

    char ch = ' ';	// char return value

    try
	{
	  ch = (char) (System.in.read ());
    }
    catch (Exception e)
    {
      System.out.println ("***** Error: getChar () *****");
      System.out.println ("\t" + e);
    }

    return ch;

  } // getChar

//----------------------------------------------------------------------------------------------------------------------------------------------------


  public static String getString()
  //
  // returns a string from the keyboard
  //
  {

    String message = new String();

	try
	{
      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
	  message = stdin.readLine();
      while(message.length() < 1)
        message = stdin.readLine();
	}
    catch (Exception e)
    {
		System.out.println ("***** Error: getString () *****");
		System.out.println ("\t" + e);
    }
    return message;

  }  // getString
//----------------------------------------------------------------------------------------------------------------------------------------------------

  public static int getInt()
  //
  // returns an integer from the keyboard
  //
  {
  	String tempStr = new String(); // temporary string value
  	int    ret;                    // converted int used as return value
  	tempStr = getString();
  	ret = Integer.parseInt(tempStr);
  	return ret;

  }  // getInt

//----------------------------------------------------------------------------------------------------------------------------------------------------

  public static double getDouble()
  //
  // returns a double from the keyboard
  //
  {
	String tempStr = new String();    // temporary string value
	double    ret;                    // converted int used as return value

	tempStr = getString();

	ret = Double.parseDouble(tempStr);

	return ret;

  }  // getDouble

//----------------------------------------------------------------------------------------------------------------------------------------------------

  public static void pressEnter()
  //
  // waits until the user presses the <ENTER> key
  //
  {
	byte choice[] = new byte[5];  // dummy variable used to induce user input

	try
	{
	  System.in.read (choice);
    }
    catch (Exception e)
    {
	  System.out.println ("***** Error: pressEnter () *****");
	  System.out.println ("\t" + e);
      } // try

  } // pressEnter

//----------------------------------------------------------------------------------------------------------------------------------------------------

  public static void clearScreen ()
  //
  // clearScreen clears the screen
  //
  {
    int	i;		// loop counter representing the number of blank lines to be printed

    //
    // Implementation-dependent code to clear the screen should replace the code
    // given below.
    //
    for (i = 0; i < 26; i++)
    {
      System.out.println ();
    }
  }  // clearScreen ()

//----------------------------------------------------------------------------------------------------------------------------------------------------

  public static void displayMainMenu ()
  //
  //displays the main menu containing all the options available to the user
  //
  throws IOException {
    boolean done = false;		// terminates while loop
    char choice;;				// user's choice
    GalleryPainting painting;   // painting object to be sold
    while (!done)
    {
      clearScreen ();

      System.out.println ("\t               MAIN MENU\n\n");
      System.out.println ("\t  Osbert Oglesby - Collector of Fine Art\n\n");
      System.out.println ("\t            1. Buy a Painting\n");
      System.out.println ("\t            2. Sell a Painting\n");
      System.out.println ("\t            3. Produce a Report\n");
      System.out.println ("\t            4. Quit\n\n");
      System.out.print ("Enter your choice and press <ENTER>: ");
      choice = getChar();
      switch (choice)
      {
          case '1':
            displayBuyPaintingMenu();
            break;

          case '2':
            painting = new GalleryPainting();
            painting.sell();
            break;

          case '3':
            displayReportMenu();
            break;

          case '4':
          case '\n':
            done = true;
            break;

          default:
            System.out.println ("\n\nNot a valid choice\n");
            System.out.println ("Press <ENTER> to return to menu...");
            pressEnter();
            break;

      } // switch
    } // while

  } //displayMainMenu

//----------------------------------------------------------------------------------------------------------------------------------------------------

  public static void displayBuyPaintingMenu ()
  //
  // allows user to select the type of painting to be purchased
  //
  throws IOException {

	boolean done = false;		// terminates do-loop
	char choice;				// user's choice
	Masterpiece	masterpiece;	// item to be bought
	Masterwork masterwork;		// item to be bought
	Other other;				// item to be bought
	Fashionability fash;		// item to be updated

	while (!done)
	{
	  clearScreen ();

	  System.out.println ("\t               BUY PAINTING MENU\n\n");
	  System.out.println ("\t  Osbert Oglesby - Collector of Fine Art\n\n");
	  System.out.println ("\t            1. Buy a Masterpiece\n");
	  System.out.println ("\t            2. Buy a Masterwork\n");
	  System.out.println ("\t            3. Buy an Other Painting\n");
	  System.out.println ("\t            4. Update Fashionability Coefficient\n");
	  System.out.println ("\t            5. Return to Main Menu\n\n");
	  System.out.print ("Enter your choice and press <ENTER>: ");
      choice = getChar();
      switch (choice)
      {

		  case '1':
		    masterpiece = new Masterpiece();
			masterpiece.buy();
			break;

		  case '2':
		    masterwork = new Masterwork();
			masterwork.buy();
			break;

		  case '3':
		    other = new Other();
			other.buy();
			break;

		  case '4':
		    fash = new Fashionability();
            fash.addNewFash();
            break;

          case '5':
		  case '\n':
			done = true;
			break;

		  default:
			System.out.println ("\n\nNot a valid choice\n");
			System.out.println ("Press <ENTER> to return to menu...");
            pressEnter();
			break;

      } // switch

	} // while

  } //displayBuyPaintingMenu

//----------------------------------------------------------------------------------------------------------------------------------------------------

  public static void displayReportMenu ()
  //
  // allows user to select the type of painting to be purchased
  //
  throws IOException {

	boolean done = false;		// terminates do-loop
	char choice;				// user's choice
	GalleryPainting	painting;	// item used to invoke report

	while (!done)
	{

	  clearScreen ();

	  System.out.println ("\t               REPORT MENU\n\n");
	  System.out.println ("\t  Osbert Oglesby - Collector of Fine Art\n\n");
	  System.out.println ("\t            1. Report on Bought Paintings\n");
	  System.out.println ("\t            2. Report on Sold Paintings\n");
	  System.out.println ("\t            3. Report on Trends\n");
	  System.out.println ("\t            4. Return to Main Menu\n\n");
	  System.out.print ("Enter your choice and press <ENTER>: ");

	  choice = getChar();

	  switch (choice)
	  {

		  case '1':
			PurchasesReport.printReport();
		    pressEnter();
			break;

		  case '2':
			SalesReport.printReport();
		    pressEnter();
			break;

		  case '3':
			ComputeFutureTrends.compute();
		    pressEnter();
			break;

		  case '4':
		  case '\n':
			done = true;
			break;

		  default:
			System.out.println ("\n\nNot a valid choice\n");
			System.out.println ("Press <ENTER> to return to menu...");
			pressEnter();
			break;

	  } // switch

	} // while

  } //displayReportMenu

//-------------------------------------------------------------------------------------------------------------------

  public static int compareStr (String str1, String str2)	// represents the two strings to compare
  //
  // compares two strings in the same manner as strcmp while ignoring any
  // question marks
  //
  {
	String temp1 = str1, temp2 = str2;	// represents strings void of any question marks

	//
	// remove all question marks from both strings
	//
	temp1 = removeChar (temp1, '?');
	temp2 = removeChar (temp2, '?');

	//
	// compare the two strings without question marks
	//
	return temp1.compareTo(temp2);

  } // compareStr

//-------------------------------------------------------------------------------------------------------------------

  private static String removeChar(String str, char c)
  //
  // remove all question marks from a string
  //
  {

     String temp = new String();

     for (int i = 0; i < str.length(); i ++)
	   if (str.charAt(i) != c) temp += str.charAt(i);

	 return temp;

  } // removeQ

//-------------------------------------------------------------------------------------------------------------------

  public static void initiateFiles()
  throws IOException {

    File fileExists;
	DataOutputStream createFile;

	fileExists = new File("fash.dat");
    if(!fileExists.exists())
    {
      createFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("fash.dat")));
      createFile.close();
    }

	fileExists = new File("artist.dat");
	if(!fileExists.exists())
	{
	  createFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("artist.dat")));
	  createFile.close();
	}

	fileExists = new File("sold.dat");
	if(!fileExists.exists())
	{
	  createFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("sold.dat")));
	  createFile.close();
	}

	fileExists = new File("gallery.dat");
	if(!fileExists.exists())
	{
	  createFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("gallery.dat")));
	  createFile.close();
	}

	fileExists = new File("auction.dat");
	if(!fileExists.exists())
	{
	  createFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("auction.dat")));
	  createFile.close();
	}

  }

//-------------------------------------------------------------------------------------------------------------------

  public static void addArtist (String fn, String ln)	// first/last name of artist to be added
  //
  // inserts an artist name into the artist file
  //
  throws IOException {
	DataInputStream inTmp, inCopy;		// stream objects used for file input
	DataOutputStream outTmp, outCopy;	// stream objects used for file output
	String tempFn;						// temporary string used for file copying
	String tempLn;						// temporary string used for file copying
	boolean found = false;				// indicates if artist insertion point found

	inTmp = new DataInputStream(new BufferedInputStream(new FileInputStream("artist.dat")));
	outTmp = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("tempArt.dat")));

    //
    // the following loop copies the current artist file to a temporary file
    //
    while (inTmp.available() != 0)
    {

		//
		// read a temporary name from the artist file
		//
		tempFn = inTmp.readUTF();
		tempLn = inTmp.readUTF();

		//
		// write the temporary name to the temporary file
		//
        outTmp.writeUTF(tempFn);
        outTmp.writeUTF(tempLn);

    }

    outTmp.close();
    inTmp.close();

	inCopy = new DataInputStream(new BufferedInputStream(new FileInputStream("tempArt.dat")));
	outCopy = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("artist.dat")));

	//
	// copy the temporary file to a new artist file
	// while inserting the new artist name in the proper location
	//
	while (inCopy.available() != 0)
	{

		//
		// read a temporary name from the temporary file
		//
		tempFn = inCopy.readUTF();
		tempLn = inCopy.readUTF();

	    //
		// write the proper record to the artist file in alphabetical order
		//
		if ((compareStr (tempLn, ln) < 0) || found)
		{
		  outCopy.writeUTF(tempFn);
		  outCopy.writeUTF(tempLn);
		}
		else
		  if (compareStr (tempLn, ln) > 0)
		  {
		    outCopy.writeUTF(fn);
		    outCopy.writeUTF(ln);
		    outCopy.writeUTF(tempFn);
		    outCopy.writeUTF(tempLn);
		    found = true;
		  }
		  else
		    if (compareStr (tempFn, fn) == 0)
		    {
			  outCopy.writeUTF(fn);
			  outCopy.writeUTF(ln);
			  found = true;
		    }
		    else
			  if (compareStr (tempFn, fn) < 0)
			  {
				outCopy.writeUTF(tempFn);
				outCopy.writeUTF(tempLn);
			  }
			  else
			  {
				outCopy.writeUTF(fn);
				outCopy.writeUTF(ln);
				outCopy.writeUTF(tempFn);
				outCopy.writeUTF(tempLn);
				found = true;
			  }

	} // while (inFile.available() != 0)

	inCopy.close ();

	//
	// write the artist name to the end of the artist file
	//
	if (!found)
	{
		outCopy.writeUTF(fn);
		outCopy.writeUTF(ln);
	}

	outCopy.close();

  } // addArtist


} // class UserInterface

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -