和#x27;独特的';Java中的对象
我正在和我的同学做一个学校作业。我们正在制作一个出租车应用程序,在那里你可以启动和停止出租车,询问当前价格,并在出租车停止时获得总价。目前它运行良好。但如果我们先启动出租车1,然后再启动出租车2,就会出现问题。出租车1的开始时间和结束时间被新对象(出租车2)的开始时间和结束时间覆盖-即使我们从和arraylist中获得一个唯一的对象,该对象基于我在系统中键入的用户编号 Main.java中的代码:和#x27;独特的';Java中的对象,java,object,unique,Java,Object,Unique,我正在和我的同学做一个学校作业。我们正在制作一个出租车应用程序,在那里你可以启动和停止出租车,询问当前价格,并在出租车停止时获得总价。目前它运行良好。但如果我们先启动出租车1,然后再启动出租车2,就会出现问题。出租车1的开始时间和结束时间被新对象(出租车2)的开始时间和结束时间覆盖-即使我们从和arraylist中获得一个唯一的对象,该对象基于我在系统中键入的用户编号 Main.java中的代码: import java.text.DecimalFormat; import java.util.
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Taxi taxi = new Taxi();
System.out.println("Hej og velkommen til Damn Fast Taxis.");
boolean isEnd = false;
DecimalFormat decimalFormat = new DecimalFormat("#.0");
while(!isEnd) {
Taxi chosenTaxi;
System.out.println("1. Start en taxi.");
System.out.println("2. Stop en taxi.");
System.out.println("3. Pause en taxi.");
System.out.println("4. Spørg efter pris.");
System.out.println("5. Gratis tur.");
System.out.println("6. Tilføj antal taxier.");
Scanner sc = new Scanner(System.in);
String choice = sc.nextLine();
switch (choice) {
case "1":
if (taxi.getTaxiListPrint().size()>=1) {
Scanner startTaxiNumber = new Scanner(System.in);
int numberChoice = startTaxiNumber.nextInt();
chosenTaxi = taxi.chooseTaxi(numberChoice);
chosenTaxi.setStartTime();
break;
} else {
System.out.println("Ingen taxier er oprettet i systemet.");
break;
}
case "2":
if (taxi.getTaxiListPrint().size()>=1) {
Scanner endTaxiNumber = new Scanner(System.in);
int numberChoice = endTaxiNumber.nextInt();
chosenTaxi = taxi.chooseTaxi(numberChoice);
chosenTaxi.setEndTime();
if (!chosenTaxi.isStopped()) {
System.out.println("Turen varede " + decimalFormat.format(((chosenTaxi.getEndTime() - chosenTaxi.getStartTime()) / 100)*0.1) + " sekunder.");
Price price = new Price();
String finalPrice = price.calculatePrice(chosenTaxi.getStartTime(), chosenTaxi.getEndTime(), decimalFormat);
System.out.println("Pris: " + finalPrice + " dollars.");
chosenTaxi.setStopped(true);
} else {
System.out.println("Denne taxi er allerede blevet stoppet.");
}
break;
} else {
System.out.println("Ingen taxier er oprettet i systemet.");
}
case "3":
break;
case "4":
if (taxi.getTaxiList().size()>=1) {
Scanner currentPriceTaxiNumber = new Scanner(System.in);
int numberChoice = currentPriceTaxiNumber.nextInt();
Taxi currentChosenTaxi = taxi.chooseTaxi(numberChoice);
currentChosenTaxi.setEndTime();
if (!currentChosenTaxi.isStopped()) {
Price priceNow = new Price();
String currentPrice = priceNow.calculatePrice(currentChosenTaxi.getStartTime(), currentChosenTaxi.getEndTime(), decimalFormat);
System.out.println("Pris: " + currentPrice + " dollars.");
} else {
System.out.println("Denne taxi er allerede blevet stoppet.");
}
break;
} else {
System.out.println("Ingen taxier er oprettet i systemet.");
break;
}
case "5":
break;
case "6":
System.out.println("Hvor mange taxier vil du tilføje?");
Scanner taxaNumber = new Scanner(System.in);
int number = taxaNumber.nextInt();
for (int i = 0; i<number;i++) {
taxi.addTaxi(taxi);
}
System.out.println(number + " " + "Taxa'er tilføjet!");
break;
default:
isEnd = true;
break;
导入java.text.DecimalFormat;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
出租车=新出租车();
System.out.println(“Hej og velkommen til该死的快出租车”);
布尔isEnd=false;
DecimalFormat DecimalFormat=新的DecimalFormat(#.0”);
而(!i结束){
出租车chosenTaxi;
System.out.println(“1.启动出租车”);
System.out.println(“2.停止滑行”);
System.out.println(“3.在滑行时暂停”);
System.out.println(“4.Spørg efter pris”);
系统输出打印(“5.免费转换”);
System.out.println(“6.Tilføj antal taxer.”);
扫描仪sc=新的扫描仪(System.in);
字符串选择=sc.nextLine();
开关(选择){
案例“1”:
if(taxi.getTaxiListPrint().size()>=1){
Scanner startTaxiNumber=新扫描仪(System.in);
int numberChoice=startTaxiNumber.nextInt();
chosenTaxi=滑行。选择滑行(numberChoice);
chosenTaxi.setStartTime();
打破
}否则{
System.out.println(“Ingen Taxer Opretet i systemet.”);
打破
}
案例“2”:
if(taxi.getTaxiListPrint().size()>=1){
扫描仪编号=新扫描仪(System.in);
int numberChoice=endTaxiNumber.nextInt();
chosenTaxi=滑行。选择滑行(numberChoice);
chosenTaxi.setEndTime();
如果(!chosenTaxi.isStopped()){
System.out.println(“Turen varede”+十进制格式(((chosenTaxi.getEndTime()-chosenTaxi.getStartTime())/100)*0.1)+“sekunder”);
价格=新价格();
字符串finalPrice=price.calculatePrice(chosenTaxi.getStartTime(),chosenTaxi.getEndTime(),十进制格式);
System.out.println(“Pris:+finalPrice+美元”);
chosenTaxi.setStopped(真);
}否则{
System.out.println(“Denne出租车司机allerede blevet stoppet”);
}
打破
}否则{
System.out.println(“Ingen Taxer Opretet i systemet.”);
}
案例“3”:
打破
案例“4”:
if(taxi.getTaxiList().size()>=1){
扫描仪currentPriceTaxiNumber=新扫描仪(System.in);
int numberChoice=currentPriceTaxiNumber.nextInt();
滑行电流CHOSENTAXI=滑行。选择滑行(编号CHOICE);
currentChosenTaxi.setEndTime();
如果(!currentChosenTaxi.isStopped()){
Price priceNow=新价格();
字符串currentPrice=priceNow.calculatePrice(currentChosenTaxi.getStartTime(),currentChosenTaxi.getEndTime(),decimalFormat);
System.out.println(“Pris:+当前价格+”美元“);
}否则{
System.out.println(“Denne出租车司机allerede blevet stoppet”);
}
打破
}否则{
System.out.println(“Ingen Taxer Opretet i systemet.”);
打破
}
案例“5”:
打破
案例“6”:
System.out.println(“管理出租车vil du tilføje?”);
Scanner taxaNumber=新扫描仪(System.in);
int number=taxaNumber.nextInt();
对于(int i=0;i我认为最简单的更改是将滑行类中的这些方法更改为static:
private static List<Taxi> taxiList = new ArrayList<>();
public static void addTaxi(Taxi taxi) {
taxiList.add(taxi);
}
public static Taxi chooseTaxi(int choice) {
return taxiList.get(choice - 1);
}
public static List<Taxi> getTaxiListPrint() {
for (int i = 1; i < taxiList.size() + 1; i++) {
System.out.println("Taxi: " + i);
}
return taxiList;
}
public static List<Taxi> getTaxiList() {
return taxiList;
}
改为
Taxi currentChosenTaxi = Taxi.chooseTaxi(numberChoice);
然后向经理添加不同的出租车:
case "6":
System.out.println("Hvor mange taxier vil du tilføje?");
Scanner taxaNumber = new Scanner(System.in);
int number = taxaNumber.nextInt();
for (int i = 0; i < number; i++) {
Taxi.addTaxi(new Taxi());
}
System.out.println(number + " " + "Taxa'er tilføjet!");
break;
案例“6”:
System.out.println(“管理出租车vil du tilføje?”);
Scanner taxaNumber=新扫描仪(System.in);
int number=taxaNumber.nextInt();
for(int i=0;i
注意:您不需要每次都制作新扫描仪(System.in)
,如果您将其置于循环之外,则可以使用一个
仅在类中包含构成滑行的属性
正如评论所说,在你的出租车
类中设置出租车名单
不是一个好主意。你的出租车
类应该只有组成出租车的必要属性。更好的结构如下所示
public class Taxi {
private long startTime;
private long endTime;
private boolean isStopped = false;
// Add the necessary getters & setters for the above attributes here
}
public class TaxiManager {
private List<Taxi> taxiList;
public class TaxiManager() { taxiList = new ArrayList<>(); }
// Here are some "management" methods you can use
// DON'T FORGET TO HANDLE EXCEPTIONS (for example ArrayOutOfBounds, ...)
public void addTaxi(Taxi newTaxi) { taxiList.add(newTaxi); }
public Taxi getTaxiAtIndex(int index) { return taxiList.get(index); }
public void stopTaxiAtIndex(int index) { taxiList.get(index).stop(); }
// Add the necessary operations here
}
public class TaxiManager {
private List<Taxi> taxiList;
private TaxiManager() {}
private static class SingletonHolder {
private static final TaxiManager INSTANCE = new TaxiManager();
}
public static TaxiManager getInstance() {
return SingletonHolder.INSTANCE;
}
// Add other methods here
}
通过列表
或出租车管理员
课程管理您的出租车
要保留您的出租车列表
,您有两个选项
在Main
类中,定义一个List taxiList=new ArrayList()
创建一个单独的类来保存列表和控制其项的逻辑
public class TaxiManager {
private List<Taxi> taxiList;
public class TaxiManager() { taxiList = new ArrayList<>(); }
// Here are some "management" methods you can use
// DON'T FORGET TO HANDLE EXCEPTIONS (for example ArrayOutOfBounds, ...)
public void addTaxi(Taxi newTaxi) { taxiList.add(newTaxi); }
public Taxi getTaxiAtIndex(int index) { return taxiList.get(index); }
public void stopTaxiAtIndex(int index) { taxiList.get(index).stop(); }
// Add the necessary operations here
}
public class Main {
public static void main(String[] args) {
TaxiManager taxiManager = new TaxiManager();
Scanner sc = new Scanner(System.in);
String choice = sc.nextLine();
switch (choice) {
case "add": {
taxiManager.addTaxi(new Taxi());
break;
}
// Include other options
}
}
}
public class TaxiManager {
private List<Taxi> taxiList;
private TaxiManager() {}
private static class SingletonHolder {
private static final TaxiManager INSTANCE = new TaxiManager();
}
public static TaxiManager getInstance() {
return SingletonHolder.INSTANCE;
}
// Add other methods here
}
TaxiManager taxiManager = TaxiManager.getInstance();