2个类和一个MainApp

时间:2014-10-23 15:04:59

标签: java arraylist tostring

尽我所能完成了任务,但我仍然陷入困境。显然,有些东西不适合代码。有人可以给我一些关于它可能有什么问题的建议吗?它工作正常,直到某些时候,但是我无法使用toString()方法打印出课程的详细信息以及时间表的详细信息,同时使用{{ 1}}方法。

toString()

班级课程没问题,我认为根本没有任何问题......

//package declaration
package assignment2app;
import javax.swing.JOptionPane;

public class Assignment2App {

    public static void main(String[] args) {
        Schedule s1=new Schedule();
        String r=JOptionPane.showInputDialog(null, 
              "Please enter the semester",
              "Information", 
              JOptionPane.INFORMATION_MESSAGE);
        s1.setSemester(r);

        String t=JOptionPane.showInputDialog(null, 
              "Please enter the year",
              "Information", 
              JOptionPane.INFORMATION_MESSAGE);
        int g=Integer.parseInt(t);
        s1.setYear(g);

        String u=JOptionPane.showInputDialog(null, 
              "Please enter the number of courses taken",
              "Information", 
              JOptionPane.INFORMATION_MESSAGE);
        int w=Integer.parseInt(u);

        while(w!=0){ 
              String y=JOptionPane.showInputDialog(null, 
                   "Please list the courses you have taken.Enter 'none' when you are done",
                   "Information", 
                   JOptionPane.INFORMATION_MESSAGE);
              Course c2=new Course(y, 3);
              if(!y.equalsIgnoreCase("none"))
                   s1.AddCourse(c2);
              else
                   w=0;
        }
  JOptionPane.showMessageDialog(null, s1.toString() ,
          "Schedule",
          JOptionPane.INFORMATION_MESSAGE);

  if(w>5){
       JOptionPane.showMessageDialog(null, "Sorry you cannot take more than 5 courses", 
            "Information", 
            JOptionPane.INFORMATION_MESSAGE);
  }
  while(w>5){
       String j=JOptionPane.showInputDialog(null, "Please enter the name of the course that you would like to remove",
          "Information",
          JOptionPane.INFORMATION_MESSAGE);
          s1.RemoveCourse(j);
       w--;   
   }

  Schedule s2=new Schedule();
  s2.setSemester("Summer1");
  s2.setYear(2014);
  int count=0;
  while(count<3){

  String f=JOptionPane.showInputDialog(null, "Please enter the Course Code",
         "Data Entry",
         JOptionPane.INFORMATION_MESSAGE);

  String h=JOptionPane.showInputDialog(null,"Please enter the Course Credits", 
         "Data Entry", 
         JOptionPane.INFORMATION_MESSAGE);
  int j=Integer.parseInt(h);
  Course c1=new Course(f, j);
  s2.AddCourse(c1);
  count++;
  }
    }
}

但是,在课程表中,我应该将课程列表作为AddCourses方法中的参数。我想我没有做到这一点。我认为我已经在编写方法中创建了不必要的ArrayLists,我认为这不会帮助我将所有信息集成到单个ArrayList中。所以我想知道我可以做的其他方法。我也应该知道如何使用主类中用户的输入将课程添加到计划中。我不知道该怎么做......所以非常感谢一些帮助。

package assignment2app;

import java.util.ArrayList;

public class Course {
    private String code;
    private int credits;

  public Course(String a, int b){
      setCode(a);
      setCredits(b);
  }

    void setCode(String c){
        code=c;
    }

    String getCode(){
        return code;
     }

    void setCredits(int cr){
        credits=cr;
    }

    int getCredits(){
        return credits;
     }


    @Override
   public String toString(){
        return code+ " "+ credits;
    }
}

2 个答案:

答案 0 :(得分:1)

你已经拥有

private ArrayList <Course> CourseList; 
你正在构造函数中初始化的

,所以在你的

void AddCourse(Course d){

只需将个别课程添加到courseList中,而不是每次都创建新的arraylist。

答案 1 :(得分:0)

将课程存储在最终课程中的第一件事应该是课程的数组列表,而不是字符串:

private ArrayList<Course> CourseList;
CourseList = new ArrayList<Course>();

接下来我想你要求在CourseList中添加和删除课程。您可以通过两种简单方法完成此操作:

public void AddCourse(Course c2) {
    CourseList.add(c2);

}

public void removeCourse(Course c2) {
    CourseList.remove(c2);

}

在您的其他课程中,您要求提供课程代码以删除该课程。

你可以这样做:

public void RemoveCourse(String j) {
    for(int i = 0 ; i < CourseList.size(); i++){
        if(CourseList.get(i).getCode().equals(j)){
            removeCourse(CourseList.get(i));
            break;
        }
    }

}

此方法会搜索您的courselist,直到找到一个与您指定的相同的课程代码,然后将其删除。

对于用户的输入,您可以使用这样的扫描仪:

 Scanner in = new Scanner(System.in);
 System.out.print("Please enter user class code : ");
 String code = in.nextLine();      
 System.out.println("Please enter your credits: ");
 String cred= in.nextLine();    
 addCourse(new Course(code, Integer.parseInt(cred)))

让我知道你是怎么过的。

相关问题