0

我对以下程序有疑问。

这是整个代码,而且我最近开始学习编程,这是我的第一个项目,所以我不太擅长缩进和 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");
        }
        }
    }
}

}

4

1 回答 1

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)

于 2013-09-29T22:12:26.573 回答