Skip to content

Instantly share code, notes, and snippets.

@ImsMoon
Forked from srialdabaoth/gist:5652001
Created February 16, 2017 17:33
Show Gist options
  • Save ImsMoon/10ac70ecd35f60e7dc89d40aa9f3c954 to your computer and use it in GitHub Desktop.
Save ImsMoon/10ac70ecd35f60e7dc89d40aa9f3c954 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testsubject;
import java.io.*;
import java.util.*;
/**
*
* @author the Hasnamuss
*/
public class TestSubject {
//Main Method
@SuppressWarnings("empty-statement")
public static void main(String[] args)throws Exception {
File file = new File("subjects.txt");
//File temp = new File("temp.txt");
//Create a user input scanner
Scanner scan = new Scanner(System.in);
// Create a Scanner for input and a PrintWriter for output
Scanner input = new Scanner(file);
//arraylist to store Subject objects
ArrayList subjectList = new ArrayList();
while (input.hasNext()) {
String c1 = input.next();
String n1 = input.nextLine();
System.out.println(n1);
Subject s1 = new Subject(c1, n1);
subjectList.add(s1);
}
System.out.println("Do you wish to add a new subject? (y/n): ");
String keepGoing = scan.nextLine();
while (keepGoing.equals("y")){
System.out.println("EXISTING DISCIPLINES:");
System.out.println("____________________");
//Get sorted and unique discipline codes, then print
ArrayList disciplines = Subject.sortedArrayList
(Subject.allDisciplines(subjectList));
printArrayList(disciplines);
System.out.println("Enter the discipline (eg ITC): ");
String userDis = scan.nextLine();
System.out.println("SUBJECTS IN THIS DISCIPLINE:");
System.out.println("___________________________");
ArrayList cpd = Subject.codesPerDiscipline(subjectList, userDis);
printArrayList(cpd);
String userCode = getUserCode(subjectList);
System.out.println("Enter the subject name: ");
String userName = scan.nextLine();
Subject s2 = new Subject(userCode, userName);
subjectList.add(s2);
System.out.println("Do you want to enter another subject? (y/n): ");
keepGoing = scan.nextLine();
}
PrintWriter output = new PrintWriter(file);
for (int i = 0; i < subjectList.size(); i++) {
output.println(((Subject)subjectList.get(i)).getCode() + ' ' +
((Subject)subjectList.get(i)).getName());
}
// Close the file
output.close();
input.close();
}
public static void printArrayList(ArrayList list) {
for (int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
//
public static String getUserCode(ArrayList subjectList){
//Create a user input scanner
Scanner scan = new Scanner(System.in);
String userCode = new String();
boolean subjectExists = true;
while (subjectExists) {
System.out.println("Enter the subject code (eg ITC421): ");
userCode = scan.nextLine();
if (Subject.isValidCode(userCode) == false){
System.out.println("Invalid subject code. "
+ "Please enter6 digit code: ");
userCode = scan.nextLine();
}
subjectExists = Subject.codeExists(subjectList, userCode);
if (subjectExists){
System.out.println
("Subject already exists. Please enter new code: ");
}
}
return userCode;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testsubject;
import java.io.*;
import java.util.*;
/**
*
* @author the Hasnamuss
*/
public class Subject {
private String code, name;
//
public Subject(String code, String name) {
this.name = name;
this.code = code;
}
//
public String getName() {
return name;
}
//
public String getCode() {
return code;
}
//
public static String getDiscipline (String code){
String discipline = code.substring(0, 3);
return discipline;
}
//Returns boolean value indicating if the subject's code is found in list
public static boolean codeExists(ArrayList subjectList, String userCode){
return subjectList.contains(userCode);
}
//Returns a string containing subject code and name
public String toString(Subject s){
return code + " " + name;
}
//
public static ArrayList allDisciplines(ArrayList subjectList){
ArrayList disList = new ArrayList();
for (int i = 0; i < subjectList.size(); i++) {
String iCode = ((Subject)subjectList.get(i)).getCode();
String dis = getDiscipline(iCode);
disList.add(dis);
}
Collections.sort(disList);
return disList;
}
//
public static ArrayList codesPerDiscipline(ArrayList subjectList, String userDis) {
ArrayList cpd = new ArrayList();
for (int i = 0; i < subjectList.size(); i++){
String iCode = ((Subject)subjectList.get(i)).getCode();
String existDis = getDiscipline(iCode);
if (userDis.equals(existDis));
cpd.add(iCode);
}
return cpd;
}
//Method for delivering sorted list with no duplicates
public static ArrayList sortedArrayList(ArrayList list){
HashSet sortSet = new HashSet();
list.addAll(list);
ArrayList sorted = new ArrayList(sortSet);
Collections.sort(sorted);
return sorted;
}
//
public static boolean isValidCode(String userCode){
if (userCode.length() == 6){
return true;
} else {
return false;
}
}
//Main Method
public static void main(String[] args) {
// TODO code application logic here
}
}
ITC421 Programming Java 1
PHL001 Introduction to Ethics
CRI101 Being Sneaky
BOR456 Inane Babbling
ITC422 Bananas in Pyjamas
Task 2
Value: 8 marks
For this task you will create a Subject class, whose instances will represent the subjects for study at a university. A subject will have a name, just a String, and a subject code, which is a six-character String. The first three characters of a subject code are alphabetic and the last three are numeric. The first three characters define the subject's discipline area. A subject code must be unique.
You will also write a TestSubject class to test the use of your Subject class. In particular this will maintain an array of subjects. In order to manage the uniqueness of the subject codes, your program will need to display information about existing subject codes as well as checking that any new subject code supplied by the user is not the same as any existing subject code.
The following state and functionality should be provided for the Subject class:
• Two fields will hold the subject’s name and the six-character subject code.
• A constructor will allow a name and a new, validated subject code to be provided when a new subject is created.
• Getters will provide access to the attributes.
• An accessor method called getDiscipline will return a string containing the first three characters of the subject code.
• Another accessor method called codeMatches will return a boolean value indicating if the subject's code matches the string argument provided. "Matches" is used here in the same sense as for the matches method of the String class.
• A toString method will return a string containing the subject code and subject name.
To assist with managing subject codes and their uniqueness you will provide the Subject class with some class methods as follows:
• An allDisciplines method will accept an array of Subject objects. It will return an array containing the different 3-character discipline codes represented in the array of subjects in alphabetically order.
• A codesPerDiscipline method will accept an array of Subject objects and a 3-character discipline code. It will return an array containing the different subject codes represented in the array of subjects for the particular discipline.
• An isValidCode method will accept a string that is a possible new subject code, and return a boolean indicating whether it satisfies the structural requirements for a subject code.
• A codeExists method will accept an array of Subject objects and a possible new subject code. It will return a boolean indicating whether that code has already been allocated to one of the subjects in the array.
• A sortDisciplines method will accept an array of Subjects objects. It will return the sorted array of subjects in alphabetically order.
Your TestSubject program will perform the following sequence of actions, using good design techniques such as in the appropriate use of methods:
• An initial array of Subject objects will be created from any data in a file that was previously saved by the program.
• The user interaction will then proceed to allow the user to add one or more new subjects to the array. If the user wishes to add new subjects, the discipline areas of existing subjects should be displayed in alphabetically order. The user will then enter a discipline code to which the program will respond by displaying any existing subject codes in that discipline. This procedure simplifies the user’s task of choosing subject codes that do not already exist, but does not prevent user mistake. Each subject code entered by the user should be checked. The user can enter any new subjects in that discipline (or indeed in other disciplines). The user should be given the choice of repeating the processing for other discipline areas.
• When the user has finished adding subjects, and only if subjects have indeed been added, the program will overwrite the data file with the updated data.
Note:
• You may use an ArrayList to implement an array if you prefer and it is appropriate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment