我遇到的问题如下:我创建了10个表示船舶停靠空间的行输出。
但码头可以容纳两种尺寸的船;货物和集装箱。行由5个小型和5个中型组成。货船(小)可以在任何可用空间停泊。集装箱船(中型)可停泊在中等空间,但不能停泊在狭小的空间内。
因此,如果我输入shipName和Container,它会搜索数组,确保少于5个Container,这样它就可以停靠,即保存在数组中。你能帮我吗? 这是我的dock方法:
import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
private static Ship[] dock1 = new Ship[10];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(true) {
System.out.println("Choose an option: 1-3");
System.out.println("1. Dock");
System.out.println("2. Undock");
System.out.println("3. Status");
int menu = scan.nextInt();
switch (menu) {
case 1:
System.out.println("1. Dock");
dock();
break;
case 2:
System.out.println("2. Undock");
undock();
break;
case 3:
System.out.println("3. Status");
printArray();
break;
case 4:
System.out.println("4. Exit");
System.exit(0);
default:
System.out.println("No such option");
break;
}
}
}
public static void dock() {
int dockCapacity = 0;
System.out.println("Enter ship's name: ");
String name = scan.nextLine();
System.out.println("Enter ship's size: ");
String size = scan.nextLine();
System.out.println("Enter the ships dock:");
//search for 5 small 3 med 2 large
// if what they entered equals shipSize more than 5 than cannot dock.
for(int i = 1; i < dock1.length; i++) {
if (dock1[i].getShipSize().equals(size)) {
System.out.print(dock1[i].getShipSize());
}
else {
System.out.println("Couldn't dock");
}
}
//Check if the dock number is valid
int i = Integer.valueOf(scan.nextLine());
if (i >= 0 && i < 10 && dock1[i] == null){
//Add ship to the dock
dock1[i] = new Ship(name, size);
System.out.println("Ship has been docked");
}
else{
System.out.println("Couldn't dock");
}
// printArray();
}
public static void undock(){
System.out.println("Status of ships: ");
printArray();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
for(int i = 1; i < dock1.length; i++){
if(dock1[i] != null && dock1[i].getShipName().equals(name)){
dock1[i] = null;
System.out.println("Ship removed");
break;
}
else{
System.out.println("Ship not docked here");
}
}
}
public static void printArray() {
System.out.println("Docks:");
for(int i = 0; i < dock1.length; i++)
{
if(dock1[i] == null)
{
System.out.println("Dock " + i + " is empty");
}
else
{
System.out.println("Dock " + i + ": " + dock1[i].getShipName() + " " + dock1[i].getShipSize());
}
}
}
}
船舶类
public class Ship {
private String shipName;
private String shipSize;
public String getShipName() {
return shipName;
}
public void setShipName(String shipName) {
this.shipName = shipName;
}
public String getShipSize() {
return shipSize;
}
public void setShipSize(String shipSize) {
this.shipSize = shipSize;
}
public Ship(String shipName, String shipSize) {
this.shipName = shipName;
this.shipSize = shipSize;
}
}
答案 0 :(得分:3)
我建议你像这样更改Ship类。它添加了ShipSize枚举,使得从字符串解析船舶尺寸和比较船舶尺寸变得更加容易。另外,我添加了define([
'jquery',
'jquery/ui'
], function($) {
"use strict";
$.widget('NameSpace.widget', {
options: {
triggerEvent: 'change',
controller: 'frontName/query/custom',
qty: '[data-role="cart-item-qty"]',
itemId: '.action.action-edit'
},
_create: function() {
this._bind();
},
_bind: function() {
var self = this;
self._ajaxSubmit();
},
_ajaxSubmit: function() {
jQuery(this.options.qty).on('change', function () {
var that = this;
// here we need to refer to the context of jQuery
var url = domine + 'NameSpace_widget/query/custom';
jQuery.ajax({
url: url,
type: 'post',
dataType: 'json',
data: //your data,
success: function(res) {
// your code
}
});
});
},
_updateOrderHandler: function () {
$(this).trigger('change');
}
});
return $.NameSpace.widget;
});
和isCargo
方法。
isContainer
我还修改了你的主类,所以它可以做你想要的。我添加了一些解析和检查数字,为扩展添加了一个初始化方法。
import java.util.Arrays;
public class Ship {
private String shipName;
private ShipSize shipSize;
public Ship(String shipName, ShipSize shipSize) {
this.shipName = shipName;
this.shipSize = shipSize;
}
public String getShipName() {
return shipName;
}
public void setShipName(String shipName) {
this.shipName = shipName;
}
public ShipSize getShipSize() {
return shipSize;
}
public void setShipSize(ShipSize shipSize) {
this.shipSize = shipSize;
}
public boolean isCargo() {
return shipSize == ShipSize.CARGO;
}
public boolean isContainer() {
return shipSize == ShipSize.CONTAINER;
}
public enum ShipSize {
CARGO,
CONTAINER;
public static ShipSize of(String size) {
return Arrays.stream(ShipSize.values()).filter(enumSize -> enumSize.name().equalsIgnoreCase(size)).findAny().orElse(null);
}
}
}
我还添加了一个Dock类。它工作正常。
import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
private static Dock[] dock1 = new Dock[10];
public static void main(String[] args) {
initializeDock();
Scanner scan = new Scanner(System.in);
while(true) {
System.out.println("Choose an option: 1-3");
System.out.println("1. Dock");
System.out.println("2. Undock");
System.out.println("3. Status");
int menu = scan.nextInt();
switch (menu) {
case 1:
System.out.println("1. Dock");
dock();
break;
case 2:
System.out.println("2. Undock");
undock();
break;
case 3:
System.out.println("3. Status");
printArray();
break;
case 4:
System.out.println("4. Exit");
System.exit(0);
default:
System.out.println("No such option");
break;
}
}
}
private static void initializeDock() {
for (int i = 0; i < 5; i++) {
dock1[i] = new Dock(Ship.ShipSize.CARGO);
}
for (int i = 5; i < 10; i++) {
dock1[i] = new Dock(Ship.ShipSize.CONTAINER);
}
}
public static void dock() {
int dockCapacity = 0;
System.out.println("Enter ship's name: ");
String name = scan.nextLine();
Ship.ShipSize size = null;
while(size == null) {
System.out.println("Enter ship's size: ");
String stringSize = scan.nextLine();
size = Ship.ShipSize.of(stringSize);
if (size == null) {
System.out.println("Could not read ship size. Only cargo and container are allowed.");
}
}
// check that ship fits into any dock
if (size == Ship.ShipSize.CONTAINER) {
long numberOfContainerShips = Arrays.stream(dock1).map(Dock::getDockedShip).filter(Objects::nonNull).filter(Ship::isContainer).count();
if (numberOfContainerShips >= 5) {
System.out.println("No place for a ship that large. Aborting.");
return;
}
}
System.out.println("Enter the ships dock:");
Integer dockNumber = null;
while(dockNumber == null) {
dockNumber = scan.nextInt();
if (dockNumber < 0 || dockNumber > dock1.length - 1) {
System.out.println("Illegal dock number. Only numbers between 0 and " + dock1.length + " are allowed.");
dockNumber = null;
}
}
Dock dock = dock1[dockNumber];
if (dock.getDockedShip() != null) {
System.out.println("Dock reserved - couldn't dock");
return;
}
if (dock.getSupportedSize() == Ship.ShipSize.CARGO && size == Ship.ShipSize.CONTAINER) {
System.out.println("Dock too small - couldn't dock");
return;
}
dock.setDockedShip(new Ship(name, size));
}
public static void undock(){
System.out.println("Status of ships: ");
printArray();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
for(int i = 1; i < dock1.length; i++){
if(dock1[i].getDockedShip() != null && dock1[i].getDockedShip().getShipName().equals(name)){
dock1[i] = null;
System.out.println("Ship removed");
break;
}
else{
System.out.println("Ship not docked here");
}
}
}
public static void printArray() {
System.out.println("Docks:");
for(int i = 0; i < dock1.length; i++)
{
if(dock1[i].getDockedShip() == null)
{
System.out.println("Dock " + i + " is empty. Size: " + dock1[i].getSupportedSize().name().toLowerCase());
}
else
{
System.out.println("Dock " + i + ": " + dock1[i].getDockedShip().getShipName() + " " + dock1[i].getDockedShip().getShipSize().name().toLowerCase());
}
}
}
}