2016-04-10 28 views
0

Bazı seçeneklerle temel bir menüye sahip olacak bir kod yazmaya çalışıyorum. Bu seçenekler aşağıdaki yöntemlerdir: AddStudent, changeName, setGrade vb. Adı, sınıfı ve yaşı olan Öğrenci adında bir nesne oluşturdum. Öğrencileri bağlantılı bir listeye eklemek istiyorum ancak yöntemi kullandığımda işe yaramıyor. İşte benim kodum:Bağlantılı Listelerdeki Nesneler

import java.util.*; 
class Student { 
    int age; 
    int grade; 
    String name; 
    static LinkedList ll = new LinkedList(); 
    public Student (String n) { //we create here a student with a name and an age 
     name=n; 
     age=0; 
     grade=0; 
    } 
//------------------------------------------------------------------------- 
    public void p(String x) { 
     System.out.println(x); 
    } 

    public void addStudent() { 
     Scanner s = new Scanner(System.in); 
     p("Enter the name that you want"); 
     String f = s.nextLine(); 
     Student a = new Student(f); 
     ll.add(a); 
    } 

    public void changeName() {       //this method is to change the name of a student 
     Scanner s = new Scanner(System.in); 
     p("Enter whose name you want to change"); 
     String c = s.nextLine(); 
     p("Enter the name that you want"); 
     String b = s.nextLine(); 

    } 

    public void setGrade(Student a) {     //this method is to put the student's grade 
     Scanner s = new Scanner(System.in); 
     p("Enter the grade that you want"); 
     int g = s.nextInt(); 
     a.grade=g; 
    } 

    public void setAge(Student a) {     //This method is to put the student's grade 
     Scanner s = new Scanner(System.in); 
     p("Enter the age that you want"); 
     int h = s.nextInt(); 
     a.age=h; 
    } 

    public String getName(Student a) { 
     return a.name; 
    } 

    public int getAge(Student a) { 
     return a.age; 
    } 

    public int getGrade(Student a) { 
     return a.grade; 
    } 
} 

Sorun, addStudent yöntemindedir. Aynı projeyi yapabileceğim başka bir yol var mı?

+0

Derleyiciden ne tür bir sorun yaşıyorsunuz? –

+1

Hangi hatayla karşılaşıyorsunuz? – Polygnome

cevap

1

Mantıksal olarak düşünün. Tek bir öğrenciyi temsil eden Student sınıfınız var. Neden bir öğrenci bir öğrenci listesine sahip olur? Bu hiç mantıklı değil.

Course gibi bir programınız veya Öğrenci Listesi'ne girecek bir programınız yok mu? Listenin ait olduğu yer burası. Zorlayıcı bir nedeniniz olmadıkça (nadir) statik kullanmayın. İşte

Course sınıf için bir başlangıç ​​olduğunu, bu Course içinde senin LinkedList öğrenci bilgileri ve depolar saklamak için Student kullanır.

Sınıf Ders:

import java.util.LinkedList; 
import java.util.Scanner; 

public class Course { 
    LinkedList ll = new LinkedList(); 
    Scanner s = new Scanner(System.in); 

    public void addStudent() { 
     p("Enter the name that you want"); 
     String f = s.nextLine(); 
     Student a = new Student(f); 
     ll.add(a); 
    } 

    public void changeName() {       //this method is to change the name of a student 
     Student student = findStudent(); 
     p("Enter the name that you want"); 
     String newName = s.nextLine(); 
     //student.setName(newName); 
    } 

    public void setGrade() {     //this method is to put the student's grade 
     Student student = findStudent(); 
     p("Enter the grade that you want"); 
     int grade = s.nextInt(); 
     //student.setGrade(grade); 
    } 

    public void setAge() {     //This method is to put the student's grade 
     Student student = findStudent(); 
     p("Enter the age that you want"); 
     int age = s.nextInt(); 
     student.setAge(age); 
    } 

    public Student findStudent(){ 
     p("Which student did you want to change? Please enter their name:"); 
     String name = s.nextLine(); 
     //Find student in the list - left for the author 
     Student student = null; 
     return student; 
    } 

    //------------------------------------------------------------------------- 
    public void p(String x) { 
     System.out.println(x); 
    } 

    public static void main(String[] args) { 
     Course course = new Course(); 
     course.addStudent(); 
     course.addStudent(); 
     course.changeName(); 
     course.setGrade(); 
    } 
} 

Ve modifiye Öğrenci sınıfı:

import java.util.*; 

public class Student { 
    int age; 
    int grade; 
    String name; 

    public Student (String n) { //we create here a student with a name and an age 
     name=n; 
     age=0; 
     grade=0; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setGrade(int grade) { 
     this.grade = grade; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public String getName(Student a) { 
     return a.name; 
    } 

    public int getAge(Student a) { 
     return a.age; 
    } 

    public int getGrade(Student a) { 
     return a.grade; 
    } 

    @Override 
    public String toString(){ 
     return "Student(name:" + name + ", age:" + age + ", grade:" + grade + ")"; 
    } 
} 
1

Bu kod tür dağınık Hala Listesini yazdırmak için findStudent yöntem ve muhtemelen bir yöntem uygulamak gerekir çünkü Student sınıfında görünmemesi gereken bir kod eklemişsiniz. Örneğin, addStudent yöntemi statik değildir ve bunu çağırmak için önce bir örnek Student örneğini başlatmanız ve yöntemi çağırmanız gerekir. Ancak, yöntem, kullanıcıdan, kötü bir tasarım olan yeni bir Student örneğinin bilgilerini girmesini istemeye çalışıyor.

Student sınıfını yalnızca yapması gereken şeyi yapın. Davanız için, Student sadece kendi yaşını, derecesini ve ad alanlarını saklamak ve bu alanları başlatmak için yapıcıları tanımlamak ve bu alanları ihtiyaçlarınıza göre ayarlamak ve almak için isteğe bağlı alıcı ve ayarlayıcı yöntemleri tanımalıdır.

Uygulamanızı yöneten bir 'yönetici' sınıfına ihtiyacınız olacaktır. Bu sınıf, öğrencilerin listesini takip eder ve kullanıcılardan yeni bir öğrencinin bilgisini girmesini ve ardından öğrenci örneğini başlatmasını ve listeye koymasını ister. Bu Yönetici sınıfı, kullanıcı girdisini veya görüntü bilgilerini kullanıcıya almanız gereken bir UI'yi bile yönetebilir. Dolayısıyla, bu sınıfın addStudent yöntemini sağlama sorumluluğu olacaktır. Sınıfın kendisi, bir ders seçim programı veya başka bir şey gibi, uygulamanızın mantığı hakkında hiçbir şey bilmemelidir. Sadece kendi bilgisini yönetir, bazı yönetici sınıfı ise uygulamanın mantığını üstlenir.