将If Else条件添加到数组中

时间:2013-09-29 22:09:07

标签: java if-statement

我正在使用以下程序。

这是整个代码,我最近开始学习编程,这是我的第一个项目,所以我不太擅长缩进和OOP概念。如果你们中的一位专家能够将这个程序变成OOP,这将对我有很大的帮助。

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


class Car {
    private String make;
    private String model;
    private String regNo;
    private int deposit;
    private int rate;

    public Car(String newMake, String newModel, String newRegNo,int newDeposit, int newRate) 
    {
            make = newMake;
            model = newModel;
            regNo = newRegNo;
            deposit = newDeposit;
            rate = newRate;
    }



    public String getMake() {
            return make;
    }

    public String getModel() {
            return model;
    }

    public String getRegNo() {
            return regNo;
    }

    public int getDeposit() {
            return deposit;
    }

    public int getRate() {
            return rate;
    }
}

public class carrenta {

public static void main(String[] args) {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy  HH:mm");
    Date date = new Date();
    System.out.println(dateFormat.format(date));

List<Car> carlist = new ArrayList();
carlist.add(new Car("Toyota", "corolla", "TA7896", 1500, 1800));
carlist.add(new Car("Toyota", "vitz", "TV9872", 1500, 1800));
carlist.add(new Car("Nissan", "paso", "NP1543", 1500, 1500));
carlist.add(new Car("Honda", "city", "HC4692", 1800, 1800));
carlist.add(new Car("Honda", "civic", "HC4521", 2000, 1600));
carlist.add(new Car("Honda", " accord", "HA5463", 2500, 2000));
carlist.add(new Car("Mitsubishi", "lancer", "ML4521", 2000, 1500));


            Scanner input = new Scanner(System.in);
            boolean modelFound = false;
            while (!modelFound) {
            System.out.print("Enter model to rent: ");
            String model = input.nextLine();

            for(Car s : carlist){
        if (model.equalsIgnoreCase(s.getModel())) {
         modelFound = true;
        System.out.println("Model  " + model + " is available");
        System.out.print("Enter number of days: ");
        int days = input.nextInt();
                        System.out.println("***************Details*****************");
        int cost = (days * s.getRate()) + s.getDeposit();
        System.out.println("Deposit  DailyRate  Duration  TotalCost");
        System.out.println(s.getDeposit() + "       " + s.getRate()+ "            " + days + "        " + cost);
        System.out.print("Proceed to rent?( y/n ): ");

        String dec = input.next();
       switch (dec) {
            case "y":
               System.out.println("Enter Customer Name: ");
                String name = input.next();
                System.out.println("Enter NIC Number: ");
                int num = input.nextInt();
                System.out.println("************Receipt*************");
                System.out.println(     "     Date                               Name           NICNo         Car        RegNo    Duration   TCost");
          System.out.println(date+"           "+name + "          " + num +    "         " + model
                + "   " + s.getRegNo() + "      " + days + "       "+cost);
                break;
            case "n":
                System.out.println("Serving Next Customer: ");
                break;
                }
    }
        else{
                System.out.println("Please enter a valid model");
        }
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

您可以尝试运行while循环:

boolean modelFound = false;
while (!modelFound) {
   // Ask question, take input
   String model = input.next();
   for (Object object : carList) {
   /* etc ... */
   if (model.equalsIgnoreCase(car.getModel()) {
       modelFound = true;
       /* other code */
   }
}

另外假设你的CarList使用Car generic:List<Car> carList,那么你不需要每次都进行投射,只需将你的for-each循环运行为for (Car c : carList)