import java.util.ArrayList;
import java.util.Scanner;
public class inventory {
ArrayList<person> customerlist = new ArrayList<person>();
ArrayList<items> itemlist = new ArrayList<items>();
public void menu (){
Scanner in = new Scanner(System.in);
try
{
System.out.println("[1] Add Customer");
System.out.println("[2] Show Total Cost ");
System.out.println("[3] exit");
System.out.print("Choose one : ");
int num = in.nextInt();
switch(num){
case 1 : AddCustomer();//add customer
break;
case 2 : DisplayTotalCost(customers);//display total cost
break;
case 3 : System.exit(0);
default : break;
}
}
catch(Exception e)
{
System.out.println("error: " + e);
}
}
public void AddCustomer(){
inventory function = new inventory();
Scanner in = new Scanner(System.in);
person addcustomer = new person();
items additem = new items();
System.out.println("Enter Customer Name:");
try {
addcustomer.name = in.nextLine();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Enter Customer Address:");
try {
addcustomer.Address = in.nextLine();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Enter Customer Contact Number:");
try {
addcustomer.contactnumber = in.nextLine();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Do you want to add an item? [1]YES [2]NO");
int x = in.nextInt();
while (x == 1){
function.addItem();
System.out.println("Do you want to add an item? [1]YES [2]NO");
x = in.nextInt();
}
System.out.println(addcustomer.totalamount);
function.menu();
}
public void addItem(){
Scanner in = new Scanner(System.in);
person addcustomer = new person();
items additem = new items();
addcustomer.totalamount = .0;
System.out.println("Enter Item Name:");
try {
additem.itemname = in.nextLine();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Enter Item Quantity:");
try {
additem.quantity = in.nextInt();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Enter Item Price:");
try {
additem.price = in.nextDouble();
}
catch (Exception e)
{
e.printStackTrace();
}
additem.total = (additem.quantity*additem.price);
System.out.println("Total Cost:" + additem.total);
}
public void DisplayTotalCost(ArrayList<person> customers){
items item = new items();
person customer = new person();
System.out.println("Name" + "\t" + "Total Cost");
for (int i=0; i<customerlist.size(); i++){
System.out.println(customer.name + "\t");
}
}
public static void main(String[] args){
inventory function = new inventory();
function.menu();
}
}
我想在功能菜单上调用函数ArrayList<person>
时传递DisplayTotalCost
个客户。我怎么做?以及我如何将addcustomer.totalamount = addcustomer.totalamount + additem.total
放在所有additem.total
?
类
public class person {
String name;
String Address;
String contactnumber;
Double totalamount;
}
public class items {
String itemname;
int quantity;
Double price;
Double total;
}
答案 0 :(得分:0)
您可以尝试并在必要时进行修改:)
import java.util.ArrayList;
import java.util.Scanner;
public class inventory {
static ArrayList<person> customerlist = new ArrayList<person>();
static ArrayList<items> itemlist = new ArrayList<items>();
public static void menu (){
Scanner in = new Scanner(System.in);
try{
System.out.println("[1] Add Customer");
System.out.println("[2] Show Total Cost ");
System.out.println("[3] exit");
System.out.print("Choose one : ");
int num = in.nextInt();
switch(num){
case 1 : AddCustomer();//add customer
break;
case 2 : DisplayTotalCost();//display total cost
break;
case 3 : System.exit(0);
default : break;
}
}
catch(Exception e){
System.out.println("error: " + e);
}
}
public static void AddCustomer(){
Scanner in = new Scanner(System.in);
person addcustomer = new person();
items additem = new items();
try {
System.out.println("Enter Customer Name:");
addcustomer.name = in.nextLine();
System.out.println("Enter Customer Address:");
addcustomer.Address = in.nextLine();
System.out.println("Enter Customer Contact Number:");
addcustomer.contactnumber = in.nextLine();
customerlist.add(addcustomer);
}
catch (Exception e) {
e.printStackTrace();
}
addItem();
menu();
}
public static void addItem(){
Scanner in = new Scanner(System.in);
person addcustomer = new person();
items additem = new items();
addcustomer.totalamount = .0;
try {
System.out.println("Enter Item Name:");
additem.itemname = in.nextLine();
System.out.println("Enter Item Quantity:");
additem.quantity = in.nextInt();
System.out.println("Enter Item Price:");
additem.price = in.nextDouble();
itemlist.add(additem);
}
catch (Exception e) {
e.printStackTrace();
}
additem.total = (additem.quantity*additem.price);
System.out.println("Total Cost:" + additem.total);
}
public static void DisplayTotalCost(){
System.out.println("Name" + "\t" + "Total Cost");
for (int i=0; i<customerlist.size(); i++){
System.out.println(customerlist.get(i).name + "\t"+itemlist.get(i).total);
}
}
public static void main(String[] args){
menu();
}
}