程序输入输出与图片要求一致
程序输入输出与图片要求一致
程序输入输出与图片要求一致
import java.util.Scanner;
public class Item {
private String name;
private double price;
private String code;
public Item(String name, String code) {
this.name = name;
this.code = code;
this.price = 0.0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public void display() {
System.out.println("Name: " + name + "; code: " + code + "; Price: " + price + ";");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Item[] bill = new Item[5];
for (int i = 0; i < 5; i++) {
System.out.print("Enter item " + (i+1) + " (name, price, code) ");
String name = input.next();
double price = input.nextDouble();
String code = input.next();
bill[i] = new Item(name, code);
bill[i].setPrice(price);
}
double totalPayment = 0.0;
for (int i = 0; i < 5; i++) {
System.out.println("Item " + (i+1));
bill[i].display();
totalPayment += bill[i].getPrice();
}
System.out.println("Total Payment: " + totalPayment);
}
}