Programming Exercise II: My Grocery Shopping (I/O)

(Industry-Level, Second-to-None Comprehensive Specifications)


Absolutely no copying others’ works
According to a study, students in computer-science courses learn much more by building large-scale exercises instead of many small-scale test programs, which give fragmented knowledge contrary to solid understanding of the language.
However, not to overwhelm students by the large size, the exercise is divided into several sub-exercises. This is the first part of a bus-itinerary exercise. Together, they give students an understanding of a large-size exercise development experience.
Though these exercises are related, you are NOT allowed to submit one exercise to cover two or more exercises.
Development Requirements
When start developing the exercise, follow the requirements below: Due Date and Submission Methods
Due on or before Monday, September 27, 2021:


Objective
This is the first part of the problem of finding a bus itinerary, which could be very complicated. The exercise is made simple on purpose, and its objective is to have students practice Java I/O programming. Keep in mind that the only effective way to learn a programming language is practicing, instead of studying concepts or writing some testing programs.
No pain, no gain 😂


Requirements
This is the first part of a bus-itinerary problem, which is to read, save, and list data. The exercise includes the following requirements:

Programming Hints
The four essential components of software are (i) algorithms, (ii) data structures, (iii) programming languages, and (iv) code, where algorithms are the most critical one. Finding a best bus itinerary is very complicated, and is most likely an NP-complete problem, which may not be solved in polynomial time. That is you have to find an approximation method to solve the problem because if it is not carefully planned, a brute-force or exhaustive method may take infinite time to find the answer. However, a brute-force or exhaustive method may just work well for a small amount of data.

This exercise is only for input and output, so you do not need to worry about the complexity. Using appropriate data structures for this exercise could save a great deal of effort from you. An example of using the dynamic array ArrayList could be found at A travel-agent case study.



A User Interface
An example of the exercise’s interface is shown below:


Plagiarism Checking
If the web interfaces are used, the instructor has the following requirements. It is for the instructor to find any plagiarism. Each interface includes a button “Display source,” which is to list ALL the source code for implementing the functions of this interface. Only one password is for all exercises and interfaces. The system will be highly suspected if fail to implement this button. The source code will be studied carefully for any suspected plagiarism. Besides, the exercise is suspicious if the results are substantially different from the assumed results from the code. The interface can be found from here.

~/public_html/course/280/exercise/1/check.html
Password:       Interface:                    
~/public_html/cgi-bin/280/exercise/1/Check.pl
#!/usr/bin/perl
use CGI;
$query     = new CGI;
$act       = $query->param( 'act' );
$password  = $query->param( 'password' );
$interface = $query->param( 'interface' );

if ( $act eq "Display source" ) {
  # Remove leading and trailing spacing.
  $password  =~ s/^\s*(\S*)\s*$/$1/;
  $interface =~ s/^\s*(\S*)\s*$/$1/;

  # For security, remove some Unix metacharacters.
  $password  =~ s/;|>|>>|<|\*|\?|\&|\|//g;
  $interface =~ s/;|>|>>|<|\*|\?|\&|\|//g;

  system( "/usr/bin/java  Check  '$password' '$interface'" );
}
elsif ( $act eq "Help" ) {
  print ( "Content-type: text/html\n\n" );
  system( "/bin/cat  Help.html" );
}
else {
  print( "Content-type: text/html\n\n" );
  print( "No such option: <em>$act</em>" );
}
~/public_html/cgi-bin/280/exercise/1/Check.java
// Import the File class.
import java.io.File;
// Import this class to handle errors.
import java.io.FileNotFoundException;
// Import the Scanner class to read text files.
import java.util.Scanner;

public class Check {
  public static void main( String[ ] args ) {

    if ( !( args[0].equals( "password" ) ) ) {
      System.out.print  ( "Content-type: text/html\n\n" );
      System.out.println( "<html><body background='http://undcemcs01.und.edu/~wen.chen.hu/bg.jpg'>" );
      System.out.println( "<font size='+1'>" );
      System.out.println( "<center>Wrong password: <b>" + args[0] );
      System.out.println( "</b></center></font></body></html>" ); 
    }
    else {
      int interfaceNo = Integer.parseInt( args[1] );
      Print p = new Print( );
      switch( interfaceNo ) {
        case 1:
          System.out.print( "Content-type: text/plain\n\n\n" );
          p.printFile( "Check.pl" );
          p.printFile( "Check.java" );
          break;
        case 2:
          System.out.print( "Content-type: text/plain\n\n\n" );
          p.printFile( "InsertRoutes.pl" );
          p.printFile( "insertRoutes.java" );
          break;
        case 3:
          System.out.print( "Content-type: text/plain\n\n\n" );
          p.printFile( "ListAllRoutes.pl" );
          p.printFile( "ListAllRoutes.java" );
          break;
        case 4:
          System.out.print( "Content-type: text/plain\n\n\n" );
          p.printFile( "ShowRoute.pl" );
          p.printFile( "ShowRoute.java" );
          break;
        default:
          System.out.print  ( "Content-type: text/html\n\n" );
          System.out.println( "<html><body background='http://undcemcs01.und.edu/~wen.chen.hu/bg.jpg'>" );
          System.out.println( "<font size='+1'>" );
          System.out.println( "<center>No such interface: <b>" + args[1] );
          System.out.println( "</b></center></font></body></html>" );
      }
    }
  }
}

class Print {
  public void printFile( String fileName ) {
    System.out.print  ( "\n// ===================== " );
    System.out.print  ( fileName );
    System.out.println( " =====================\n" );
    try {
      File myObj = new File( fileName );
      Scanner myReader = new Scanner( myObj );
      while ( myReader.hasNextLine( ) )
        System.out.println( myReader.nextLine( ) );
      myReader.close( );
    }
    catch( FileNotFoundException e ) {
      System.out.println( "An error occurred." );
      e.printStackTrace( );
    } 
  }
}

Modify the password in the Line 11 to the password you pick.



Evaluations
The following features will be considered when grading:



      “When someone loves you, the way they talk about you is different.    
      You feel safe and comfortable.”    
      ― Jess C. Scott, The Intern