私のプログラムは実行されますが、論理的な誤りがあり、見つけられません。私は名前とすべてのものを入力しますが、私は旅を印刷することを選択したときに出力し唯一の電話番号と、ここでプログラムは何も印刷しません(オブジェクトとクラス)
import java.util.*;
class Trip{
private static String customerName;
private static String mobileNo;
private static String reservationCode;
public static int totalReservations=0;
public static Scanner input=new Scanner (System.in);
public Trip(){//The constructer
customerName = " ";
mobileNo = " ";
}
public Trip(String Name, String MobileNo) {
customerName=Name;
mobileNo=MobileNo;
totalReservations++; //increase the total reservations
}
public void setcustomerName(String Name){
customerName = Name;
}
public void setmobileNo(String MobileNo){
mobileNo = MobileNo;
}
//Getters
public String getcustomerName() {
return customerName;
}
public String getmobileNo() {
return mobileNo;
}
public String getreservationCode() {
return reservationCode;
}
public static void generateReservationCode(){
}
public static void printTripInfo(){
System.out.println("Customer Name:"+customerName);
System.out.println("Phone Number:"+mobileNo);
}
}//end class trip
public class CabCompany{
private static int reservations_size=20;
private static Trip[] trip;
Trip newTrip;
public int totalNumber=0;
public static int user;
public static Scanner input=new Scanner (System.in);
public static void main(String[]args){//MAIN
trip=new Trip[reservations_size];
do{
System.out.println("1. Add a new trip\n2. Find a trip by reservation code\n3. List all trips\n");
user=input.nextInt();
if (user == 1) {
System.out.println("Name:");
String Name = input.nextLine();
input.nextLine();
System.out.println("MobileNo:");
String MobileNo = input.next();
CabCompany Y = new CabCompany();
boolean flag;
flag = Y.addTrip(Name,MobileNo);
if(flag == true) {
System.out.println("the trip has been added succssfully");
} else if (flag == false){
System.out.println("hasn't been added");
}
}
if(user == 2) {
printAll();
}
if(user == 3){
System.out.println("The total Reservations is:"+Trip.totalReservations);
}
} while (user != 7);//BREAK
}
public boolean addTrip(String customerName,String mobileNo){
boolean flag = true;
if ((mobileNo.length() == 10)) {
newTrip=new Trip(customerName, mobileNo);
int i = Trip.totalReservations - 1;
newTrip.generateReservationCode();
trip[i] = newTrip;
flag = true;
} else {
flag = false;
}
return flag;
}
public static void printAll() {
for(int t=0; t<Trip.totalReservations; t++){
trip[t].printTripInfo();
}
}
}
ヒント:私たちがあなたのために時間を費やすことを望みます**。だからあなたは、あなたのコードをすべて適切にフォーマット/インデントする時間を費やしてください。そして、コード品質に関する副作用:すべての種類のブロックの周りに{中括弧}を使用してください。そして反復して、elsesで休憩をして...ちょっと醜い。あなたは "クリーンコード"について少し読んでみたいです。あなたのコードはそれから利益を得ることができます... – GhostCat
良いアドバイス、私はあなたに感謝します – Dipie
それはプログラミングの問題についての質問ではない、それはプログラミングの問題についての質問です( "私は何をやっているのかわからない、どのようにプログラムをJavaで? ")。どうか、Javaの仕組みを学んでください。これは、Javaでプログラミングするための本やチュートリアルを読んでください。 SOからプログラムする方法を習得しようと時間を無駄にしないでください。 – zubergu