Program się ładnie kompiluje, ale zawsze remove czy find method pokazuje mi, nie działa, tzn pokazuje że nie ma takich appointments.
Jeśli ktośby był tak dobroduszny i pomugł mi z tym oto kodem:
KLASA APPOINTMENT:
import java.util.*;
/**
* @author Adam Omelak - [email protected]
* @version 22-11-2007
*/
public class Appointment
implements Comparable<Appointment>
{
private String subject;
//private String date;
private int dayApp;
private int monthApp;
private int yearApp;
private int timeStart;
private int timeEnd;
private String person;
private String date = Integer.toString(dayApp)+Integer.toString(monthApp )+Integer.toString(yearApp );
public Appointment()
{
subject = "unknown";
dayApp = 00;
monthApp = 00;
yearApp = 0000;
timeStart = 0000;
timeEnd = 0000;
person = "unknown";
}
public Appointment(String s, int dA, int mA, int yA, int t, int t2, String p) {
subject = s;
dayApp = dA;
monthApp = mA;
yearApp = yA;
timeStart = t;
timeEnd = t2;
person = p;
}
public void readKeyboard() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the subject for appoinment: ");
subject=in.next();
System.out.print("Enter person with who you have got appointment: ");
person=in.next();
System.out.print("Enter the day of appointment (dd): ");
dayApp=in.nextInt();
System.out.print("Enter the month of appointment (mm): ");
monthApp=in.nextInt();
System.out.print("Enter the year of appointment (yyyy): ");
yearApp=in.nextInt();
System.out.print("Enter starting time (for example: 1254): ");
timeStart=in.nextInt();
System.out.print("Enter ending time (for example: 1354): ");
timeEnd=in.nextInt();
System.out.println("***********************");
System.out.println("I'm writing into a file");
System.out.println("***********************");
this.printOut();
}
public void date()
{
System.out.println("Date: "+dayApp+"/"+monthApp+"/"+yearApp);
}
public void dateInt()
{
System.out.println(""+date);
}
public void printOut() {
System.out.println("Subject of Appointment: " + subject);
System.out.println("Appointment with: " + person);
date();
System.out.println("Start time: " + timeStart);
System.out.println("Ends time: " + timeEnd);
}
public void setSubject(String s) {
subject = s;
}
public String getSubject()
{
return subject;
}
public void setDate(String d) {
date = d;
}
public void setTime(int t) {
timeStart = t;
}
public void setTime2(int t2) {
timeEnd = t2;
}
public void setPerson(String p) {
person = p;
}
public boolean equals(Appointment other)
{
if((this.date).equals (other.date))
return true;
else
return false;
}
public int compareTo(Appointment other) {
return (this.date).compareTo(other.date);
}
public void print2()
{
System.out.println("- DATE "+dayApp+"/"+monthApp+"/"+yearApp+", Start: "+timeStart+", End: "+timeEnd+", Subject: "+subject+", With: "+person);
}
public String toString() {
return ""+dayApp+" "+monthApp+" "+yearApp+" "+timeStart+" "+timeEnd+" "+subject+" "+person;
}
}
KLASA DIARY:
import java.util.*;
import java.io.*;
/**
* @author Adam Omelak - [email protected]
* @version 22-11-2007
*/
public class Diary
{
private List <Appointment> appointments;
Scanner in = new Scanner(System.in);
private int nextFreeLocation;
private Appointment appointment;
public Diary()
{
appointments = new ArrayList<Appointment>();
}
public void addAppointment(Appointment theAppointment)
{
appointments.add(theAppointment);
nextFreeLocation = nextFreeLocation + 1;
}
public void printOut() {
String results = " \n";
for (Appointment a:appointments)
{
a.print2();
}
}
public void remove2()
{
Appointment appToRem=new Appointment();
boolean flag=false;
System.out.print("On which day do you want remove appointments? ");
Scanner in=new Scanner(System.in);
String d=in.next();
appToRem.setDate(d);
for (int i=0;i<appointments.size();i++){
if (appointments.get(i).equals(appToRem)){
appointments.remove(i); i--;
flag=true;
System.out.println("Appointments removed");
}
}
if(flag==false)
System.out.println("There is no appointments on this day");
}
public void removeAppointment(Appointment oneToFind) {
int index = 0;
while ((!(appointments.get(index)).equals(oneToFind)) &&
(index < nextFreeLocation))
{
index++;
}
if ((appointments.get(index)).equals(oneToFind))
{
appointments.remove(index);
}
else
{
System.out.println("Unable to find appointments for this day");
}
}
public void findDate() {
Appointment oneToFind = new Appointment();
System.out.println("On which date do you looking for appointments? ");
String d = in.next();
oneToFind.setDate(d);
for (Appointment a:appointments)
{
if (a.equals(oneToFind))
{
a.print2();
}
}
}
public Appointment find(Appointment who) {
for (Appointment a:appointments) {
if (a.equals(who))
return a;
}
return null;
}
public String toString() {
String results = " \n";
for (Appointment a:appointments)
{
results = results + a.toString() + "\n";
}
return results;
}
public int size()
{
return appointments.size();
}
public void sort()
{
Collections.sort(appointments);
}
}
KLASA DIARY APP
import java.util.*;
import java.io.*;
/**
* @author Adam Omelak - [email protected]
* @version 22-11-2007
*/
public class DiaryApp
{
//private ArrayList <Appointment> appointments;
private Diary diary;
private Scanner scan;
Scanner in = new Scanner(System.in);
private Appointment appointment;
private String today;
private int day2;
private int month2;
private int year2;
public void today() {
System.out.println("Diary updated for date: "+day2+"/"+month2+"/"+year2);
}
public DiaryApp()
{
System.out.println("**Program is running**");
scan = new Scanner(System.in);
diary = new Diary();
System.out.println("Enter todays day: (dd)");
day2 = in.nextInt();
System.out.println("Enter todays month: (mm)");
month2 = in.nextInt();
System.out.println("Enter todays year: (yyyy)");
year2 = in.nextInt();
}
public void load() throws IOException
{
Scanner in =new Scanner(System.in);
today();
System.out.println("loading from the file");
System.out.println("Using file: app.txt \n");
Scanner infile = new Scanner(new InputStreamReader (new FileInputStream("app.txt")));
int num = infile.nextInt();
for (int i=0;i<num;i++)
{
int dA = infile.nextInt();
int mA = infile.nextInt();
int yA = infile.nextInt();
int t = infile.nextInt();
int t2 = infile.nextInt();
String s = infile.next();
String p = infile.next();
Appointment a = new Appointment(s, dA, mA, yA, t, t2, p);
diary.addAppointment(a);
}
infile.close();
diary.sort();
}
public void save() throws IOException
{
PrintWriter outfile = new PrintWriter(new OutputStreamWriter(new FileOutputStream("app.txt")));
outfile.println(diary.size());
outfile.println(diary.toString());
outfile.close();
}
//public void update()
//{
// if(today < appointment.d)
// {
// appointments.remove(d);
// }
// else
// return null;
//}
public void runMenu() throws IOException
{
char ch;
do{
System.out.println("Type any letter below and press enter to confirm:");
System.out.println("Type 'A' to add new appointment");
System.out.println("Type 'R' to remove appointment");
System.out.println("Type 'F' to search appointment");
System.out.println("Type 'P' to print out list of appointments");
System.out.println("Type 'S' to save to a file");
System.out.println("Type 'Q' to quit the program");
System.out.println("Type here ");
Scanner in=new Scanner(System.in);
String choice=in.next();
ch=choice.charAt(0);
switch(ch) {
case 'A': case 'a':
System.out.println("************");
System.out.println("Type new appointment");
System.out.println("************");
Appointment a = new Appointment();
a.readKeyboard();
diary.addAppointment(a);
diary.sort();
System.out.println("*****************");
System.out.println("Appointment was added");
System.out.println("*****************");
break;
case 'F': case 'f':
diary.findDate();
diary.sort();
System.out.println("\n");
break;
case 't':
appointment.dateInt();
break;
case 'P': case 'p':
System.out.println("*************");
System.out.println("List of appointments:");
System.out.println("*************");
diary.sort();
diary.printOut();
System.out.println("\n");
break;
case 'R': case 'r':
diary.remove2();
diary.sort();
break;
case 'S': case 's':
save();
System.out.println("File was successful saved \n");
break;
case 'Q': case 'q':
System.out.println("******************");
System.out.println("Files updated!");
System.out.println("******************");
System.out.println("Program was closed");
System.out.println("******************");
System.out.println("Thank you");
System.out.println("*********");
diary.sort();
break;
default:
System.out.println("***********************************");
System.out.println("Error!, Type again accepted letter!");
System.out.println("***********************************");
}
}while ((ch!='Q')&(ch!='q'));
}
public void find() {
Appointment oneToFind = new Appointment();
System.out.println("On which date do you looking for appointments? ");
String d = in.next();
oneToFind.setDate(d);
Appointment found = diary.find(oneToFind);
if (found==null)
System.out.println("There is no appointments on this day");
else
System.out.println("Appointments for that day: \n");
found.print2();
System.out.println("\n");
}
public static void main(String args[]) throws IOException
{
DiaryApp demo = new DiaryApp();
demo.load();
demo.runMenu();
demo.save();
}
}
Za każda odpowiedź serdecznie dziękuję.