day08 增删改查
1.FruitItem.java类
public class FruitItem {
int ID; //商品编号
String name; //商品名称
double price; //商品单价
double number; //商品数量
double money; //商品金额
}
2.shopping.java
import java.util.ArrayList;
import java.util.*;
public class FruitStore {
public static void main(String[] args) {
// TODO Auto-generated method stub 菠萝
ArrayList<FruitItem> list = new ArrayList<FruitItem>();
init(list);
while(true)
{
mainMenu();
chooseFunction(list);
}
}
public static void init(ArrayList<FruitItem> list)
{
FruitItem item=new FruitItem();
item.ID=10011;
item.name="苹果";
item.number=11;
item.price=3.5;
FruitItem item2 = new FruitItem();
item2.name = "尚康杂粮牡丹饼";
item2.ID = 9002;
item2.price = 20;
FruitItem item3 = new FruitItem();
item3.name = "新疆原产哈密瓜";
item3.ID = 3;
item3.price = 9007;
list.add(item);
list.add(item2);
list.add(item3);
}
public static void mainMenu()
{
System.out.println("=========================欢迎光临itcast超市=========================");
System.out.println("1:查询货物 2:添加新货物 3:删除货物 4:修改货物");
System.out.println("5:退出系统");
}
public static void chooseFunction(ArrayList<FruitItem> list)
{
System.out.println("请输入您要操作的序号");
Scanner sc=new Scanner(System.in);
String choose=sc.next();
switch(choose)
{
case "1":
showFruitlist(list);break;
case "2":
addFuitlist(list);break;
case "3":
delFruitlist(list);break;
case "4":
modFruitList(list);break;
case "5":
System.out.println("退出系统");
return ;
default:
System.out.println("对不起,没有您输入的功能,请重新选择");
break;
}
}
public static void showFruitlist(ArrayList<FruitItem> list)
{
System.out.println("=======================商品库存清单=======================");
System.out.println("商品编号\t 商品名称 \t\t 商品单价");
for(int i=0;i<list.size();i++)
{
FruitItem item=list.get(i);
System.out.println(item.ID +"\t"+item.name +"\t"+item.price);
}
}
public static void addFuitlist(ArrayList<FruitItem> list)
{
FruitItem newItem=new FruitItem();
Scanner scann=new Scanner(System.in);
System.out.println("请输入新水果的编号");
newItem.ID=scann.nextInt();
System.out.println("请输入新水果的名称");
newItem.name=scann.next();
System.out.println("请输入新水果的单价");
newItem.price=scann.nextDouble();
list.add(newItem);
}
public static void delFruitlist(ArrayList<FruitItem> list)
{
System.out.println("请输入您要删除的水果编号");
Scanner sc=new Scanner(System.in);
int deleID=sc.nextInt();
System.out.println("开始表长:"+ list.size());
for(int i=0;i<list.size();i++)
{
FruitItem thisItem= list.get(i);
if(thisItem.ID==deleID)
{
list.remove(thisItem);
System.out.println("已删除完毕!");
System.out.println("表长:"+ list.size());
return ;
}
}
System.out.println("对不起!没有该水果编号!");
}
public static void modFruitList(ArrayList<FruitItem> list)
{
System.out.println("请输入您要修改的水果编号");
Scanner sc=new Scanner(System.in);
int modID=sc.nextInt();
for(int i=0;i<list.size();i++)
{
FruitItem thisItem=list.get(i);
if(thisItem.ID==modID)
{
System.out.println("请输入您要修改的水果编号");
thisItem.ID=sc.nextInt();
System.out.println("请输入您要修改的水果名称");
thisItem.name=sc.next();
System.out.println("请输入您要修改的水果单价");
thisItem.price=sc.nextDouble();
System.out.println("水果信息更新完毕!");
return;
}
}
System.out.println("没有您要修改的水果编号!");
}
}
day09 面向对象(随机点名系统)
1、student.java
package day09;
public class Student {
private int StdID;
private int Stdage;
private String Stdname;
public int getID(){ return this.StdID; }
public int getAge() { return this.Stdage; }
public String getName(){ return this.Stdname;}
public void setID(int StdID){ this.StdID=StdID; }
public void setAge(int Stdage) {this.Stdage=Stdage;}
public void setName(String Stdname) {this.Stdname=Stdname;}
}
2.CallName.java
package day09;
import java.util.*;
public class CallName {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> list=new ArrayList<Student>();
/*
* 1.存储全班同学信息
*/
addStudent(list);
/*
* 2.打印全班同学每一个人的信息(姓名、年龄)
*/
printStudent(list);
/*
* 3.随机对学生点名,打印学生信息
*/
randomStudent(list);
}
public static void addStudent(ArrayList<Student> list)
{
int totalnumber;
System.out.println("请输入学生的总人数:");
Scanner sc=new Scanner(System.in);
totalnumber=sc.nextInt();
for(int i=0;i<totalnumber;i++)
{
Student sd=new Student();
System.out.println("请输入学生的学号:");
sd.setID(sc.nextInt());
System.out.println("请输入学生的年龄:");
sd.setAge(sc.nextInt());
System.out.println("请输入学生的名字:");
sd.setName(sc.next());
list.add(sd);
}
}
public static void printStudent(ArrayList<Student> list)
{
for(int i=0;i<list.size();i++)
{
Student sd=list.get(i);
System.out.println("学生的学号:"+sd.getID()+"\n"+"学生的年龄:"+sd.getAge()+"\t"+"学生的姓名:"+sd.getName());
}
} public static void randomStudent(ArrayList<Student> list)
{
int index=new Random().nextInt(list.size());
Student sd=list.get(index);
System.out.println("学生的学号:"+sd.getID()+"\n"+"学生的年龄:"+sd.getAge()+"学生的姓名:"+sd.getName());
}
}