package HW1;
import java.util.ArrayList;
public class Arrays {
public ArrayList Case() {
ArrayList <Case> cases = new ArrayList<Case>();
Case c1 = new Case("Antec Twelve Hundred V3", 20.20, 8.39, 22.87, "ATX Full Tower", 154.99);
Case c3 = new Case("BitPhenix Phenom M Arctic", 9.80, 12.95, 14.69, "MicroATX Mini Tower", 89.99);
Case c2 = new Case("Corsair 250D", 13.8, 10.9, 11.4, "MiniITX Tower", 99.9);
cases.add(c1);
cases.add(c2);
cases.add(c3);
for (int i = 0; i < cases.size(); i++ ) {
System.out.println("Name: " + cases.get(i).getName() + "\n" + "Dimensions: " + cases.get(i).getLength() + "' x " + cases.get(i).getWidth() + "' x " + cases.get(i).getHeight() + "' " + "Type: " +cases.get(i).getType() + "\n" + "Price: $" + cases.get(i).getPrice()+ "\n");
}
return cases;
}
public ArrayList Monitor() {
ArrayList <Monitor> monitor = new ArrayList<Monitor>();
Monitor m1 = new Monitor("Asus PB278Q", 27, 549.99);
Monitor m2 = new Monitor("Dell U2412M", 24, 263.99);
Monitor m3 = new Monitor("Samsung S22300H", 21.5, 139.99);
monitor.add(m1);
monitor.add(m2);
monitor.add(m3);
for (int i = 0; i < monitor.size(); i++ ) {
System.out.println("Name: " + monitor.get(i).getName() + "\n" + "Size: " + monitor.get(i).getSize() + "' " + "\n" + "Price: $" + monitor.get(i).getPrice() + "\n");
}
return monitor;
}
public ArrayList CPU() {
ArrayList <CPU> cpu = new ArrayList<CPU>();
CPU cpu1 = new CPU("AMDFX-8350", 4.0, 188.99);
CPU cpu2 = new CPU("Intel Core i5-3570K", 3.4, 179.99);
CPU cpu3 = new CPU("Intel Core i7-3770K", 3.5, 249.99);
cpu.add(cpu1);
cpu.add(cpu2);
cpu.add(cpu3);
for (int i = 0; i < cpu.size(); i++ ) {
System.out.println("Name: " + cpu.get(i).getName() + "\n" + "Speed: " + cpu.get(i).getSpeed() + "GHz " + "\n" + "Price: $" + cpu.get(i).getPrice() + "\n");
}
return cpu;
}
public ArrayList VideoCard() {
ArrayList <VideoCard> videoCard = new ArrayList<VideoCard>();
VideoCard vd1 = new VideoCard("Nvidia GeForce GTX 770", 2, 346.99);
VideoCard vd2 = new VideoCard("Nvidia GeForce GTX 670", 2, 333.99);
VideoCard vd3 = new VideoCard("AMD Radeon HD 7950", 3, 399.99);
videoCard.add(vd1);
videoCard.add(vd2);
videoCard.add(vd3);
for (int i = 0; i < videoCard.size(); i++ ) {
System.out.println("Name: " + videoCard.get(i).getName() + "\n" + "Memory Size: " + videoCard.get(i).getMemorySize() + "GB " + "\n" + "Price: $" + videoCard.get(i).getPrice() + "\n");
}
return videoCard;
}
public ArrayList HardDrive() {
ArrayList <HardDrive> hardDrive = new ArrayList<HardDrive>();
HardDrive hd1 = new HardDrive("Samsung MZ-7PD256BW", 256, 199.99);
HardDrive hd2 = new HardDrive("Corsair CSSD-F240GBGT-BK", 240, 206.99);
HardDrive hd3 = new HardDrive("Crucial CT128M4SSD1", 128, 199.95);
hardDrive.add(hd1);
hardDrive.add(hd2);
hardDrive.add(hd3);
for (int i = 0; i < hardDrive.size(); i++ ) {
System.out.println("Name: " + hardDrive.get(i).getName() + "\n" + "Memory Size: " + hardDrive.get(i).getSize() + "GB " + "\n" + "Price: $" + hardDrive.get(i).getPrice()+ "\n");
}
return hardDrive;
}
public ArrayList Memory() {
ArrayList <Memory> memory = new ArrayList<Memory>();
Memory me1 = new Memory("Corsair Vengeance", 8, 69.99);
Memory me2 = new Memory("G.Skill Ares Series", 16, 149.99);
Memory me3 = new Memory("Kingston Black Series", 8, 79.99);
memory.add(me1);
memory.add(me2);
memory.add(me3);
for (int i = 0; i < memory.size(); i++ ) {
System.out.println("Name: " + memory.get(i).getName() + "\n" + "Memory Size: " + memory.get(i).getSize() + "GB " + "\n" + "Price: $" + memory.get(i).getPrice()+"\n");
}
return memory;
}
}
假设我要访问c3
的价格。在同一方法中,我可以打印它,但我想在另一个类的主方法上访问c3
的价格。这是代码。假设用户从菜单中输入1
并决定购买第一个选项。我得到它打印选项,但我不知道如何获得它的价格。
package HW1;
import java.util.*;
public class Main {
public static void printMenu()
{
System.out.println("Menu");
System.out.println("1: Choose a Case");
System.out.println("2: Choose a Monitor");
System.out.println("3: Choose a CPU");
System.out.println("4: Choose a Video Card");
System.out.println("5: Choose a Hard Drive");
System.out.println("6: Choose a Memory");
System.out.println("7: Delete Current Computer");
System.out.println("8: Display Receipt");
System.out.println("9: Quit program");
}
public static void main(String[] args) {
printMenu();
System.out.println("please select and option");
Arrays arrays = new Arrays();
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
if (choice == 1) {
ArrayList cases = arrays.Case();
System.out.println(cases);
System.out.println("please select a case");
if (choice == 1) {
System.out.println(c1.getPrice());
}
}
}
}
答案 0 :(得分:0)
好的,所以你可以从Cases
的课程中获得array
......
ArrayList cases = arrays.Case();
接下来,您需要找出用户感兴趣的案例......
int choice = -1;
do {
System.out.println("please select a case");
choice = in.nextInt();
if (choice >= cases.size()) {
System.out.println("Please select from 0-" + (cases.size() - 1));
}
} while (choice < cases.size());
然后,您需要从ArrayList
// Allow for a non-positive exit value...
if (choice >= 0) {
// I know, blind cast, bad idea...keep reading...
Case aCase = (Case)cases.get(choice);
System.out.println(aCase.getPrice());
}
详细了解Collections了解更多详情。
<强>建议强>
请注意,库中已经有一个名为Arrays
的类,重命名它以消除混淆可能是个好主意。
为了防止需要从ArrayList
转换元素,您应该提供通用的返回结果,例如......
而不是public ArrayList Case() {
,你应该使用public ArrayList<Case> Case() {
,这可以确保当你执行cases.get
时,编译器会期望Case
并且意味着你可以逃脱做一些像Case case = cases.get(choice);
更安全的事情。
在Java中,建议Java方法名称以小写
开头public ArrayList<Case> case() {...
public ArrayList<Monitor> monitor() {...
将get
用于&#34; get&#34;的方法也可能是合适的。东西,例如......
public ArrayList<Case> getCases() {...
public ArrayList<Monitor> getMonitors() {...