我正在根據多個變量(如年(annee),品牌(marque),顏色(couleur)里程錶(kilometrage),已售出),傳輸(automatique),評論(commentaire)和門數(nbrPortes)。創建對象時的構造函數,安裝程序,Getter問題
代碼中的參數是法文的,因爲我在法國的一所大學,所以請和我一起裸照。
我的代碼需要具有setter,constructors和cloner方法,就像它們現在(強加)一樣。我自己做了getter和IDnumber和compteurvendue(出售車的數量)的增量。
這裏是我的代碼:
public class Automobile {
private static final String[] COULEURS = { "Autre", "Noir", "Blanc", "Bleu Nuit", "Bleu Clair", "Vert Pomme", "Vert Bouteille", "Taupe", "Argent", "Sable", "Gris Charbon", "Gris Clair", "Orange", "Rouge", "Jaune", "Brun" };
private static final String[] MARQUES = { "Autre", "Mazda", "Toyota", "Ford", "GM", "Hyunday", "BMW", "SAAB", "Honda", "Mitsubishi", "Mercedes", "KIA", "Wolkswagen"};
public static final int COULEUR_DEF = 8;
public static final int COULEUR_MIN = 0;
public static final int COULEUR_MAX = COULEURS.length - 1;
public static final int MARQUE_DEF = 4;
public static final int MARQUE_MIN = 0;
public static final int MARQUE_MAX = MARQUES.length - 1;
public static final double KILO_DEFAUT = 55000;
public static final double KILO_MIN = 15000;
public static final double KILO_MAX = 140000;
public static final int DEUX_PORTES = 2;
public static final int QUATRE_PORTES = 4;
public static final int PORTES_DEFAUT = QUATRE_PORTES;
public static final boolean AUTO_DEF = true;
public static final int ANNEE_MIN = 1997;
public static final int ANNEE_MAX = 2016;
public static final int ANNEE_DEFAUT = 2007;
public static final String COMM_DEFAUT = "";
public static final boolean VENDUE_DEF = false;
//private variables
private int marque;
private int annee;
private int couleur;
private boolean a;
private boolean automatique;
private double k;
private double kilometrage;
private int p;
private int nbrPortes;
private String c;
private String commentaire;
private boolean v;
private boolean vendue;
private int compteurvendue = 0;
private int IDnumber = 0;
// CONSTRUCTORS. I do not know I have to make 2, I am thinking for the purpose of the cloning without copying certain variables? I'm confused about this.
public Automobile (int marque, int annee, int couleur, boolean automatique, double kilometrage) {
if (marque>= MARQUE_MIN && marque <= MARQUE_MAX) {
this.marque = marque;
} else {
this.marque = MARQUE_DEF;
}
if (annee>= ANNEE_MIN && annee <= ANNEE_MAX) {
this.annee = annee;
} else {
this.annee = ANNEE_DEFAUT;
}
if (couleur>= COULEUR_MIN && couleur <= COULEUR_MAX) {
this.couleur = couleur;
} else {
this.couleur = COULEUR_DEF;
}
if (a == true || a == false) {
this.automatique = a;
} else {
this.automatique = AUTO_DEF;
}
if (k >= KILO_MIN && k <= KILO_MAX) {
this.kilometrage = k;
} else {
this.kilometrage = KILO_DEFAUT;
}
this.IDnumber = IDnumber + 1;
}
public Automobile (int marque, int annee, int couleur, boolean automatique, double kilometrage,
int nbrPortes, String commentaire, boolean vendue){
if (marque>= MARQUE_MIN && marque <= MARQUE_MAX) {
this.marque = marque;
} else {
this.marque = MARQUE_DEF;
}
if (annee>= ANNEE_MIN && annee <= ANNEE_MAX) {
this.annee = annee;
} else {
this.annee = ANNEE_DEFAUT;
}
if (couleur>= COULEUR_MIN && couleur <= COULEUR_MAX) {
this.couleur = couleur;
} else {
this.couleur = COULEUR_DEF;
}
if (a == true || a == false) {
this.automatique = a;
} else {
this.automatique = AUTO_DEF;
}
if (k >= KILO_MIN && k <= KILO_MAX) {
this.kilometrage = k;
} else {
this.kilometrage = KILO_DEFAUT;
}
if (p == DEUX_PORTES || p == QUATRE_PORTES) {
this.nbrPortes = p;
} else {
this.nbrPortes = PORTES_DEFAUT;
}
if (c != "") {
this.commentaire = c;
} else {
this.commentaire = COMM_DEFAUT;
}
if (v == true || v == false) {
this.vendue = v;
} else {
this.vendue = VENDUE_DEF;
}
this.IDnumber = IDnumber + 1; //The problem is that the IDnumber is 1 for every new instance I create, wether I have the this. in front or not.
}
//HERE I AM SUPPOSED TO PUT IN A CLASS GETTER...I have not managed to make it work in any way
public static Automobile getAutomobile() {
return null; //just put a null return in the meantime so I can compile
}
//GETTERS
public int getAnnee() {
return annee;
}
public int getMarque() {
return marque;
}
public int getCouleur() {
return couleur;
}
public int getNbrPortes() {
return p;
}
public double getKilometrage() {
return k;
}
public boolean getVendue() {
return v;
}
public String getCommentaire() {
return c;
}
public boolean getAutomatique() {
return a;
}
//Setters. According to the imposed settings, Not supposed to have any error message or correction in case of wrong input, it just goes right through?
public void setAnnee (int annee) {
//A COMPLETER
if (annee >= ANNEE_MIN && annee <= ANNEE_MAX) {
this.annee = annee;
}
}
public void setMarque (int marque){
//A COMPLETER
if (marque >= MARQUE_MIN && marque <= MARQUE_MAX) {
this.marque = marque;
}
}
public void setCouleur (int couleur) {
//A COMPLETER
if (couleur >= COULEUR_MIN && couleur <= COULEUR_MAX){
this.couleur = couleur;
}
}
public void setNbrPortes (int p) {
//A COMPLETER
if (p == DEUX_PORTES || p == QUATRE_PORTES){
this.p = p;
}
}
public void setKilometrage (double k) {
//A COMPLETER
if (k >= KILO_MIN && k <= KILO_MAX) {
this.k = k;
}
}
// This is supposed to count if I select a car as sold, and counts how many are sold in total (So I can later show it to the user)
public void setVendue (boolean v) {
if (v == true || v == false) {
this.v = v;
}
if (v = true) {
compteurvendue = compteurvendue + 1;
}
}
//Sets comment as long as it is not null
public void setCommentaire (String c){
if (c != null) {
this.c = c;
}
}
public void setAutomatique (boolean a){
if (a == true || a == false) {
this.a = a;
}
}
//Supposed to be able to create a clone of the car with all the same variables except the comment and sold that are reverted to default, and the IDnumber which will be a new one (+1)
public Automobile cloner() {
Automobile Clone = new Automobile(marque,annee,couleur,a,k,p,COMM_DEFAUT,VENDUE_DEF);
return Clone;
}
} // Automobile
但是也有一些目前沒有工作的我的代碼的多個部分:
汽車第一個構造函數接受我的變量輸入時,我在BlueJ環境中測試它,但布爾值(vendue和automatique)總是出現爲FALSE,並且註釋總是以COMM_DEFAUT(空字符串「」)出現。
即使我輸入有效值,第二個構造函數也會將我的實例中的所有內容都作爲默認值。布爾和評論仍然有同樣的問題。
我不知道如何創建一個「類的getter」 ......什麼它的目的是在這種情況下
的的ID號永遠是爲1。它不會保存在我創建每個新對象之間的記憶。
我覺得我的克隆方法是正確的,但我無法調用它或測試它甚至創建一輛默認汽車。
***請保持在除了私有變量和getter方法,所有制定者的頭,構造,最終變量和拷貝機頭強加,並且不能被修改的頭腦(這是是什麼讓這更難)。
我可以做些什麼來解決這些不同的問題,我使用我的代碼?非常感謝你,因爲這個社區已經非常有幫助!
是或不是? – NiVeR
作爲一個說明,你的顏色和品牌確實應該是枚舉。這將顯着簡化您的代碼。 – chrylis