Heyas, I'm struggling to come up with some working code for a piece of software I've to make. I know it's got something to do with Arrays, or array lists, which just so happen to be the one thing I missed in C and Java! Student life, eh? :P Anyway, the thing I've to do is write a piece of software that permits a user to enter up to 100 contract details (ie. name, number, expected completion date). Although the user to enter an 'actual' completion date for the contracts, and finally, determines whether each contract was completed on schedule, and prints out the contracts completed late. Any help would be great and yeah, I know it's a bit to ask, but, you don't ask, you don't get right? :P The following is the code I've came up with so far, Apologies for the lack of indentation, it reset it all once posted: Code: import java.util.Scanner; public class Contract { class ContractArray { [INDENT] [/INDENT] public static void main(String[] args) { [INDENT] [/INDENT] String[] contArray; //Declares string of arrays [INDENT] [/INDENT] contArray = new String[100]; class Date { // properties [INDENT] [/INDENT] private int day, month, year; // methods [INDENT] [/INDENT] public void readDate(){ // prompts for and reads in a Date from the keyboard [INDENT] [/INDENT] Scanner sc = new Scanner(System.in); [INDENT] [/INDENT]do { [INDENT] [/INDENT]System.out.print("Enter day: "); [INDENT] [/INDENT]day = sc.nextInt(); [INDENT] [/INDENT]System.out.print("Enter month: "); [INDENT] [/INDENT]month = sc.nextInt(); [INDENT] [/INDENT]System.out.print("Enter year: "); [INDENT] [/INDENT]year = sc.nextInt(); [INDENT] [/INDENT]if (!validDate()) [INDENT] [/INDENT]System.out.println("Date not valid! Try again..."); } [INDENT] [/INDENT]while (!validDate()); }//readDate public boolean after(Date D){ // checks if a Date is after Date D if (year > D.year) return true; else if ((year == D.year) && (month > D.month)) return true; else if ((year == D.year) && (month == D.month) && (day > D.day)) return true; else return false; }//after public void printDate() { System.out.println(day + "/" + month + "/" + year); }//printDate public boolean validDate() { if (!validYear(year)) return false; else if (!validMonth(month)) return false; else if (!validDay(day,month)) return false; else return true; }//validDate private boolean validYear(int y) { if ((y < 1990) || (y > 2100)) return false; else return true; }//validYear private boolean validMonth(int m) { if ((m < 0) || (m > 12)) return false; else return true; }//validMonth private boolean validDay(int d, int m) { if ((d < 0) || (d > daysinMonth(m))) return false; else return true; }//validDay private int daysinMonth(int m) { int NumDays; switch(m) { case 1:case 3:case 5:case 7: case 8:case 10:case 12 : NumDays = 31; break; case 4:case 6:case 9:case 11: NumDays = 30; break; case 2 : NumDays = 28; break; default: NumDays = 0; } return NumDays; }//daysinMonth } // end of class Date public class contractRecord { String custName; String contractNum; int cost; int exCompDate; // int actCompDate; Scanner sc = new Scanner(System.in); System.out.println("Please enter Customer Name: "); custName = sc.nextString(); System.out.println("Please enter Contract Number: "); contractNum = sc.nextString(); System.out.println("Please enter Expected cost:£ "); cost = sc.nextInt (); System.out.println("Please enter Expected completion date (DDMMYYYY): "); exCompDate = sc.nextInt(); // System.out.println("Please enter Actual completion date (DDMMYYYY): "); // Not meant for here. // actCompDate = sc.nextInt(); } //Run Date Class /* //if late System.out.println("********* Contract Number: " + contractNum[]); System.out.print("*********"); System.out.println(""); System.out.println("Customer: " + custName); System.out.println(""); System.out.println("Expected Date of Completion: " + exCompDate); System.out.println(""); System.out.println("Actual Date of Completion: " +actCompDate); System.out.println(""); System.out.println("Cost:£ " + cost); */ }
Been thinking about this and I think one of the problems I were having was saving like 5 pieces of data in a single array. Was thinking about using a number of arrays, one for each piece of information. Possible?
Managed to come up with the following. Code: import java.util.Scanner; class Contract { public class CustArray { class ContractDetails { public static void main(String [] a) { String s1, s2, s3; int s4, s5, s6; Scanner sc = new Scanner(System.in); System.out.println("Please enter Customer Name: "); s1 = sc.nextString(); System.out.println("Please enter Contract Number: "); s2 = sc.nextString(); System.out.println("Please enter Expected cost:£ "); s3 = sc.nextInt (); class Date { // properties private int day, month, year; // methods public void readDate(){ // prompts for and reads in a Date from the keyboard Scanner sc = new Scanner(System.in); do { System.out.print("Enter day: "); day = sc.nextInt(); System.out.print("Enter month: "); month = sc.nextInt(); System.out.print("Enter year: "); year = sc.nextInt(); if (!validDate()) System.out.println("Date not valid! Try again..."); } while (!validDate()); }//readDate public boolean after(Date D){ // checks if a Date is after Date D if (year > D.year) return true; else if ((year == D.year) && (month > D.month)) return true; else if ((year == D.year) && (month == D.month) && (day > D.day)) return true; else return false; }//after public void printDate() { System.out.println(day + "/" + month + "/" + year); }//printDate public boolean validDate() { if (!validYear(year)) return false; else if (!validMonth(month)) return false; else if (!validDay(day,month)) return false; else return true; }//validDate private boolean validYear(int y) { if ((y < 1990) || (y > 2100)) return false; else return true; }//validYear private boolean validMonth(int m) { if ((m < 0) || (m > 12)) return false; else return true; }//validMonth private boolean validDay(int d, int m) { if ((d < 0) || (d > daysinMonth(m))) return false; else return true; }//validDay private int daysinMonth(int m) { int NumDays; switch(m) { case 1:case 3:case 5:case 7: case 8:case 10:case 12 : NumDays = 31; break; case 4:case 6:case 9:case 11: NumDays = 30; break; case 2 : NumDays = 28; break; default: NumDays = 0; } return NumDays; }//daysinMonth } // end of class Date System.out.println("Please enter Actual completion date."); public void readDate(){ } /* //if late System.out.println("********* Contract Number: " + s1[]); System.out.print("*********"); System.out.println(""); System.out.println("Customer: " + s2); System.out.println(""); System.out.println("Expected Date of Completion: " + s4 + s5 + s6); System.out.println(""); System.out.println("Actual Date of Completion: " + s7 + s8 +s9); System.out.println(""); System.out.println("Cost:£ " + s3); */ }//End of single array }//End of Contract Details }//End of CustArray }//End of Main //Run Date Class